~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlprofile/templatetags/custom_date.py

  • Committer: franku
  • Author(s): GunChleoc
  • Date: 2016-12-13 18:30:38 UTC
  • mfrom: (438.1.6 pyformat_util)
  • Revision ID: somal@arcor.de-20161213183038-5cgmvfh2fkgmoc1s
adding a script to run pyformat over the code base

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
ZERO = timedelta(0)
30
30
HOUR = timedelta(hours=1)
31
31
 
 
32
 
32
33
class FixedOffset(tzinfo):
33
34
    """Fixed offset in minutes east from UTC."""
34
35
 
35
36
    def __init__(self, offset, name):
36
 
        self.__offset = timedelta(minutes = offset)
 
37
        self.__offset = timedelta(minutes=offset)
37
38
        self.__name = name
38
39
 
39
40
    def utcoffset(self, dt):
45
46
    def dst(self, dt):
46
47
        return ZERO
47
48
 
48
 
def do_custom_date( format, date, timezone, now= None ):
49
 
    """
50
 
    Returns a string formatted representation of date according to format. This accepts
51
 
    all formats that strftime also accepts, but it also accepts some new options which are dependant
52
 
    on the current date.
 
49
 
 
50
def do_custom_date(format, date, timezone, now=None):
 
51
    """Returns a string formatted representation of date according to format.
 
52
    This accepts all formats that strftime also accepts, but it also accepts
 
53
    some new options which are dependant on the current date.
 
54
 
53
55
        %NY(format)        (natural year) for example %NY(.%Y) will give ".2008" if this year is not 2008, else
54
56
                            an empty string
55
57
        %ND(alternatives)  (natural day) for example %ND(%d.%m.%Y)
59
61
    date        - datetime object to display
60
62
    timezone    - vaild timzone as int
61
63
    now         - overwrite the value for now; only for debug reasons
 
64
 
62
65
    """
63
66
    if now is None:
64
67
        now = datetime.now()
80
83
    ForumStdTimeZone = FixedOffset(60, 'UTC+1')
81
84
 
82
85
    # set the user's timezone information
83
 
    ForumUserTimeZone = FixedOffset(timezone*60, tz_info)
 
86
    ForumUserTimeZone = FixedOffset(timezone * 60, tz_info)
84
87
 
85
88
    # if there is tzinfo not set
86
89
    try:
87
90
        if not date.tzinfo:
88
91
            date = date.replace(tzinfo=ForumStdTimeZone)
89
92
        date = date.astimezone(ForumUserTimeZone)
90
 
    except AttributeError: # maybe this is no valid date object?
 
93
    except AttributeError:  # maybe this is no valid date object?
91
94
        return format
92
95
 
93
96
    # If it's done, timezone informations are now available ;)
95
98
 
96
99
    def _replace_ny(g):
97
100
        if now.year == date.year:
98
 
            return ""
 
101
            return ''
99
102
        return g.group(1)
100
103
 
101
104
    def _replace_nd(g):
102
 
        delta = ddate(date.year,date.month,date.day) - \
103
 
                ddate(now.year,now.month,now.day)
 
105
        delta = ddate(date.year, date.month, date.day) - \
 
106
            ddate(now.year, now.month, now.day)
104
107
        if delta.days == 0:
105
108
            return _(ur'\T\o\d\a\y')
106
109
        elif delta.days == 1:
112
115
    try:
113
116
        while 1:
114
117
            oformat = format
115
 
            format = natural_year_expr.sub(_replace_ny,format)
116
 
            format = natural_day_expr.sub(_replace_nd,format)
 
118
            format = natural_year_expr.sub(_replace_ny, format)
 
119
            format = natural_day_expr.sub(_replace_nd, format)
117
120
            if oformat == format:
118
121
                break
119
122
 
120
 
        data = django_date(date,format)
 
123
        data = django_date(date, format)
121
124
    except NotImplementedError:
122
125
        return format
123
126
 
124
127
    return data
125
128
 
 
129
 
126
130
@register.filter
127
 
def custom_date( date, user ):
128
 
    """
129
 
    If this user is logged in, return his representation,
130
 
    otherwise, return a sane default
131
 
    """
 
131
def custom_date(date, user):
 
132
    """If this user is logged in, return his representation, otherwise, return
 
133
    a sane default."""
132
134
    if not user.is_authenticated():
133
 
        return do_custom_date( DEFAULT_TIME_DISPLAY, date, float(DEFAULT_TIME_ZONE) )
 
135
        return do_custom_date(DEFAULT_TIME_DISPLAY, date, float(DEFAULT_TIME_ZONE))
134
136
    try:
135
 
        userprofile = User.objects.get(username=user).wlprofile        
136
 
        return do_custom_date( userprofile.time_display, date, userprofile.time_zone )
 
137
        userprofile = User.objects.get(username=user).wlprofile
 
138
        return do_custom_date(userprofile.time_display, date, userprofile.time_zone)
137
139
    except ObjectDoesNotExist:
138
 
        return do_custom_date( DEFAULT_TIME_DISPLAY, date, float(DEFAULT_TIME_ZONE) )
 
140
        return do_custom_date(DEFAULT_TIME_DISPLAY, date, float(DEFAULT_TIME_ZONE))
139
141
 
140
142
custom_date.is_safe = False
141
143
 
 
144
 
142
145
def total_seconds(td):
143
146
    # Not defined in 2.6, so we redefine it.
144
147
    return (td.microseconds + (td.seconds + td.days * 24. * 3600.) * 1000000.) / 1000000.
163
166
    else:
164
167
        return sign + '%d hours' % (hours)
165
168
 
 
169
 
166
170
@register.simple_tag
167
171
def current_time(user):
168
172
    time = datetime.today()
169
 
    time = custom_date(time, user);
 
173
    time = custom_date(time, user)
170
174
    return time
171