From ea001d6fafc60a26db6fab03a3a16531bdf1687c Mon Sep 17 00:00:00 2001 From: saduqz Date: Thu, 18 Jun 2020 15:05:13 -0500 Subject: [PATCH 1/2] Added support for django rest framework --- django_school/django_school/settings.py | 19 +++++++++++-------- requirements.txt | 4 ++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/django_school/django_school/settings.py b/django_school/django_school/settings.py index 2c232237..75ea6c27 100644 --- a/django_school/django_school/settings.py +++ b/django_school/django_school/settings.py @@ -17,7 +17,6 @@ # 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/2.0/howto/deployment/checklist/ @@ -29,7 +28,6 @@ ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = [ @@ -44,6 +42,7 @@ 'crispy_forms', 'classroom', + "rest_framework", ] MIDDLEWARE = [ @@ -78,7 +77,6 @@ WSGI_APPLICATION = 'django_school.wsgi.application' - # Database # https://docs.djangoproject.com/en/2.0/ref/settings/#databases @@ -98,7 +96,6 @@ # } } - # Internationalization # https://docs.djangoproject.com/en/2.0/topics/i18n/ @@ -112,7 +109,6 @@ USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.0/howto/static-files/ @@ -122,7 +118,6 @@ os.path.join(BASE_DIR, 'static'), ] - # Custom Django auth settings AUTH_USER_MODEL = 'classroom.User' @@ -135,7 +130,6 @@ LOGOUT_REDIRECT_URL = 'home' - # Messages built-in framework MESSAGE_TAGS = { @@ -146,7 +140,16 @@ messages.ERROR: 'alert-danger', } - # Third party apps configuration CRISPY_TEMPLATE_PACK = 'bootstrap4' + +# django-reset-framework +# ------------------------------------------------------------------------------- +# django-rest-framework - https://www.django-rest-framework.org/api-guide/settings/ +REST_FRAMEWORK = { + "DEFAULT_AUTHENTICATION_CLASSES": ( + "rest_framework.authentication.SessionAuthentication", + ), + "DEFAULT_PERMISSION_CLASSES": (), +} diff --git a/requirements.txt b/requirements.txt index 4b50123a..a3500ab3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,6 @@ +asgiref==3.2.9 Django==3.0.4 django-crispy-forms==1.9.0 +djangorestframework==3.11.0 +pytz==2020.1 +sqlparse==0.3.1 From f11b5632216e0de3ae6c7c3509946ea5e778fbe5 Mon Sep 17 00:00:00 2001 From: saduqz Date: Thu, 18 Jun 2020 15:05:34 -0500 Subject: [PATCH 2/2] Added classroom api and quiz model Api View --- django_school/classroom/api/__init__.py | 0 django_school/classroom/api/quiz.py | 16 +++++++++++++++ django_school/classroom/api_urls.py | 20 +++++++++++++++++++ .../classroom/serializers/quiz_serializer.py | 11 ++++++++++ django_school/django_school/api_router.py | 17 ++++++++++++++++ django_school/django_school/urls.py | 4 ++++ 6 files changed, 68 insertions(+) create mode 100644 django_school/classroom/api/__init__.py create mode 100644 django_school/classroom/api/quiz.py create mode 100644 django_school/classroom/api_urls.py create mode 100644 django_school/classroom/serializers/quiz_serializer.py create mode 100644 django_school/django_school/api_router.py diff --git a/django_school/classroom/api/__init__.py b/django_school/classroom/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/django_school/classroom/api/quiz.py b/django_school/classroom/api/quiz.py new file mode 100644 index 00000000..a3efee9b --- /dev/null +++ b/django_school/classroom/api/quiz.py @@ -0,0 +1,16 @@ +# Django + +# Rest framework +from rest_framework.mixins import ListModelMixin +from rest_framework.viewsets import GenericViewSet + +# Models +from classroom.models import Quiz + +# Serializers +from classroom.serializers.quiz_serializer import QuizModelSerializer + + +class QuizViewSet(ListModelMixin, GenericViewSet): + queryset = Quiz.objects.all() + serializer_class = QuizModelSerializer diff --git a/django_school/classroom/api_urls.py b/django_school/classroom/api_urls.py new file mode 100644 index 00000000..14b70e63 --- /dev/null +++ b/django_school/classroom/api_urls.py @@ -0,0 +1,20 @@ +# Django +from django.urls import include, path +from django.conf import settings + +# Importing Django rest libraries. +from rest_framework.routers import DefaultRouter, SimpleRouter + +# Views +from classroom.api.quiz import QuizViewSet + +if settings.DEBUG: + router = DefaultRouter() +else: + router = SimpleRouter() + +router.register("", QuizViewSet, basename='workers') + +urlpatterns = [ + path('', include(router.urls)) +] diff --git a/django_school/classroom/serializers/quiz_serializer.py b/django_school/classroom/serializers/quiz_serializer.py new file mode 100644 index 00000000..b8faa13b --- /dev/null +++ b/django_school/classroom/serializers/quiz_serializer.py @@ -0,0 +1,11 @@ +# Rest Framework +from rest_framework import serializers + +# Models +from classroom.models import Quiz + + +class QuizModelSerializer(serializers.ModelSerializer): + class Meta: + model = Quiz + fields = ('owner', 'name', 'subject') diff --git a/django_school/django_school/api_router.py b/django_school/django_school/api_router.py new file mode 100644 index 00000000..57e791fb --- /dev/null +++ b/django_school/django_school/api_router.py @@ -0,0 +1,17 @@ +# Django +from django.urls import include, path +from django.conf import settings + +# Django Rest Framework +from rest_framework.routers import DefaultRouter, SimpleRouter + +if settings.DEBUG: + router = DefaultRouter() +else: + router = SimpleRouter() + +app_name = "api" + +urlpatterns = [ + path('quiz/', include('classroom.api_urls')), +] diff --git a/django_school/django_school/urls.py b/django_school/django_school/urls.py index 28455b2f..4fa4ebfb 100644 --- a/django_school/django_school/urls.py +++ b/django_school/django_school/urls.py @@ -11,4 +11,8 @@ path('accounts/signup/', classroom.SignUpView.as_view(), name='signup'), path('accounts/signup/student/', students.StudentSignUpView.as_view(), name='student_signup'), path('accounts/signup/teacher/', teachers.TeacherSignUpView.as_view(), name='teacher_signup'), + + # API urls + path("api/", include("django_school.api_router")), + ]