當前位置: 首頁>>代碼示例>>Python>>正文


Python Month.get_days方法代碼示例

本文整理匯總了Python中schedule.periods.Month.get_days方法的典型用法代碼示例。如果您正苦於以下問題:Python Month.get_days方法的具體用法?Python Month.get_days怎麽用?Python Month.get_days使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在schedule.periods.Month的用法示例。


在下文中一共展示了Month.get_days方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: monthly_schedule

# 需要導入模塊: from schedule.periods import Month [as 別名]
# 或者: from schedule.periods.Month import get_days [as 別名]
def monthly_schedule(request, year, month):
    if int(year) > 2037 or int(year) < 1970 or int(month) < 1 or int(month) > 12:
        raise Http404

    MonthlyScheduleFormset = formset_factory(wraps(MonthlyScheduleForm)(partial(MonthlyScheduleForm, days=monthrange(int(year), int(month))[1])), extra=0)

    initial_data = []
    for barber in Barber.objects.all():
        data = {}
        calendar = Calendar.objects.get_or_create(name='barber_schedule')[0]
        month_period = Month(calendar.events.get_for_object(barber), datetime(int(year), int(month), 1))
        for day_period in month_period.get_days():
            if day_period.has_occurrences():
                data['day_{}'.format(day_period.start.day)] = True
        initial_data.append(data)

    if request.method == 'POST':
        formset = MonthlyScheduleFormset(request.POST, initial=initial_data)
        if formset.is_valid():
            for form, barber in zip(formset, Barber.objects.all()):
                for day in form.changed_data:
                    if not form.cleaned_data[day]:
                        calendar = Calendar.objects.get(name='barber_schedule')
                        events = calendar.events.get_for_object(barber)
                        period = Day(events, datetime(int(year), int(month), int(day[4:])))
                        if period.has_occurrences():
                            for occurrence in period.get_occurrences():
                                Event.objects.get(id=occurrence.event_id).delete()
                    else:
                        calendar = Calendar.objects.get_or_create(name='barber_schedule')[0]
                        event = Event(
                            start=make_aware(datetime(int(year), int(month), int(day[4:]), settings.DAY_START)),
                            end=make_aware(datetime(int(year), int(month), int(day[4:]), settings.DAY_END))
                        )
                        event.save()
                        calendar.events.add(event)
                        relation = EventRelation.objects.create_relation(event, barber)
                        relation.save()

    else:
        formset = MonthlyScheduleFormset(initial=initial_data)

    context = dict(
        admin.site.each_context(request),
        days=range(1, monthrange(int(year), int(month))[1] + 1),
        first_weekday=monthrange(int(year), int(month))[0],
        barbers=zip(Barber.objects.all(), formset),
        formset=formset,
        prev_date=(datetime(int(year), int(month), 1) - timedelta(days=1)),
        current_date=datetime.now(),
        next_date=(datetime(int(year), int(month), monthrange(int(year), int(month))[1]) + timedelta(days=1)),
    )
    if request.method == 'POST':
        return redirect(reverse('admin:monthly_schedule', kwargs={'year': year, 'month': month}), context)
    else:
        return render(request, 'admin/monthly_schedule.html', context)
開發者ID:funn,項目名稱:factory,代碼行數:58,代碼來源:views.py

示例2: test_get_values

# 需要導入模塊: from schedule.periods import Month [as 別名]
# 或者: from schedule.periods.Month import get_days [as 別名]
 def test_get_values(self):
     self.client.login(username='admin', password='pa55w0rd')
     date = datetime.datetime(2015, 6, 1)
     resp = self.client.get(reverse('admin:monthly_schedule', kwargs={'year': date.year, 'month': date.month}))
     formset = resp.context['formset']
     self.assertEqual(len(formset), Barber.objects.all().count())
     for form, barber in zip(formset, Barber.objects.all()):
         month = Month(self.calendar.events.get_for_object(barber), date=date)
         self.assertEqual(len(form.visible_fields()), len(list(month.get_days())))
         self.assertEqual(len(form.changed_data), len(month.get_occurrences()))
         for day, occ in zip(form.changed_data, month.cached_get_sorted_occurrences()):
             self.assertEqual(int(day[4:]), occ.end.day)
開發者ID:funn,項目名稱:factory,代碼行數:14,代碼來源:tests.py


注:本文中的schedule.periods.Month.get_days方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。