Skip to content

Commit 81f2fbe

Browse files
authored
Merge pull request #431 from MongoEngine/code-cleanup2
Code style adjusted in sessions.py and __init__.py slightly simplified
2 parents d96baad + 3653909 commit 81f2fbe

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

flask_mongoengine/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,12 @@ def _include_mongoengine(obj):
7070
"""
7171
# TODO why do we need this? What's wrong with importing from the
7272
# original modules?
73-
for module in (mongoengine, mongoengine.fields):
74-
for attr_name in module.__all__:
75-
if not hasattr(obj, attr_name):
76-
setattr(obj, attr_name, getattr(module, attr_name))
73+
for attr_name in mongoengine.__all__:
74+
if not hasattr(obj, attr_name):
75+
setattr(obj, attr_name, getattr(mongoengine, attr_name))
7776

78-
# patch BaseField if available
79-
_patch_base_field(obj, attr_name)
77+
# patch BaseField if available
78+
_patch_base_field(obj, attr_name)
8079

8180

8281
def current_mongoengine_instance():

flask_mongoengine/sessions.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import datetime
1+
from datetime import timedelta, datetime
22
import uuid
33

44
from bson.tz_util import utc
@@ -49,12 +49,11 @@ class DBSession(db.Document):
4949

5050
self.cls = DBSession
5151

52-
def get_expiration_time(self, app, session):
52+
def get_expiration_time(self, app, session) -> timedelta:
5353
if session.permanent:
5454
return app.permanent_session_lifetime
55-
if "SESSION_TTL" in app.config:
56-
return datetime.timedelta(**app.config["SESSION_TTL"])
57-
return datetime.timedelta(days=1)
55+
# Fallback to 1 day session ttl, if SESSION_TTL not set.
56+
return timedelta(**app.config.get("SESSION_TTL", {"days": 1}))
5857

5958
def open_session(self, app, request):
6059
sid = request.cookies.get(app.session_cookie_name)
@@ -67,7 +66,7 @@ def open_session(self, app, request):
6766
if not expiration.tzinfo:
6867
expiration = expiration.replace(tzinfo=utc)
6968

70-
if expiration > datetime.datetime.utcnow().replace(tzinfo=utc):
69+
if expiration > datetime.utcnow().replace(tzinfo=utc):
7170
return MongoEngineSession(
7271
initial=stored_session.data, sid=stored_session.sid
7372
)
@@ -85,9 +84,9 @@ def save_session(self, app, session, response):
8584
response.delete_cookie(app.session_cookie_name, domain=domain)
8685
return
8786

88-
expiration = datetime.datetime.utcnow().replace(
89-
tzinfo=utc
90-
) + self.get_expiration_time(app, session)
87+
expiration = datetime.utcnow().replace(tzinfo=utc) + self.get_expiration_time(
88+
app, session
89+
)
9190

9291
if session.modified:
9392
self.cls(sid=session.sid, data=session, expiration=expiration).save()

flask_mongoengine/wtf/orm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def convert(self, model, field, field_args):
5656
kwargs.update(field_args)
5757

5858
if kwargs["validators"]:
59-
# Create a copy of the list since we will be modifying it.
59+
# Create a copy of the list since we will be modifying it, and if
60+
# validators set as shared list between fields - duplicates/conflicts may
61+
# be created.
6062
kwargs["validators"] = list(kwargs["validators"])
6163

6264
if field.required:
@@ -79,8 +81,6 @@ def convert(self, model, field, field_args):
7981
return f.RadioField(**kwargs)
8082
return f.SelectField(**kwargs)
8183

82-
ftype = type(field).__name__
83-
8484
if hasattr(field, "to_form_field"):
8585
return field.to_form_field(model, kwargs)
8686

0 commit comments

Comments
 (0)