diff --git a/flaskinventory/add/routes.py b/flaskinventory/add/routes.py index cadb2810..1999fe88 100644 --- a/flaskinventory/add/routes.py +++ b/flaskinventory/add/routes.py @@ -102,7 +102,7 @@ def from_draft(entity=None, uid=None): @add.route("/add/", methods=['GET', 'POST']) @add.route("/add//draft/", methods=['GET', 'POST']) @login_required -def new(dgraph_type=None, draft=None): +def new(dgraph_type=None, draft=None, populate_form: dict=None): try: dgraph_type = Schema.get_type(dgraph_type) except: @@ -111,7 +111,7 @@ def new(dgraph_type=None, draft=None): if not dgraph_type: return abort(404) - form = Schema.generate_new_entry_form(dgraph_type=dgraph_type) + form = Schema.generate_new_entry_form(dgraph_type=dgraph_type, populate_obj=populate_form) if draft is None: draft = request.args.get('draft') diff --git a/flaskinventory/flaskdgraph/schema.py b/flaskinventory/flaskdgraph/schema.py index 6e7fe969..d2817a10 100644 --- a/flaskinventory/flaskdgraph/schema.py +++ b/flaskinventory/flaskdgraph/schema.py @@ -150,8 +150,34 @@ def permissions_edit(cls, _cls) -> int: _cls = _cls.__name__ return cls.__perm_registry_edit__[_cls] + @staticmethod + def populate_form(form: FlaskForm, populate_obj: dict, fields: dict) -> FlaskForm: + from flaskinventory.flaskdgraph.dgraph_types import SingleChoice + + for k, value in populate_obj.items(): + if hasattr(form, k): + if type(value) is dict: + if 'uid' in value.keys(): + value = value['uid'] + elif type(value) is list and not isinstance(fields[k], SingleChoice): + if type(value[0]) is str: + delimiter = getattr(fields[k], 'delimiter', ',') + value = delimiter.join(value) + elif type(value[0]) is int: + value = [str(val) for val in value] + elif 'uid' in value[0].keys(): + value = [subval['uid'] for subval in value] + if len(value) == 1: + value = value[0] + if isinstance(getattr(form, k), IntegerField) and isinstance(value, datetime): + # cast datetime as year if field does not need to be too specific + value = value.year + setattr(getattr(form, k), 'data', value) + return form + + @classmethod - def generate_new_entry_form(cls, dgraph_type=None) -> FlaskForm: + def generate_new_entry_form(cls, dgraph_type=None, populate_obj: dict=None) -> FlaskForm: if dgraph_type: fields = cls.get_predicates(dgraph_type) @@ -177,8 +203,14 @@ def get_field(self, field): for k, v in fields.items(): if v.new: setattr(F, k, v.wtf_field) + + form = F() + # ability to pre-populate the form with data + if populate_obj: + form = cls.populate_form(form, populate_obj, fields) + + return form - return F() @classmethod def generate_edit_entry_form(cls, dgraph_type=None, populate_obj: dict={}, entry_review_status='pending', skip_fields: list=None) -> FlaskForm: @@ -226,24 +258,6 @@ def get_field(self, field): form = F() # Populate instance with existing values - for k, value in populate_obj.items(): - if hasattr(form, k): - if type(value) is dict: - if 'uid' in value.keys(): - value = value['uid'] - elif type(value) is list: - if type(value[0]) is str: - delimiter = getattr(fields[k], 'delimiter', ',') - value = delimiter.join(value) - elif type(value[0]) is int: - value = [str(val) for val in value] - elif 'uid' in value[0].keys(): - value = [subval['uid'] for subval in value] - if len(value) == 1: - value = value[0] - if isinstance(getattr(form, k), IntegerField) and isinstance(value, datetime): - # cast datetime as year if field does not need to be too specific - value = value.year - setattr(getattr(form, k), 'data', value) + form = cls.populate_form(form, populate_obj, fields) return form \ No newline at end of file