Skip to content

Commit

Permalink
Black
Browse files Browse the repository at this point in the history
  • Loading branch information
rh0dium committed May 28, 2021
1 parent 75f8493 commit 7e72df8
Show file tree
Hide file tree
Showing 34 changed files with 1,558 additions and 1,179 deletions.
24 changes: 11 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ repos:
exclude: .idea/.*|.*\.blg|.*\.json|.*\.dat
- id: mixed-line-ending
exclude: .idea/.*
- id: double-quote-string-fixer
exclude: .idea/.*|.*\.blg|.*\.dat
- id: check-json
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.3
hooks:
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
exclude: migrations/.*\.py
args:
- --max-line-length=120
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.20
- repo: https://github.com/pivotal-energy-solutions/pre-commit-hooks
rev: ee64c7bc9b4e56eb4bab54df3dbdc001b6370218
hooks:
- id: isort
name: iSort
args: [ '--recursive', '--diff' ]
- id: copyright-checker
exclude: manage.py|wsgi.py|migrations/.*\.py
45 changes: 34 additions & 11 deletions datatableview/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
# -*- coding: utf-8 -*-

from .datatables import Datatable, ValuesDatatable, LegacyDatatable
from .columns import (Column, TextColumn, DateColumn, DateTimeColumn, BooleanColumn, IntegerColumn,
FloatColumn, DisplayColumn, CompoundColumn, CheckBoxSelectColumn)
from .columns import (
Column,
TextColumn,
DateColumn,
DateTimeColumn,
BooleanColumn,
IntegerColumn,
FloatColumn,
DisplayColumn,
CompoundColumn,
CheckBoxSelectColumn,
)
from .exceptions import SkipRecord

__name__ = 'datatableview'
__author__ = 'Autumn Valenta'
__name__ = "datatableview"
__author__ = "Autumn Valenta"
__version_info__ = (1, 0, 0)
__version__ = '.'.join(map(str, __version_info__))
__date__ = '2013/11/14 2:00:00 PM'
__credits__ = ['Autumn Valenta', 'Steven Klass']
__license__ = 'See the file LICENSE.txt for licensing information.'
__version__ = ".".join(map(str, __version_info__))
__date__ = "2013/11/14 2:00:00 PM"
__credits__ = ["Autumn Valenta", "Steven Klass"]
__license__ = "See the file LICENSE.txt for licensing information."

__all__ = ['Datatable', 'ValuesDatatable', 'LegacyDatatable', 'Column', 'TextColumn',
'DateColumn', 'DateTimeColumn', 'BooleanColumn', 'IntegerColumn', 'FloatColumn',
'DisplayColumn', 'CompoundColumn', 'CheckBoxSelectColumn', 'SkipRecord']
__all__ = [
"Datatable",
"ValuesDatatable",
"LegacyDatatable",
"Column",
"TextColumn",
"DateColumn",
"DateTimeColumn",
"BooleanColumn",
"IntegerColumn",
"FloatColumn",
"DisplayColumn",
"CompoundColumn",
"CheckBoxSelectColumn",
"SkipRecord",
]
44 changes: 22 additions & 22 deletions datatableview/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@

class cache_types(object):
NONE = None
DEFAULT = 'default'
SIMPLE = 'simple' # Stores queryset objects directly in cache
PK_LIST = 'pk_list' # Stores queryset pks in cache for later expansion back to queryset
DEFAULT = "default"
SIMPLE = "simple" # Stores queryset objects directly in cache
PK_LIST = "pk_list" # Stores queryset pks in cache for later expansion back to queryset


try:
CACHE_BACKEND = settings.DATATABLEVIEW_CACHE_BACKEND
except AttributeError:
CACHE_BACKEND = 'default'
CACHE_BACKEND = "default"

try:
CACHE_PREFIX = settings.DATATABLEVIEW_CACHE_PREFIX
except AttributeError:
CACHE_PREFIX = 'datatableview_'
CACHE_PREFIX = "datatableview_"

try:
DEFAULT_CACHE_TYPE = settings.DATATABLEVIEW_DEFAULT_CACHE_TYPE
Expand All @@ -49,7 +49,7 @@ class cache_types(object):


def _hash_key_component(s):
return hashlib.sha1(s.encode('utf-8')).hexdigest()[hash_slice]
return hashlib.sha1(s.encode("utf-8")).hexdigest()[hash_slice]


def get_cache_key(datatable_class, view=None, user=None, **kwargs):
Expand All @@ -61,54 +61,54 @@ def get_cache_key(datatable_class, view=None, user=None, **kwargs):
"""

datatable_name = datatable_class.__name__
if datatable_name.endswith('_Synthesized'):
if datatable_name.endswith("_Synthesized"):
datatable_name = datatable_name[:-12]
datatable_id = '%s.%s' % (datatable_class.__module__, datatable_name)
datatable_id = "%s.%s" % (datatable_class.__module__, datatable_name)
if CACHE_KEY_HASH:
datatable_id = _hash_key_component(datatable_id)

cache_key = 'datatable_%s' % (datatable_id,)
cache_key = "datatable_%s" % (datatable_id,)

if view:
if not inspect.isclass(view):
# Reduce view to its class
view = view.__class__

view_id = '%s.%s' % (view.__module__, view.__name__)
view_id = "%s.%s" % (view.__module__, view.__name__)
if CACHE_KEY_HASH:
view_id = _hash_key_component(view_id)
cache_key += '__view_%s' % (view_id,)
cache_key += "__view_%s" % (view_id,)

if user and user.is_authenticated:
cache_key += '__user_%s' % (user.pk,)
cache_key += "__user_%s" % (user.pk,)

# All other kwargs are used directly to create a hashed suffix
# Order the kwargs by key name, then convert them to their repr() values.
items = sorted(kwargs.items(), key=lambda item: item[0])
values = []
for k, v in items:
values.append('%r:%r' % (k, v))
values.append("%r:%r" % (k, v))

if values:
kwargs_id = '__'.join(values)
kwargs_id = "__".join(values)
kwargs_id = _hash_key_component(kwargs_id)
cache_key += '__kwargs_%s' % (kwargs_id,)
cache_key += "__kwargs_%s" % (kwargs_id,)

log.debug('Cache key derived for %r: %r (from kwargs %r)', datatable_class, cache_key, values)
log.debug("Cache key derived for %r: %r (from kwargs %r)", datatable_class, cache_key, values)

return cache_key


def get_cached_data(datatable, **kwargs):
""" Returns the cached object list under the appropriate key, or None if not set. """
cache_key = '%s%s' % (CACHE_PREFIX, datatable.get_cache_key(**kwargs))
"""Returns the cached object list under the appropriate key, or None if not set."""
cache_key = "%s%s" % (CACHE_PREFIX, datatable.get_cache_key(**kwargs))
data = cache.get(cache_key)
log.debug('Reading data from cache at %r: %r', cache_key, data)
log.debug("Reading data from cache at %r: %r", cache_key, data)
return data


def cache_data(datatable, data, **kwargs):
""" Stores the object list in the cache under the appropriate key. """
cache_key = '%s%s' % (CACHE_PREFIX, datatable.get_cache_key(**kwargs))
log.debug('Setting data to cache at %r: %r', cache_key, data)
"""Stores the object list in the cache under the appropriate key."""
cache_key = "%s%s" % (CACHE_PREFIX, datatable.get_cache_key(**kwargs))
log.debug("Setting data to cache at %r: %r", cache_key, data)
cache.set(cache_key, data)
Loading

0 comments on commit 7e72df8

Please sign in to comment.