Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace if statement with if expression #633

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions social_core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,28 @@ def expiration_timedelta(self):
timedelta is inferred from current time (using UTC timezone). None is
returned if there's no value stored or it's invalid.
"""
if self.extra_data and 'expires' in self.extra_data:
try:
expires = int(self.extra_data.get('expires'))
except (ValueError, TypeError):
return None
if not self.extra_data or 'expires' not in self.extra_data:
return
try:
expires = int(self.extra_data.get('expires'))
except (ValueError, TypeError):
return None

now = datetime.utcnow()
now = datetime.utcnow()

# Detect if expires is a timestamp
if expires > time.mktime(now.timetuple()):
# expires is a datetime, return the remaining difference
return datetime.utcfromtimestamp(expires) - now
else:
# expires is the time to live seconds since creation,
# check against auth_time if present, otherwise return
# the value
auth_time = self.extra_data.get('auth_time')
if auth_time:
reference = datetime.utcfromtimestamp(auth_time)
return (reference + timedelta(seconds=expires)) - now
else:
return timedelta(seconds=expires)
if expires > time.mktime(now.timetuple()):
# expires is a datetime, return the remaining difference
return datetime.utcfromtimestamp(expires) - now
# expires is the time to live seconds since creation,
# check against auth_time if present, otherwise return
# the value
auth_time = self.extra_data.get('auth_time')
if not auth_time:
return timedelta(seconds=expires)

reference = datetime.utcfromtimestamp(auth_time)
return (reference + timedelta(seconds=expires)) - now

def expiration_datetime(self):
# backward compatible alias
Expand Down
16 changes: 8 additions & 8 deletions social_core/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ def send_email_validation(self, backend, email, partial_token=None):

def validate_email(self, email, code):
verification_code = self.storage.code.get_code(code)
if not verification_code or verification_code.code != code:
if (
not verification_code
or verification_code.code != code
or verification_code.email != email
or verification_code.verified
):
return False
elif verification_code.email != email:
return False
elif verification_code.verified:
return False
else:
verification_code.verify()
return True
verification_code.verify()
return True

def render_html(self, tpl=None, html=None, context=None):
"""Render given template or raw html with given context"""
Expand Down
39 changes: 17 additions & 22 deletions social_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,24 @@ def sanitize_redirect(hosts, redirect_to):


def user_is_authenticated(user):
if user and hasattr(user, 'is_authenticated'):
if callable(user.is_authenticated):
authenticated = user.is_authenticated()
if user:
if hasattr(user, 'is_authenticated'):
return (
user.is_authenticated()
if callable(user.is_authenticated)
else user.is_authenticated
)

else:
authenticated = user.is_authenticated
elif user:
authenticated = True
else:
authenticated = False
return authenticated
return True


def user_is_active(user):
if user and hasattr(user, 'is_active'):
if callable(user.is_active):
is_active = user.is_active()
if user:
if hasattr(user, 'is_active'):
return user.is_active() if callable(user.is_active) else user.is_active
else:
is_active = user.is_active
elif user:
is_active = True
else:
is_active = False
return is_active
return True


# This slugify version was borrowed from django revision a61dbd6
Expand Down Expand Up @@ -235,10 +230,10 @@ def setting_url(backend, *names):
for name in names:
if is_url(name):
return name
else:
value = backend.setting(name)
if is_url(value):
return value
value = backend.setting(name)
if is_url(value):
return value
yezz123 marked this conversation as resolved.
Show resolved Hide resolved
return None
yezz123 marked this conversation as resolved.
Show resolved Hide resolved


def handle_http_errors(func):
Expand Down