Add ability to customize format used to display dates throughout the application

This commit is contained in:
Joshua Taillon
2022-10-25 15:27:05 -06:00
committed by Sam
parent 37e9e88bd8
commit b2509ff1c6
22 changed files with 127 additions and 22 deletions

View File

@ -0,0 +1,28 @@
"""Add date_format for date display to user preferences in DB
Revision ID: bf13b8f5589d
Revises: 84d840ce853b
Create Date: 2022-10-25 18:53:59.378423
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'bf13b8f5589d'
down_revision = '84d840ce853b'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('date_format', sa.String(length=50), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'date_format')
# ### end Alembic commands ###

View File

@ -173,6 +173,7 @@ def register_user() -> Union[Tuple[Dict, int], HttpResponse]:
if not user:
new_user = User(username=username, email=email, password=password)
new_user.timezone = 'Europe/Paris'
new_user.date_format = 'dd/MM/yyyy'
new_user.confirmation_token = secrets.token_urlsafe(30)
new_user.language = language
db.session.add(new_user)
@ -780,6 +781,7 @@ def edit_user_preferences(auth_user: User) -> Union[Dict, HttpResponse]:
"bio": null,
"birth_date": null,
"created_at": "Sun, 14 Jul 2019 14:09:58 GMT",
"date_format": "dd/MM/yyyy",
"display_ascent": true,
"email": "sam@example.com",
"first_name": null,
@ -854,6 +856,7 @@ def edit_user_preferences(auth_user: User) -> Union[Dict, HttpResponse]:
}
:<json boolean display_ascent: display highest ascent records and total
:<json string date_format: the format used to format dates throughout the interface
:<json boolean imperial_units: display distance in imperial units
:<json string language: language preferences
:<json string timezone: user time zone
@ -874,6 +877,7 @@ def edit_user_preferences(auth_user: User) -> Union[Dict, HttpResponse]:
# get post data
post_data = request.get_json()
user_mandatory_data = {
'date_format',
'display_ascent',
'imperial_units',
'language',
@ -883,6 +887,7 @@ def edit_user_preferences(auth_user: User) -> Union[Dict, HttpResponse]:
if not post_data or not post_data.keys() >= user_mandatory_data:
return InvalidPayloadErrorResponse()
date_format = post_data.get('date_format')
display_ascent = post_data.get('display_ascent')
imperial_units = post_data.get('imperial_units')
language = get_language(post_data.get('language'))
@ -890,6 +895,7 @@ def edit_user_preferences(auth_user: User) -> Union[Dict, HttpResponse]:
weekm = post_data.get('weekm')
try:
auth_user.date_format = date_format
auth_user.display_ascent = display_ascent
auth_user.imperial_units = imperial_units
auth_user.language = language

View File

@ -33,6 +33,7 @@ class User(BaseModel):
bio = db.Column(db.String(200), nullable=True)
picture = db.Column(db.String(255), nullable=True)
timezone = db.Column(db.String(50), nullable=True)
date_format = db.Column(db.String(50), nullable=True)
# does the week start Monday?
weekm = db.Column(db.Boolean, default=False, nullable=False)
workouts = db.relationship(
@ -190,6 +191,7 @@ class User(BaseModel):
serialized_user = {
**serialized_user,
**{
'date_format': self.date_format,
'display_ascent': self.display_ascent,
'imperial_units': self.imperial_units,
'language': self.language,