change api structure

This commit is contained in:
michigg 2017-11-13 01:31:07 +01:00
parent cd152a2324
commit 0d976f063a
22 changed files with 109 additions and 70 deletions

3
ofu_app/api/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
ofu_app/api/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'

3
ofu_app/api/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
ofu_app/api/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

28
ofu_app/api/urls.py Normal file
View File

@ -0,0 +1,28 @@
"""ofu_app URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from rest_framework import routers
from api.views import food_views
router = routers.DefaultRouter()
router.register(r'food', food_views.FoodViewSet, base_name='Food')
urlpatterns = [
# url(r'^api/v1/', ),
url(r'^api/v1/', include(router.urls)),
url(r'^api/auth/', include('rest_framework.urls', namespace='rest_framework'))
]

View File

View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from rest_framework import generics
class APIRoot(generics.GenericAPIView):
"""
My API documentation
"""

View File

@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from api.serializers.food_serializers import MenuSerializer
from apps.food.models import Menu
from rest_framework import viewsets
class FoodViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Menu.objects.all()
serializer_class = MenuSerializer

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-11-11 09:13
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('donar', '0002_auto_20171030_0029'),
]
operations = [
migrations.AlterField(
model_name='lecture_terms',
name='starttime',
field=models.TimeField(default=datetime.datetime(2017, 11, 11, 9, 13, 24, 139058, tzinfo=utc)),
),
]

View File

@ -13,32 +13,23 @@ Including another URLconf
1. Import the include() function: from django.conf.urls import url, include 1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
""" """
from django.conf.urls import url, include from django.conf.urls import url
from rest_framework import routers
from apps.food import views from apps.food import views
router = routers.DefaultRouter()
router.register(r'all', views.FoodViewSet)
router.register(r'loc/(?P<location>.+)/$', views.FoodList.as_view(), base_name="food-on-loaction")
router.register(r'date/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.FoodList.as_view(),
base_name="food-on-day")
# router.register(r'(?P<location>.+)/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.FoodList.as_view(), base_name="food-list")
urlpatterns = [ urlpatterns = [
url(r'^$', views.food, name='food'), url(r'^$', views.food, name='food'),
# Daily Menus # Daily Menus
url(r'^daily/$', views.daily_food, name='daily-food'), url(r'^daily/$', views.daily_food, name='daily-food'),
# Weekly Menus
url(r'^weekly/$', views.weekly_food, name='weekly-food'), url(r'^weekly/$', views.weekly_food, name='weekly-food'),
url(r'^all/$', views.food, name='all-food'),
# All known Menus
# url(r'^all/$', views.food, name='all-food'),
# Food Rating
url(r'^daily/rating/$', views.food_rating, name='rating-food'), url(r'^daily/rating/$', views.food_rating, name='rating-food'),
url(r'^weekly/rating/$', views.food_rating, name='rating-food'), url(r'^weekly/rating/$', views.food_rating, name='rating-food'),
url(r'^api/', include(router.urls)),
url(r'^api/(?P<location>[a-zA-Z]+)/$', views.FoodList.as_view(), name='rating-food'),
url(r'^api/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.FoodList.as_view(),
name='rating-food'),
url(r'^api/(?P<location>[a-zA-Z]+)/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$',
views.FoodList.as_view(),
name='rating-food'),
url(r'^detail/(?P<id>[0-9]+)/$', views.food_detail, name='food-detail')
] ]

View File

@ -3,15 +3,10 @@ from __future__ import unicode_literals
import datetime import datetime
from django.http import HttpResponse
from django.shortcuts import render from django.shortcuts import render
from apps.food.models import Menu, HappyHour, SingleFood, UserRating from apps.food.models import Menu, HappyHour, SingleFood, UserRating
from django.http import HttpResponse
from rest_framework import viewsets, generics
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from apps.food.serializers import MenuSerializer, SingleFoodSerializer
# Create your views here. # Create your views here.
@ -113,42 +108,3 @@ def food_image(request):
return HttpResponse(status=200) return HttpResponse(status=200)
return HttpResponse(status=404) return HttpResponse(status=404)
@api_view(['GET'])
def serialize_daily_food(request, location="", year="", month="", day=""):
request_date = datetime.datetime.strptime((year + "-" + month + "-" + day), "%Y-%m-%d")
queryset = Menu.objects.filter(location__contains=location).filter(date__exact=request_date)
serializer = MenuSerializer(queryset)
return Response(serializer)
class FoodViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Menu.objects.all()
serializer_class = MenuSerializer
class FoodList(generics.ListAPIView):
serializer_class = MenuSerializer
def get_queryset(self):
"""
This view should return a list of all the purchases for
the user as determined by the username portion of the URL.
"""
if 'location' in self.kwargs:
location = self.kwargs['location']
else:
location = ""
if 'year' in self.kwargs and 'month' in self.kwargs and 'day' in self.kwargs:
request_date = datetime.datetime.strptime(
(self.kwargs['year'] + "-" + self.kwargs['month'] + "-" + self.kwargs['day']), "%Y-%m-%d")
else:
request_date = datetime.datetime.now()
print("LOCATION: " + location)
print("DATE: " + str(request_date))
return Menu.objects.filter(location__contains=location).filter(date__exact=request_date)

0
ofu_app/core/__init__.py Normal file
View File

0
ofu_app/core/models.py Normal file
View File

View File

@ -40,6 +40,7 @@ INSTALLED_APPS = [
'apps.events', 'apps.events',
'apps.donar', 'apps.donar',
'apps.registration', 'apps.registration',
'api',
'rest_framework', 'rest_framework',
'analytical', 'analytical',
] ]
@ -62,7 +63,7 @@ REST_FRAMEWORK = {
# 'django.middleware.clickjacking.XFrameOptionsMiddleware', # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
# ] # ]
ROOT_URLCONF = 'ofu_app.urls' ROOT_URLCONF = 'core.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django_jinja.backend.Jinja2', 'BACKEND': 'django_jinja.backend.Jinja2',
@ -71,7 +72,7 @@ TEMPLATES = [
], ],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'environment': 'ofu_app.jinja2.environment' 'environment': 'core.jinja2.environment'
}, },
}, },
{ {
@ -89,7 +90,7 @@ TEMPLATES = [
}, },
] ]
WSGI_APPLICATION = 'ofu_app.wsgi.application' WSGI_APPLICATION = 'core.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases # https://docs.djangoproject.com/en/1.11/ref/settings/#databases

View File

@ -13,23 +13,26 @@ Including another URLconf
1. Import the include() function: from django.conf.urls import url, include 1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
""" """
from core import views
from django.conf.urls import url, include from django.conf.urls import url, include
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import views as auth_views from django.contrib.auth import views as auth_views
from ofu_app import views
urlpatterns = [ urlpatterns = [
url(r'^login/$', auth_views.login, {'template_name': 'registration/login.jinja'}, name='login'), url(r'^login/$', auth_views.login, {'template_name': 'registration/login.jinja'}, name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': 'home'}, name='logout'), url(r'^logout/$', auth_views.logout, {'next_page': 'home'}, name='logout'),
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
# url(r'^signup/$', core_views.signup, name='signup'), # url(r'^signup/$', core_views.signup, name='signup'),
url(r"^account/", include("apps.registration.urls")), url(r'^account/', include("apps.registration.urls")),
url(r'^$', views.home, name="home"), url(r'^$', views.home, name="home"),
# -- Apps -- # -- Apps --
url(r'^food/', include('apps.food.urls')), url(r'^food/', include('apps.food.urls')),
url(r'^events/', include('apps.events.urls')), url(r'^events/', include('apps.events.urls')),
url(r'^donar/', include('apps.donar.urls')), url(r'^donar/', include('apps.donar.urls')),
url(r'^links/$', views.links, name='links-home'), url(r'^links/$', views.links, name='links-home'),
url(r'^impressum/$', views.impressum, name='impressum'), url(r'^impressum/$', views.impressum, name='impressum'),
url(r'', include('api.urls'))
] ]

View File

@ -11,6 +11,6 @@ import os
from django.core.wsgi import get_wsgi_application from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ofu_app.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
application = get_wsgi_application() application = get_wsgi_application()

View File

@ -3,7 +3,7 @@ import os
import sys import sys
if __name__ == "__main__": if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ofu_app.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError: except ImportError: