Example #1
0
    def setUp(self):
        """Create test opening hours and holidays:

        - late opening for Monday and Tuesday
        - some planned breaks
        - closed on a Sunday
        """
        test_data = {
            1: ['08:30 12:00', '12:30 22:00'],
            2: ['08:30 12:00', '12:30 18:00', '18:30 22:00'],
            3: ['09:00 17:00'],
            4: ['09:00 17:00'],
            5: ['09:00 17:00'],
            6: ['10:00 13:00'],
            7: None,
        }
        self.company, created = Company.objects.get_or_create(
            name="Company Ltd.", slug="company-ltd")
        for day, hours in test_data.items():
            if not hours:
                continue
            for slot in hours:
                from_hour, to_hour = slot.split()
                OpeningHours.objects.get_or_create(
                    company=self.company,
                    weekday=day,
                    from_hour=str_to_time(from_hour),
                    to_hour=str_to_time(to_hour))

        holiday = ClosingRules.objects.create(
            company=self.company,
            start=datetime(2015, 12, 25) - timedelta(days=2),
            end=datetime(2015, 12, 25) + timedelta(days=4),
            reason="Public holiday")
    def post(self, request, pk):
        """ Clean the data and save opening hours in the database.
        Old opening hours are purged before new ones are saved.
        """
        location = self.get_object()
        # open days, disabled widget data won't make it into request.POST
        present_prefixes = [x.split('-')[0] for x in request.POST.keys()]
        day_forms = OrderedDict()
        for day_no, day_name in WEEKDAYS:
            for slot_no in (1, 2):
                prefix = self.form_prefix(day_no, slot_no)
                # skip closed day as it would be invalid form due to no data
                if prefix not in present_prefixes:
                    continue
                day_forms[prefix] = (day_no, Slot(request.POST, prefix=prefix))

        if all([day_form[1].is_valid() for pre, day_form in day_forms.items()]):
            OpeningHours.objects.filter(company=location).delete()
            for prefix, day_form in day_forms.items():
                day, form = day_form
                opens, shuts = [str_to_time(form.cleaned_data[x])
                                for x in ('opens', 'shuts')]
                if opens != shuts:
                    OpeningHours(from_hour=opens, to_hour=shuts,
                                 company=location, weekday=day).save()
        return redirect(request.path_info)
Example #3
0
    def post(self, request, pk):
        """ Clean the data and save opening hours in the database.
        Old opening hours are purged before new ones are saved.
        """
        location = self.get_object()
        # open days, disabled widget data won't make it into request.POST
        present_prefixes = [x.split('-')[0] for x in request.POST.keys()]
        day_forms = OrderedDict()
        for day_no, day_name in WEEKDAYS:
            for slot_no in (1, 2):
                prefix = self.form_prefix(day_no, slot_no)
                # skip closed day as it would be invalid form due to no data
                if prefix not in present_prefixes:
                    continue
                day_forms[prefix] = (day_no, Slot(request.POST, prefix=prefix))

        if all([day_form[1].is_valid()
                for pre, day_form in day_forms.items()]):
            OpeningHours.objects.filter(company=location).delete()
            for prefix, day_form in day_forms.items():
                day, form = day_form
                opens, shuts = [
                    str_to_time(form.cleaned_data[x])
                    for x in ('opens', 'shuts')
                ]
                if opens != shuts:
                    OpeningHours(from_hour=opens,
                                 to_hour=shuts,
                                 company=location,
                                 weekday=day).save()
        return redirect(request.path_info)
Example #4
0
    def setUp(self):
        """Create test opening hours and holidays:

        - late opening for Monday and Tuesday
        - some planned breaks
        - closed on a Sunday
        """
        test_data = {
            1: ['08:30 12:00', '12:30 22:00'],
            2: ['08:30 12:00', '12:30 18:00', '18:30 22:00'],
            3: ['09:00 17:00'],
            4: ['09:00 17:00'],
            5: ['09:00 17:00'],
            6: ['10:00 13:00'],
            7: None,
        }
        self.company, created = Company.objects.get_or_create(name="Company Ltd.",
                                                         slug="company-ltd")
        for day, hours in test_data.items():
            if not hours:
                continue
            for slot in hours:
                from_hour, to_hour = slot.split()
                OpeningHours.objects.get_or_create(
                    company=self.company,
                    weekday=day,
                    from_hour=str_to_time(from_hour),
                    to_hour=str_to_time(to_hour)
                )

        holiday = ClosingRules.objects.create(
            company=self.company,
            start=datetime(2015, 12, 25) - timedelta(days=2),
            end=datetime(2015, 12, 25) + timedelta(days=4),
            reason="Public holiday"
        )
Example #5
0
    def process_post(self, request):
        """
        Clean the data and save opening hours in the database.
        Old opening hours are purged before new ones are saved.
        """
        location = self.get_object()
        # open days, disabled widget data won't make it into request.POST
        present_prefixes = [x.split('-')[0] for x in request.POST.keys()]

        day_forms = OrderedDict()
        for day_no, day_name in WEEKDAYS:
            for slot_no in (1, 2):
                prefix = self.form_prefix(day_no, slot_no)
                # skip closed day as it would be invalid form due to no data
                if prefix not in present_prefixes:
                    continue
                day_forms[prefix] = (day_no, Slot(request.POST, prefix=prefix))

        if all([day_form[1].is_valid() for pre, day_form in day_forms.items()]):
            OpeningHours.objects.filter(company=location).delete()
            for prefix, day_form in day_forms.items():
                day, form = day_form
                opens, shuts = [str_to_time(form.cleaned_data[x])
                                for x in ('opens', 'shuts')]
                if opens != shuts:
                    OpeningHours(from_hour=opens, to_hour=shuts,
                                 company=location, weekday=day).save()

        # assume that forms are validated
        ClosingRulesFormSet = modelformset_factory(ClosingRules, ClosingRulesForm)
        formset = ClosingRulesFormSet(request.POST)
        for form in formset.forms:
            if form.is_valid():
                closing_rule = form.save(commit=False)
                closing_rule.start = construct_tz_aware_time(form.cleaned_data['start_date'],
                                                                  form.cleaned_data['start_time'],
                                                                  location.timezone)
                closing_rule.end = construct_tz_aware_time(form.cleaned_data['end_date'],
                                                                  form.cleaned_data['end_time'],
                                                                  location.timezone)
                closing_rule.company = location
                closing_rule.save()
            elif form.is_bound:
                ClosingRules.objects.filter(pk=form.instance.pk).delete()