def likes_view(request, username): user = User.objects.get(username=username) # For words context words = Word.objects.filter(user=user).order_by("-timestamp") # Quotations liked by current user user_likes = Like.objects.filter(user=user) quotations = [] for user_like in user_likes: word = user_like.title.id quotation = Quotation.objects.get(pk=word) if quotation not in quotations: quotations.append(quotation) quotations.sort(key=lambda x: x.title.timestamp, reverse=True) # Page pagination p = Paginator(quotations, 5) page_number = request.GET.get("page") page_obj = p.get_page(page_number) return render( request, "wordPlay/likes.html", { "update_user": UpdateUser(), "words": words, "quotations": quotations, "page_obj": page_obj, "user": user, }, )
def trending_words(request): # Trending quotations trending_quotes = Quotation.objects.all().order_by("-like_count") # Trending words words = Word.objects.all().order_by("-bookmark_count") # Get three most liked quotations first_quotes = trending_quotes[:3] # Page pagination p = Paginator(words, 5) page_number = request.GET.get("page") page_obj = p.get_page(page_number) return render( request, "wordPlay/trending_words.html", { "words": words, "first_quotes": first_quotes, "page_obj": page_obj, }, )
def likeds_view(request, username): user = User.objects.get(username=username) # For words context words = Word.objects.filter(user=user).order_by("-timestamp") # Count likeds liked_count = 0 other_users = User.objects.exclude(pk=user.id) # Quotations liked by current user quotations = [] for other_user in other_users: # if all_user.id is not request.user.id: other_likeds = Like.objects.filter(user=other_user) for other_liked in other_likeds: word = other_liked.title if word.title.user.id is user.id: liked_count += 1 if word not in quotations: quotations.append(word) quotations.sort(key=lambda x: x.title.timestamp, reverse=True) # Page pagination p = Paginator(quotations, 5) page_number = request.GET.get("page") page_obj = p.get_page(page_number) return render( request, "wordPlay/likeds.html", { "update_user": UpdateUser(), "words": words, "quotations": quotations, "page_obj": page_obj, "user": user, }, )
def quotations(request, word_id): # Words as context for custom template tag words = Word.objects.all().order_by("-timestamp") # Trending quotations trending_quotes = Quotation.objects.all().order_by("-like_count") # Trending words trending_words = Word.objects.all().order_by("-bookmark_count") # Get three most liked quotations first_words = trending_words[:5] # Get three most liked quotations first_quotes = trending_quotes[:3] word = Word.objects.get(pk=word_id) quotations = Quotation.objects.filter(title=word).order_by("-timestamp") # Page pagination p = Paginator(quotations, 10) page_number = request.GET.get("page") page_obj = p.get_page(page_number) return render( request, "wordPlay/quotations.html", { "word": word, "words": words, "quotations": quotations, "trending_quotes": trending_quotes, "first_words": first_words, "first_quotes": first_quotes, "page_obj": page_obj, }, )
def word(request, word_id): # Currnet user's words as context for custom template tag words = Word.objects.all().order_by("-timestamp") try: word_id = int(word_id) word = Word.objects.get(pk=int(word_id)) except ValueError: try: word = Word.objects.get(title=word_id) except Word.DoesNotExist: return HttpResponseRedirect(reverse("new")) # Count likeds liked_count = 0 all_users = User.objects.all() # Quotations liked by current user quotations = [] for all_user in all_users: if all_user.id is not request.user.id: other_likeds = Like.objects.filter(user=all_user) for other_liked in other_likeds: w = other_liked.title if w.title.user.id is request.user.id: quotations.append(w) liked_count += 1 # Toggle bookmark fontawsome color all_bookmarks = Bookmark.objects.all() # Toggle like fontawsome color all_likings = [] for quotation in quotations: likings = Like.objects.filter(title=quotation) for liking in likings: all_likings.append(liking) try: word_synonym = Synonym.objects.get(title=word) except Synonym.DoesNotExist: pass try: word_antonym = Antonym.objects.get(title=word) except Antonym.DoesNotExist: pass # Try to get the latest quotation of the word try: word_quotations = Quotation.objects.filter(title=word) word_quotation = word_quotations.order_by("-timestamp")[0] except Quotation.DoesNotExist: word_quotation = word_quotations = None add_comment = CreateComment() comments = Comment.objects.filter(title=word).order_by("-comment_at") # Page pagination p = Paginator(comments, 5) page_number = request.GET.get("page") page_obj = p.get_page(page_number) # Add comment if request.method == "POST": # Get comment text comment = request.POST.get("comment") new = Comment(comment=comment, user=request.user, title=word) new.save() return HttpResponseRedirect(reverse("word", args=(word_id, ))) return render( request, "wordPlay/word.html", { "word": word, "words": words, "word_synonym": word_synonym, "word_antonym": word_antonym, "word_quotation": word_quotation, "page_obj": page_obj, "all_bookmarks": all_bookmarks, "all_likings": all_likings, "add_comment": add_comment, "comments": comments, }, )