def form_valid(self, form): if self.request.is_ajax(): tag_name = self.request.POST["tag"] # Is this a brand new tag or did a user select a tag that already exists in the database? tag_type = self.request.POST["tag_type"] # If it's a new tag, save it. if tag_type == "new": tag = Tags(name=tag_name) tag.handle = tag.clean_handle(tag_name) tag.save() else: tag = Tags.objects.get(name=tag_name) # Save the post and format the title to make it url safe. title = form.cleaned_data["title"] user_id = self.request.user.id date_created = int(time.time()) post = Posts(title=title,description=form.cleaned_data["description"],formatted_description=form.cleaned_data["formatted_description"],user_id=user_id, type="experience", date_created = date_created) post.save() title_url = post.clean_title(title) post.url = title_url post.save() post.tags.add(tag) # Return data. data = { 'post_pk': post.id, 'post_url': post.url } return self.render_to_json_response(data) else: # Request must be made via Ajax. If error redirect to home feed. return HttpResponseRedirect(reverse('feed:post_list'))
def SaveTag(request): if request.POST: type = request.POST["type"] tag = request.POST["tag_text"] contribution_id = request.POST["contribution_id"] post = Posts.objects.get(pk=contribution_id) title = post.title.replace(" ", "") if type is not None and tag is not None and contribution_id is not None: if type == "new": #Save to tags_tags tagLib = Tags(name=tag) tagLib.handle = tagLib.clean_handle(tag) tagLib.save() else: tagLib = Tags.objects.get(name = tag) #save to posts_tags post.tags.add(tagLib) post.save() #add to Redis Tags redis = ManageTags(tag) redis.add() if request.is_ajax(): return render(request, 'tags/tag.html', {'tag': tagLib}) else: return HttpResponseRedirect(reverse('singleCont:detail', kwargs={'pk': contribution_id, 'title': title} )) else: if request.is_ajax(): HttpResponse(400) else: return HttpResponseRedirect(reverse('singleCont:detail', kwargs={'pk': contribution_id, 'title': title} )) return HttpResponseRedirect(reverse('contributions:post_list'))