def collect_article(request): if request.method == 'GET': try: user_info = UserInfo.objects.get(user_id=request.user.id) except UserInfo.DoesNotExist: user_info = UserInfo() user = User.objects.get(id=request.user.id) article = user.collect.all() content = { "user_info": user_info, "article": article } return render(request, 'bbs/collection.html', content) elif request.method == 'POST': article_id = int(request.POST.get('article_id')) article = Article.objects.get(id=article_id) if article.collects.filter(id=request.user.id): article.collects.remove(request.user) status = 0 else: article.collects.add(request.user) status = 1 content = { "status": status } return HttpResponse(dumps(content))
def article_page(request, article_id): if request.method == 'GET': try: user_info = UserInfo.objects.get(user_id=request.user.id) except UserInfo.DoesNotExist: user_info = UserInfo() article = Article.objects.get(Q(id=article_id) & Q(if_publish=True)) article.views += 1 article.save(update_fields=['views']) is_collect = False if article.collects.filter(id=request.user.id): is_collect = True is_love = False if article.loves.filter(id=request.user.id): is_love = True is_follow = False if not request.user.is_anonymous: if FriendShip.objects.filter(following=request.user, followed=article.author): is_follow = True comments = Comments.objects.filter(article=article) replys = CommentsReply.objects.filter(article=article) content = { "user_info": user_info, "article": article, "collect": is_collect, "love": is_love, "follow": is_follow, "comments": comments, "replys": replys } return render(request, 'bbs/article.html', content)
def index(request): user = request.user if user.is_anonymous: user_info = UserInfo() recommend_author = UserInfo.objects.all().order_by('-words', '-fans', '-loved')[:5] else: user_info = UserInfo.objects.get(user_id=user.id) recommend_author = UserInfo.objects.all().exclude(user__followed__following=user).order_by('-words', '-fans', '-loved')[:5] article = Article.objects.filter(if_publish=True).order_by('-last_modified_time')[:6] for author in recommend_author: if author.words > 999: author.words = str(round(author.words/1000, 1)) + 'k' else: author.words = str(author.words) if author.loved > 999: author.loved = str(round(author.loved/1000, 1)) + 'k' else: author.loved = str(author.loved) content = { "user": user, "user_info": user_info, "article": article, "rec_author": recommend_author } return render(request, 'bbs/index.html', content)
def setting_profile(request): if request.method == 'GET': try: user_info = UserInfo.objects.get(user_id=request.user.id) except UserInfo.DoesNotExist: user_info = UserInfo() content = { "user_info": user_info } return render(request, 'bbs/profile.html', content)
def get_main_page(request): if request.method == 'GET': user_id = request.user.id try: user_info = UserInfo.objects.get(user_id=user_id) except UserInfo.DoesNotExist: user_info = UserInfo() if request.GET.get('user', None): user_id = request.GET.get('user') other_user = UserInfo.objects.get(user_id=user_id) content = {"user_info": user_info, "other_user": other_user} return render(request, 'bbs/info.html', content)
def get_following_article(request): if request.method == 'GET': try: user_info = UserInfo.objects.get(user_id=request.user.id) except UserInfo.DoesNotExist: user_info = UserInfo() friends = FriendShip.objects.filter(following=request.user).exclude(followed=request.user) article = Article.objects.filter(author__followed__following=request.user).order_by('-last_modified_time')[:6] content = { "user_info": user_info, "followers": friends, "article": article } return render(request, 'bbs/follow.html', content)
def love_article(request): if request.method == 'GET': user_id = request.user.id try: user_info = UserInfo.objects.get(user_id=user_id) except UserInfo.DoesNotExist: user_info = UserInfo() if request.GET.get('user', None): user_id = request.GET.get('user') other_user = UserInfo.objects.get(user_id=user_id) article = User.objects.get(id=user_id).love.all() content = { "user_info": user_info, "article": article, "other_user": other_user } return render(request, 'bbs/loved.html', content) elif request.method == 'POST': article_id = int(request.POST.get('article_id')) article = Article.objects.get(id=article_id) user = UserInfo.objects.get(user=article.author) if article.loves.filter(id=request.user.id): with transaction.atomic(): article.loves.remove(request.user) article.love = article.love - 1 article.save() user.loved -= 1 user.save() status = 0 else: with transaction.atomic(): article.loves.add(request.user) article.love = article.love + 1 article.save() user.loved += 1 user.save() status = 1 content = { "status": status } return HttpResponse(dumps(content))