コード例 #1
0
    def post(self, request):
        serializer = LoginSerializer(data=request.data)
        if serializer.is_valid():
            telephone = serializer.validated_data.get('telephone')
            try:
                user = User.objects.get(telephone=telephone)
                user.last_login = now()
                user.save()
            except:
                username = "******" + self.generate_number()
                password = ''
                user = User.objects.create(username=username,
                                           password=password,
                                           telephone=telephone,
                                           last_login=now())

            serializer = UserSerializer(user)
            token = authentications.generate_jwt(user, 7)
            return Response({
                'token': token,
                'user': serializer.data
            },
                            status=status.HTTP_200_OK)
        else:
            return Response(data={"message": dict(serializer.errors)},
                            status=status.HTTP_400_BAD_REQUEST)
コード例 #2
0
ファイル: views.py プロジェクト: xiaoxiaolulu/mtserver
 def post(self, request):
     serializer = AuthTokenSerializer(data=request.data)
     if serializer.is_valid():
         user = serializer.validated_data.get('user')
         user.last_login = now()
         user.save()
         token = generate_jwt(user)
         user_serializer = UserSerializer(user)
         return Response({"token": token, "user": user_serializer.data})
     else:
         return Response({"message": "用户名或密码错误!"}, status=status.HTTP_400_BAD_REQUEST)
コード例 #3
0
ファイル: views.py プロジェクト: yankangjia/meituan_Django
 def post(self, request):
     serializer = AuthTokenSerializer(data=request.data)
     if serializer.is_valid():
         user = serializer.validated_data.get('user')
         user.last_login = now()
         user.save()
         user_serializer = UserSerializer(instance=user)
         token = generic_jwt(user)
         return Response(data={
             'token': token,
             'user': user_serializer.data
         })
     else:
         return Response(data={'message': '用户名或密码错误'},
                         status=status.HTTP_400_BAD_REQUEST)
コード例 #4
0
 def post(self, request):
     # authenticate user info
     serializer = AuthTokenSerializer(data=request.data)
     # if it passed or valid, user is considered to logged in successfully
     if serializer.is_valid():
         user = serializer.validated_data.get('user')
         user.last_login = now()
         user.save()
         token = generate_jwt(user)
         user_serializer = UserSerializer(instance=user)
         return Response({
             "token": token,
             "user": user_serializer.data
         },
                         status=status.HTTP_200_OK)
     else:
         return Response({"message": "用户名或密码错误!"},
                         status=status.HTTP_401_UNAUTHORIZED)
コード例 #5
0
    def post(self, request):
        serializer = LoginSerializer(data=request.data)
        if serializer.is_valid():
            telephone = serializer.validated_data.get('telephone')

            try:
                user = User.objects.get(telephone=telephone)
                user.last_login = now()
                user.save()
            except:
                username = "******" + self.generate_sms_code()
                password = ""
                user = User.objects.create(username=username, password=password, telephone=telephone, last_login=now())

            serializer = UserSerializer(user)
            token = generate_jwt(user)
            return Response({"user": serializer.data, "token": token})
        else:
            return Response(data={"message": dict(serializer.errors)}, status=status.HTTP_400_BAD_REQUEST)
コード例 #6
0
ファイル: views.py プロジェクト: yankangjia/meituan_Django
 def post(self, request):
     serializer = LoginSerializer(data=request.data)
     if serializer.is_valid():
         telephone = serializer.validated_data.get('telephone')
         try:
             user = User.objects.get(telephone=telephone)
             user.last_login = now()
             user.save()
         except:
             username = '******' + self.generic_number()
             user = User.objects.create(telephone=telephone,
                                        username=username,
                                        password='',
                                        last_login=now())
         user_serializer = UserSerializer(instance=user)
         token = generic_jwt(user)
         return Response(data={
             'user': user_serializer.data,
             'token': token
         })
     else:
         return Response(data={'message': dict(serializer.errors)},
                         status=status.HTTP_400_BAD_REQUEST)
