Added subjects model

This commit is contained in:
keannu125 2023-03-21 21:22:14 +08:00
parent fcbb57bd98
commit 4c8eb00197
12 changed files with 96 additions and 2 deletions

View file

@ -2,5 +2,6 @@ from django.contrib import admin
from django.urls import path, include from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('accounts/', include('accounts.urls')) path('', include('subjects.urls')),
path('accounts/', include('accounts.urls')),
] ]

View file

@ -41,7 +41,8 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework', 'rest_framework',
'rest_framework.authtoken', 'rest_framework.authtoken',
'djoser' 'djoser',
'subjects'
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View file

View file

@ -0,0 +1,6 @@
from django.contrib import admin
from .models import Subject
# Register your models here.
admin.site.register(Subject)

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class SubjectsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'subjects'

View file

@ -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)),
],
),
]

View file

View file

@ -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

View file

@ -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')

View file

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

12
infotech/subjects/urls.py Normal file
View file

@ -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)),
]

View file

@ -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')