示例#1
0
    def remove_user(self, request, *args, **kwargs):
        group = self.get_object()
        UserInGroup.objects.remove_user(request.user, group)

        request.user.refresh_from_db()
        ret = UserDetailSerializer(request.user).data
        return success_rsp(ret)
示例#2
0
    def add_user(self, request, *args, **kwargs):
        group = self.get_object()
        UserInGroup.objects.add_user(request.user, group)

        raise_heat.delay(group.id, 'UserGroup', 1)
        request.user.refresh_from_db()
        ret = UserDetailSerializer(request.user).data
        return success_rsp(ret)
示例#3
0
 def like(self, request, *args, **kwargs):
     message = self.get_object()
     try:
         Likes.objects.create(message=message, user=request.user)
         add_likes.delay(message.id)
         raise_heat.delay(message.id, 'Message', 1)
     except IntegrityError as e:
         raise BusinessException('1011')
     return success_rsp()
示例#4
0
 def check_password(self, request, *args, **kwargs):
     form = CheckPasswordForm(data=request.data)
     check_or_business_error(form.is_valid(),
                             '1000',
                             error_data=form.errors)
     check_or_business_error(
         request.user.check_password(form.validated_data.get('password')),
         '1002')
     return success_rsp()
示例#5
0
 def change(self, request, *args, **kwargs):
     form = UpdateUserForm(request.user, data=request.data, partial=True)
     check_or_business_error(form.is_valid(),
                             '1000',
                             error_data=form.errors)
     form.save()
     request.user.refresh_from_db()
     ret = UserDetailSerializer(request.user).data
     return success_rsp(ret)
示例#6
0
 def update(self, request, *args, **kwargs):
     group = self.get_object()
     check_or_business_error(group.create_user == request.user, '1008')
     form = UserGroupForm(data=request.data, partial=True)
     check_or_business_error(form.is_valid(),
                             '1000',
                             error_data=form.errors)
     UserGroup.objects.update_group(group, **form.validated_data)
     return success_rsp()
示例#7
0
 def post(self, request, *args, **kwargs):
     phone_number = kwargs.get('phone')
     sms_client = SMSClient(phone_number)
     sms_type = request.query_params.get('type')
     check_or_business_error(
         sms_type in ('register', 'change_mobile', 'reset_password'),
         '1009')
     sms_client.send_sms(sms_type)
     return success_rsp()
示例#8
0
 def create(self, request, *args, **kwargs):
     form = UserGroupForm(data=request.data)
     check_or_business_error(form.is_valid(),
                             '1000',
                             error_data=form.errors)
     group = UserGroup.objects.create_group(request.user,
                                            **form.validated_data)
     check_or_business_error(group is not None, '1007')
     data = self.get_serializer(group).data
     return success_rsp(data)
示例#9
0
 def change_mobile(self, request, *args, **kwargs):
     form = ChangeMobileForm(data=request.data)
     check_or_business_error(form.is_valid(),
                             '1000',
                             error_data=form.errors)
     request.user.mobile = form.validated_data.get('mobile')
     request.user.save()
     request.user.refresh_from_db()
     ret = UserDetailSerializer(request.user).data
     return success_rsp(ret)
示例#10
0
 def reset_password(self, request, *args, **kwargs):
     form = ResetPasswordForm(data=request.data)
     check_or_business_error(form.is_valid(),
                             '1000',
                             error_data=form.errors)
     user = User.objects.filter(
         mobile=form.validated_data.get('mobile')).first()
     check_or_business_error(user, '1014')
     user.set_password(form.validated_data.get('new_password'))
     user.save()
     return success_rsp()
示例#11
0
    def add_topic(self, request, *args, **kwargs):
        group = self.get_object()
        check_or_business_error(group in request.user.member_groups.all(),
                                '1010')
        form = TopicForm(data=request.data)
        check_or_business_error(form.is_valid(),
                                '1000',
                                error_data=form.errors)
        topic = form.save(group=group, create_user=request.user, is_topic=True)
        data = TopicDetailSerializer(topic).data

        raise_heat.delay(group.id, 'UserGroup', 2)
        return success_rsp(data)
示例#12
0
 def post(self, request, *args, **kwargs):
     form = RegisterForm(data=request.data)
     check_or_business_error(form.is_valid(),
                             error_code='1000',
                             error_data=form.errors)
     user = User.objects.create_user(
         form.validated_data['mobile'],
         form.validated_data['password'],
         form.validated_data['nickname'],
     )
     check_or_business_error(user is not None, '1003')
     login(request, user)
     user_data = UserDetailSerializer(user).data
     return success_rsp(user_data)
示例#13
0
 def post(self, request, *args, **kwargs):
     form = LoginForm(data=request.data)
     check_or_business_error(form.is_valid(),
                             error_code='1000',
                             error_data=form.errors)
     differ = {'exist': True}
     user = authenticate(request,
                         username=form.validated_data['username'],
                         password=form.validated_data['password'],
                         differ=differ)
     if not user:
         if differ['exist']:
             return error_rsp('1002', u'密码错误')
         else:
             return error_rsp('1001', u'用户名不存在')
     login(request, user)
     user_data = UserDetailSerializer(user).data
     return success_rsp(user_data)
示例#14
0
    def answer_topic(self, request, *args, **kwargs):
        topic = self.get_object()
        form = AnswerTopicForm(data=request.data)
        check_or_business_error(form.is_valid(),
                                '1000',
                                error_data=form.errors)
        answer_message = form.validated_data.get('answer_to_message')
        if answer_message:
            check_or_business_error(answer_message.answer_to_topic == topic,
                                    '1012')
        answer = form.save(create_user=request.user,
                           answer_to_topic=topic,
                           group=topic.group,
                           is_topic=False)
        data = AnswerSerializer(answer).data

        add_answers.delay(topic.id)
        raise_heat.delay(topic.id, 'Message', 2)
        return success_rsp(data)
示例#15
0
    def retrieve(self, request, *args, **kwargs):
        topic = self.get_object()
        page_replies = self.paginate_queryset(
            topic.replies.all().order_by('create_time'))
        topic_data = TopicDetailSerializer(topic).data
        replies_data = AnswerSerializer(page_replies, many=True).data
        replies_data = {
            'count': self.paginator.page.paginator.count,
            'data': replies_data
        }
        like_data = []
        if request.user.is_authenticated:
            q = Q(message=topic) | Q(message__in=page_replies)
            like_data = request.user.likes_set.filter(q).values_list('message',
                                                                     flat=True)

        ret = {
            'topic': topic_data,
            'replies': replies_data,
            'likes': like_data
        }
        return success_rsp(ret)
示例#16
0
 def details(self, request, *args, **kwargs):
     ret = UserDetailSerializer(request.user).data
     return success_rsp(ret)
示例#17
0
 def post(self, request, *args, **kwargs):
     logout(request)
     return success_rsp()
示例#18
0
    def retrieve(self, request, *args, **kwargs):
        group = self.get_object()
        data = self.get_serializer(group).data

        raise_heat.delay(group.id, 'UserGroup', 1)
        return success_rsp(data)