示例#1
0
 def put(self, request, wishlist=None, *args, **kwargs):
     user_id = request.user_id
     title = request.data.get('title')
     text = request.data.get('text')
     items = json.loads(request.data.get('items', '[]'))
     context = {'message': 'wishlist does not exist'}
     try:
         wishlist = WishLists.objects.get(id=wishlist, author_id=user_id)
     except WishLists.DoesNotExist:
         return JSONResponse(context, status=HTTP_200_OK)
     if wishlist:
         data_edit = {
             'title': title,
             'text': text,
         }
         serializer = WishListEditSerializer(wishlist, data=data_edit)
         if serializer.is_valid():
             serializer.save()
             if items:
                 wishlist_id = serializer.data.get('id')
                 update_items(items, wishlist_id)
             context = {'message': WishListSerializer(wishlist).data}
             return JSONResponse(context, status=HTTP_200_OK)
         else:
             errors = str_errors(serializer.errors)
             context = {'errors': errors}
             return JSONResponse(context, status=HTTP_200_OK)
     return JSONResponse(context, status=HTTP_200_OK)
示例#2
0
 def post(self, request, *args, **kwargs):
     data = request.data
     password1 = data.get('password1')
     password2 = data.get('password2')
     username = data.get('username')
     email = data.get('email', '')
     context = {}
     if not username:
         return JSONResponse({'error': 'username exist'},
                             status=HTTP_200_OK)
     if password1 == password2:
         data_create = {
             'username': username,
             'password': password1,
             'email': email
         }
         user, created = Users.objects.get_or_create(username=username,
                                                     defaults=data_create)
         if created:
             token = generate_token(user)
             context['message'] = 'user created'
             context['token'] = token
         else:
             context['error'] = 'user with this username already exist'
     else:
         context['error'] = 'password don\'t match'
     return JSONResponse(context, status=HTTP_200_OK)
示例#3
0
    def post(self, request, wishlist=None, *args, **kwargs):
        user_id = request.user_id
        data = request.query_params
        reserve = data.get('reserve')
        context = {'message': 'wish not reserved'}
        if reserve:
            try:
                wish = WishLists.objects.get(share_users__in=[user_id],
                                             id=wishlist)
                wish_items = WishListsItems.objects.filter(wishlist_id=wish.id)
                new_wish = WishLists.objects.create(title=wish.title,
                                                    text=wish.text,
                                                    author_id=user_id)
                for item in wish_items:
                    WishListsItems.objects.create(text=item.text,
                                                  wishlist_id=new_wish.id)

            except WishLists.DoesNotExist:
                return JSONResponse({'error': 'wishlist does not exist'},
                                    status=HTTP_200_OK)
            except json.JSONDecodeError:
                return JSONResponse({'error': 'share users list wrong format'},
                                    status=HTTP_200_OK)
            context['message'] = 'wish reserved'
            return JSONResponse(context, status=HTTP_200_OK)
        return JSONResponse(context, status=HTTP_200_OK)
示例#4
0
 def delete(self, request, wishlist=None, *args, **kwargs):
     user_id = request.user_id
     context = {'message': 'wishlist does not exist'}
     wishlist = WishLists.objects.filter(author_id=user_id, id=wishlist)
     if wishlist:
         wishlist.delete()
         context['message'] = 'wishlist delete'
         return JSONResponse(context, status=HTTP_200_OK)
     return JSONResponse(context, status=HTTP_200_OK)
示例#5
0
 def get(self, request, wishlist=None, *args, **kwargs):
     user_id = request.user_id
     wishlist = WishLists.objects.filter(
         Q(author_id=user_id, id=wishlist)
         | Q(share_users__in=[user_id], id=wishlist)).first()
     if not wishlist:
         return JSONResponse({'message': 'wishlist does not exist'},
                             status=HTTP_200_OK)
     serializer = WishListSerializer(wishlist)
     context = {'message': serializer.data}
     return JSONResponse(context, status=HTTP_200_OK)
示例#6
0
 def delete(self, request, wishlist=None, *args, **kwargs):
     user_id = request.user_id
     context = {'message': 'users deleted'}
     try:
         share_users = json.loads(request.data.get('share_users', '[]'))
         wishlist = WishLists.objects.get(author_id=user_id, id=wishlist)
         for user in share_users:
             wishlist.share_users.remove(
                 Users.objects.get(id=user.get('id')).id)
     except WishLists.DoesNotExist:
         return JSONResponse({'error': 'wishlist does not exist'},
                             status=HTTP_200_OK)
     except json.JSONDecodeError:
         return JSONResponse({'error': 'share users list wrong format'},
                             status=HTTP_200_OK)
     return JSONResponse(context, status=HTTP_200_OK)
示例#7
0
    def get(self, request):
        data = request.GET
        auth = data.get('auth', '{}')
        auth = json.loads(auth)
        username = auth.get('username')
        password = auth.get('password')
        if auth:
            try:
                user = Users.objects \
                    .only('username', 'password') \
                    .get(username=username)
                _hash = user.password
                if _hash != password:
                    # Авторизация не прошла
                    raise WrongHashException
            except Users.DoesNotExist:
                return JSONResponse({'error': {
                    'auth': True
                }},
                                    status=HTTP_401_UNAUTHORIZED)
            except WrongHashException:
                return JSONResponse({'error': {
                    'auth': True
                }},
                                    status=HTTP_401_UNAUTHORIZED)
            except Exception as e:
                log_debug(e)
                return JSONResponse({'error': {
                    'authexception': True
                }},
                                    status=HTTP_401_UNAUTHORIZED)

            # Генерим токен
            token = generate_token(user)
            response = {
                'auth': True,
                # Получили токен типа byte (python 3+). Конвертим в строку
                'token': token.decode('utf-8')
            }
            return JSONResponse(response, status=HTTP_200_OK)

        return JSONResponse({'error': {
            'auth': True
        }},
                            status=HTTP_401_UNAUTHORIZED)
示例#8
0
 def post(self, request, *args, **kwargs):
     user_id = request.user_id
     data = request.data
     title = data.get('title')
     text = data.get('text')
     items = json.loads(data.get('items', '[]'))
     data_create = {
         'title': title,
         'author_id': user_id,
         'text': text,
     }
     serializer = WishListNewSerializer(data=data_create)
     if serializer.is_valid():
         serializer.save()
         if items:
             wishlist_id = serializer.data.get('id')
             create_items(items, wishlist_id)
         context = {'message': serializer.data}
         return JSONResponse(context, status=HTTP_200_OK)
     else:
         errors = str_errors(serializer.errors)
         context = {'errors': errors}
         return JSONResponse(context, status=HTTP_200_OK)
示例#9
0
 def post(self, request, *args, **kwargs):
     # clear cech in browser
     context = {'message': 'logout'}
     return JSONResponse(context, status=HTTP_200_OK)