コード例 #1
0
ファイル: views.py プロジェクト: satish107/shopkeeper
 def post(self, request, format=None):
     serializer = ShopSerializer(data=request.data)
     if serializer.is_valid():
         print('true')
         serializer.save(owner=request.user)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #2
0
ファイル: views.py プロジェクト: satish107/shopkeeper
 def put(self, request, pk, format=None):
     shop_instance = get_object_or_404(Shop, pk=pk)
     serializer = ShopSerializer(shop_instance, data=request.data)
     if serializer.is_valid():
         print('true')
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #3
0
 def create(self, request, *args, **kwargs):
     serializer = ShopSerializer(data={
         'name': request.data['name'],
         'shop_owner_id': request.user.id
     })
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #4
0
    def create(self, request):
        shop = request.data.get('shop')
        serializer = ShopSerializer(data=shop)

        if serializer.is_valid(raise_exception=True):
            Shop.objects.create(title=shop['title'])

            return Response(serializer.data)

        else:
            data = {'error': 'Error Creating Shop', 'status_code': 400}
            error_serializer = ErrorSerializer(data=data)
            error_serializer.is_valid(True)

            return Response(error_serializer.data)
コード例 #5
0
    def get(self, request, *args, **kwargs):
        if request.user.type != 'shop':
            return Response({'status': False, 'error': 'Только для магазинов'}, status=status.HTTP_403_FORBIDDEN)

        shop = request.user.shop
        serializer = ShopSerializer(shop)
        return Response(serializer.data)
コード例 #6
0
    def get(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return JsonResponse({'Status': False, 'Error': 'Log in required'}, status=403)

        if request.user.type != 'shop':
            return JsonResponse({'Status': False, 'Error': 'Только для магазинов'}, status=403)

        shop = request.user.shop
        serializer = ShopSerializer(shop)
        return Response(serializer.data)
コード例 #7
0
    def update(self, request, pk=None):
        shop_to_update = request.data.get('shop')
        serializer = ShopSerializer(data=shop_to_update)

        if serializer.is_valid(raise_exception=True):
            queryset = Shop.objects.all()
            shop = get_object_or_404(queryset, pk=pk)
            shop.title = shop_to_update['title']
            shop.save()

            serializer_to_return = ShopSerializer(shop)

            return Response(serializer_to_return.data)

        else:
            data = {'error': 'Error Updating Shop', 'status_code': 400}
            error_serializer = ErrorSerializer(data=data)
            error_serializer.is_valid(True)

            return Response(error_serializer.data)
コード例 #8
0
def getShops(request):
    user = request.query_params.get('user', None)
    level = request.query_params.get('level', None)
    if user:
        shops = Shop.objects.filter(user=user)
    elif level:
        shops = Shop.objects.filter(level=level)
    else:
        shops = Shop.objects.all()
    serializer = ShopSerializer(shops, many=True)
    return Response(serializer.data)
コード例 #9
0
def createShop(request):
    data = request.data
    user = User.objects.get(id=data['id'])
    shop = Shop.objects.create(user=user,
                               name='Sample Name',
                               level=6,
                               shop_number=50,
                               block='C',
                               description='')
    serializer = ShopSerializer(shop, many=False)
    return Response(serializer.data)
コード例 #10
0
def updateShop(request, pk):
    data = request.data
    shop = Shop.objects.get(id=pk)

    shop.name = data['name']
    shop.level = data['level']
    shop.shop_number = data['shop_number']
    shop.block = data['block']
    shop.description = data['description']
    shop.save()
    serializer = ShopSerializer(shop, many=False)
    return Response(serializer.data)
コード例 #11
0
    def list(self, request):
        shops = Shop.objects.all()
        serializer = ShopSerializer(shops, many=True)

        return Response(serializer.data)
コード例 #12
0
    def retrieve(self, request, pk=None):
        queryset = Shop.objects.all()
        shop = get_object_or_404(queryset, pk=pk)
        serializer = ShopSerializer(shop)

        return Response(serializer.data)
コード例 #13
0
def getShop(request, pk):
    shops = Shop.objects.get(id=pk)
    serializer = ShopSerializer(shops, many=False)
    return Response(serializer.data)
コード例 #14
0
ファイル: views.py プロジェクト: satish107/shopkeeper
 def get(self, request, pk, format=None):
     shop_instance = get_object_or_404(Shop, pk=pk)
     serializer = ShopSerializer(shop_instance)
     return SuccessResponse(serializer.data)
コード例 #15
0
ファイル: views.py プロジェクト: satish107/shopkeeper
 def get(self, request, format=None):
     shop_list = Shop.objects.all().order_by('-added_on')
     serializer = ShopSerializer(shop_list, many=True)
     return SuccessResponse(serializer.data)