Update converter

This commit is contained in:
Olivia Rodriguez Valdes 2019-03-03 15:33:11 -05:00
parent 0541b713e9
commit 6ea5e2bbaf
2 changed files with 13 additions and 3 deletions

View File

@ -86,7 +86,13 @@ def convert_form_field_to_id(field):
return ID(required=field.required)
def convert_form_field_with_choices(name, field):
def get_form_name(form):
"""Get form name"""
class_name = str(form.__class__).split('.')[-1]
return class_name[:-2]
def convert_form_field_with_choices(field, name=None, form=None):
"""
Helper method to convert a field to graphene Field type.
:param name: form field's name
@ -96,7 +102,11 @@ def convert_form_field_with_choices(name, field):
choices = getattr(field, 'choices', None)
if choices:
name = to_camel_case(field.label or name)
if form:
name = to_camel_case("{}_{}".format(get_form_name(form), field.label or name))
else:
name = field.label or name
name = to_camel_case(name.replace(' ', '_'))
choices = list(get_choices(choices))
named_choices = [(c[0], c[1]) for c in choices]
named_choices_descriptions = {c[0]: c[2] for c in choices}

View File

@ -30,7 +30,7 @@ def fields_for_form(form, only_fields, exclude_fields):
if is_not_in_only or is_excluded:
continue
fields[name] = convert_form_field_with_choices(name, field)
fields[name] = convert_form_field_with_choices(field, name=name, form=form)
return fields