mirror of
https://github.com/lemeow125/InfoTech-Backend.git
synced 2024-11-17 06:29:26 +08:00
Added subjects model
This commit is contained in:
parent
fcbb57bd98
commit
4c8eb00197
12 changed files with 96 additions and 2 deletions
|
@ -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')),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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 = [
|
||||||
|
|
0
infotech/subjects/__init__.py
Normal file
0
infotech/subjects/__init__.py
Normal file
6
infotech/subjects/admin.py
Normal file
6
infotech/subjects/admin.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
from .models import Subject
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
|
|
||||||
|
admin.site.register(Subject)
|
6
infotech/subjects/apps.py
Normal file
6
infotech/subjects/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class SubjectsConfig(AppConfig):
|
||||||
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
|
name = 'subjects'
|
25
infotech/subjects/migrations/0001_initial.py
Normal file
25
infotech/subjects/migrations/0001_initial.py
Normal 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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
0
infotech/subjects/migrations/__init__.py
Normal file
0
infotech/subjects/migrations/__init__.py
Normal file
18
infotech/subjects/models.py
Normal file
18
infotech/subjects/models.py
Normal 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
|
12
infotech/subjects/serializers.py
Normal file
12
infotech/subjects/serializers.py
Normal 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')
|
3
infotech/subjects/tests.py
Normal file
3
infotech/subjects/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
12
infotech/subjects/urls.py
Normal file
12
infotech/subjects/urls.py
Normal 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)),
|
||||||
|
]
|
10
infotech/subjects/views.py
Normal file
10
infotech/subjects/views.py
Normal 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')
|
Loading…
Reference in a new issue