Exemple #1
0
 def form_valid(self, form):
     ask = form.save(commit=False)
     ask.author = self.request.user
     ask.save()
     logger.info('{} 提了问题:{}'.format(self.request.user, ask))
     topics = self.request.POST.get('topics_list', '')
     topics = topics.split(',')
     ask.add_topics(topics)
     return redirect('asks:detail', pk=ask.id)
Exemple #2
0
 def form_valid(self, form):
     ask_id = self.kwargs['pk']
     ask = Ask.objects.filter(id=ask_id).first()
     user = self.request.user
     if ask is not None:
         answer = form.save(commit=False)
         answer.ask = ask
         answer.author = user
         answer.save()
         logger.info('{} 回答了问题 : {}'.format(self.request.user, answer))
     return redirect(self.request.META.get('HTTP_REFERER', '/'))
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
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)
Exemple #6
0
def register(request):
    if request.user.is_authenticated:
        return redirect('/')
    if request.method == 'POST':
        data = dict()
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save()
            auth.login(request, user)
            logger.info('register: {}'.format(user))
            data['ok'] = True
        else:
            data['ok'] = False
            data['errors'] = form.errors
        return JsonResponse(data, status=200)
    else:
        form = RegisterForm()
    return render(request, 'users/register.html', {'form': form})
Exemple #7
0
 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', '/'))
Exemple #8
0
def login(request):
    if request.user.is_authenticated:
        return redirect('/')
    if request.method == 'POST':
        data = dict()
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(username=username, password=password)
        if user is not None and user.is_active:
            auth.login(request, user)
            logger.info('login: {}'.format(user))
            data['ok'] = True
        else:
            data['ok'] = False
            data['message'] = '用户名或密码错误'
        return JsonResponse(data, status=200)
    else:
        form = LoginForm()
    return render(request, 'users/login.html', {'form': form})
Exemple #9
0
 def get_success_url(self):
     logger.info('评论:{} 删除成功'.format(self.object.id))
     return self.request.META.get('HTTP_REFERER', '/')
Exemple #10
0
def unfollow(request, pk):
    user = request.user
    if user.unfollow(pk):
        logger.info('{} unfollow: {}'.format(user, User.objects.get(id=pk)))
    return redirect(request.META.get('HTTP_REFERER', '/'))
Exemple #11
0
 def get_success_url(self):
     logger.info('{} 修改密码成功'.format(self.request.user))
     return reverse('users:detail', kwargs={'pk': self.request.user.id})
Exemple #12
0
 def form_valid(self, form):
     user = self.object
     user.update(form.cleaned_data)
     logger.info('{} 修改了资料'.format(user))
     return redirect('users:detail', pk=user.id)