Esempio n. 1
0
 def post(self, request, *args, **kwargs):
     '''
     用户登录
     '''
     username = request.data.get('username')
     password = request.data.get('password')
     user = authenticate(username=username, password=password)
     if user:
         payload = jwt_payload_handler(user)
         token = jwt_encode_handler(payload)
         user.token = token
         return ApiResponse({'token': token}, code=200, msg='ok')
     else:
         return ApiResponse(code=400, msg='用户名或密码错误')
Esempio n. 2
0
 def get(self, request):
     '''
     获取图片验证码
     :param request:
     :return:
     '''
     hashkey = CaptchaStore.generate_key()
     _id = CaptchaStore.objects.filter(hashkey=hashkey).first().id
     image = captcha_image(request, hashkey)
     # 将图片转换为base64
     image_base = base64.b64encode(image.content)
     data = {"id": _id, "image_base": image_base.decode('utf-8'), "type": "image/png", "encoding": "base64"}
     return ApiResponse(data, code=200, msg='ok', status=status.HTTP_201_CREATED)
Esempio n. 3
0
    def update(self, request, *args, **kwargs):
        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        if getattr(instance, '_prefetched_objects_cache', None):
            # If 'prefetch_related' has been applied to a queryset, we need to
            # forcibly invalidate the prefetch cache on the instance.
            instance._prefetched_objects_cache = {}

        return ApiResponse(serializer.data, code=200, msg='ok')
Esempio n. 4
0
 def get(self, request):
     '''
     获取当前用户信息
     '''
     if request.user.id is not None:
         data = {
             "id": request.user.id,
             "username": request.user.username,
             "nickname": request.user.nickname,
             "avatar": request._request._current_scheme_host + '/media/' + str(request.user.avatar),
             "email": request.user.email,
             "verify": request.user.verify
         }
         return ApiResponse(data, code=200, msg='ok')
     else:
         raise exceptions.APIException('用户不存在!')
Esempio n. 5
0
 def post(self, request, *args, **kwargs):
     '''
     用户头像上传更新 上传图片类型文件,参数名: avatar
     '''
     if request.user.id is not None:
         avatar = request.FILES.get('avatar',None)
         if not avatar:
             raise exceptions.APIException("avatar文件未指定")
         ext = avatar.name.split('.')[-1]
         if ext.lower() not in ["jpg","jpeg","png"]:
             raise exceptions.APIException("上传文件类型必须为:jpg,jpeg,png")
         request.user.avatar = avatar
         request.user.save()
         data = {
             "uid": request.user.id,
             "username": request.user.username,
             "avatar": request._request._current_scheme_host + '/media/' + str(request.user.avatar)
         }
         return ApiResponse(data, code=200, msg='ok')
     else:
         raise exceptions.APIException('用户不存在!')
Esempio n. 6
0
 def post(self, request, *args, **kwargs):
     # 邮件验证
     email = request.data.get("email",None)
     if not email:
         raise exceptions.APIException("email参数必传")
     if request.user.verify:
         raise exceptions.APIException("用户已激活")
     info = {
         'uid': request.user.id,
         'email': email
     }
     sign_token = self.ser.dumps(info).decode()
     verify_url = request._request._current_scheme_host + request.path_info + f'?sign_token={sign_token}'
     html_message = '<h1>{},欢迎注册</h1>请点击下面链接激活您的用户<br><a href="{}" rel="external nofollow" >{}</a>'.format(
         request.user.username, verify_url, verify_url)
     try:
         send_mail('Salad邮箱验证','',settings.EMAIL_FROM,[email,],html_message=html_message)
     except:
         raise exceptions.APIException("邮件发送失败,请确认邮箱地址是否正确")
     request.user.email = email
     request.user.save()
     return ApiResponse(info,code=200, msg='ok')
Esempio n. 7
0
 def create(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     self.perform_create(serializer)
     headers = self.get_success_headers(serializer.data)
     return ApiResponse(serializer.data, code=200, msg='ok', status=status.HTTP_201_CREATED, headers=headers)