diff --git a/config/urls.py b/config/urls.py index ee86e23..ffeecb9 100644 --- a/config/urls.py +++ b/config/urls.py @@ -24,6 +24,7 @@ urlpatterns = ( path("admin/", admin.site.urls), path("", include("market.urls")), path("user/", include("user.urls")), + path("graph/", include("graph.urls")), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/graph/admin.py b/graph/admin.py index 8c38f3f..aef6443 100644 --- a/graph/admin.py +++ b/graph/admin.py @@ -1,3 +1,6 @@ from django.contrib import admin -# Register your models here. +from .models import Transaction, MonthlyTransaction + +admin.site.register(Transaction) +admin.site.register(MonthlyTransaction) diff --git a/graph/methods.py b/graph/methods.py new file mode 100644 index 0000000..175ceaf --- /dev/null +++ b/graph/methods.py @@ -0,0 +1,55 @@ +from django.db.models import Avg +from django.utils import timezone + +from graph.models import Transaction, MonthlyTransaction +from market.models import Product + + +def create_transaction(post): + transaction = Transaction.objects.create( + product=post.product, price=post.price + ) + return transaction + + +def get_average_price(transactions): + average = transactions.aggregate(Avg("price"))["price__avg"] or 0 + return round(average, -2) + + +def create_monthly_transaction(): + year = timezone.now().year + month = timezone.now().month + products = Product.objects.all() + for product in products: + if MonthlyTransaction.objects.filter( + product=product, year=year, month=month + ).exists(): + continue + transactions = Transaction.objects.filter(product=product) + if transactions: + price = get_average_price(transactions) + MonthlyTransaction.objects.create( + product=product, + year=year, + month=month, + price=price, + ) + + +def create_monthly_transaction_by_product(product): + year = timezone.now().year + month = timezone.now().month + if MonthlyTransaction.objects.filter( + product=product, year=year, month=month + ).exists(): + return + transactions = Transaction.objects.filter(product=product) + if transactions: + price = get_average_price(transactions) + MonthlyTransaction.objects.create( + product=product, + year=year, + month=month, + price=price, + ) diff --git a/graph/serializers.py b/graph/serializers.py new file mode 100644 index 0000000..2743338 --- /dev/null +++ b/graph/serializers.py @@ -0,0 +1,16 @@ +# from rest_framework import serializers +from rest_framework.serializers import ModelSerializer + +from graph.models import Transaction, MonthlyTransaction + + +class TransactionSerializer(ModelSerializer): + class Meta: + model = Transaction + fields = ("id", "product", "price", "created_at") + + +class MonthlyTransactionSerializer(ModelSerializer): + class Meta: + model = MonthlyTransaction + fields = ("year", "month", "price") diff --git a/graph/urls.py b/graph/urls.py new file mode 100644 index 0000000..c8c6fb1 --- /dev/null +++ b/graph/urls.py @@ -0,0 +1,20 @@ +from django.conf import settings + +from rest_framework import routers + +from .viewsets import ( + GraphViewset, + MonthlyTransactionViewset, + TransactionViewset, +) + +if settings.DEBUG: + router = routers.DefaultRouter() +else: + router = routers.SimpleRouter() + +router.register("g", GraphViewset) +router.register("t", TransactionViewset) +router.register("m", MonthlyTransactionViewset) + +urlpatterns = router.urls diff --git a/graph/views.py b/graph/views.py deleted file mode 100644 index 91ea44a..0000000 --- a/graph/views.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.shortcuts import render - -# Create your views here. diff --git a/graph/viewsets.py b/graph/viewsets.py new file mode 100644 index 0000000..f92d66c --- /dev/null +++ b/graph/viewsets.py @@ -0,0 +1,48 @@ +from rest_framework.decorators import action +from rest_framework.permissions import AllowAny +from rest_framework.response import Response +from rest_framework.viewsets import ReadOnlyModelViewSet, GenericViewSet + +from core.mixins import ActionBasedMixin +from graph.models import Transaction, MonthlyTransaction +from graph.serializers import ( + TransactionSerializer, + MonthlyTransactionSerializer, +) +from market.models import Product + + +class TransactionViewset(ActionBasedMixin, ReadOnlyModelViewSet): + queryset = Transaction.objects.all().select_related("product") + serializer_class = TransactionSerializer + permission_classes = [AllowAny] + + @action(detail=False, methods=["GET"]) + def monthly(self, request): + queryset = MonthlyTransaction.objects.all() + + page = self.paginate_queryset(queryset) + if page is not None: + serializer = MonthlyTransactionSerializer(page, many=True) + return self.get_paginated_response(serializer.data) + + serializer = MonthlyTransactionSerializer(queryset, many=True) + return Response(serializer.data) + + +class MonthlyTransactionViewset(ActionBasedMixin, ReadOnlyModelViewSet): + queryset = MonthlyTransaction.objects.all().select_related("product") + serializer_class = MonthlyTransactionSerializer + permission_classes = [AllowAny] + + +class GraphViewset(ActionBasedMixin, GenericViewSet): + queryset = Product.objects.all() + queryset_map = {} + serializer_class = MonthlyTransactionSerializer + + def retrieve(self, request, *args, **kwargs): + product = self.get_object() + data = reversed(MonthlyTransaction.objects.filter(product=product)[:6]) + serializer = self.get_serializer(data, many=True) + return Response(serializer.data)