Skip to content
Open
Changes from all 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
14 changes: 12 additions & 2 deletions johnny/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def disallowed_table(*tables):
return not bool(settings.WHITELIST.issuperset(tables)) if settings.WHITELIST\
else bool(settings.BLACKLIST.intersection(tables))

def is_query_random(query):
"""
Controls every query for the possibility of ORDER BY RAND().
"""
pattern = re.compile('RAND\(\)', re.IGNORECASE)
matches = pattern.search(query)
if matches:
return True
return False


def get_backend(**kwargs):
"""
Expand Down Expand Up @@ -346,7 +356,7 @@ def newfun(cls, *args, **kwargs):
# check the blacklist for any of the involved tables; if it's not
# there, then look for the value in the cache.
tables = get_tables_for_query(cls.query)
if tables and not disallowed_table(*tables):
if tables and not disallowed_table(*tables) and not is_query_random(cls.as_sql()[0]):
gen_key = self.keyhandler.get_generation(*tables,
**{'db': db})
key = self.keyhandler.sql_key(gen_key, sql, params,
Expand Down Expand Up @@ -500,7 +510,7 @@ def newfun(cls, result_type=MULTI):
tables = get_tables_for_query11(cls)
# check the blacklist for any of the involved tables; if it's not
# there, then look for the value in the cache.
if tables and not disallowed_table(*tables):
if tables and not disallowed_table(*tables) and not is_query_random(cls.as_sql()[0]):
gen_key = self.keyhandler.get_generation(*tables)
key = self.keyhandler.sql_key(gen_key, sql, params,
cls.ordering_aliases, result_type)
Expand Down