Esempio n. 1
0
 def put(self, request, pk):
     category = self.get_object(pk)
     serializer = CategorySerializer(category, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def addNewCategory(self, data):
     if 'admin' in data:
         serializer = CategorySerializer(data=data)
         if serializer.is_valid():
             serializer.save()
             return
         else:
             serializer.errors
             return
     token = data['token']
     user = Token.objects.get(key=token).user
     if user:
         if user.has_perm():
             data = {'name': data['category']}
             serializer = CategorySerializer(data=data)
             if serializer.is_valid():
                 serializer.save()
                 return {'result': 'success'}
             else:
                 return {'error': 'Taka nazwa kategorii już istnieje'}
         else:
             return {
                 'error': 'Nie masz uprawnień do wykonania tej czynności'
             }
     else:
         return {'error': 'Nie masz uprawnień do wykonania tej czynności'}
Esempio n. 3
0
 def post(self, request):
     request.data["company"] = request.user.company.id
     serializer = CategorySerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 4
0
def category_list(request):
    # GET list of Category, POST a new Category, DELETE all Category
    if request.method == 'GET':
        category = Category.objects.all() 
        
        name = request.GET.get('name', None)
        if name is not None:
            category = category.filter(name__icontains=name)
        
        category_serializer = CategorySerializer(category, many=True)
        return JsonResponse(category_serializer.data, safe=False)
        
        # 'safe=False' for objects serialization
    elif request.method == 'POST':
        # parser_classes = (FormParser, MultiPartParser, JSONParser)
        # category_data = MultiPartParser().parse(request)
        category_serializer = CategorySerializer(data=request.data)
        if category_serializer.is_valid():
            category_serializer.save()
            return JsonResponse(category_serializer.data, status=status.HTTP_201_CREATED) 
        return JsonResponse(category_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        count = Category.objects.all().delete()
        return JsonResponse({'message': '{} Categories was/were deleted successfully!'.format(count[0])}, status=status.HTTP_204_NO_CONTENT)
Esempio n. 5
0
    def post(self, request):
        serializer = CategorySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()

            # Send email 
            email_sub = 'Category Created'
            email_msg = 'New Category Name: {}'.format(serializer.data.get('name'))
            # trigger_email.delay(email_sub, email_msg)

            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 6
0
def category_detail(request, pk):
    # find category by pk (id)
    try: 
        category = Category.objects.get(pk=pk) 
        if request.method == 'GET': 
            category_serializer = CategorySerializer(category) 
            return JsonResponse(category_serializer.data)
        elif request.method == 'PUT': 
            category_serializer = CategorySerializer(category, data=request.data) 
            if category_serializer.is_valid(): 
                category_serializer.save() 
                return JsonResponse(category_serializer.data) 
            return JsonResponse(category_serializer.errors, status=status.HTTP_400_BAD_REQUEST) 
        elif request.method == 'DELETE': 
            category.delete() 
            return JsonResponse({'message': 'Category was deleted successfully!'}, status=status.HTTP_204_NO_CONTENT)
    except Category.DoesNotExist: 
        return JsonResponse({'message': 'The Category does not exist'}, status=status.HTTP_404_NOT_FOUND)