def unfollow_ask(request, pk): data = dict(r=1, ) if request.method == 'POST': user = request.user ask = Ask.objects.filter(id=pk).first() if ask is not None: ret = user.unfollow_ask(ask) if ret is True: data['r'] = 0 logger.info('{} 取消关注了问题: {}'.format(user, ask.id)) else: logger.error('{} 取消关注问题失败: {}'.format(user, ask.id)) return JsonResponse(data, status=201)
def uncollect(request, pk): data = dict( r=1, ) if request.method == 'POST': user = request.user answer = Answer.objects.filter(id=pk).first() if answer is not None: ret = user.uncollect(answer) if ret is True: data['r'] = 0 logger.info('{} 取消了收藏: {}'.format(user, answer.id)) else: logger.error('{} 取消收藏失败: {}'.format(user, answer.id)) return JsonResponse(data, status=201)
def vote_down(request, pk): data = dict( r=1, ) if request.method == 'POST': user = request.user answer = Answer.objects.filter(id=pk).first() if answer is not None: ret = user.votedown(answer) if ret is True: data['r'] = 0 data['count'] = answer.votes logger.info('{} 取消了赞: {}'.format(user, answer.id)) else: logger.error('{} 取消赞失败: {}'.format(user, answer.id)) return JsonResponse(data, status=201)
def form_valid(self, form): try: answer = Answer.objects.filter(id=self.kwargs['pk']).first() except Answer.DoesNotExist: logger.error('评论错误: 答案 {} 不存在'.format(self.kwargs['pk'])) return redirect(self.request.META.get('HTTP_REFERER', '/')) comment = form.save(commit=False) comment.author = self.request.user comment.answer = answer reply_id = self.request.POST.get('reply_id', 0) try: reply = Comment.objects.filter(id=reply_id).first() except ValueError: logger.error('回复评论错误: reply_id = {}'.format(reply_id)) return redirect(self.request.META.get('HTTP_REFERER', '/')) comment.reply_to = reply comment.save() logger.info('{} 评论了 {} 的回答'.format(comment.author, answer.author)) return redirect(self.request.META.get('HTTP_REFERER', '/'))
def form_invalid(self, form): logger.error('comment error') return redirect(self.request.META.get('HTTP_REFERER', '/'))
def form_invalid(self, form): logger.error('提问题错误') return redirect('index')
def form_invalid(self, form): logger.error('{} 修改资料格式错误'.format(self.object))