コード例 #7
0
ファイル: views.py プロジェクト: CDownPace/MeiTuan-PC-python
    def post(self,request):
        serializer = AuthTokenSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.validated_data.get('user')
            user.last_login = now()
            user.save()
            token = generate_jwt(user)
            user_serializer = UserSerializer(user)
            return Response({"token":token,"user":user_serializer.data})
        else:
            print(dict(serializer.errors))
            return Response({"message":"用户名或密码错误!"}
                            # ,status=status.HTTP_400_BAD_REQUEST
            )
 #
 # class MerchantViewSet(viewsets.ModelViewSet):
 #    queryset = Merchant.objects.order_by("-create_time").all()
 #    serializer_class = MerchantSerializer
 #    pagination_class = MerchantPagination
 #    permission_classes = [permissions.IsAuthenticated,IsEditorUser]

# Create,Update,Destroy,Retrieve
# class CategoryViewSet(
#     CmsBaseView,
#     viewsets.GenericViewSet,
#     mixins.CreateModelMixin,
#     mixins.UpdateModelMixin,
#     mixins.DestroyModelMixin,
#     mixins.RetrieveModelMixin
# ):
    # queryset = GoodsCategory.objects.all()
    # serializer_class = GoodsCategorySerializer
    # permission_classes = [permissions.IsAuthenticated, IsEditorUser]

    # def destroy(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     if instance.goods_list.count() > 0:
    #         return Response(status=status.HTTP_400_BAD_REQUEST)
    #     else:
    #         self.perform_destroy(instance)
    #         return Response(status=status.HTTP_204_NO_CONTENT)

    # # /cms/category/merchant/<int:merchant_id>
    # @action(['GET'],detail=False,url_path="merchant/(?P<merchant_id>\d+)")
    # def merchant_category(self,request,merchant_id=None):
    #     queryset = self.get_queryset()
    #     seriazlier_class = self.get_serializer_class()
    #     categories = queryset.filter(merchant=merchant_id)
    #     serializer = seriazlier_class(categories,many=True)
    #     return Response(serializer.data)

# class GoodsViewSet(
#     CmsBaseView,
#     viewsets.GenericViewSet,
#     mixins.CreateModelMixin,
#     mixins.UpdateModelMixin,
#     mixins.DestroyModelMixin,
#     mixins.RetrieveModelMixin
# ):
    # queryset = Goods.objects.all()
    # serializer_class = GoodsSerializer
    # permission_classes = [permissions.IsAuthenticated, IsEditorUser]

# class PictureUploadView(CmsBaseView,APIView):
#     def save_file(self,file):
#         # 肯德基.jpg = ('肯德基,'.jpg')
#         filename = shortuuid.uuid() + os.path.splitext(file.name)[-1]
#         filepath = os.path.join(settings.MEDIA_ROOT,filename)
#         with open(filepath,'wb') as fp:
#             for chunk in file.chunks():
#                 fp.write(chunk)
#         # http://127.0.0.1:8000/media/abc.jpg
#         return self.request.build_absolute_uri(settings.MEDIA_URL + filename)
#
#     def post(self,request):
#         file = request.data.get('file')
#         file_url = self.save_file(file)
#         return Response({"picture":file_url})
#
# from django.contrib.auth import get_user_model
# from django.contrib.auth.models import Group
# MTUser = get_user_model()
#
# class InitStaff(APIView):
#     permission_classes = []
#     def get(self,request):
#         users = MTUser.objects.all()
#         for user in users:
#             user.set_password("111111")
#             user.save()
#
#         edit_group = Group.objects.get(name="编辑")
#         user1 = MTUser.objects.get(telephone="18899990000")
#         user1.groups.add(edit_group)
#         user1.save()
#
#         finance_group = Group.objects.get(name="财务")
#         user2 = MTUser.objects.get(telephone="18800009999")
#         user2.groups.add(finance_group)
#         user2.save()
#         return Response("success")