GymProgram and Appointment entities

This commit is contained in:
Kukl3v 2024-10-09 21:15:08 +04:00
parent 276168f328
commit e03c6c0a14
31 changed files with 350 additions and 23 deletions

View File

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

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

View File

@ -0,0 +1,25 @@
# Generated by Django 4.2.16 on 2024-10-09 16:37
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Appointment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('start_date', models.DateField(default=django.utils.timezone.now)),
],
options={
'db_table': 'appointment_table',
},
),
]

View File

@ -0,0 +1,22 @@
# Generated by Django 4.2.16 on 2024-10-09 16:37
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('gym_program', '0001_initial'),
('appointment', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='appointment',
name='gym_program',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='gym_program.gymprogram'),
),
]

View File

@ -0,0 +1,23 @@
# Generated by Django 4.2.16 on 2024-10-09 16:37
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('appointment', '0002_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddField(
model_name='appointment',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]

View File

@ -0,0 +1,11 @@
from django.db import models
from django.utils import timezone
class Appointment(models.Model):
user = models.ForeignKey('user.User', on_delete=models.CASCADE)
gym_program = models.ForeignKey('gym_program.GymProgram', on_delete=models.CASCADE)
start_date = models.DateField(blank=False, null=False, default=timezone.now)
class Meta:
db_table = 'appointment_table'

View File

@ -0,0 +1,16 @@
from rest_framework import serializers
from server.user.serializers import CustomUserDetailsSerializer
from server.gym_program.serializers import GymProgramSerializer
from .models import Appointment
class AppointmentSerializer(serializers.ModelSerializer):
user = CustomUserDetailsSerializer(read_only=True)
gym_program = GymProgramSerializer(read_only=True)
class Meta:
model = Appointment
fields = ('id',
'user',
'gym_program',
'start_date')

View File

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

View File

@ -0,0 +1,11 @@
from rest_framework.decorators import api_view
from .models import Appointment
from .serializers import AppointmentSerializer
from rest_framework.response import Response
@api_view(['GET'])
def appointment_list(request):
appointments = Appointment.objects.all()
serializer = AppointmentSerializer(appointments, many=True)
return Response(serializer.data)

View File

@ -0,0 +1,27 @@
# Generated by Django 4.2.16 on 2024-10-09 16:37
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='GymProgram',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(default='', max_length=64)),
('description', models.TextField(blank=True, default='')),
('duration', models.PositiveSmallIntegerField(validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(4)])),
],
options={
'db_table': 'gym_program_table',
},
),
]

View File

@ -0,0 +1,23 @@
# Generated by Django 4.2.16 on 2024-10-09 16:37
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('gym_program', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('appointment', '0003_initial'),
]
operations = [
migrations.AddField(
model_name='gymprogram',
name='users',
field=models.ManyToManyField(through='appointment.Appointment', to=settings.AUTH_USER_MODEL),
),
]

View File

@ -1,3 +1,16 @@
from django.db import models from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
# Create your models here.
class GymProgram(models.Model):
name = models.CharField(max_length=64, blank=False, default='')
description = models.TextField(blank=True, default='')
duration = models.PositiveSmallIntegerField(validators=[MinValueValidator(1), MaxValueValidator(4)])
users = models.ManyToManyField('user.User', through='appointment.Appointment')
def __str__(self):
return f"Name: {self.name}, Description: {self.description}, Duration: {self.duration}"
class Meta:
db_table = 'gym_program_table'

View File

@ -0,0 +1,16 @@
from rest_framework import serializers
from .models import GymProgram
from server.user.serializers import CustomUserDetailsSerializer
class GymProgramSerializer(serializers.ModelSerializer):
users = CustomUserDetailsSerializer(many=True, read_only=True)
class Meta:
model = GymProgram
fields = ('id',
'name',
'description',
'duration',
'users'
)

View File

@ -1,3 +1,64 @@
from django.shortcuts import render from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework import status
from django.shortcuts import get_object_or_404
# Create your views here. from .models import GymProgram
from server.appointment.models import Appointment
from .serializers import GymProgramSerializer
from server.appointment.serializers import AppointmentSerializer
from server.user.models import User
@api_view(['GET', 'POST'])
def gym_program_list(request):
if request.method == 'GET':
gym_programs = GymProgram.objects.all()
serializer = GymProgramSerializer(gym_programs, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = GymProgramSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET', 'PUT', 'DELETE'])
def gym_program_detail(request, pk):
gym_program = get_object_or_404(GymProgram, pk=pk)
if request.method == 'GET':
serializer = GymProgramSerializer(gym_program)
return Response(serializer.data)
elif request.method == 'PUT':
serializer = GymProgramSerializer(gym_program, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
gym_program.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
@api_view(['POST', 'DELETE'])
def manage_user_program(request, gym_program_pk, user_pk):
gym_program = get_object_or_404(GymProgram, pk=gym_program_pk)
user = get_object_or_404(User, pk=user_pk)
if request.method == 'POST':
if Appointment.objects.filter(user=user, gym_program=gym_program).exists():
return Response({"detail": "User is already part of the program."}, status=status.HTTP_400_BAD_REQUEST)
appointment = Appointment.objects.create(user=user, gym_program=gym_program)
serializer = AppointmentSerializer(appointment)
return Response(serializer.data, status=status.HTTP_201_CREATED)
elif request.method == 'DELETE':
appointment = get_object_or_404(Appointment, gym_program=gym_program, user=user)
appointment.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

View File

@ -1,9 +1,7 @@
# Generated by Django 4.2.16 on 2024-10-05 14:16 # Generated by Django 4.2.16 on 2024-10-09 16:37
from django.conf import settings
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -11,7 +9,6 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
] ]
operations = [ operations = [
@ -23,7 +20,9 @@ class Migration(migrations.Migration):
('weight', models.FloatField(default=0.0, validators=[django.core.validators.MinValueValidator(0.0)])), ('weight', models.FloatField(default=0.0, validators=[django.core.validators.MinValueValidator(0.0)])),
('height', models.FloatField(default=0.0, validators=[django.core.validators.MinValueValidator(0.0)])), ('height', models.FloatField(default=0.0, validators=[django.core.validators.MinValueValidator(0.0)])),
('birth_date', models.DateField(default='2000-01-01')), ('birth_date', models.DateField(default='2000-01-01')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
], ],
options={
'db_table': 'metric_table',
},
), ),
] ]

