def front_page_view(request): try: print('try works') form = OrderForm(request.COOKIES['order']) if request.COOKIES['order'] == 'best': print('if its best') all_entries = Post.objects.all().order_by('-vote_score') else: all_entries = Post.objects.all().order_by('-timestamp') except KeyError: form = OrderForm(None) all_entries = Post.objects.all().order_by('-vote_score') current_user = (Profile.objects.get( user=request.user) if request.user.is_authenticated else None) user_post_upvotes, user_post_downvotes = (get_user_votes( request, all_entries)) data = { 'posts': all_entries, 'user_post_upvotes': user_post_upvotes, 'user_post_downvotes': user_post_downvotes, 'current_user': current_user, 'form': form, } response = render(request, 'front_page.html', data) response.set_cookie('order', 'best') return response
def subreddit_view(request, subreddit): html = 'subreddit.html' subreddit_obj = Subreddit.objects.get(name=subreddit) current_user = (Profile.objects.get( user=request.user) if request.user.is_authenticated else None) subscriptions = (current_user.subscriptions.all() if request.user.is_authenticated else None) is_creator = False if subreddit_obj.created_by == current_user: is_creator = True if subreddit_obj is not None: posts = Post.objects.filter( subreddit_id=subreddit_obj).order_by('-vote_score') else: posts = None return HttpResponse('r/{} does not exist yet'.format(subreddit)) user_post_upvotes, user_post_downvotes = get_user_votes(request, posts) data = { 'subreddit': subreddit_obj, 'posts': posts, 'user_post_upvotes': user_post_upvotes, 'user_post_downvotes': user_post_downvotes, 'subscriptions': subscriptions, 'is_creator': is_creator, 'current_user': current_user } return render(request, html, data)
def individual_post_view(request, post): current_user = (Profile.objects.get( user=request.user) if request.user.is_authenticated else None) if request.method == 'POST' and 'comment' in request.POST: form = CommentForm(request.POST) if form.is_valid(): comment = form.cleaned_data Comment.objects.create( content=comment['content'], profile_id=Profile.objects.filter(user=request.user).first(), post_id=Post.objects.filter(id=post).first(), parent_id=1, ) return HttpResponseRedirect('/p/{}/'.format(post)) else: form = CommentForm replyform = ReplyForm html = 'post.html' post_obj = Post.objects.filter(id=post).first() comments = Comment.objects.filter(post_id=post_obj) replies = Reply.objects.all() user_post_upvotes, user_post_downvotes = get_user_votes( request, Post.objects.filter(id=post)) user_comment_upvotes, user_comment_downvotes = get_user_votes( request, comments, ) data = { 'post': post_obj, 'form': form, 'reply_form': replyform, 'comments': comments, 'replies': replies, 'user_post_upvotes': user_post_upvotes, 'user_post_downvotes': user_post_downvotes, 'user_comment_upvotes': user_comment_upvotes, 'user_comment_downvotes': user_comment_downvotes, 'current_user': current_user } return render(request, html, data)
def profile_view(request, author): html = 'user_profile.html' profile_obj = Profile.objects.filter(username=author).first() current_user = Profile.objects.get(user=request.user) cakeday_info = re.findall(r'\d+', str(profile_obj.user.date_joined)) year = int(cakeday_info[0]) month = calendar.month_name[int(cakeday_info[1])] day = cakeday_info[2] if day[0] == 0: day = day[1:] cakeday = '{} {}, {}'.format(month, day, year) if profile_obj is not None: posts = Post.objects.filter(profile_id=profile_obj) comments = Comment.objects.filter(profile_id=profile_obj) user_post_upvotes, user_post_downvotes = get_user_votes(request, posts) else: posts = None comments = None user_post_upvotes = None user_post_downvotes = None return HttpResponse('u/{} does not exist yet'.format(author)) data = { 'profile': profile_obj, 'cakeday': cakeday, 'posts': posts, 'comments': comments, 'user_post_upvotes': user_post_upvotes, 'user_post_downvotes': user_post_downvotes, 'current_user': current_user } return render(request, html, data)
def test_get_user_votes(self): posts = models.Post.objects.all() request = self.factory.get('/') request.user = self.user self.assertEqual(get_user_votes(request, posts), ([], []))