Add serializers, viewsets in graph app

This commit is contained in:
jhyns 2023-06-23 02:09:00 +09:00
parent 507c1b7349
commit bfe201ae29
7 changed files with 144 additions and 4 deletions

View File

@ -24,6 +24,7 @@ urlpatterns = (
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("", include("market.urls")), path("", include("market.urls")),
path("user/", include("user.urls")), path("user/", include("user.urls")),
path("graph/", include("graph.urls")),
] ]
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

View File

@ -1,3 +1,6 @@
from django.contrib import admin from django.contrib import admin
# Register your models here. from .models import Transaction, MonthlyTransaction
admin.site.register(Transaction)
admin.site.register(MonthlyTransaction)

55
graph/methods.py Normal file
View File

@ -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,
)

16
graph/serializers.py Normal file
View File

@ -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")

20
graph/urls.py Normal file
View File

@ -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

View File

@ -1,3 +0,0 @@
from django.shortcuts import render
# Create your views here.

48
graph/viewsets.py Normal file
View File

@ -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)