Ejemplo n.º 1
0
 def partial_update(self, request, pk=None):
     instance = Category.objects.get(pk=pk)
     serializer = CategorySerializer(instance=instance,
                                     data=request.data,
                                     partial=True)
     if serializer.is_valid(raise_exception=True):
         serializer.save()
         return Response(serializer.data)
     else:
         return Response(serializer.errors, status=status.HTTP_306_RESERVED)
Ejemplo n.º 2
0
def index(request):
    ''' Returns page depending on the user credentials
    If user is logged in, returns the user template with a object containing its data,
    if not - returns the front page.
    '''

    if request.user.is_authenticated():

        # Make a couple of lookups to get the user data for this month and app settings
        expenses = ExpenseViewSet().get_queryset_this_month(request)
        serializedExpenses = ExpenseSerializer(expenses, many=True)

        categories = Category.objects.filter(user_id__exact=request.user.id)
        serializedCategories = CategorySerializer(categories, many=True)

        planned = PlannedViewSet().get_queryset_this_month(request)
        serializedPlanned = PlannedSerializer(planned, many=True)

        app_settings = AppSettings.objects.get(user_id__exact=request.user.id)

        # Pack the serialized data in a dict and return it
        # This is needed to escape the extra requests when the front-end app is loading
        bootstrapped_data = {'expenses': json.dumps(serializedExpenses.data, cls=DjangoJSONEncoder),
                            'categories': json.dumps(serializedCategories.data, cls=DjangoJSONEncoder),
                            'planned': json.dumps(serializedPlanned.data, cls=DjangoJSONEncoder),
                            'currency': app_settings.currency,
                            'show_category_creation_form': bool(app_settings.show_CategoryCreationForm)}
        return render(request, 'user/index.html', bootstrapped_data)
    else:
        return render(request, 'index.html')
Ejemplo n.º 3
0
 def get(request):
     language_code = request.META.get('HTTP_LANGUAGE',
                                      DEFAULT_LANGUAGE_CODE).lower()
     categories = CategoryRepository.get_all_categories()
     return Response(camelize(
         CategorySerializer(categories,
                            context={
                                'language_code': language_code
                            },
                            many=True).data),
                     status=200)
Ejemplo n.º 4
0
def initial_data(request):
    categories_queryset = Category.objects.filter(level=1)
    categories = CategorySerializer(categories_queryset, many=True).data

    slides_queryset = Slide.objects.filter(is_active=True)
    slides = SlideSerializer(slides_queryset, many=True).data

    segments_queryset = HomepageSegment.objects.filter(is_active=True).order_by("order")
    segments = SegmentSerializer(segments_queryset, many=True).data

    return JsonResponse({
        "categories": categories,
        "slides": slides,
        "segments": segments,
    })
def category_list(request):
    if request.method == 'GET':
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return JsonResponse(serializer.data,
                            safe=False,
                            status=status.HTTP_200_OK)
    elif request.method == 'POST':
        data = json.loads(request.body)
        serializer = CategorySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(serializer.errors)
Ejemplo n.º 6
0
 def get(self, request):
     categorys = Category.objects.all()
     serializer = CategorySerializer(categorys, many=True)
     return Response({"data": serializer.data})
Ejemplo n.º 7
0
 def get(self, request):
     categories = Category.objects.all()
     serializer = CategorySerializer(categories, many=True)
     return Response(serializer.data, status=status.HTTP_200_OK)
Ejemplo n.º 8
0
 def post(self, request):
     serializer = CategorySerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Ejemplo n.º 9
0
 def retrieve(self, request, pk=None):
     cats = Category.objects.all()
     cat = get_object_or_404(cats, pk=pk)
     serializer = CategorySerializer(cat)
     return Response(serializer.data)
Ejemplo n.º 10
0
 def list(self, request):
     categories = Category.objects.all()
     serializer = CategorySerializer(categories, many=True)
     return Response(serializer.data)
Ejemplo n.º 11
0
 def get_category(self, request, year):
     #get Category by year
     queryset = Category.objects.filter(year__iexact=year)
     return Response(CategorySerializer(queryset, many=True).data,
                     status=status.HTTP_200_OK)
Ejemplo n.º 12
0
 def retrieve(self, request, pk=None):
     queryset = Category.objects.get(pk=pk)
     return Response(CategorySerializer(queryset).data)
Ejemplo n.º 13
0
 def list(self, request):
     queryset = Category.objects.all()
     return Response(CategorySerializer(queryset, many=True).data)