Changeset 31adfa5 in OpenWorkouts-current for ow/models


Ignore:
Timestamp:
Dec 21, 2018, 11:00:25 AM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
fe6089a
Parents:
d507f75
Message:

(#14) Timezones support:

  • Added pytz as a new dependency, please install it in your existing envs:

pip install pytz

  • Added a timezone attribute to users, to store in which timezone they are, defaults to 'UTC'. Ensure any users you could have in your database have such attribute. You can add it in pshell:

for user in root.users:

user.timezone = 'UTC'

request.tm.commit()

  • Modified schemas/templates/views to let users choose their timezone based on a list of "common" timezones provided by pytz
  • Added two methods to the Workout model so we can get the start and end dates formatted in the appropiate timezone (all datetime objects are stored in UTC)
  • Modified the templates where we show workout dates and times so the new timezone-formatting methods are used.
Location:
ow/models
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ow/models/user.py

    rd507f75 r31adfa5  
    3333        self.gender = kw.get('gender', 'female')
    3434        self.picture = kw.get('picture', None)  # blob
     35        self.timezone = kw.get('timezone', 'UTC')
    3536        self.__password = None
    3637        self.last_workout_id = 0
  • ow/models/workout.py

    rd507f75 r31adfa5  
    33from decimal import Decimal
    44
     5import pytz
    56import gpxpy
    67from repoze.folder import Folder
     
    7475        return self.start.strftime('%H:%M')
    7576
     77    def start_in_timezone(self, timezone):
     78        """
     79        Return a string representation of the start date and time,
     80        localized into the given timezone
     81        """
     82        _start = self.start.astimezone(pytz.timezone(timezone))
     83        return _start.strftime('%d/%m/%Y %H:%M (%Z)')
     84
     85    def end_in_timezone(self, timezone):
     86        """
     87        Return a string representation of the end date and time,
     88        localized into the given timezone
     89        """
     90        _end = self.end.astimezone(pytz.timezone(timezone))
     91        return _end.strftime('%d/%m/%Y %H:%M (%Z)')
     92
    7693    def split_duration(self):
    7794        hours, remainder = divmod(int(self.duration.total_seconds()), 3600)
Note: See TracChangeset for help on using the changeset viewer.