Fix some glitches with removing values

This commit is contained in:
Kevin Chung
2020-08-17 15:44:02 -04:00
parent 96c50f26b1
commit 681a86b15c
6 changed files with 43 additions and 34 deletions

View File

@@ -30,7 +30,12 @@ def SettingsForm(*args, **kwargs):
for field in new_fields:
form_field = getattr(self, f"fields[{field.id}]")
form_field.data = user_fields.get(field.id, "")
initial = user_fields.get(field.id, "")
form_field.data = initial
if form_field.render_kw:
form_field.render_kw["initial"] = initial
else:
form_field.render_kw = {"data-initial": initial}
entry = (field.name, form_field)
fields.append(entry)
return fields

View File

@@ -70,7 +70,12 @@ def UserEditForm(*args, **kwargs):
for field in new_fields:
form_field = getattr(self, f"fields[{field.id}]")
form_field.data = user_fields.get(field.id, "")
initial = user_fields.get(field.id, "")
form_field.data = initial
if form_field.render_kw:
form_field.render_kw["initial"] = initial
else:
form_field.render_kw = {"data-initial": initial}
entry = (field.name, form_field)
fields.append(entry)
return fields

View File

@@ -199,35 +199,35 @@ class UserSchema(ma.ModelSchema):
user_id = data.get("id")
if user_id:
target_user = Users.query.filter_by(id=data["id"]).first()
provided_ids = []
for f in fields:
f.pop("id", None)
field_id = f.get("field_id")
# # Check that we have an existing field for this. May be unnecessary b/c the foriegn key should enforce
field = UserFields.query.filter_by(id=field_id).first_or_404()
# Get the existing field entry if one exists
entry = FieldEntries.query.filter_by(
field_id=field.id, user_id=target_user.id
).first()
if entry:
f["id"] = entry.id
# Extremely dirty hack to prevent deleting previously provided data.
# This needs a better soln.
entries = (
FieldEntries.query.options(load_only("id"))
.filter_by(user_id=current_user.id)
.all()
)
print(entries)
for entry in entries:
if entry.id not in provided_ids:
fields.append({"id": entry.id})
else:
# Marshmallow automatically links the fields to newly created users
pass
target_user = current_user
provided_ids = []
for f in fields:
f.pop("id", None)
field_id = f.get("field_id")
# # Check that we have an existing field for this. May be unnecessary b/c the foriegn key should enforce
field = UserFields.query.filter_by(id=field_id).first_or_404()
# Get the existing field entry if one exists
entry = FieldEntries.query.filter_by(
field_id=field.id, user_id=target_user.id
).first()
if entry:
f["id"] = entry.id
provided_ids.append(entry.id)
# Extremely dirty hack to prevent deleting previously provided data.
# This needs a better soln.
entries = (
FieldEntries.query.options(load_only("id"))
.filter_by(user_id=target_user.id)
.all()
)
for entry in entries:
if entry.id not in provided_ids:
fields.append({"id": entry.id})
else:
provided_ids = []
for f in fields:
@@ -259,7 +259,6 @@ class UserSchema(ma.ModelSchema):
.filter_by(user_id=current_user.id)
.all()
)
print(entries)
for entry in entries:
if entry.id not in provided_ids:
fields.append({"id": entry.id})

File diff suppressed because one or more lines are too long

View File

@@ -26,7 +26,7 @@ $.fn.serializeJSON = function(omit_nulls) {
if (x.value !== null && x.value !== "") {
params[x.name] = x.value;
} else {
let input = form.find(`:input[name=${x.name}]`);
let input = form.find(`:input[name='${x.name}']`);
if (input.data("initial") !== input.val()) {
params[x.name] = x.value;
}

File diff suppressed because one or more lines are too long