[AF] UTC/Timezone in Database and onupdate functions. Can I get current_user.timezone?
Howdy!
I have been venturing in and out of the terrible timezone conundrum. I am on "US/Pacific". I have been trying to store all writes to the database in UTC but my web application is basically centered around time. A very import query below, will query based on the users hotel, filter messages posted today or updated today, and then filter out posts that even though they were wrote or updated today, since they post_date is in the future, not to show in the current day's message queue. I included a Base/Meta class that all the DB Models inherit from. The onupdate function are amazing. But with "US/Pacific" being -8 hr behind UTC there is problems in that gap in the below query.
Question: I know it is a major design flaw/risk that I am will to take. Does anyone know how to feed Flask-Login's current_user variable so I could replace datetime based on current_user.timzone instead of UTC. Example below. I know it is not it will take a little more code to make that actually happen then the below code but just and interested in anyone's initial thoughts and if that is possible. Basically I would like anytime this updates, to update based on the current_user.timezone rather than UTC. Thanks in advance for any help or advice.
What I would like:
timestamp = db.Column(db.DateTime, default=db.func.now()) #current
timestamp = db.Column(db.DateTime, default=current_user.timezone) #if possible, how?
def query_messages(date=None, tz=None, hotelcode=None, role='user'):
"""Query database for messages. I find it easier if query
is for 1 day instead of a range. .filter(Message.post_date <=date)
prevents messages scheduled for the future from displaying in current
days messages.
TODO: add flexibility if role='admin' or for role for multiple hotels
"""
print("DB query! username={}, hotelcode={}, timezone={}, is searching for {}".\
format(current_user.username, current_user.hotelcode, current_user.timezone, date))
q = Message.query.filter(Message.hotelcode == hotelcode)\
.filter(or_(Message.post_date==date, Message.last_updated_date_only==date))\
.filter(Message.post_date <= date)\
.order_by(Message.last_updated.desc()).all()
return q
class Base(db.Model):
"Base/Meta SQL table to inherit from"
__abstract__ = True
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
timestamp = db.Column(db.DateTime, default=db.func.now())
last_updated = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now())
last_updated_date_only = db.Column(db.Date, default=db.func.current_date(), onupdate=db.func.current_date())
created_by = db.Column(db.String(40), default=None)
base_hotel = db.Column(db.String(5), default=None)
archive = db.Column(db.Boolean, default=False)
Class User(Base):
"""SQL Table"""
__tablename__ = 'user'
username = db.Column(db.String(64), unique=True)
_password = db.Column(db.String(128))
email = db.Column(db.String(128), default=None)
authenticated = db.Column(db.Boolean, default=True)
department = db.Column(db.String, default=None)
base_hotel = db.Column(db.String, default=None)
timezone = db.Column(db.String, default=None)
role = db.Column(db.String, default='user')
/r/flask
https://redd.it/5q63rw
Howdy!
I have been venturing in and out of the terrible timezone conundrum. I am on "US/Pacific". I have been trying to store all writes to the database in UTC but my web application is basically centered around time. A very import query below, will query based on the users hotel, filter messages posted today or updated today, and then filter out posts that even though they were wrote or updated today, since they post_date is in the future, not to show in the current day's message queue. I included a Base/Meta class that all the DB Models inherit from. The onupdate function are amazing. But with "US/Pacific" being -8 hr behind UTC there is problems in that gap in the below query.
Question: I know it is a major design flaw/risk that I am will to take. Does anyone know how to feed Flask-Login's current_user variable so I could replace datetime based on current_user.timzone instead of UTC. Example below. I know it is not it will take a little more code to make that actually happen then the below code but just and interested in anyone's initial thoughts and if that is possible. Basically I would like anytime this updates, to update based on the current_user.timezone rather than UTC. Thanks in advance for any help or advice.
What I would like:
timestamp = db.Column(db.DateTime, default=db.func.now()) #current
timestamp = db.Column(db.DateTime, default=current_user.timezone) #if possible, how?
def query_messages(date=None, tz=None, hotelcode=None, role='user'):
"""Query database for messages. I find it easier if query
is for 1 day instead of a range. .filter(Message.post_date <=date)
prevents messages scheduled for the future from displaying in current
days messages.
TODO: add flexibility if role='admin' or for role for multiple hotels
"""
print("DB query! username={}, hotelcode={}, timezone={}, is searching for {}".\
format(current_user.username, current_user.hotelcode, current_user.timezone, date))
q = Message.query.filter(Message.hotelcode == hotelcode)\
.filter(or_(Message.post_date==date, Message.last_updated_date_only==date))\
.filter(Message.post_date <= date)\
.order_by(Message.last_updated.desc()).all()
return q
class Base(db.Model):
"Base/Meta SQL table to inherit from"
__abstract__ = True
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
timestamp = db.Column(db.DateTime, default=db.func.now())
last_updated = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now())
last_updated_date_only = db.Column(db.Date, default=db.func.current_date(), onupdate=db.func.current_date())
created_by = db.Column(db.String(40), default=None)
base_hotel = db.Column(db.String(5), default=None)
archive = db.Column(db.Boolean, default=False)
Class User(Base):
"""SQL Table"""
__tablename__ = 'user'
username = db.Column(db.String(64), unique=True)
_password = db.Column(db.String(128))
email = db.Column(db.String(128), default=None)
authenticated = db.Column(db.Boolean, default=True)
department = db.Column(db.String, default=None)
base_hotel = db.Column(db.String, default=None)
timezone = db.Column(db.String, default=None)
role = db.Column(db.String, default='user')
/r/flask
https://redd.it/5q63rw
reddit
[AF] UTC/Timezone in Database and onupdate functions.... • /r/flask
Howdy! I have been venturing in and out of the terrible timezone conundrum. I am on "US/Pacific". I have been trying to store all writes to the...
'polymorphic_identity': 'candle',
'inherit_condition': (id == Product.id)
}
class AdministratorView(ModelView):
can_create = True
can_delete = True
can_edit = True
column_display_pk = True
def is_accessible(self):
#TODO
return True #current_user.is_authenticated and current_user.has_role("Administrateur")
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('security.login', next=request.url))
class ProductView(AdministratorView):
column_display_pk = True
column_labels = {'id':'Référence',
'name':'Désignation',
'image':'Image',
'availability':'Disponibilité',
'price':'Prix de vente',
'description':'Description du produit',
'type':'Type de produit',
'collections':'Collections'}
form_columns = ('id', 'name', 'image', 'availability', 'price', 'description', 'type')
# HERE'S THE PROBLEMATIC INSTRUCTION
inline_models = (Candle,)
admin.add_view(ProductView(Product, db.session, name='All', category='Products'))
# if i let AdministratorView, it works, but not as i want.
admin.add_view(AdministratorView(Candle, db.session, name='Candlery', category='Products'))
Could you tell me why, please ?
Edit 1 Formatting
Edit 2 Solved : in fact, after fiddling with the forms, i realised i took the problem backwards :
The Candle ModelView form manages the inheritance and does the right thing : inserting its Product part in Product, and its Candle part in Candle. No need to first create a Product, then linking a Candle to it.
I'll let this post here. In case someone, someday has the same twisted mind problem.
/r/flask
https://redd.it/6msraw
'inherit_condition': (id == Product.id)
}
class AdministratorView(ModelView):
can_create = True
can_delete = True
can_edit = True
column_display_pk = True
def is_accessible(self):
#TODO
return True #current_user.is_authenticated and current_user.has_role("Administrateur")
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('security.login', next=request.url))
class ProductView(AdministratorView):
column_display_pk = True
column_labels = {'id':'Référence',
'name':'Désignation',
'image':'Image',
'availability':'Disponibilité',
'price':'Prix de vente',
'description':'Description du produit',
'type':'Type de produit',
'collections':'Collections'}
form_columns = ('id', 'name', 'image', 'availability', 'price', 'description', 'type')
# HERE'S THE PROBLEMATIC INSTRUCTION
inline_models = (Candle,)
admin.add_view(ProductView(Product, db.session, name='All', category='Products'))
# if i let AdministratorView, it works, but not as i want.
admin.add_view(AdministratorView(Candle, db.session, name='Candlery', category='Products'))
Could you tell me why, please ?
Edit 1 Formatting
Edit 2 Solved : in fact, after fiddling with the forms, i realised i took the problem backwards :
The Candle ModelView form manages the inheritance and does the right thing : inserting its Product part in Product, and its Candle part in Candle. No need to first create a Product, then linking a Candle to it.
I'll let this post here. In case someone, someday has the same twisted mind problem.
/r/flask
https://redd.it/6msraw
reddit
[AF Flask-SQLAlchemy / Flask-Admin] Problem with... • r/flask
Hi gals & guys. 3 days i'm stuck with a problem with Flask_admin. I think i'll need something stronger than a stack of online doc and a rubber...