Exemple #1
0
def topicomment(request):
	if request.is_ajax() and request.method == 'POST':
#		print request.POST.get('comment')
		text = request.POST.get('comment')
		topicid = request.POST.get('topicid')
		topic = Topic.objects.get(pk=topicid)
#		print request.POST.get('comment')
#		print request.POST.get('topicid')
		user = request.user
#		print user
		try:
			c = Comment(user=user, topic=topic, text=text)
			c.save()
			if topic.writer != user:
				notify.send(sender=user, target_object= None
						, recipient = topic.writer, verb='#'
						, text=text, target_article = None
						, target_products = None
						, target_topic = topic)
				cachekey = "user_unread_count" + str(topic.writer.id)
				if cache.get(cachekey) != None:
					cache.incr(cachekey)
				else:
					unread = Notification.objects.filter(recipient = topic.writer).filter(read = False).count()
					cache.set(cachekey,  unread, settings.CACHE_EXPIRETIME)
			topic.updated = timezone.now()
			instancesave.delay(topic)
			cachekey = "topic_comment_" + str(topicid)
			if cache.get(cachekey) != None:
				cache.incr(cachekey)
			else:
				cache.set(cachekey, topic.comment_set.count(), settings.CACHE_EXPIRETIME)
			userlist = atwho(text = text, sender = user, targetcomment = None, targetproducts = None
							, targetarticle = None, targetopic = topic)
			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);
				test = "@<a href='" +'/user/'+str(atwhouser.id)+'/informations/'+"'>"+atwhouser.username+"</a>"+' '
				test1 = "@<a href='" +'/user/'+str(atwhouser.id)+'/informations/'+"'>"+atwhouser.username+"</a>"+'&nbsp;'
				text = text.replace('@'+item+' ', test);
				text = text.replace('@'+item+'&nbsp;', test1);
			# c = Comment(user=user, topic=topic, text=text)
			# c.save()
			data = {
			"user": user.username,
			"text": text,
			"commentid": c.id
			}
#			print data['commentid']
			json_data = json.dumps(data)
			print 'data'
			return HttpResponse(json_data, content_type='application/json')
		except:
			traceback.print_exc()
			raise Http404(traceback)
	else:
		raise Http404
	def setread(self):
		self.read = True
		instancesave.delay(self)
		cachekey = "user_unread_count" + str(self.recipient.id)
		if cache.get(cachekey) != None:
			cache.decr(cachekey)
			return ''
		else:
			unread = Notification.objects.filter(recipient = self.recipient).filter(read = False).count()
			cache.set(cachekey,  unread, settings.CACHE_EXPIRETIME)
			return ''
Exemple #3
0
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')
Exemple #4
0
def new_notification(sender, **kwargs):
    kwargs.pop("signal", None)
    target_object = kwargs.pop("target_object", None)
    text = kwargs.pop("text")
    verb = kwargs.pop("verb")
    sender_object = sender
    recipient = kwargs.pop("recipient")
    target_article = kwargs.pop("target_article", None)
    target_topic = kwargs.pop("target_topic", None)
    target_products = kwargs.pop("target_products", None)
    try:
        c = Notification(
            target_object=target_object,
            sender_object=sender_object,
            target_topic=target_topic,
            target_article=target_article,
            target_products=target_products,
            recipient=recipient,
            verb=verb,
            text=text,
        )
        instancesave.delay(c)
    except:
        traceback.print_exc()