创建日历表条目,但当前进程非常有限

时间:2018-01-17 23:52:53

标签: python django python-3.x django-models django-views

我试图从几个模型中获取信息并在模型日历中生成表条目。我想完善这个方法,以便它可以在任何一天完成,并且只会延伸到具有未来最多日期的Calendar对象中的条目。目前在视图中,您可以看到,它只是在Calendar表中投影当前对象,并将其向前投影一定天数。这限制了我只在星期一这样做,否则它与TimeSlots表不同步。同样取决于max_jobs,TimeSlot表条目我需要创建多个日历条目...目前它只适用于两个,但我想我可以增加它,但代码是非常重复的

模板:

<form action="{% url 'portal:custom_admin' %}" method="post">
    {% csrf_token %}
    <div style="background-color:red; width:15%;">
        <button name="submit">Project</button>
        DANGER! Only click on mondays, until future projecter can go based off futuremost monday generated into Calendar
    </div>
</form>

查看我正在努力的方法:

@login_required
def custom_admin(request):
"""
Displays MaxSettings, also
Takes time_slots and create a week of it into the future (only should be done on mondays)
Note:eventually this should be able to generate from the future most entry in the calendar instead of today
"""
max_settings = MaxSettings.objects.filter()
time_slots = TimeSlots.objects.filter()

if request.method != 'POST':
    pass
    # Do nothing
else:
    # Project A week forward
    for slot in time_slots:
        if slot.day.weekday == 'Monday':
            new_entry = Calendar(
                date=timezone.now() + datetime.timedelta(days=7),
                timeslot_A=slot.timeslot_A,
                timeslot_B=slot.timeslot_B,
                booked=False
            )
            new_entry.save()
        if slot.day.weekday == 'Tuesday':
            new_entry = Calendar(
                date=timezone.now() + datetime.timedelta(days=8),
                timeslot_A=slot.timeslot_A,
                timeslot_B=slot.timeslot_B,
                booked=False
            )
            new_entry.save()
        if slot.day.weekday == 'Wednesday':
            new_entry = Calendar(
                date=timezone.now() + datetime.timedelta(days=9),
                timeslot_A=slot.timeslot_A,
                timeslot_B=slot.timeslot_B,
                booked=False
            )
            new_entry.save()
        if slot.day.weekday == 'Thursday':
            new_entry = Calendar(
                date=timezone.now() + datetime.timedelta(days=10),
                timeslot_A=slot.timeslot_A,
                timeslot_B=slot.timeslot_B,
                booked=False
            )
            new_entry.save()
        if slot.day.weekday == 'Friday':
            new_entry = Calendar(
                date=timezone.now() + datetime.timedelta(days=11),
                timeslot_A=slot.timeslot_A,
                timeslot_B=slot.timeslot_B,
                booked=False
            )
            new_entry.save()

        if slot.max_jobs == 2:
            if slot.day.weekday == 'Monday':
                new_entry = Calendar(
                    date=timezone.now() + datetime.timedelta(days=7),
                    timeslot_A=slot.timeslot_A,
                    timeslot_B=slot.timeslot_B,
                    booked=False
                )
                new_entry.save()
            if slot.day.weekday == 'Tuesday':
                new_entry = Calendar(
                    date=timezone.now() + datetime.timedelta(days=8),
                    timeslot_A=slot.timeslot_A,
                    timeslot_B=slot.timeslot_B,
                    booked=False
                )
                new_entry.save()
            if slot.day.weekday == 'Wednesday':
                new_entry = Calendar(
                    date=timezone.now() + datetime.timedelta(days=9),
                    timeslot_A=slot.timeslot_A,
                    timeslot_B=slot.timeslot_B,
                    booked=False
                )
                new_entry.save()
            if slot.day.weekday == 'Thursday':
                new_entry = Calendar(
                    date=timezone.now() + datetime.timedelta(days=10),
                    timeslot_A=slot.timeslot_A,
                    timeslot_B=slot.timeslot_B,
                    booked=False
                )
                new_entry.save()
            if slot.day.weekday == 'Friday':
                new_entry = Calendar(
                    date=timezone.now() + datetime.timedelta(days=11),
                    timeslot_A=slot.timeslot_A,
                    timeslot_B=slot.timeslot_B,
                    booked=False
                )
                new_entry.save()

    return HttpResponseRedirect(reverse('portal:custom_admin'))

calendar = Calendar.objects.filter().order_by('date')
context = {
    'max_settings': max_settings,
    'calendar': calendar,
}
return render(request, 'portal/custom_admin.html', context)

以下是相关型号:

class MaxSettings(models.Model):
"""
Everyweek day has its own specified open/close time, time slots along with number of available jobs
"""
MONDAY = 'Monday'
TUESDAY = 'Tuesday'
WEDNESDAY = 'Wednesday'
THURSDAY = 'Thursday'
FRIDAY = 'Friday'
SATURDAY = 'Saturday'
SUNDAY = 'Sunday'
WEEKDAY_CHOICES = (
    (MONDAY, 'monday'),
    (TUESDAY, 'tuesday'),
    (WEDNESDAY, 'wednesday'),
    (THURSDAY, 'thursday'),
    (FRIDAY, 'friday'),
    (SATURDAY, 'saturday'),
    (SUNDAY, 'sunday'),
)
weekday = models.CharField(max_length=9, choices=WEEKDAY_CHOICES, )
operating = models.BooleanField()
start_work_time = models.TimeField()
end_work_time = models.TimeField()

def __str__(self):
    """Return a string representation of the model."""
    return self.weekday


class TimeSlots(models.Model):
"""time slots along with number of available jobs for that slot"""
day = models.ForeignKey(MaxSettings, on_delete=models.CASCADE)
timeslot_A = models.TimeField()
timeslot_B = models.TimeField()
max_jobs = models.PositiveSmallIntegerField()

def __str__(self):
    """Return a string representation of the model."""
    return '%s %s %s' % (self.timeslot_A, self.timeslot_B, self.day)


class Calendar(models.Model):
"""
this will get it details from TimeSlots and be generated into the future from the  MaxSettings
"""
date = models.DateField()
timeslot_A = models.TimeField()
timeslot_B = models.TimeField()
booked = models.BooleanField()

0 个答案:

没有答案