def productscommentcomment(request): if request.is_ajax() and request.method == 'POST': print 'commentcomment' text = request.POST.get('comment') productsid = request.POST.get('productsid') #parenttext = request.POST.get('parenttext') preentid = request.POST.get('preentid') products = Products.objects.get(pk=productsid) #comment = Comment.objects.filter(products=products) targetcomment = Comment.objects.get(pk=preentid) user = request.user if targetcomment.user == user: pass; else: receiver = targetcomment.user.username text="@"+receiver+" "+text try: c = Comment(user=user, products=products, text=text, parent=targetcomment) c.save() #文章回复数量缓存 增加1 cachekey = "products_comment_" + str(productsid) if cache.get(cachekey) != None: cache.incr(cachekey) else: cache.set(cachekey, products.comment_set.count(), settings.CACHE_EXPIRETIME) #被评论的评论readers+1放到消息队列中 readersin.delay(targetcomment) #返回@用户的列表,并向@的用户发送消息 userlist = atwho(text = text, sender = user, targetcomment = targetcomment, targetarticle = None , targetproducts = products, targetopic = None ) #给被@的用户增加链接 for item in userlist: print 'for item in userlist:' atwhouser = MyUser.objects.get(username = item) test = "@<a href='" +'/user/'+str(atwhouser.id)+'/informations/'+"'>"+atwhouser.username+"</a>"+' ' text = text.replace('@'+item+' ', test); data = { "user": user.username, "text": text, "parentcommentext": c.parent.text, "parentcommentuser": str(c.parent.user), "commentid":c.id, } json_data = json.dumps(data) return HttpResponse(json_data, content_type='application/json') except: traceback.print_exc() raise Http404(traceback) else: raise Http404
def commentdislike(request): try: commentid = request.POST.get('commentid') comment = Comment.objects.get(pk=commentid) user = request.user except Article.DoesNotExist: raise Http404("Article does not exist") try: commentdislike = CommentDisLike.objects.get(comment=comment, user=user) except: commentdislike = None if commentdislike: commentdislikecount = -1 #commentdislike.delete() instancedelete.delay(commentdislike) #减去缓存中评论点赞数 cachekey = "comment_dislike_count_" + str(comment.id) if cache.get(cachekey) != None: cache.decr(cachekey) else: cache.set(cachekey, comment.commentdislike_set.count()) cache.decr(cachekey) # comment.readers = comment.readers - 1 # comment.save() readersout.delay(comment) else: commentdislikecount = +1 #加上缓存中评论点赞数 cachekey = "comment_dislike_count_" + str(comment.id) if cache.get(cachekey) != None: cache.incr(cachekey) else: cache.set(cachekey, comment.commentdislike_set.count()) cache.incr(cachekey) # comment.readers = comment.readers + 1 # comment.save() readersin.delay(comment) c = CommentDisLike(user=user, comment=comment) #c.save() instancesave.delay(c) data = { 'commentdislikecount': commentdislikecount, } json_data = json.dumps(data) #print 'commentlike' return HttpResponse(json_data, content_type='application/json')
def article_detail(request, article_id): try: article = Article.objects.get(pk=article_id) #文章回复数 #numofcomment = article.comment_set.count() #作者文章总数 #numwriter = article.writer.article_set.count() #文章所有类别 #category = Category.objects.all() #作者所有文章总阅读数 # numreaders = 0 # for x in article.writer.article_set.all(): # numreaders = x.readers + numreaders except Article.DoesNotExist: raise Http404("Article does not exist") #阅读数+1 # article.readers += 1 # article.save() #阅读数+1,将readers+1的操作放到celery中,加快网页的加载时间 readersin.delay(article) #缓存的readers 增加1 cachekey = "article_readers_" + str(article_id) if cache.get(cachekey) != None: cache.incr(cachekey) else: cache.set(cachekey, article.readers, settings.CACHE_EXPIRETIME) #当前读者对象 user = request.user #读者是否收藏该文章 try: collection = Collection.objects.get(article=article, user=user.id) collection = '已收藏' except: collection = '收藏' # if collection: # collection = '已收藏' # else: # collection = '收藏' #评论按热度排序 commentorderbyreaders = Comment.objects.filter(article=article).filter(readers__gt=ARTICLE_DETAIL_HOTCOMMENT_READERSRANGE).order_by('-readers')[0:3] #commentorderbyreaderscount = commentorderbyreaders.count() #文章回复按时间 comment = Comment.objects.filter(article=article).order_by('-timestamp') #文章回复数大于5才有更多回复按钮 if comment.count() > 5: moercomment = True else: moercomment = False#链接最热回复和按时间排序的回复 #链接热度排序和时间排序回复 # comment = list(chain(commentorderbyreaders, comment)) # comment = sorted(set(comment), key=comment.index) #显示头5个回复 comment = comment[:5] #把本页地址装入session request.session['lastpage'] = request.get_full_path() #分享链接的地址 sharelink = request.get_host()+request.get_full_path() print sharelink #文章属于哪些类别 thisrelationtag = Relation.objects.filter(article=article) print thisrelationtag[0].category #右边栏文章统一类的按readers排序 hotarticle = Article.objects.all().filter(timestamp__gte=datetime.date.today() - timedelta(days=ARTICLE_DETAIL_RIGHTSIDERANK_TIMERANGE)).filter(category=thisrelationtag[0].category).exclude(id = article.id).order_by('-readers')[:5] #统一类的按timestamp排序文章 thisrelationtagarticle = Relation.objects.filter(category=thisrelationtag[0].category).exclude(article = article).order_by('-timestamp') print thisrelationtagarticle.count() # if thisrelationtagarticle.count()==0:#如果类别中除了自己就没有其他的文章了,就需要从另外的列别中查找文章 # thisrelationtagarticle = Relation.objects.filter(category=thisrelationtag[1].category).exclude(article = article).order_by('-timestamp') # if thisrelationtagarticle.count()==0: # thisrelationtagarticle = Relation.objects.filter(category=thisrelationtag[2].category).exclude(article = article).order_by('-timestamp') #文章被多少人收藏过 #usercollectioncount = Collection.objects.filter(article=article).count() context = { 'article':article, 'user':user, "form": CommentForm, #"submit_btn": "发表", "comment": comment, #'numofcomment': numofcomment, #'numwriter': numwriter, #'numreaders': numreaders, 'hotarticle': hotarticle, #'category': category, 'moercomment': moercomment, 'collection' : collection, 'thisrelationtag' : thisrelationtag, 'thisrelationtagarticle': thisrelationtagarticle[0:ARTICLE_DETAIL_REALATIONARTICLE_COUNT], #'usercollectioncount' : usercollectioncount, 'sharelink': sharelink, "commentorderbyreaders": commentorderbyreaders, } return render(request, 'article_detail.html', context)
def topic_detail(request, topic_id): try: topic = Topic.objects.get(pk=topic_id) except Topic.DoesNotExist: raise Http404("Does not exist") #count = Comment.objects.filter(topic=topic).count() # 按时间顺序排序 comment = Comment.objects.filter(topic=topic).filter(parent=None).order_by('timestamp') # 前三个回复是最热回复; readers__gt=3 = readers 大于3 commentorderbyreaders = Comment.objects.filter(topic=topic).filter(parent=None).filter(readers__gt=TOPIC_DETAIL_HOTCOMMENT_READERSRANGE).order_by('-readers')[0:3] #最新话题 #newtopic = Topic.objects.filter(group=topic.group).order_by('-timestamp')[0:3] #最热话题 hottopic = Topic.objects.filter(group=topic.group).filter(timestamp__gte=datetime.date.today() - timedelta(days=TOPIC_DETAIL_HOTTOPIC_TIMERAGE)).order_by('-readers')[0:5] #当前读者对象 user = request.user #读者是否收藏该文章 collection = CollectionTopic.objects.filter(topic=topic, user=user.id) print type(collection) if collection: collection = '已收藏' else: collection = '收藏' # 分页 paginator = Paginator(comment, 5) page = request.GET.get('page') try: contacts = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. contacts = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. contacts = paginator.page(paginator.num_pages) # topic.readers += 1 # topic.save() readersin.delay(topic) #缓存的readers 增加1 cachekey = "topic_readers_" + str(topic_id) if cache.get(cachekey) != None: cache.incr(cachekey) else: cache.set(cachekey, topic.readers, settings.CACHE_EXPIRETIME) #如果页数大于1则不显示最热回复 ifhotcomment = True; if page: page = int(page) if page > 1: ifhotcomment = None; else: page = 1; request.session['lastpage'] = request.get_full_path() #分享链接的地址 sharelink = request.get_host()+request.get_full_path() context = { 'topic':topic, 'user':user, "form": CommentForm, "submit_btn": "发表", "comment": comment, 'contacts': contacts, #"count": count, #"newtopic": newtopic, "group": topic.group, "hottopic": hottopic, "commentorderbyreaders":commentorderbyreaders,#热门回复 "ifhotcomment": ifhotcomment, 'page': page, 'collection': collection, 'sharelink': sharelink, } #print "topic_detail" return render(request, 'topic_detail.html', context)