View File

@ -0,0 +1,23 @@
# Generated by Django 4.2.16 on 2024-10-09 16:37
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('metric', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='metric',
name='user',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]

View File

@ -20,4 +20,7 @@ class Metric(models.Model):
birth_date = models.DateField(blank=False, null=False, default='2000-01-01') birth_date = models.DateField(blank=False, null=False, default='2000-01-01')
def __str__(self): def __str__(self):
return f"{self.gender}, Weight: {self.weight}, Height: {self.height}, Birth date: {self.birth_date}" return f"Gender: {self.gender}, Weight: {self.weight}, Height: {self.height}, Birth date: {self.birth_date}"
class Meta:
db_table = 'metric_table'

View File

@ -11,3 +11,20 @@ class MetricSerializer(serializers.ModelSerializer):
'height', 'height',
'birth_date' 'birth_date'
) )
class MetricListSerializer(serializers.ModelSerializer):
email = serializers.SerializerMethodField()
class Meta:
model = Metric
fields = ('id',
'email',
'gender',
'weight',
'height',
'birth_date'
)
def get_email(self, obj):
return obj.user.email

View File

@ -3,18 +3,29 @@ from rest_framework.response import Response
from rest_framework import status from rest_framework import status
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from .models import Metric from .models import Metric
from .serializers import MetricSerializer from .serializers import MetricSerializer, MetricListSerializer
@api_view(['GET', 'POST']) @api_view(['GET', 'POST'])
def metric_list(request): def metric_list(request):
user = request.user
if request.method == 'GET': if request.method == 'GET':
metrics = Metric.objects.all() metrics = Metric.objects.all()
serializer = MetricSerializer(metrics, many=True) serializer = MetricListSerializer(metrics, many=True)
return Response(serializer.data) return Response(serializer.data)
elif request.method == 'POST':
if Metric.objects.filter(user=user).exists():
return Response({'detail': 'Metric already exists for this user.'}, status=status.HTTP_400_BAD_REQUEST)
serializer = MetricSerializer(data=request.data)
if serializer.is_valid():
serializer.save(user=user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET', 'PUT', 'POST', 'DELETE'])
@api_view(['GET', 'PUT', 'DELETE'])
def metric_detail(request): def metric_detail(request):
user = request.user user = request.user
@ -31,15 +42,6 @@ def metric_detail(request):
return Response(serializer.data) return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'POST':
if Metric.objects.filter(user=user).exists():
return Response({'detail': 'Metric already exists for this user.'}, status=status.HTTP_400_BAD_REQUEST)
serializer = MetricSerializer(data=request.data)
if serializer.is_valid():
serializer.save(user=user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE': elif request.method == 'DELETE':
metric = get_object_or_404(Metric, user=user) metric = get_object_or_404(Metric, user=user)
metric.delete() metric.delete()

View File

@ -51,6 +51,8 @@ INSTALLED_APPS = [
'corsheaders', 'corsheaders',
'user.apps.UserConfig', 'user.apps.UserConfig',
'metric.apps.MetricConfig', 'metric.apps.MetricConfig',
'gym_program.apps.GymProgramConfig',
'appointment.apps.AppointmentConfig',
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View File

3
server/training/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
server/training/apps.py Normal file
View File

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

View File

View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
server/training/tests.py Normal file
View File

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

3
server/training/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.16 on 2024-10-05 11:19 # Generated by Django 4.2.16 on 2024-10-09 16:37
from django.db import migrations, models from django.db import migrations, models
import user.models import user.models
@ -27,7 +27,7 @@ class Migration(migrations.Migration):
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
], ],
options={ options={
'abstract': False, 'db_table': 'user_table',
}, },
managers=[ managers=[
('objects', user.models.UserManager()), ('objects', user.models.UserManager()),

View File

@ -33,3 +33,6 @@ class User(AbstractBaseUser, PermissionsMixin):
def __str__(self): def __str__(self):
return self.email return self.email
class Meta:
db_table = 'user_table'