Created
December 19, 2014 06:47
-
-
Save ddemid/82b518290a278c76530d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _render_ref_no(self, template): | |
""" | |
Renders template provided in the string.Template format. Uses output of *TemplateSerializer as the template's | |
context. | |
""" | |
ref_no_template = Template(template) | |
from legal.api.serializers import MatterTemplateSerializer, SubMatterTemplateSerializer | |
srlzr = MatterTemplateSerializer if isinstance(self, Matter) else SubMatterTemplateSerializer | |
def flatten(data, prefix=''): | |
""" | |
Flattens nested dicts. For Example: | |
{'title': 'lala', 'matter_type': {'title': 'Patent', 'code': 'P'}} -> | |
{'title': 'lala', 'matter_type_title': 'Patent', 'matter_type_code': 'P'} | |
Dicts can be nested arbitrary deep. | |
""" | |
result = {} | |
for key in data.keys(): | |
if isinstance(data[key], dict): | |
result.update(flatten(data[key], prefix + key + '_')) | |
else: | |
result[prefix + key] = data[key] | |
return result | |
return ref_no_template.substitute(flatten(srlzr(self).data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment