Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
#170 #154 restructured pro populating forms
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwunderbar666 committed Aug 18, 2022
1 parent 81bf443 commit ec339e4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
4 changes: 2 additions & 2 deletions flaskinventory/add/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def from_draft(entity=None, uid=None):
@add.route("/add/<string:dgraph_type>", methods=['GET', 'POST'])
@add.route("/add/<string:dgraph_type>/draft/<string: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:
Expand All @@ -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')
Expand Down
56 changes: 35 additions & 21 deletions flaskinventory/flaskdgraph/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit ec339e4

Please sign in to comment.