mirror of
https://github.com/lemeow125/InfoTech-Backend.git
synced 2025-05-17 03:48:08 +08:00
Added time schedule model
This commit is contained in:
parent
864643278c
commit
1b02c8f3c6
20 changed files with 193 additions and 21 deletions
0
infotech/timeslots/__init__.py
Normal file
0
infotech/timeslots/__init__.py
Normal file
6
infotech/timeslots/admin.py
Normal file
6
infotech/timeslots/admin.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.contrib import admin
|
||||
from .models import TimeSchedule, TimeSlot
|
||||
|
||||
# Register your models here.
|
||||
admin.register(TimeSlot)
|
||||
admin.register(TimeSchedule)
|
6
infotech/timeslots/apps.py
Normal file
6
infotech/timeslots/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class TimeslotsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'timeslots'
|
33
infotech/timeslots/migrations/0001_initial.py
Normal file
33
infotech/timeslots/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
# Generated by Django 4.2 on 2023-04-22 05:25
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='TimeSlot',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.TextField()),
|
||||
('time_start', models.TimeField(max_length=40)),
|
||||
('time_end', models.TimeField(max_length=20)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='TimeSchedule',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.TextField()),
|
||||
('day', models.TextField(choices=[('Monday', 'M'), ('Tuesday', 'T'), ('Wednesday', 'W'), ('Thursday', 'Th'), ('Friday', 'F'), ('Saturday', 'S'), ('Sunday', 'Sn')])),
|
||||
('time_window', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='timeslots.timeslot')),
|
||||
],
|
||||
),
|
||||
]
|
0
infotech/timeslots/migrations/__init__.py
Normal file
0
infotech/timeslots/migrations/__init__.py
Normal file
40
infotech/timeslots/models.py
Normal file
40
infotech/timeslots/models.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
from django.db import models
|
||||
from django.core.validators import MaxValueValidator, MinValueValidator
|
||||
from django.utils.timezone import now
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class TimeSlot(models.Model):
|
||||
name = models.TextField()
|
||||
time_start = models.TimeField(max_length=40)
|
||||
time_end = models.TimeField(max_length=20)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.name = f"{self.time_start} - {self.time_end}"
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class TimeSchedule(models.Model):
|
||||
class Days(models.TextChoices):
|
||||
Monday = 'Monday'
|
||||
Tuesday = 'Tuesday'
|
||||
Wednesday = 'Wednesday'
|
||||
Thursday = 'Thursday'
|
||||
Friday = 'Friday'
|
||||
Saturday = 'Saturday'
|
||||
Sunday = 'Sunday'
|
||||
|
||||
name = models.TextField()
|
||||
day = models.TextField(choices=Days.choices)
|
||||
time_window = models.ForeignKey(
|
||||
TimeSlot, on_delete=models.CASCADE)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.name = f"{self.day} - {self.time_window}"
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
20
infotech/timeslots/serializers.py
Normal file
20
infotech/timeslots/serializers.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from rest_framework import serializers
|
||||
from django.contrib.auth.models import User
|
||||
from .models import TimeSlot, TimeSchedule
|
||||
|
||||
|
||||
class TimeSlotSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = TimeSlot
|
||||
fields = ('id', 'name', 'time_start', 'time_end')
|
||||
read_only_fields = ['id', 'name']
|
||||
|
||||
|
||||
class TimeScheduleSerializer(serializers.HyperlinkedModelSerializer):
|
||||
time_window = serializers.SlugRelatedField(
|
||||
queryset=TimeSlot.objects.all(), slug_field='name')
|
||||
|
||||
class Meta:
|
||||
model = TimeSchedule
|
||||
fields = ('id', 'name', 'day', 'time_window')
|
||||
read_only_fields = ['id', 'name']
|
3
infotech/timeslots/tests.py
Normal file
3
infotech/timeslots/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
12
infotech/timeslots/urls.py
Normal file
12
infotech/timeslots/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'timeslots', views.TimeSlotViewSet)
|
||||
router.register(r'timeschedules', views.TimeScheduleViewSet)
|
||||
# Wire up our API using automatic URL routing.
|
||||
# Additionally, we include login URLs for the browsable API.
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
]
|
17
infotech/timeslots/views.py
Normal file
17
infotech/timeslots/views.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
from rest_framework import viewsets, generics
|
||||
from .models import TimeSlot, TimeSchedule
|
||||
from .serializers import TimeScheduleSerializer, TimeSlotSerializer
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
class TimeSlotViewSet(viewsets.ModelViewSet):
|
||||
# permission_classes = [IsAuthenticated]
|
||||
serializer_class = TimeSlotSerializer
|
||||
queryset = TimeSlot.objects.all()
|
||||
|
||||
|
||||
class TimeScheduleViewSet(viewsets.ModelViewSet):
|
||||
# permission_classes = [IsAuthenticated]
|
||||
serializer_class = TimeScheduleSerializer
|
||||
queryset = TimeSchedule.objects.all()
|
Loading…
Add table
Add a link
Reference in a new issue