def entries_month(request, year, month): context = dict() if year.isdigit() and month.isdigit(): year = int(year) month = int(month) month_name = get_month_name(month) else: return HttpResponse(status=400) items = Entry.objects.filter(published=True, modification_date__month=month, modification_date__year=year) if items: headline = _('Showing results for: %(month)s %(year)d') % {'month': month_name, 'year': year} page = request.GET.get('page', 1) paginate(items, page, context) else: headline = _('No Search Results for: %(month)s %(year)d') % {'month': month_name, 'year': year} context['headline'] = headline context['year_expanded'] = year t = get_template('djournal/entries.html') html = t.render(RequestContext(request, context)) return HttpResponse(html)
def render(self, context): date_menu_items = cache.get('djournal_date_menu') if not date_menu_items: cursor = connection.cursor() ''' TODO: This should be tested for each database and abstracted into a better separation of logic. ''' if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.mysql': cursor.execute("SELECT COUNT(*) AS total, " \ "MONTH(creation_date) AS month, " \ "YEAR(creation_date) AS year " \ "FROM djournal_entry v " \ "WHERE enabled = 1 " \ "GROUP BY YEAR(creation_date), " \ "MONTH(creation_date) " \ "ORDER BY year DESC, month") elif settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3': select = "SELECT COUNT(*) AS total, " \ "creation_date " \ "FROM djournal_entry v " \ "WHERE published = 1 " \ "GROUP BY strftime('%Y', creation_date), " \ "strftime('%m', creation_date) " \ "ORDER BY creation_date" cursor.execute(select) elif settings.DATABASES['default']['ENGINE'] == 'django.db.backends.oracle': cursor.execute("SELECT COUNT(*) AS total, " \ "to_char(creation_date, 'mm') AS month, " \ "to_char(creation_date, 'yyyy') AS year " \ "FROM djournal_entry v " \ "WHERE published = 1 " \ "to_char(creation_date, 'yyyy'), " \ "to_char(creation_date, 'mm') " \ "ORDER BY year DESC, month") elif settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2': cursor.execute("SELECT COUNT(*) AS total, " \ "date_part('month', creation_date) AS month, " \ "date_part('year', creation_date) AS year " \ "FROM djournal_entry v " \ "WHERE published = 1 " \ "date_part('year', creation_date) AS year, " \ "date_part('month', creation_date) " \ "ORDER BY year DESC, month") year_dict = {} for row in cursor.fetchall(): total = int(row[0]) month_number = int(row[1].month) year_number = int(row[1].year) month_name = get_month_name(month_number) item = [month_number, month_name, total] if year_number in year_dict: year_dict[year_number].append(item) else: year_dict[year_number] = [] year_dict[year_number].append(item) date_menu_items = year_dict.items() cache.set('djournal_date_menu', date_menu_items) context_dict = { 'date_menu_items': date_menu_items, 'curfrent_year': datetime.datetime.today().year, 'year_expanded': context.get('year_expanded', None) } t = get_template('djournal/templatetags/navigation/date_menu.html') c = Context(context_dict) try: return t.render(c) except TemplateSyntaxError, e: if settings.DEBUG: return "[Included template had syntax error: %s]" % e else: # Fail silently for invalid included templates. return ''