本文整理汇总了Python中django.utils.dates.MONTHS.get方法的典型用法代码示例。如果您正苦于以下问题:Python MONTHS.get方法的具体用法?Python MONTHS.get怎么用?Python MONTHS.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.dates.MONTHS
的用法示例。
在下文中一共展示了MONTHS.get方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: from django.utils.dates import MONTHS [as 别名]
# 或者: from django.utils.dates.MONTHS import get [as 别名]
def render(self, context, instance, placeholder):
# # check if we can reverse list view for configured namespace
# # if no prepare a message to admin users.
namespace = instance.app_config_id and instance.app_config.namespace
if not is_valid_namespace(namespace):
# add message, should be properly handled in template
context['plugin_configuration_error'] = NO_APPHOOK_ERROR_MESSAGE
return context
year = context.get('event_year')
month = context.get('event_month')
if not all([year, month]):
year = str(timezone.now().date().year)
month = str(timezone.now().date().month)
current_date = datetime.date(int(year), int(month), 1)
language = instance.language
context['event_year'] = year
context['event_month'] = month
context['days'] = build_calendar(year, month, language, namespace)
context['current_date'] = current_date
context['last_month'] = current_date + datetime.timedelta(days=-1)
context['next_month'] = current_date + datetime.timedelta(days=35)
context['calendar_label'] = u'%s %s' % (MONTHS.get(int(month)), year)
context['calendar_namespace'] = namespace
context['calendar_language'] = language
return context
示例2: render
# 需要导入模块: from django.utils.dates import MONTHS [as 别名]
# 或者: from django.utils.dates.MONTHS import get [as 别名]
def render(self, context, instance, placeholder):
context = super(CalendarPlugin, self).render(context, instance,
placeholder)
if context.get('plugin_configuration_error') is not None:
return context
namespace = self.get_namespace(instance)
language = self.get_language(context['request'])
site_id = getattr(get_current_site(context['request']), 'id', None)
year = context.get('event_year')
month = context.get('event_month')
if not all([year, month]):
year = str(timezone.now().date().year)
month = str(timezone.now().date().month)
current_date = datetime.date(int(year), int(month), 1)
context['event_year'] = year
context['event_month'] = month
context['days'] = build_calendar(
year, month, language, namespace, site_id)
context['current_date'] = current_date
context['last_month'] = current_date + datetime.timedelta(days=-1)
context['next_month'] = current_date + datetime.timedelta(days=35)
context['calendar_label'] = u'%s %s' % (MONTHS.get(int(month)), year)
context['calendar_namespace'] = namespace
return context
示例3: build_calendar_context
# 需要导入模块: from django.utils.dates import MONTHS [as 别名]
# 或者: from django.utils.dates.MONTHS import get [as 别名]
def build_calendar_context(year, month, language, namespace):
# if not have a selected date
today = timezone.now().date()
if not all([year, month]):
year = today.year
month = today.month
year, month = int(year), int(month)
current_date = date(year, month, 1)
if namespace:
try:
EventsConfig.objects.get(namespace=namespace)
except EventsConfig.DoesNotExist:
raise template.TemplateSyntaxError(
"'namespace' must be a existent EventConfig namespace, "
"not '{0}'.".format(namespace)
)
context = {
'today': today,
'current_date': current_date,
'last_month': current_date - timedelta(days=1),
'next_month': (current_date + timedelta(days=31)).replace(day=1),
'label': u"{0} {1}".format(MONTHS.get(int(month)), year),
'namespace': namespace
}
# add css classes here instead in template
# TODO: can configure css classes in appconfig ;)
_calendar = build_calendar(year, month, language, namespace)
calendar_list = []
for day, events in _calendar.items():
css = []
if events:
for event in events:
if event.start_date == day:
css.append('events')
break
else:
css.append('multiday-events')
if day.weekday() in [5, 6]:
css.append('weekend')
if day == today:
css.append('today')
# disable days that isn't from this month
if day <= context['last_month'] or day >= context['next_month']:
css.append('disabled')
calendar_list.append((day, events, ' '.join(css)))
context['calendar'] = calendar_list
return context
示例4: date_hierarchy
# 需要导入模块: from django.utils.dates import MONTHS [as 别名]
# 或者: from django.utils.dates.MONTHS import get [as 别名]
def date_hierarchy(cl):
"""
Displays the date hierarchy for date drill-down functionality.
"""
if cl.date_hierarchy:
field_name = cl.date_hierarchy
field = cl.opts.get_field_by_name(field_name)[0]
dates_or_datetimes = 'datetimes' if isinstance(field, models.DateTimeField) else 'dates'
year_field = '%s__year' % field_name
month_field = '%s__month' % field_name
day_field = '%s__day' % field_name
field_generic = '%s__' % field_name
year_lookup = cl.params.get(year_field)
month_lookup = cl.params.get(month_field)
day_lookup = cl.params.get(day_field)
link = lambda filters: cl.get_query_string(filters, [field_generic])
if not (year_lookup or month_lookup or day_lookup):
# select appropriate start level
date_range = cl.queryset.aggregate(first=models.Min(field_name),
last=models.Max(field_name))
if date_range['first'] and date_range['last']:
if date_range['first'].year == date_range['last'].year:
year_lookup = date_range['first'].year
if date_range['first'].month == date_range['last'].month:
month_lookup = date_range['first'].month
if year_lookup and month_lookup and day_lookup:
day = datetime.date(int(year_lookup), int(month_lookup), int(day_lookup))
return {
'field_name': field.verbose_name,
'show': True,
'back': {
'link': link({year_field: year_lookup, month_field: month_lookup}),
'title': capfirst(formats.date_format(day, 'YEAR_MONTH_FORMAT'))
},
'choices': [{'title': capfirst(formats.date_format(day, 'MONTH_DAY_FORMAT'))}]
}
elif year_lookup and month_lookup:
days = cl.queryset.filter(**{year_field: year_lookup, month_field: month_lookup})
days = getattr(days, dates_or_datetimes)(field_name, 'day')
return {
'show': True,
'field_name': field.verbose_name,
'current_filter': capfirst(MONTHS.get(int(month_lookup), 'UNKNOWN')),
'back': {
'link': link({year_field: year_lookup}),
'title': str(year_lookup)
},
'choices': [{
'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
'title': capfirst(formats.date_format(day, 'MONTH_DAY_FORMAT'))
} for day in days]
}
elif year_lookup:
months = cl.queryset.filter(**{year_field: year_lookup})
months = getattr(months, dates_or_datetimes)(field_name, 'month')
return {
'show': True,
'field_name': field.verbose_name,
'current_filter': year_lookup,
'back': {
'link': link({}),
'title': _('All dates')
},
'choices': [{
'link': link({year_field: year_lookup, month_field: month.month}),
'title': capfirst(formats.date_format(month, 'YEAR_MONTH_FORMAT'))
} for month in months]
}
else:
years = getattr(cl.queryset, dates_or_datetimes)(field_name, 'year')
return {
'show': True,
'current_filter': _('All'),
'field_name': field.verbose_name,
'choices': [{
'link': link({year_field: str(year.year)}),
'title': str(year.year),
} for year in years]
}
示例5: date_hierarchy
# 需要导入模块: from django.utils.dates import MONTHS [as 别名]
# 或者: from django.utils.dates.MONTHS import get [as 别名]
def date_hierarchy(cl):
"""
Displays the date hierarchy for date drill-down functionality.
"""
if cl.date_hierarchy:
field_name = cl.date_hierarchy
field = cl.opts.get_field_by_name(field_name)[0]
dates_or_datetimes = "datetimes" if isinstance(field, models.DateTimeField) else "dates"
year_field = "%s__year" % field_name
month_field = "%s__month" % field_name
day_field = "%s__day" % field_name
field_generic = "%s__" % field_name
year_lookup = cl.params.get(year_field)
month_lookup = cl.params.get(month_field)
day_lookup = cl.params.get(day_field)
link = lambda filters: cl.get_query_string(filters, [field_generic])
if not (year_lookup or month_lookup or day_lookup):
# select appropriate start level
date_range = cl.queryset.aggregate(first=models.Min(field_name), last=models.Max(field_name))
if date_range["first"] and date_range["last"]:
if date_range["first"].year == date_range["last"].year:
year_lookup = date_range["first"].year
if date_range["first"].month == date_range["last"].month:
month_lookup = date_range["first"].month
if year_lookup and month_lookup and day_lookup:
day = datetime.date(int(year_lookup), int(month_lookup), int(day_lookup))
return {
"field_name": field.verbose_name,
"show": True,
"back": {
"link": link({year_field: year_lookup, month_field: month_lookup}),
"title": capfirst(formats.date_format(day, "YEAR_MONTH_FORMAT")),
},
"choices": [{"title": capfirst(formats.date_format(day, "MONTH_DAY_FORMAT"))}],
}
elif year_lookup and month_lookup:
days = cl.queryset.filter(**{year_field: year_lookup, month_field: month_lookup})
days = getattr(days, dates_or_datetimes)(field_name, "day")
return {
"show": True,
"field_name": field.verbose_name,
"current_filter": capfirst(MONTHS.get(int(month_lookup), "UNKNOWN")),
"back": {"link": link({year_field: year_lookup}), "title": str(year_lookup)},
"choices": [
{
"link": link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
"title": capfirst(formats.date_format(day, "MONTH_DAY_FORMAT")),
}
for day in days
],
}
elif year_lookup:
months = cl.queryset.filter(**{year_field: year_lookup})
months = getattr(months, dates_or_datetimes)(field_name, "month")
return {
"show": True,
"field_name": field.verbose_name,
"current_filter": year_lookup,
"back": {"link": link({}), "title": _("All dates")},
"choices": [
{
"link": link({year_field: year_lookup, month_field: month.month}),
"title": capfirst(formats.date_format(month, "YEAR_MONTH_FORMAT")),
}
for month in months
],
}
else:
years = getattr(cl.queryset, dates_or_datetimes)(field_name, "year")
return {
"show": True,
"current_filter": _("All"),
"field_name": field.verbose_name,
"choices": [{"link": link({year_field: str(year.year)}), "title": str(year.year)} for year in years],
}