summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorДилян Палаузов <Dilyan.Palauzov@db.com>2018-01-03 18:52:12 -0500
committerTim Graham <timograham@gmail.com>2018-01-03 20:12:23 -0500
commitd7b2aa24f75434c2ce50100cfef3586071e0747a (patch)
tree9074eb7522888e744f948c52174f367a4281c200 /django/utils
parentc2d0f8c084456b5073252a91eeb09ab3d7453b18 (diff)
Fixed #28982 -- Simplified code with and/or.
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/cache.py6
-rw-r--r--django/utils/crypto.py3
-rw-r--r--django/utils/dateparse.py6
-rw-r--r--django/utils/deprecation.py3
-rw-r--r--django/utils/feedgenerator.py6
-rw-r--r--django/utils/numberformat.py3
-rw-r--r--django/utils/timesince.py3
7 files changed, 10 insertions, 20 deletions
diff --git a/django/utils/cache.py b/django/utils/cache.py
index ce7faaedbc..cb49806ee9 100644
--- a/django/utils/cache.py
+++ b/django/utils/cache.py
@@ -142,12 +142,10 @@ def get_conditional_response(request, etag=None, last_modified=None, response=No
# Get HTTP request headers.
if_match_etags = parse_etags(request.META.get('HTTP_IF_MATCH', ''))
if_unmodified_since = request.META.get('HTTP_IF_UNMODIFIED_SINCE')
- if if_unmodified_since:
- if_unmodified_since = parse_http_date_safe(if_unmodified_since)
+ if_unmodified_since = if_unmodified_since and parse_http_date_safe(if_unmodified_since)
if_none_match_etags = parse_etags(request.META.get('HTTP_IF_NONE_MATCH', ''))
if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE')
- if if_modified_since:
- if_modified_since = parse_http_date_safe(if_modified_since)
+ if_modified_since = if_modified_since and parse_http_date_safe(if_modified_since)
# Step 1 of section 6 of RFC 7232: Test the If-Match precondition.
if if_match_etags and not _if_match_passes(etag, if_match_etags):
diff --git a/django/utils/crypto.py b/django/utils/crypto.py
index f6f44856d0..61d02b65cf 100644
--- a/django/utils/crypto.py
+++ b/django/utils/crypto.py
@@ -78,8 +78,7 @@ def pbkdf2(password, salt, iterations, dklen=0, digest=None):
"""Return the hash of password using pbkdf2."""
if digest is None:
digest = hashlib.sha256
- if not dklen:
- dklen = None
+ dklen = dklen or None
password = force_bytes(password)
salt = force_bytes(salt)
return hashlib.pbkdf2_hmac(digest().name, password, salt, iterations, dklen)
diff --git a/django/utils/dateparse.py b/django/utils/dateparse.py
index e0e5e244c7..8d08b7d1d3 100644
--- a/django/utils/dateparse.py
+++ b/django/utils/dateparse.py
@@ -89,8 +89,7 @@ def parse_time(value):
match = time_re.match(value)
if match:
kw = match.groupdict()
- if kw['microsecond']:
- kw['microsecond'] = kw['microsecond'].ljust(6, '0')
+ kw['microsecond'] = kw['microsecond'] and kw['microsecond'].ljust(6, '0')
kw = {k: int(v) for k, v in kw.items() if v is not None}
return datetime.time(**kw)
@@ -107,8 +106,7 @@ def parse_datetime(value):
match = datetime_re.match(value)
if match:
kw = match.groupdict()
- if kw['microsecond']:
- kw['microsecond'] = kw['microsecond'].ljust(6, '0')
+ kw['microsecond'] = kw['microsecond'] and kw['microsecond'].ljust(6, '0')
tzinfo = kw.pop('tzinfo')
if tzinfo == 'Z':
tzinfo = utc
diff --git a/django/utils/deprecation.py b/django/utils/deprecation.py
index 07a7f853a9..1cb1df105c 100644
--- a/django/utils/deprecation.py
+++ b/django/utils/deprecation.py
@@ -88,8 +88,7 @@ class MiddlewareMixin:
response = None
if hasattr(self, 'process_request'):
response = self.process_request(request)
- if not response:
- response = self.get_response(request)
+ response = response or self.get_response(request)
if hasattr(self, 'process_response'):
response = self.process_response(request, response)
return response
diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py
index a0f63901c7..7fc63f5adb 100644
--- a/django/utils/feedgenerator.py
+++ b/django/utils/feedgenerator.py
@@ -86,8 +86,7 @@ class SyndicationFeed:
feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs):
def to_str(s):
return str(s) if s is not None else s
- if categories:
- categories = [str(c) for c in categories]
+ categories = categories and [str(c) for c in categories]
self.feed = {
'title': to_str(title),
'link': iri_to_uri(link),
@@ -117,8 +116,7 @@ class SyndicationFeed:
"""
def to_str(s):
return str(s) if s is not None else s
- if categories:
- categories = [to_str(c) for c in categories]
+ categories = categories and [to_str(c) for c in categories]
self.items.append({
'title': to_str(title),
'link': iri_to_uri(link),
diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py
index e910be9206..c6f9af6893 100644
--- a/django/utils/numberformat.py
+++ b/django/utils/numberformat.py
@@ -42,8 +42,7 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
int_part, dec_part = str_number, ''
if decimal_pos is not None:
dec_part = dec_part + ('0' * (decimal_pos - len(dec_part)))
- if dec_part:
- dec_part = decimal_sep + dec_part
+ dec_part = dec_part and decimal_sep + dec_part
# grouping
if use_grouping:
try:
diff --git a/django/utils/timesince.py b/django/utils/timesince.py
index 8acfb6a484..1616c9a2ea 100644
--- a/django/utils/timesince.py
+++ b/django/utils/timesince.py
@@ -35,8 +35,7 @@ def timesince(d, now=None, reversed=False):
if now and not isinstance(now, datetime.datetime):
now = datetime.datetime(now.year, now.month, now.day)
- if not now:
- now = datetime.datetime.now(utc if is_aware(d) else None)
+ now = now or datetime.datetime.now(utc if is_aware(d) else None)
if reversed:
d, now = now, d