Esempio n. 1
0
    def post(self, request, format=None):
        _name = request.user.customer.name
        _avatar_url = request.user.customer.avatar_url
        if 'condition' in request.data:
            required_fields = [
                'age_range', 'height_range', 'profession', 'education',
                'income', 'marital_status', 'child_status', 'years_to_marry'
            ]
            for key in required_fields:
                if key not in json.loads(request.data.get('condition')):
                    return Response(data={'detail': 'condition缺少字段或格式错误'},
                                    status=status.HTTP_400_BAD_REQUEST)

        serializer = CustomerSerializer(request.user.customer,
                                        data=request.data,
                                        partial=True)
        if serializer.is_valid(raise_exception=True):
            if 'avatar_url' in serializer.validated_data:
                serializer.validated_data[
                    'avatar_url'] = serializer.validated_data['avatar_url']
                avatar_url = serializer.validated_data.pop('avatar_url')
                serializer.validated_data['avatar_status'] = 0
                mm_Picture.add_picture(request.user.customer.id, avatar_url)
            serializer.save()
            if any([
                    not _name == serializer.data['name'],
                    not _avatar_url == serializer.data['avatar_url']
            ]):
                IMServe.refresh_token(request.user.customer.id,
                                      request.user.customer.name,
                                      request.user.customer.avatar_url)
            return Response(Tool.format_data(serializer.data))
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
def make_verified(modeladmin, request, queryset):
    for picture in queryset:
        picture.customer.avatar_url = picture.url
        picture.customer.avatar_status = 1
        picture.customer.save()
        picture.is_verified = True
        picture.save()
        IMServe.refresh_token(picture.customer.id, picture.customer.name, picture.url)
Esempio n. 3
0
 def get(self, request, format=None):
     data = IMServe.gen_token(request.user.customer.id,
                              request.user.customer.name,
                              request.user.customer.avatar_url)
     request.user.customer.im_token = data['token']
     request.user.customer.save()
     return Response(Tool.format_data(data))
Esempio n. 4
0
    def add(self, account, password, **kwargs):
        try:
            with transaction.atomic():
                user = self._add_user(account, password)
                customer = self.create(user=user, account=account, **kwargs)
                customer.im_token = IMServe.gen_token(customer.id, account)['token']
                customer.save()

                return customer
        except IntegrityError:
            raise DBException('账号已注册')
        except:
            msg = traceback.format_exc()
            raise DBException(msg)
Esempio n. 5
0
 def get_toptics(self, name, customer_id, user_id, logo_url=None):
     topic, created = self.get_or_create(name=name, defaults={'customer_id': customer_id, 'logo_url': logo_url})
     if created:
         group_id = topic.id
         IMServe.create_group(customer_id, group_id=group_id, group_name=name)
     return topic
Esempio n. 6
0
 def destroy(self, request, *args, **kwargs):
     obj = self.get_object()
     IMServe.leave_group(request.user.customer.id, obj.id)
     return Response(Tool.format_data(msg=messages.LEAVE_GROUP_OK))
Esempio n. 7
0
 def retrieve(self, request, *args, **kwargs):
     obj = self.get_object()
     IMServe.join_group(request.user.customer.id, obj.id, obj.name)
     return Response(Tool.format_data(msg=messages.JOIN_GROUP_OK))
Esempio n. 8
0
 def delete(self, request, *args, **kwargs):
     topic = self.get_object()
     IMServe.destory_group(topic.customer.user.id, topic.id)
     return super().delete(request, *args, **kwargs)