0

I am new to python and can't wrap my head around a solution for my simple issue for days now.

I have an Event model which should allow the user to book a timeslot, room and event type that will be shown in a timetable.

class Event(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    get_date = models.DateTimeField()      # set via form, has year and day
    start_time_choices = [('1', '07:30'), ('2', '07:45'), ('3', '08:00'), ('4', '08:15'), ('5', '08:30')]
    timeslots = models.TimeField(
        choices=start_time_choices,
        "%H:%M",
        default='1',
        null=True)
    room_choices = [('1', 'Room 1'), ('2', 'Room 2'), ('3', 'Room 3'), ('4', 'Room 4'), ('5', 'Room 5')]
    room = models.CharField(
        choices=room_choices,
        max_length=1,
        default='1',
        null=True)
    event_choices = [('1', 'Event A'), ('2', 'Event B')]      # A is 15min long, B is 30min long
    events= models.CharField(
        choices=event_choices,
        max_length=1,
        default='1',
        null=True)

Now I want to calculate the start_time and end_time for this event which is used for the calendar view

  • Where do I put the following calculation? Directly in models.py?
  • How do I handle proper date calculation?
# this is not a real code

    @property
    def date_calc(self):
        start_date = get_date + timeslots      # Year & Day + Hour & Min

        if events == "Event A":
          end_date = start_date + "00:15"
        else:
          end_date = end_date + "00:30"

        return start_time, end_time


petezurich
  • 9,280
  • 9
  • 43
  • 57

1 Answers1

0

Where do I put the following calculation? Directly in models.py?

You can add it to your models.py, but as that module grows, you might want to create a models folder with separate modules.

How do I handle proper date calculation?

You can overwrite

def save(self, *args, **kwargs):

And do the calculation there. Basically it calculates start_time and end_time before the actual save and then saves them.

def save(self, *args, **kwargs):
    start_date = get_date + timeslots      # Year & Day + Hour & Min

    if events == "Event A":
      end_date = start_date + "00:15"
    else:
      end_date = end_date + "00:30"

    super(Event, self).save(*args, **kwargs)
Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67