Example #1
0
    def record(self, request, id=None, currency=None):
        """
        GET Product by id .
        """
        # GET records from table
        item = Product.objects.filter(id=id).first()

        if item:
            item = ProductSerializer(item, many=False).data
            if currency != None:
                headers = {'Content-type': 'application/json'}
            fixer_response = requests.get(settings.FIXER_BASE_URL + 'latest?' +
                                          'access_key=' +
                                          settings.FIXER_ACCESS_KEY,
                                          headers=headers)
            if (fixer_response.status_code == 200):
                currencies = fixer_response.json()['rates']
                if currency in currencies:
                    item['user_currancy_price'] = item[
                        'price_eur'] * currencies[currency]
            return Response(prepare_response({"total": 1}, item))
        # if record not found
        else:
            return Response(
                prepare_response(
                    {
                        "status": 404,
                        "instance": request.get_full_path()
                    }, {}, False), 404)
Example #2
0
    def purchase(self, request):
        """
        purchase new products.
        """
        data = request.data
        try:
            with transaction.atomic():
                # update or create data
                item, created = UserProduct.objects.update_or_create(
                    product_id=data['product_id'], user=request.user)
                if not created:
                    item.count += 1
                    item.save()
                # GET new data
                serializer = UserProductSerializer(item, many=False)
                # retuen new data
                return Response(
                    prepare_response({"total": 1}, serializer.data), 201)

        except Exception as error:
            # return valiation error
            return Response(
                prepare_response(
                    {
                        "status": 422,
                        "instance": request.get_full_path()
                    }, error, False), 422)
Example #3
0
    def create(self, request):
        """
        create new products.
        """
        item = Product()
        # create all from post
        for attr, value in request.data.items():
            # if field is allowed
            if attr not in Product.protected():
                setattr(item, attr, value)
        try:
            with transaction.atomic():
                # save data
                item.save()
                # GET new data
                serializer = ProductSerializer(item, many=False)
                # retuen new data
                return Response(
                    prepare_response({"total": 1}, serializer.data), 201)

        except Exception as error:

            # return valiation error
            return Response(
                prepare_response(
                    {
                        "status": 422,
                        "instance": request.get_full_path()
                    }, error, False), 422)
Example #4
0
    def purchased_records(self, request, currency=None):
        """
        List all purchased products to normal users.
        """
        # GET total records
        items = UserProduct.objects.filter(user=request.user).all()
        total = items.count()
        # create pegnation [ start record ]
        start = ((0 if
                  ('page' not in request.GET) else int(request.GET['page']) -
                  1)) * settings.PER_PAGE
        # create pegnation [ end record ]
        end = start + settings.PER_PAGE
        order = '-id'
        # GET all records from the table with order and pegnation
        items = items.order_by(order)[start:end]

        serializer = UserProductSerializer(items, many=True)
        # use fixer for conversion
        if currency != None:
            headers = {'Content-type': 'application/json'}
            fixer_response = requests.get(settings.FIXER_BASE_URL + 'latest?' +
                                          'access_key=' +
                                          settings.FIXER_ACCESS_KEY,
                                          headers=headers)
            if (fixer_response.status_code == 200):
                currencies = fixer_response.json()['rates']
                if currency in currencies:
                    for row in serializer.data:
                        row['product']['user_currancy_price'] = row['product'][
                            'price_eur'] * currencies[currency]
                else:
                    return Response(
                        prepare_response(
                            {
                                "status": 422,
                                "instance": request.get_full_path()
                            }, {}, False), 422)
        # return the data
        return Response(
            prepare_response(
                {
                    "total":
                    total,
                    "count":
                    settings.PER_PAGE,
                    "page":
                    1 if ('page' not in request.GET) else request.GET['page']
                }, serializer.data))
Example #5
0
    def record(self, request, id=None):
        """
        GET Product by id .
        """
        # GET records from table
        item = Product.objects.filter(id=id).first()

        if item:
            serializer = ProductSerializer(item, many=False)
            return Response(prepare_response({"total": 1}, serializer.data))
        # if record not found
        else:
            return Response(
                prepare_response(
                    {
                        "status": 404,
                        "instance": request.get_full_path()
                    }, {}, False), 404)
Example #6
0
    def records(self, request):
        """
        List all products.
        """
        # GET total records
        items = Product.objects.all()
        total = items.count()
        # create pegnation [ start record ]
        start = ((0 if
                  ('page' not in request.GET) else int(request.GET['page']) -
                  1)) * settings.PER_PAGE
        # create pegnation [ end record ]
        end = start + settings.PER_PAGE

        # sort update
        if 'order' in request.GET:
            if request.GET['order'].find('asc') != -1:
                order = "-" + request.GET['order'].split('.')[0]
            else:
                order = request.GET['order'].split('.')[0]
        else:
            # if not order data will order by ID DESC
            order = '-id'

        # multi search for the data
        if 'search' in request.GET and len(
                request.GET['search'].split(',')) >= 1:
            # Turn list of values into list of Q objects
            # return Response(request.GET['search'].split(','))
            queries = [
                Q(name__contains=value)
                for value in request.GET['search'].split(',')
            ]
            # Take one Q object from the list
            initQuery = queries.pop()
            # Or the Q object with the ones remaining in the list
            for query in queries:
                initQuery |= query
            items = items.filter(initQuery)

        # GET all records from the table with order and pegnation
        items = items.order_by(order)[start:end]

        serializer = ProductSerializer(items, many=True)
        # return the data
        return Response(
            prepare_response(
                {
                    "total":
                    total,
                    "count":
                    settings.PER_PAGE,
                    "page":
                    1 if ('page' not in request.GET) else request.GET['page']
                }, serializer.data))
