Skip to content

Commit

Permalink
Feat/menu redo (#27)
Browse files Browse the repository at this point in the history
* adding stats endpoint from API.

* removing end date and auto updating complete date
  • Loading branch information
RyanNoelk authored Mar 8, 2019
1 parent 14fa625 commit 80a691b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
22 changes: 22 additions & 0 deletions v1/menu/migrations/0010_auto_20190308_1254.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.1.7 on 2019-03-08 12:54

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('menu', '0009_auto_20190301_1353'),
]

operations = [
migrations.RenameField(
model_name='menuitem',
old_name='end_date',
new_name='complete_date',
),
migrations.RemoveField(
model_name='menuitem',
name='all_day',
),
]
3 changes: 1 addition & 2 deletions v1/menu/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class MenuItem(models.Model):
"""
author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, related_name='menu_recipe')
all_day = models.BooleanField(default=False, blank=True)
complete = models.BooleanField(default=False, blank=True)
start_date = models.DateTimeField(null=True, blank=True)
end_date = models.DateTimeField(null=True, blank=True)
complete_date = models.DateTimeField(null=True, blank=True)

class Meta:
ordering = ['start_date', 'id']
Expand Down
15 changes: 13 additions & 2 deletions v1/menu/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# encoding: utf-8

from rest_framework import serializers
import pytz
from datetime import datetime

from v1.recipe.mixins import FieldLimiter
from v1.recipe.serializers import MiniBrowseSerializer
Expand All @@ -22,10 +24,19 @@ class Meta:
'author',
'complete',
'recipe',
'all_day',
'start_date',
'end_date',
'complete_date',
'recipe',
'recipe_data',
]
extra_kwargs = {'recipe': {'write_only': True}}

def create(self, validated_data):
if validated_data.get('complete'):
validated_data['complete_date'] = datetime.now(pytz.utc)
return super(MenuItemSerializer, self).create(validated_data)

def update(self, instance, validated_data):
if validated_data.get('complete') and not instance.complete:
validated_data['complete_date'] = datetime.now(pytz.utc)
return super(MenuItemSerializer, self).update(instance, validated_data)
9 changes: 7 additions & 2 deletions v1/menu/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# encoding: utf-8

from django.conf.urls import url, include
from django.urls import include, path
from rest_framework.routers import DefaultRouter

from . import views
Expand All @@ -11,5 +11,10 @@
router.register(r'menu-item', views.MenuItemViewSet)

urlpatterns = [
url('', include(router.urls)),
path('', include(router.urls)),
path(
'menu-stats/',
views.MenuStatsViewSet.as_view(),
name='menu_stats'
),
]
34 changes: 31 additions & 3 deletions v1/menu/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/usr/bin/env python
# encoding: utf-8

from rest_framework import viewsets
from rest_framework import viewsets, views
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.db.models import Count, Max
from django.core import serializers

from v1.recipe.models import Recipe
from .models import MenuItem
from .serializers import MenuItemSerializer
from .permissions import IsMenuItemOwner
Expand All @@ -16,10 +21,33 @@ class MenuItemViewSet(viewsets.ModelViewSet):
queryset = MenuItem.objects.all()
serializer_class = MenuItemSerializer
permission_classes = (IsMenuItemOwner,)
filter_fields = ('recipe', 'start_date', 'end_date', 'complete')
filter_fields = ('recipe', 'start_date', 'complete_date', 'complete')

def get_queryset(self):
user = self.request.user
if user and not user.is_anonymous:
return MenuItem.objects.filter(author=user)
return MenuItem.objects.none()
return MenuItem.objects.none()


class MenuStatsViewSet(views.APIView):
permission_classes = (IsAuthenticated,)

def get(self, request):
return Response(
Recipe.objects.annotate(
num_menuitems=Count('menu_recipe'),
last_made=Max('menu_recipe__complete_date')
).filter(
num_menuitems__gte=1,
menu_recipe__complete=True
).values(
'slug',
'title',
'num_menuitems',
'last_made',
).order_by(
'-last_made',
'num_menuitems',
)
)

0 comments on commit 80a691b

Please sign in to comment.