hobo: do not lose varname when updating profile (#7253)

This commit is contained in:
Frédéric Péters 2015-05-15 11:22:11 +02:00
parent 82124a4edd
commit 70e312a0fa
1 changed files with 12 additions and 11 deletions

View File

@ -107,32 +107,33 @@ class CmdCheckHobos(Command):
def update_profile(self, profile, pub):
formdef = UserFieldsFormDef(publisher=pub)
profile_fields = {}
profile_field_names = [x['name'] for x in profile.get('fields', [])]
profile_field_names = ['_' + x['name'] for x in profile.get('fields', [])]
for field in formdef.fields:
if field.varname in profile_field_names:
profile_fields[field.varname] = field
if field.id in profile_field_names:
profile_fields[field.id] = field
# create or update profile fields
for attribute in profile.get('fields', []):
if not attribute['name'] in profile_fields:
field_id = '_' + attribute['name']
if not field_id in profile_fields:
field_class, field_typename = StringField, 'string'
if attribute['kind'] == 'email':
field_class, field_typename = EmailField, 'email'
new_field = field_class(label=attribute['label'].encode('utf-8'),
type=field_typename,
varname=attribute['name'])
new_field.id = '_' + attribute['name']
profile_fields[new_field.varname] = new_field
new_field.id = field_id
profile_fields[field_id] = new_field
else:
# remove it for the moment
formdef.fields.remove(profile_fields[attribute['name']])
formdef.fields.remove(profile_fields[field_id])
profile_fields[attribute['name']].label = attribute['label'].encode('utf-8')
profile_fields[attribute['name']].hint = attribute['description'].encode('utf-8')
profile_fields[attribute['name']].required = attribute['required']
profile_fields[field_id].label = attribute['label'].encode('utf-8')
profile_fields[field_id].hint = attribute['description'].encode('utf-8')
profile_fields[field_id].required = attribute['required']
if attribute['disabled']:
profile_field_names.remove(attribute['name'])
profile_field_names.remove('_' + attribute['name'])
# insert profile fields at the beginning
formdef.fields = [profile_fields[x] for x in profile_field_names] + formdef.fields