From 00ac7d7f268375849d575ed6c9c6e599cc6a0200 Mon Sep 17 00:00:00 2001 From: James Gordon Date: Wed, 13 Sep 2017 09:53:54 -0500 Subject: [PATCH] Add file_name property to proxy models --- .../opencivicdata/ballotmeasurecontests.py | 2 +- .../models/proxies/opencivicdata/base.py | 23 +++++++++++-------- .../proxies/opencivicdata/candidacies.py | 2 +- .../opencivicdata/retentioncontests.py | 2 +- calaccess_processed/models/tracking.py | 6 ++++- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/calaccess_processed/models/proxies/opencivicdata/ballotmeasurecontests.py b/calaccess_processed/models/proxies/opencivicdata/ballotmeasurecontests.py index 2aacb808..3e9d2b0c 100644 --- a/calaccess_processed/models/proxies/opencivicdata/ballotmeasurecontests.py +++ b/calaccess_processed/models/proxies/opencivicdata/ballotmeasurecontests.py @@ -134,4 +134,4 @@ class Meta: Make this a proxy model. """ proxy = True - verbose_name_plural = 'BallotMeasures' + verbose_name_plural = 'ballot measures' diff --git a/calaccess_processed/models/proxies/opencivicdata/base.py b/calaccess_processed/models/proxies/opencivicdata/base.py index 6dafb1af..1167deae 100644 --- a/calaccess_processed/models/proxies/opencivicdata/base.py +++ b/calaccess_processed/models/proxies/opencivicdata/base.py @@ -14,30 +14,33 @@ class OCDProxyModelMixin(object): @property def base_model(self): """ - Returns the model class that is being proxied. + The model being proxied. """ return self.__class__.__bases__[0] @property def is_flat(self): """ - Return True if the proxy model is used to flatten relational data models. + True if the proxy model is used to flatten relational data models. """ return 'Flat' in self._meta.object_name @property - def object_name(self): + def file_name(self): """ - Return the model's object name as a string. + The name for the csv to which the model's contents will be dumped. - If the model is flat model proxy, include the prefix "Flat". Otherwise, - just use the object name of the model that is being proxied. + If the model is a flat model proxy, return the model's verbose_name_plural + in CamelCase. Otherwise, return the object_name of the base_model. """ if self.is_flat: - object_name = 'Flat%s' % self.base_model._meta.object_name + file_name = ''.join( + x for x in str(self._meta.verbose_name_plural).title() + if not x.isspace() + ) else: - object_name = self.base_model._meta.object_name - return object_name + file_name = self.base_model._meta.object_name + return file_name @property def doc(self): @@ -55,7 +58,7 @@ def doc(self): @property def klass_group(self): """ - Return the model's group. + The model's group. """ if self.is_flat: group = "Flat" diff --git a/calaccess_processed/models/proxies/opencivicdata/candidacies.py b/calaccess_processed/models/proxies/opencivicdata/candidacies.py index f6b9da4b..f80a9c70 100644 --- a/calaccess_processed/models/proxies/opencivicdata/candidacies.py +++ b/calaccess_processed/models/proxies/opencivicdata/candidacies.py @@ -379,4 +379,4 @@ class Meta: Make this a proxy model. """ proxy = True - verbose_name_plural = 'Candidates' + verbose_name_plural = 'candidates' diff --git a/calaccess_processed/models/proxies/opencivicdata/retentioncontests.py b/calaccess_processed/models/proxies/opencivicdata/retentioncontests.py index 8c909b7c..9a06bc6e 100644 --- a/calaccess_processed/models/proxies/opencivicdata/retentioncontests.py +++ b/calaccess_processed/models/proxies/opencivicdata/retentioncontests.py @@ -147,4 +147,4 @@ class Meta: Make this a proxy model. """ proxy = True - verbose_name_plural = 'RecallMeasures' + verbose_name_plural = 'recall measures' diff --git a/calaccess_processed/models/tracking.py b/calaccess_processed/models/tracking.py index 04192186..8c71da78 100644 --- a/calaccess_processed/models/tracking.py +++ b/calaccess_processed/models/tracking.py @@ -5,6 +5,7 @@ """ from __future__ import unicode_literals import os +import re from hurry.filesize import size as sizeformat from django.apps import apps from django.db import models @@ -224,10 +225,13 @@ def model(self): 'OCD%sProxy' % self.file_name, ) except LookupError: + # convert from camel case + s1 = re.sub(r'(.)([A-Z][a-z]+)', r'\1 \2', self.file_name) + s2 = re.sub(r'([a-z0-9])([A-Z])', r'\1 \2', s1).lower() # try looking up by plural name model_list = [ m for m in apps.get_models() - if m._meta.verbose_name_plural == self.file_name + if m._meta.verbose_name_plural == s2 ] try: model = model_list.pop()