Exemplo n.º 1
0
def fetch_hours(request):
    forms = []
    for dow, label in hours.DOW_CHOICES:
        form = TimeSlotForm(request.POST, prefix=dow)
        forms.append(form)
    if all(form.is_valid() for form in forms):
        instances = []
        for dow, form in zip([dow for dow, label in hours.DOW_CHOICES], forms):
            instance = form.save(commit=False)
            if instance:
                instance.dow = dow
                instances.append(instance)
        hour_string = hours.calculate_hour_string(instances)
        return HttpResponse(hour_string)
    else:
        return HttpResponse('')
Exemplo n.º 2
0
def fetch_hours(request):
    forms = []
    for dow, label in hours.DOW_CHOICES:
        form = TimeSlotForm(request.POST, prefix=dow)
        forms.append(form)
    if all(form.is_valid() for form in forms):
        instances = []
        for dow, form in zip([dow for dow, label in hours.DOW_CHOICES], forms):
            instance = form.save(commit=False)
            if instance:
                instance.dow = dow
                instances.append(instance)
        hour_string = hours.calculate_hour_string(instances)
        return HttpResponse(hour_string)
    else:
        return HttpResponse('')
Exemplo n.º 3
0
def add_edit_schedule(request, schedule_id=None):
    schedule = None
    if schedule_id is not None:
        schedule = Schedule.objects.get(id=schedule_id)

    def get_forms(data=None):
        forms = []
        for dow, label in hours.DOW_CHOICES:
            instance = None
            if schedule is not None:
                try:
                    instance = TimeSlot.objects.get(schedule=schedule, dow=dow)
                except TimeSlot.DoesNotExist:
                    instance = None
            form = TimeSlotForm(data=data, instance=instance, prefix=dow)
            forms.append(form)
        return forms

    if request.method == 'POST':
        forms = get_forms(request.POST)
        if all(form.is_valid() for form in forms):
            if schedule is None:
                schedule = Schedule.objects.create(site=request.site)
            for dow, form in zip([dow for dow, label in hours.DOW_CHOICES],
                                 forms):
                instance = form.save()
                # overridden save() will return None if no times are given for a day
                if instance is not None:
                    instance.dow = dow
                    instance.schedule = schedule
                    instance.save()
            display_hours = request.POST.get('hours_display')
            schedule.display = display_hours or hours.calculate_hour_string(
                schedule.timeslot_set.all())
            schedule.save()
            messages.success(request, 'Hours updated successfully.')
            return HttpResponseRedirect(reverse('edit_hours'))
    else:
        forms = get_forms()
    form_list = zip([label for dow, label in hours.DOW_CHOICES], forms)
    extra_context = {'form_list': form_list, 'schedule': schedule}
    return direct_to_template(request,
                              template='dashboard/restaurant/hours.html',
                              extra_context=extra_context)
Exemplo n.º 4
0
def add_edit_schedule(request, schedule_id=None):
    schedule = None
    if schedule_id is not None:
        schedule = Schedule.objects.get(id=schedule_id)
    def get_forms(data=None):
        forms = []
        for dow, label in hours.DOW_CHOICES:
            instance = None
            if schedule is not None:
                try:
                    instance = TimeSlot.objects.get(schedule=schedule, dow=dow)
                except TimeSlot.DoesNotExist:
                    instance = None
            form = TimeSlotForm(data=data, instance=instance, prefix=dow)
            forms.append(form)
        return forms
    if request.method == 'POST':
        forms = get_forms(request.POST)
        if all(form.is_valid() for form in forms):
            if schedule is None:
                schedule = Schedule.objects.create(site=request.site)
            for dow, form in zip([dow for dow, label in hours.DOW_CHOICES], forms):
                instance = form.save()
                # overridden save() will return None if no times are given for a day
                if instance is not None:
                    instance.dow = dow
                    instance.schedule = schedule
                    instance.save()
            display_hours = request.POST.get('hours_display')
            schedule.display = display_hours or hours.calculate_hour_string(schedule.timeslot_set.all())
            schedule.save()
            messages.success(request, 'Hours updated successfully.')
            return HttpResponseRedirect(reverse('edit_hours'))
    else:
        forms = get_forms()
    form_list = zip([label for dow, label in hours.DOW_CHOICES], forms)
    extra_context = {'form_list': form_list, 'schedule': schedule}
    return direct_to_template(request, template='dashboard/restaurant/hours.html', extra_context=extra_context)
Exemplo n.º 5
0
 def mobile_schedule(self):
     return hours.calculate_hour_string(self.timeslot_set.all(), for_mobile=True)
Exemplo n.º 6
0
 def mobile_schedule(self):
     return hours.calculate_hour_string(self.timeslot_set.all(),
                                        for_mobile=True)