Example #7
0
    def delete(self, request, id=None):
        """
        delete Product by id .
        """
        # GET records from table
        item = Product.objects.filter(id=id).first()
        if item:
            with transaction.atomic():
                # delete record
                Product.objects.filter(id=id).update(
                    status=StatusChoices.DELETED)

                return Response(prepare_response({"total": 0}, {}), 201)
        # if record not found
        else:
            return Response(
                prepare_response(
                    {
                        "status": 404,
                        "instance": request.get_full_path()
                    }, {}, False), 404)
Example #8
0
 def update(self, request, id=None):
     """
     update Product by id .
     """
     # GET records from table
     item = Product.objects.get(id=id)
     if item:
         # update all from post
         for attr, value in request.data.items():
             # if field is allowed
             if attr not in Product.protected():
                 setattr(item, attr, value)
         try:
             with transaction.atomic():
                 # save data
                 item.save()
                 # GET new data
                 serializer = ProductSerializer(item, many=False)
                 # retuen new data
                 return Response(
                     prepare_response({"total": 1}, serializer.data), 201)
         except Exception as error:
             # return valiation error
             return Response(
                 prepare_response(
                     {
                         "status": 422,
                         "instance": request.get_full_path()
                     }, error, False), 422)
     # if record not found
     else:
         return Response(
             prepare_response(
                 {
                     "status": 404,
                     "instance": request.get_full_path()
                 }, {}, False), 404)
Example #9
0
 def get_total_revenue(self, request):
     """
     GET total products revenue.
     """
     raw_items_data = UserProduct.objects.all().values(
         'product_id').annotate(revenue=Sum(F('count') *
                                            F('product__price_eur'),
                                            output_field=FloatField()))
     items = raw_items_data.annotate(purchased_count=F('count'),
                                     name=F('product__name'),
                                     price_eur=F('product__price_eur'))
     total_revenue = sum(raw_items_data.values_list('revenue', flat=True))
     if raw_items_data:
         return Response(
             prepare_response({}, {
                 "total_revenue_eur": total_revenue,
                 "details": items
             }))
     else:
         return Response(
             prepare_response({}, {
                 "total_revenue_eur": 0,
                 "details": []
             }))
Example #10
0
    def records(self, request, currency=None):
        """
        List all products to normal users.
        """
        # GET total records
        items = Product.objects.all()
        total = items.count()
        # create pegnation [ start record ]
        start = ((0 if
                  ('page' not in request.GET) else int(request.GET['page']) -
                  1)) * settings.PER_PAGE
        # create pegnation [ end record ]
        end = start + settings.PER_PAGE

        # sort update
        if 'order' in request.GET:
            if request.GET['order'].find('asc') != -1:
                order = "-" + request.GET['order'].split('.')[0]
            else:
                order = request.GET['order'].split('.')[0]
        else:
            # if not order data will order by ID DESC
            order = '-id'

        # multi search for the data
        if 'search' in request.GET and len(
                request.GET['search'].split(',')) >= 1:
            # Turn list of values into list of Q objects
            # return Response(request.GET['search'].split(','))
            queries = [
                Q(name__contains=value)
                for value in request.GET['search'].split(',')
            ]
            # Take one Q object from the list
            initQuery = queries.pop()
            # Or the Q object with the ones remaining in the list
            for query in queries:
                initQuery |= query
            items = items.filter(initQuery)

        # GET all records from the table with order and pegnation
        items = items.order_by(order)[start:end]

        serializer = ProductSerializer(items, many=True)
        # use fixer for conversion
        if currency != None:
            headers = {'Content-type': 'application/json'}
            fixer_response = requests.get(settings.FIXER_BASE_URL + 'latest?' +
                                          'access_key=' +
                                          settings.FIXER_ACCESS_KEY,
                                          headers=headers)
            if (fixer_response.status_code == 200):
                currencies = fixer_response.json()['rates']
                if currency in currencies:
                    for row in serializer.data:
                        row['user_currancy_price'] = row[
                            'price_eur'] * currencies[currency]

        # return the data
        return Response(
            prepare_response(
                {
                    "total":
                    total,
                    "count":
                    settings.PER_PAGE,
                    "page":
                    1 if ('page' not in request.GET) else request.GET['page']
                }, serializer.data))