Fix array values in Contact for contact_sub_type

This commit is contained in:
Cassowary Rusnov 2022-03-10 08:10:53 -08:00
parent 3344394fbc
commit 12d094c392

View File

@ -63,7 +63,8 @@ LOAD_TRIVIAL = ["FinancialType",
"CustomGroup",
"OptionGroup",
"OptionValue",
"Domain",]
"Domain",
"Contact"]
# This is a payment processor we can assign contribution pages to in order for them to work.
# FIXME this seems to produce a non-working setup.
@ -107,6 +108,10 @@ def object_to_table(instr: str) -> str:
return 'civicrm_' + '_'.join([x.lower() for x in words])
def array_to_weird_array(val: List) -> str:
return '"\x01' + ('\x01'.join([str(x) for x in val])) + '\x01"'
def python_value_to_sql(val: Any) -> str:
"""
"""
@ -119,7 +124,8 @@ def python_value_to_sql(val: Any) -> str:
if (isinstance(val, (int, float, complex))):
return str(val)
if (type(val) == list):
return "'{}'".format(",".join([str(v) for v in val]))
# weird list serialization
return "'" + ','.join([str(x) for x in val]) + "'"
if (type(val) == dict):
return "'{}'".format(mysql.escape_string(phpserialize.dumps(val).decode()).decode())
return "'{}'".format(mysql.escape_string(val).decode())
@ -130,7 +136,13 @@ def dict_to_insert(table: str, objdict: Dict) -> str:
"""
columns = tuple(x for x in objdict.keys())
values = tuple(python_value_to_sql(objdict[x]) for x in columns)
values = list()
for col in columns:
# any weird array we have to process here if there are others
if table == "civicrm_contact" and col == "contact_sub_type":
values.append(array_to_weird_array(objdict[col]))
else:
values.append(python_value_to_sql(objdict[col]))
return "REPLACE INTO {} ({}) VALUES ({});".format(table, ",".join(columns), ",".join(values))
@ -238,7 +250,6 @@ def main() -> int:
print("dumping", table)
with output.open("w") as of:
of.write(json.dumps(data))
# dump org contacts
output = args.output / ("Contact.json")
data = api.get("Contact", where=[["contact_sub_type", "=", "Political_Party"]])
@ -258,10 +269,9 @@ def main() -> int:
cursor.execute("SET FOREIGN_KEY_CHECKS=0;")
query = dict_to_insert("civicrm_payment_processor", STANDIN_PAYMENT_PROCESSOR)
print(query)
print(cursor.execute(query))
for table in LOAD_TRIVIAL:
# exceptions that require extra processing
print(table)
with open((args.input / (table + ".json"))) as inf:
indata = json.load(inf)
table_name = object_to_table(table)
@ -269,9 +279,11 @@ def main() -> int:
if table == "ContributionPage":
for row in indata:
row['payment_processor'] = STANDIN_PAYMENT_PROCESSOR_ID
tot = 0;
for row in indata:
query = dict_to_insert(table_name, row)
cursor.execute(query)
tot += cursor.execute(query)
print(tot)
# create custom field data tables
custom_fields = defaultdict(list)