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
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
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 namespace_is_apphooked(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
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'] = '%s %s' % (MONTHS.get(int(month)), year) context['calendar_namespace'] = namespace return context
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
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
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] }
def date_hierarchy(cl): """Display the date hierarchy for date drill-down functionality.""" if cl.date_hierarchy: field_name = cl.date_hierarchy field = get_fields_from_path(cl.model, field_name)[-1] 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) def link(filters): return 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] }
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], }
def get_month(self): return MONTHS.get(self.month)