From 25572c23c29bfc8b0aabeee5f9d4332988f9cfb9 Mon Sep 17 00:00:00 2001 From: Ching Date: Tue, 18 Jun 2019 10:08:24 +0800 Subject: [PATCH] [A] add timer app [A] add office hours viewset [A] setup urls --- develop_requirements.txt | 4 + dsite/settings.py | 121 +-------------------------- dsite/settings_default.py | 139 +++++++++++++++++++++++++++++++ dsite/urls.py | 17 +++- timer/__init__.py | 0 timer/admin.py | 3 + timer/apis.py | 0 timer/apps.py | 5 ++ timer/migrations/0001_initial.py | 26 ++++++ timer/migrations/__init__.py | 0 timer/models.py | 9 ++ timer/serializers.py | 21 +++++ timer/tests.py | 3 + timer/urls.py | 11 +++ timer/views.py | 31 +++++++ 15 files changed, 266 insertions(+), 124 deletions(-) create mode 100644 develop_requirements.txt create mode 100644 dsite/settings_default.py create mode 100644 timer/__init__.py create mode 100644 timer/admin.py create mode 100644 timer/apis.py create mode 100644 timer/apps.py create mode 100644 timer/migrations/0001_initial.py create mode 100644 timer/migrations/__init__.py create mode 100644 timer/models.py create mode 100644 timer/serializers.py create mode 100644 timer/tests.py create mode 100644 timer/urls.py create mode 100644 timer/views.py diff --git a/develop_requirements.txt b/develop_requirements.txt new file mode 100644 index 0000000..e67b3bb --- /dev/null +++ b/develop_requirements.txt @@ -0,0 +1,4 @@ +Django +djangorestframework +markdown # Markdown support for the browsable API. +django-filter diff --git a/dsite/settings.py b/dsite/settings.py index 2d3085c..e6095ae 100644 --- a/dsite/settings.py +++ b/dsite/settings.py @@ -1,120 +1 @@ -""" -Django settings for dsite project. - -Generated by 'django-admin startproject' using Django 1.11.18. - -For more information on this file, see -https://docs.djangoproject.com/en/1.11/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.11/ref/settings/ -""" - -import os - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '26l=h^$2k%&o80c1nb6)u#upg3cch!+6)7_5%f=^q1yd=pl+^m' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', -] - -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'dsite.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, -] - -WSGI_APPLICATION = 'dsite.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/1.11/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } -} - - -# Password validation -# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/1.11/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.11/howto/static-files/ - -STATIC_URL = '/static/' +from .settings_default import * diff --git a/dsite/settings_default.py b/dsite/settings_default.py new file mode 100644 index 0000000..404ba94 --- /dev/null +++ b/dsite/settings_default.py @@ -0,0 +1,139 @@ +""" +Django settings for dsite project. + +Generated by 'django-admin startproject' using Django 1.11.18. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.11/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '26l=h^$2k%&o80c1nb6)u#upg3cch!+6)7_5%f=^q1yd=pl+^m' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + # third party + 'rest_framework', + + # user apps + 'timer', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'dsite.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'dsite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/1.11/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.11/howto/static-files/ + +STATIC_URL = '/static/' + + +# rest framework +REST_FRAMEWORK = { + # Use Django's standard `django.contrib.auth` permissions, + # or allow read-only access for unauthenticated users. + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' + ], + + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 10 +} diff --git a/dsite/urls.py b/dsite/urls.py index 517fd2d..60ca177 100644 --- a/dsite/urls.py +++ b/dsite/urls.py @@ -12,10 +12,19 @@ Class-based views 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 -from django.contrib import admin +# # """ + +from django.urls import include, path +from django.conf.urls import url + +from rest_framework import routers + +router = routers.DefaultRouter() +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. urlpatterns = [ - url(r'^admin/', admin.site.urls), + path('', include(router.urls)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), + url('timer/', include('timer.urls')), ] diff --git a/timer/__init__.py b/timer/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/timer/admin.py b/timer/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/timer/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/timer/apis.py b/timer/apis.py new file mode 100644 index 0000000..e69de29 diff --git a/timer/apps.py b/timer/apps.py new file mode 100644 index 0000000..b155b44 --- /dev/null +++ b/timer/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class TimerConfig(AppConfig): + name = 'timer' diff --git a/timer/migrations/0001_initial.py b/timer/migrations/0001_initial.py new file mode 100644 index 0000000..c49660d --- /dev/null +++ b/timer/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 2.2.1 on 2019-06-03 06:15 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='OfficeHours', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('begins_at', models.DateTimeField()), + ('ends_at', models.DateTimeField()), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/timer/migrations/__init__.py b/timer/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/timer/models.py b/timer/models.py new file mode 100644 index 0000000..1a642b6 --- /dev/null +++ b/timer/models.py @@ -0,0 +1,9 @@ +from django.contrib.auth.models import User +import django.db +from django.db import models + + +class OfficeHours(models.Model): + begins_at = models.DateTimeField() + ends_at = models.DateTimeField() + user = models.ForeignKey(User, on_delete=django.db.models.deletion.CASCADE) diff --git a/timer/serializers.py b/timer/serializers.py new file mode 100644 index 0000000..8789ba1 --- /dev/null +++ b/timer/serializers.py @@ -0,0 +1,21 @@ +from django.contrib.auth.models import User, Group +from rest_framework import serializers + +import timer.models + + +class OfficeHoursSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = timer.models.OfficeHours + + +class UserSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = User + fields = ('url', 'username', 'email', 'groups') + + +class GroupSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Group + fields = ('url', 'name') diff --git a/timer/tests.py b/timer/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/timer/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/timer/urls.py b/timer/urls.py new file mode 100644 index 0000000..b5a0c24 --- /dev/null +++ b/timer/urls.py @@ -0,0 +1,11 @@ +from django.conf.urls import include, url +from django.urls import path +from rest_framework import routers +from timer import views + + +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. +urlpatterns = [ + url('office-hours', views.OfficeHoursViewSet.as_view({'get': 'list'})) +] diff --git a/timer/views.py b/timer/views.py new file mode 100644 index 0000000..dc67e21 --- /dev/null +++ b/timer/views.py @@ -0,0 +1,31 @@ +from django.contrib.auth.models import User, Group +from django.shortcuts import render +from rest_framework import viewsets + +import timer.serializers +import timer.models + + +class OfficeHoursViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows office hours to be viewed or edited. + """ + + queryset = timer.models.OfficeHours.objects.order_by('-id') + serializer_class = timer.serializers.OfficeHoursSerializer + + +class UserViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows users to be viewed or edited. + """ + queryset = User.objects.all().order_by('-date_joined') + serializer_class = timer.serializers.UserSerializer + + +class GroupViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows groups to be viewed or edited. + """ + queryset = Group.objects.all() + serializer_class = timer.serializers.GroupSerializer