-
-
Save mvidalgarcia/62af074d1a7d6efc1e320844b562e982 to your computer and use it in GitHub Desktop.
regform_fix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy.orm.attributes import flag_modified | |
import uuid | |
lst = [] | |
for reg_item in RegistrationFormItem.find(input_type='accommodation', is_deleted=False): | |
choice_id = unicode(uuid.uuid4()) | |
needs_caption = False | |
for rffd in RegistrationFormFieldData.find(field_id=reg_item.id): | |
if not any(('is_no_accommodation' in choice) for choice in rffd.versioned_data['choices']): | |
print reg_item.registration_form, reg_item, rffd, not reg_item.is_required | |
lst.append((reg_item.registration_form, reg_item, rffd, not reg_item.is_required)) | |
needs_caption = True | |
no_acc_choice = {"is_no_accommodation": True, | |
"is_enabled": not reg_item.is_required, | |
"price": 0, | |
"is_billable": False, | |
"places_limit": 0, | |
"placeholder": "Title of the \"None\" option", | |
"id": choice_id} | |
rffd.versioned_data['choices'].append(no_acc_choice) | |
flag_modified(rffd, 'versioned_data') | |
if needs_caption: | |
reg_item.data['captions'][choice_id] = 'No accommodation' | |
flag_modified(reg_item, 'data') | |
# ----- SQL + python ----- | |
import json | |
import uuid, itertools | |
from alembic import context, op | |
from operator import attrgetter | |
#if context.is_offline_mode(): | |
# raise Exception('This upgrade is only possible in online mode') | |
#conn = op.get_bind() | |
conn = db.engine | |
field_data_query = conn.execute(""" | |
SELECT ffd.field_id, ffd.id, fi.is_required, ffd.versioned_data, fi.data, registration_form_id | |
FROM event_registration.form_field_data ffd | |
JOIN event_registration.form_items fi ON fi.id = ffd.field_id | |
WHERE fi.input_type = 'accommodation' AND NOT fi.is_deleted | |
ORDER BY ffd.field_id | |
""") | |
for field_id, data in itertools.groupby(field_data_query, attrgetter('field_id')): | |
choice_id = unicode(uuid.uuid4()) | |
needs_caption = False | |
for row in data: | |
if not any(('is_no_accommodation' in choice) for choice in row.versioned_data['choices']): | |
print "\n\nREGFORM_ID: " + str(row.registration_form_id) + " NO_ACC_SHOWS?:" + str(not row.is_required) | |
needs_caption = True | |
no_acc_choice = {'is_no_accommodation': True, | |
'is_enabled': not row.is_required, | |
'price': 0, | |
'is_billable': False, | |
'places_limit': 0, | |
'placeholder': 'Title of the "None" option', | |
'id': choice_id} | |
row.versioned_data['choices'] += [no_acc_choice] | |
print "\nVERSIONED_DATA: " + str(row.versioned_data) | |
conn.execute("UPDATE event_registration.form_field_data SET versioned_data = %s::json WHERE id = %s", | |
(json.dumps(row.versioned_data), row.id)) | |
if needs_caption: | |
row.data['captions'][choice_id] = 'No accommodation' | |
print "\nFIELD DATA: " + str(row.data) | |
conn.execute("UPDATE event_registration.form_items SET data = %s::json WHERE id = %s", | |
(json.dumps(row.data), field_id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment