コード例 #1
0
ファイル: common_tags.py プロジェクト: drtyrsa/common-tags
def render_field(context, field, no_fieldset=False):
    '''
    Renders one form field: input, errors, help_text, etc.
    '''
    return {'field': field, 'no_fieldset': no_fieldset}
register.easyinctag(render_field, template_name='common_tags/render_field.html')

def smart_date(date):
    '''
    Outputs date in human(Russian)-readable format.
    '''
    if date.date() == datetime.today().date():
        return date.strftime('%H:%M')
    if date.date() == (datetime.today() - timedelta(days=1)).date():
        return u'Вчера'
    if date.year == datetime.today().year:
        months = (u'января', u'февраля', u'марта', u'апреля', u'мая', u'июня',
                  u'июля', u'августа', u'сентября', u'октября',
                  u'ноября', u'декабря')
        return mark_safe('%d %s' % (date.day, months[date.month - 1]))
    return date.strftime(u'%d.%m.%Y')
register.filter(smart_date)

def page_title(context, title):
    '''
    Outputs ``title`` and adds ``{{ page_title }}`` variable to global context.
    It will be accessible from any template block.
    '''
    context.dicts[0]['page_title'] = title
    return title
register.easytag(page_title)