Skip to content

Commit

Permalink
pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
jmerdich committed Nov 30, 2016
1 parent 298104b commit ef08a35
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions report_builder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@

AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')


@lru_cache(maxsize=None)
def get_model_contenttype(name):
if "." in name:
# Fully qualified model name
split_name = name.lower().split('.')
# Negative indexing allows, eg. "django.contrib.auth.User"
model_query = {
'app_label':split_name[-2],
'model': split_name[-1]
}
'app_label': split_name[-2],
'model': split_name[-1]
}
model_name = "%s.%s" % (model_query['app_label'], model_query['model'])
else:
# short model name
Expand All @@ -43,30 +44,27 @@ def get_model_contenttype(name):
model_ct = ContentType.objects.get(**model_query)
except ContentType.DoesNotExist:
raise ImproperlyConfigured(
"REPORT_BUILDER: Model '%s' (from '%s') could not be found." %
(model_name, name)
)
"REPORT_BUILDER: Model '%s' (from '%s') could not be found." % (model_name, name))
except ContentType.MultipleObjectsReturned:
possible_cts = ContentType.objects.filter(**model_query).values_list('app_label', 'model')
possible_cts = [".".join(ct) for ct in possible_cts]

raise ImproperlyConfigured(
"REPORT_BUILDER: Model '%s' is ambiguous. Possible values: %s" %
(name, str(possible_cts))
)
"REPORT_BUILDER: Model '%s' is ambiguous. Possible values: %s" % (name, str(possible_cts)))
else:
return model_ct


def get_allowed_models():
models = ContentType.objects.all()
if getattr(settings, 'REPORT_BUILDER_INCLUDE', False):
ct_pks = [] # pks of all included model contenttypes
ct_pks = [] # pks of all included model contenttypes
for element in settings.REPORT_BUILDER_INCLUDE:
ct_pks.append(get_model_contenttype(element).pk)
models = ContentType.objects.filter(pk__in=ct_pks)

if getattr(settings, 'REPORT_BUILDER_EXCLUDE', False):
ct_pks = [] # pks of all excluded model contenttypes
ct_pks = [] # pks of all excluded model contenttypes
for element in settings.REPORT_BUILDER_EXCLUDE:
ct_pks.append(get_model_contenttype(element).pk)
models = models.exclude(pk__in=ct_pks)
Expand Down

0 comments on commit ef08a35

Please sign in to comment.