diff --git a/infotech/api/urls.py b/infotech/api/urls.py index b296cca..ad97463 100644 --- a/infotech/api/urls.py +++ b/infotech/api/urls.py @@ -2,5 +2,6 @@ from django.contrib import admin from django.urls import path, include urlpatterns = [ - path('accounts/', include('accounts.urls')) + path('', include('subjects.urls')), + path('accounts/', include('accounts.urls')), ] diff --git a/infotech/config/settings.py b/infotech/config/settings.py index fa55436..eaec162 100644 --- a/infotech/config/settings.py +++ b/infotech/config/settings.py @@ -41,7 +41,8 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'rest_framework', 'rest_framework.authtoken', - 'djoser' + 'djoser', + 'subjects' ] MIDDLEWARE = [ diff --git a/infotech/subjects/__init__.py b/infotech/subjects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/infotech/subjects/admin.py b/infotech/subjects/admin.py new file mode 100644 index 0000000..aba787c --- /dev/null +++ b/infotech/subjects/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from .models import Subject + +# Register your models here. + +admin.site.register(Subject) diff --git a/infotech/subjects/apps.py b/infotech/subjects/apps.py new file mode 100644 index 0000000..a577f47 --- /dev/null +++ b/infotech/subjects/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SubjectsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'subjects' diff --git a/infotech/subjects/migrations/0001_initial.py b/infotech/subjects/migrations/0001_initial.py new file mode 100644 index 0000000..a192c45 --- /dev/null +++ b/infotech/subjects/migrations/0001_initial.py @@ -0,0 +1,25 @@ +# Generated by Django 4.1.7 on 2023-03-21 13:20 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Subject', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=40)), + ('enrolled_count', models.IntegerField(default=0, validators=[django.core.validators.MaxValueValidator(60), django.core.validators.MinValueValidator(0)])), + ('max_slots', models.IntegerField(default=60)), + ('year_level', models.CharField(max_length=20)), + ], + ), + ] diff --git a/infotech/subjects/migrations/__init__.py b/infotech/subjects/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/infotech/subjects/models.py b/infotech/subjects/models.py new file mode 100644 index 0000000..9d96b83 --- /dev/null +++ b/infotech/subjects/models.py @@ -0,0 +1,18 @@ +from django.db import models +from django.core.validators import MaxValueValidator, MinValueValidator +# Create your models here. + + +class Subject(models.Model): + name = models.CharField(max_length=40) + enrolled_count = models.IntegerField( + default=0, + validators=[ + MaxValueValidator(60), + MinValueValidator(0) + ]) + max_slots = models.IntegerField(default=60) + year_level = models.CharField(max_length=20) + + def __str__(self): + return self.name diff --git a/infotech/subjects/serializers.py b/infotech/subjects/serializers.py new file mode 100644 index 0000000..f730855 --- /dev/null +++ b/infotech/subjects/serializers.py @@ -0,0 +1,12 @@ +from rest_framework import serializers +from django.contrib.auth.models import User +from .models import Subject + + +class SubjectSerializer(serializers.HyperlinkedModelSerializer): + enrolled_count = serializers.IntegerField(required=False, default=0) + + class Meta: + model = Subject + fields = ('id', 'name', 'enrolled_count', 'max_slots', 'year_level') + read_only_fields = ('id', 'max_slots') diff --git a/infotech/subjects/tests.py b/infotech/subjects/tests.py new file mode 100644 index 0000000..de8bdc0 --- /dev/null +++ b/infotech/subjects/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/infotech/subjects/urls.py b/infotech/subjects/urls.py new file mode 100644 index 0000000..f65cbf9 --- /dev/null +++ b/infotech/subjects/urls.py @@ -0,0 +1,12 @@ +from django.urls import include, path +from rest_framework import routers +from . import views + +router = routers.DefaultRouter() +router.register(r'subjects', views.SubjectViewSet) + +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. +urlpatterns = [ + path('', include(router.urls)), +] diff --git a/infotech/subjects/views.py b/infotech/subjects/views.py new file mode 100644 index 0000000..64529e2 --- /dev/null +++ b/infotech/subjects/views.py @@ -0,0 +1,10 @@ +from rest_framework.permissions import IsAuthenticated +from rest_framework import viewsets +from .serializers import SubjectSerializer +from .models import Subject + + +class SubjectViewSet(viewsets.ModelViewSet): + # permission_classes = [IsAuthenticated] + serializer_class = SubjectSerializer + queryset = Subject.objects.all().order_by('-year_level')