def post_form_view(request): ImageFormSet = formset_factory(PictureForm, extra=3) user = request.user if request.method == "POST": form = PostForm(request.POST) formset = ImageFormSet(request.POST, request.FILES) # validating both postform and pictureform if form.is_valid() and formset.is_valid(): # saving post to the db post = form.save(commit=False) post.author = user post.save() # saving formset for form_entry in formset: data = form_entry.save(commit=False) data.post = post data.save() save_activity(user, post, 'created a thread', False) return HttpResponseRedirect(post.get_absolute_url()) else: form = PostForm() formset = ImageFormSet() context = { 'form': form, 'formset': formset } return render(request, "forum/post_form_and_list.html", context)
def follow_post(request, post_id): get_post = get_object_or_404(Post, pk=post_id) user = request.user data = {} if user not in get_post.user_followed.all(): get_post.user_followed.add(user) save_activity(user, get_post, 'following', False) data['added'] = True else: get_post.user_followed.remove(user) data['added'] = False return JsonResponse(data)
def comment_reply(request, comment_id): get_comment = get_object_or_404(Comment, pk=comment_id) user = request.user if request.method == 'POST': form = ReplyForm(request.POST) if form.is_valid(): form_data = form.save(commit=False) form_data.author = user form_data.comment = get_comment form_data.save() save_activity(user, get_comment.post, 'replied to a comment', False) return redirect('comment_details', get_comment.pk) else: form = ReplyForm() return render(request, 'forum/comment_details.html', {'form':form})
def post_thumped_down(request, post_id): user = request.user data = {} get_post = get_object_or_404(Post, pk=post_id) if user not in get_post.post_thumped_down.all(): get_post.post_thumped_down.add(user) save_activity(user, get_post, 'thumped down', False) thumped_notification(get_post, get_post.author, user, 'thumped down', False) data['added'] = True if user in get_post.post_thumped_up.all(): get_post.post_thumped_up.remove(user) data['already_thumped_up'] = True else: get_post.post_thumped_down.remove(user) data['added'] = False return JsonResponse(data)
def content_view(request): if request.method == 'POST': form = ContentForm(request.POST) if form.is_valid(): content_data = form.save(commit=False) content_data.author = request.user content_data.save() # abtraction of content creating activity save_activity(content_data.author, content_data, 'created', False) return redirect('content_details', pk=content_data.pk, slug=content_data.slug) else: form = ContentForm() return render(request, "cms/content_form.html", {'form': form})
def comment_thump_up(request, comment_id, post_id): user = request.user get_comment = get_object_or_404(Comment, pk=comment_id) get_post = get_object_or_404(Post, pk=post_id) # check if user has already thumped the comment if user not in get_comment.comment_thumped_up.all(): get_comment.comment_thumped_up.add(user) save_activity(user, get_post, 'thumped up', True) thumped_notification(get_post, get_post.author, user, 'thumped up', True) # remove user from the manaytomany of thumped_down # because a user cannot thump_up and thump_down a comment. Need to choice if user in get_comment.comment_thumped_down.all(): get_comment.comment_thumped_down.remove(user) else: get_comment.comment_thumped_up.remove(user) return redirect(get_post.get_absolute_url())
def comment_thump_up_for_comment_main(request, comment_id): user = request.user data = {} get_comment = get_object_or_404(Comment, pk=comment_id) # check if user has already thumped the comment if user not in get_comment.comment_thumped_up.all(): get_comment.comment_thumped_up.add(user) save_activity(user, get_comment.post, 'thumped up', False) thumped_notification(get_comment.post, get_comment.post.author, user, 'thumped up', True) data['added'] = True # remove user from the manaytomany of thumped_up # because a user cannot thump_up and thump_down a comment. Need to choice one if user in get_comment.comment_thumped_down.all(): get_comment.comment_thumped_down.remove(user) data['already_thumped_down'] = True else: get_comment.comment_thumped_up.remove(user) data['added'] = False return JsonResponse(data)
def comment_thump_up(request, pk): data = {} comment = get_object_or_404(Comment, pk=pk) user = request.user if user in comment.thump_down.all(): comment.thump_down.remove(user) if user not in comment.thump_up.all(): comment.thump_up.add(user) save_activity(request.user, comment, 'Thumped Up', True) thumped_notification(comment, comment.author, request.user, 'Thumped Up', True) data['thumped_up'] = True else: comment.thump_up.remove(user) data['thumped_up'] = False numb_of_thump_up = get_object_or_404(Comment, pk=pk) data['thump_up_count'] = numb_of_thump_up.thump_up.count() data['thump_down_count'] = numb_of_thump_up.thump_down.count() return JsonResponse(data)
def comment_view(request, pk): journalism_pk = get_object_or_404(Journalism, pk=pk) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): form_data = form.save(commit=False) form_data.journalism = journalism_pk form_data.comment_date = timezone.now() form_data.author = request.user form_data.save() # abtracting comment activity save_activity(form_data.author, journalism_pk, 'commented on', False) comment_notification(form_data.author, journalism_pk, journalism_pk.author) return redirect('content_details', pk=journalism_pk.id, slug=journalism_pk.slug) else: form = CommentForm() return render(request, 'cms/content_details.html', {'form': form})
def post_comment_view(request, pk): post = get_object_or_404(Post, pk=pk) user = request.user CommentPictureFormset = formset_factory(CommentPictureForm, extra=3) if request.method == "POST": form = CommentForm(request.POST) formset = CommentPictureFormset(request.POST, request.FILES) if form.is_valid() and formset.is_valid(): comment = form.save(commit=False) comment.post = post comment.author = request.user comment.published_date = timezone.now() comment.save() for form_instance in formset: form_data = form_instance.save(commit=False) form_data.comment = comment form_data.save() save_activity(user, post, 'commented on', False) return HttpResponseRedirect(post.get_absolute_url()) else: form = CommentForm() formset = CommentPictureFormset() return render(request, "forum/post_details.html", {'form': form, 'formset':formset})