From e8bc600572fe297bbe71fd90e0eef5bada2bb947 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Thu, 16 Jul 2015 20:30:37 -0300 Subject: [PATCH] Add code gen of crispy form from scraped data --- comissoes/forms.py | 2 +- legacy/scripts/scrap_original_forms.py | 51 ++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/comissoes/forms.py b/comissoes/forms.py index 2732eb004..4deb5bd67 100644 --- a/comissoes/forms.py +++ b/comissoes/forms.py @@ -10,7 +10,7 @@ class ComissaoForm(forms.ModelForm): class Meta: model = Comissao - fields = [f.name for f in Comissao._meta.fields] + exclude = [] def __init__(self, *args, **kwargs): super(ComissaoForm, self).__init__(*args, **kwargs) diff --git a/legacy/scripts/scrap_original_forms.py b/legacy/scripts/scrap_original_forms.py index e04b241d5..b7d96211d 100644 --- a/legacy/scripts/scrap_original_forms.py +++ b/legacy/scripts/scrap_original_forms.py @@ -1,3 +1,4 @@ +import pprint import os import re import string @@ -214,11 +215,55 @@ old_names_adjustments = yaml.load(pkg_resources.resource_string( @listify def extract_fieldsets_for_current(model): + __, fieldsets = extract_title_and_fieldsets(model) + if not fieldsets: + return + reverse_field_renames = {v: k for k, v in field_renames[model].items()} adjustments = old_names_adjustments.get(model.__name__) - for adjusted_key, key in adjustments.items(): - reverse_field_renames[adjusted_key] = reverse_field_renames[key] - __, fieldsets = extract_title_and_fieldsets(model) + if adjustments: + for adjusted_key, key in adjustments.items(): + reverse_field_renames[adjusted_key] = reverse_field_renames[key] + for fieldset in fieldsets: rows = [colsplit([reverse_field_renames[name] for name, __ in line]) for line in fieldset['lines']] yield [fieldset['legend']] + rows + + +class Under(object): + + def __init__(self, arg): + self.arg = arg + + def __repr__(self): + return "_('%s')" % self.arg + + +GAP = 12 +pretty_printer = pprint.PrettyPrinter(width=80 - GAP) + + +def print_crispy_form(model): + fieldsets = extract_fieldsets_for_current(model) + if fieldsets: + print(""" +class %(name)sForm(forms.ModelForm): + + class Meta: + model = %(name)s + exclude = [] + + def __init__(self, *args, **kwargs): + super(ComissaoForm, self).__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.layout = SaplFormLayout( +""" % {'name': model.__name__}) + + for legend, *rows in fieldsets: + lines = pretty_printer.pformat([Under(legend)] + rows) + ',\n\n' + for line in lines.splitlines(): + print(' ' * GAP + line if line.strip() else '') + + print(""" + ) +""".strip('\n'))