File: timecolumn.py

package info (click to toggle)
django-tables 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,752 kB
  • sloc: python: 7,120; makefile: 132; sh: 74
file content (26 lines) | stat: -rw-r--r-- 822 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from django.db import models

from .base import library
from .templatecolumn import TemplateColumn


@library.register
class TimeColumn(TemplateColumn):
    """
    A column that renders times in the local timezone.

    Arguments:
        format (str): format string in same format as Django's ``time`` template filter (optional).
        short (bool): if *format* is not specified, use Django's ``TIME_FORMAT`` setting.
    """

    def __init__(self, format=None, *args, **kwargs):
        if format is None:
            format = "TIME_FORMAT"
        template = '{{ value|date:"%s"|default:default }}' % format
        super().__init__(template_code=template, *args, **kwargs)

    @classmethod
    def from_field(cls, field, **kwargs):
        if isinstance(field, models.TimeField):
            return cls(**kwargs)