本文整理汇总了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)
示例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)