コード例 #1
0
ファイル: views.py プロジェクト: kij8323/mynewspaper
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
コード例 #2
0
ファイル: views.py プロジェクト: kij8323/mynewspaper
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
コード例 #3
0
ファイル: views.py プロジェクト: kij8323/mynewspaper_current
def topcommentcomment(request):
	if request.is_ajax() and request.method == 'POST':
		text = request.POST.get('comment')
		topicid = request.POST.get('topicid')
		#parenttext = request.POST.get('parenttext')
		preentid = request.POST.get('preentid')
		topic = Topic.objects.get(pk=topicid)
		comment = Comment.objects.filter(topic=topic)
		targetcomment = Comment.objects.get(pk=preentid)
		user = request.user
		print user
		try:
			c = Comment(user=user, topic=topic, text=text, parent=targetcomment)
			c.save()
			topic.updated = timezone.now()
			topic.save()
			cachekey = "topic_comment_" + str(topicid)
			if cache.get(cachekey) != None:
				cache.incr(cachekey)
			else:
				cache.set(cachekey, topic.comment_set.count(), settings.CACHE_EXPIRETIME)
			targetcomment.readers = targetcomment.readers + 1
			targetcomment.save()
			userlist = atwho(text = text, sender = user, targetcomment = targetcomment
							, 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);
			# c = Comment(user=user, topic=topic, text=text, parent=targetcomment)
			# c.save()
			print 'z'
			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
コード例 #4
0
ファイル: views.py プロジェクト: kij8323/mynewspaper
def articlecomment(request):
	if request.is_ajax() and request.method == 'POST':
		text = request.POST.get('comment')#评论的内容
		articleid = request.POST.get('articleid')
		article = Article.objects.get(pk=articleid)
		user = request.user
		try:
			c = Comment(user=user, article=article, text=text)
			c.save()
			#文章回复数量redis缓存 增加1
			cachekey = "article_comment_" + str(articleid)
			if cache.get(cachekey) != None:
				cache.incr(cachekey)
			else:
				cache.set(cachekey, article.comment_set.count(), settings.CACHE_EXPIRETIME)
			#返回@用户的列表,并向@的用户发送消息
			userlist = atwho(text = text, sender = user
							, targetcomment = None, targetarticle = article, targetproducts = None
							, 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);
				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);
			data = {
			"user": user.username,
			"text": text,
			"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
コード例 #5
0
ファイル: views.py プロジェクト: kij8323/mynewspaper
def newtopic(request):
	grouptitle = request.session.get('group', False)
	group = Group.objects.get(title = grouptitle)
	# group = False
	if request.method == 'POST':
		form = TopicForm(request.POST)
		if form.is_valid():
			content = form.cleaned_data['content']
			title = form.cleaned_data['title']
			new_topic = Topic()
			new_topic.content = content
			new_topic.title = title
			new_topic.writer = request.user
			new_topic.group = group
			new_topic.save()
			group.topicount += 1
			group.save()
			cachekey = "group_topic_count_" + str(group.id)
			if cache.get(cachekey) != None:
				cache.incr(cachekey)
			else:
				group = Group.objects.get(id=group.id)
				cache.set(cachekey,  group.topicount)
			userlist = atwho(text = content, sender = request.user, targetcomment = None, targetproducts = None
							, targetarticle = None, targetopic = new_topic)
			return redirect(request.session['lastpage'])
		else:
			messages.error(request, '您输入的话题内容有误,请改正!')
			return redirect("newtopic")
	else:
		print  request.user
		context = {
			'myform': TopicForm,
			'group': group,
			}
	return render(request, 'newtopic.html',  context)