def post(self, request): """ 发布一条评论 ``POST`` `comments/create/ <http://192.168.1.222:8080/v1/comments/create>`_ :param tid: 评论对象(瓦片)的 id. :param content: 评论内容 """ params = request.POST.copy() tid = params.get("tid") if not params.get('name', ''): params["name"] = request.user.get_full_name() or request.user.username if not params.get('email', ''): params["email"] = request.user.email try: ct = ContentType.objects.get_by_natural_key("kinger", "tile") except ContentType.DoesNotExist: return rc.NOT_HERE comment = Comment(object_pk=tid, content_type=ct, site_id=settings.SITE_ID) comment.comment = params.get("content") comment.user_name = params.get("name") comment.email = params.get("email") comment.ip_address = request.META.get("REMOTE_ADDR", None) if request.user.is_authenticated(): comment.user = request.user # Signal that the comment is about to be saved responses = signals.comment_will_be_posted.send( sender=comment.__class__, comment=comment, request=request ) for (receiver, response) in responses: if response == False: return rc.BAD_REQUEST # return CommentPostBadRequest( # "comment_will_be_posted receiver %r killed the comment" % receiver.__name__) # Save the comment and signal that it was saved comment.save() signals.comment_was_posted.send( sender=comment.__class__, comment=comment, request=request ) return comment
def add_comment(self, request, article, profile, c): variables = ["comment_cache_%s-%s" % (article.id, profile.user.id),] #self.debug(variables) #hash = md5_constructor(u':'.join([urlquote(var) for var in variables])) #cache_key = 'template.cache.%s.%s' % ('comments', hash.hexdigest()) #cache_key = 'template.cache.%s.%s' % ('commenton', hash.hexdigest()) comment = Comment(); comment.ip_address = request.META.get("REMOTE_ADDR", None) comment.user = profile.user comment.comment = c comment.content_type = ContentType.objects.get(app_label="articles", model="article") comment.object_pk = article.id comment.site = Site.objects.get(id=1) comment.save() cache.delete(variables) #cache.delete(cache_key) return comment