Skip to content

Commit

Permalink
Add file_name property to proxy models
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonje committed Sep 13, 2017
1 parent 68925ad commit 00ac7d7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
Expand Up @@ -134,4 +134,4 @@ class Meta:
Make this a proxy model.
"""
proxy = True
verbose_name_plural = 'BallotMeasures'
verbose_name_plural = 'ballot measures'
23 changes: 13 additions & 10 deletions calaccess_processed/models/proxies/opencivicdata/base.py
Expand Up @@ -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):
Expand All @@ -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"
Expand Down
Expand Up @@ -379,4 +379,4 @@ class Meta:
Make this a proxy model.
"""
proxy = True
verbose_name_plural = 'Candidates'
verbose_name_plural = 'candidates'
Expand Up @@ -147,4 +147,4 @@ class Meta:
Make this a proxy model.
"""
proxy = True
verbose_name_plural = 'RecallMeasures'
verbose_name_plural = 'recall measures'
6 changes: 5 additions & 1 deletion calaccess_processed/models/tracking.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 00ac7d7

Please sign in to comment.