So I got these tasks:
Creating meeting room
Get meeting room reservations and the possibility to filter by employee
Create reservation (Reservation has title, from and to dates, employees)
First of all I have created a meeting room, this is the model:
class MeetingRooms(models.Model):
public_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, blank=False, null=False, max_length=36)
creator = models.ForeignKey(Employees, on_delete=models.CASCADE)
reservations = models.ManyToManyField(MeetingRoomsReservations, blank=True)
secret_key = models.CharField(max_length=128, null=False, blank=False)
date_created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "MeetingRooms"
def __str__(self):
return str(self.id)
So as the task says I need to create some kind of reservations, so basically I was thinking that I could create reservations
object and make it ManyToManyField, so it could serve many reservations for different users, imagine the meeting could have like 10 people.
Then I have created MeetingRoomsReservations
model to handle all the reservations:
class MeetingRoomsReservations(models.Model):
statusTypes = [
(0, "Valid"),
(1, "Cancelled")
]
receiver = models.ForeignKey(Employees, on_delete=models.CASCADE)
meeting_room = models.ForeignKey('MeetingRooms', on_delete=models.CASCADE, default=None)
title = models.CharField(max_length=150, blank=False, null=False)
status = models.IntegerField(choices=statusTypes, null=False, blank=False, default=0)
date_from = models.DateTimeField()
date_to = models.DateTimeField()
class Meta:
db_table = "MeetingRoomsReservations"
def __str__(self):
return str(self.id)
As far as I understand the logic, the MeetingRoomsReservations will handle all the reservations that were registered for the MeetingRoom.
MeetingRoom > Reservations > [ List Of Reservations ] = Each of the reservation has a time and etc..
Okay, now I have to create API endpoint :
from main.models import MeetingRooms, MeetingRoomsReservations
from rest_framework import viewsets, generics
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated, AllowAny
from .serializers import MeetingRoomSerializer, ReservationSerializer
from django.http import JsonResponse
class MeetingRoomViewSet(viewsets.ModelViewSet):
permission_classes = [
AllowAny
]
queryset = MeetingRooms.objects.all()
serializer_class = MeetingRoomSerializer
def perform_create(self, serializer):
serializer.save()
class ReservationViewSet(viewsets.ModelViewSet):
And right now what's left is to create a Serializer? Sound easy? Let's see..
from rest_framework import serializers from main.models import MeetingRooms, MeetingRoomsReservations
class ReservationSerializer(serializers.ModelSerializer):
class Meta:
model = MeetingRoomsReservations
fields = ('receiver', 'meeting_room', 'status', 'title', 'date_from', 'date_to')
class MeetingRoomSerializer(serializers.ModelSerializer):
class Meta:
model = MeetingRooms
fields = ('creator', 'public_id', 'creator', 'date_created', 'secret_key', 'reservations')
extra_kwargs = {'secret_key': {'write_only': True, 'min_length': 8}}
Let's see our Rest Framework:
So now it's the part which I don't understand. To create a Meeting room is it enough to only post these values?
After creating a Meeting Room this is how the Reservations should be added?
After many hours of trying I wasn't able to correctly create reservations and assign them to specific Meeting Room. How can I correctly handle this operation?