Compare commits

...

6 Commits

2 changed files with 27 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import MySQLdb as mysql
from collections import defaultdict
from typing import Any, Dict, List
import civicrmapi4
from civicrmapi4.civicrmapi4 import APIv4
import phpserialize
@ -40,7 +41,8 @@ DUMP_TRIVIAL = ["FinancialType",
"OptionGroup",
"OptionValue",
"Domain",
"SavedSearch"]
"SavedSearch",
"Tag"]
# "ContributionPage", needs payment processors & payment_processor column formatted correctly.
@ -66,7 +68,13 @@ LOAD_TRIVIAL = ["FinancialType",
"OptionValue",
"Domain",
"Contact",
"SavedSearch"]
"SavedSearch",
"Tag"]
WEIRD_LIST = [
("civicrm_contact", "contact_sub_type"),
("civicrm_custom_group", "extends_entity_column_value")
]
# 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.
@ -111,6 +119,9 @@ def object_to_table(instr: str) -> str:
def array_to_weird_array(val: List) -> str:
if (val is None):
return "NULL"
return '"\x01' + ('\x01'.join([str(x) for x in val])) + '\x01"'
def value_to_php_serialized(val: Any) -> str:
@ -143,7 +154,7 @@ def dict_to_insert(table: str, objdict: Dict) -> str:
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":
if (table, col) in WEIRD_LIST:
values.append(array_to_weird_array(objdict[col]))
elif table == "civicrm_saved_search" and col == "form_values":
values.append(value_to_php_serialized(objdict[col]))
@ -212,6 +223,16 @@ def parse_arguments() -> argparse.Namespace:
return parser.parse_args()
def wrap_api_get(api, obj, args=None):
if args is None:
args = []
try:
return api.get(obj, args)
except civicrmapi4.civicrmapi4.CallFailed:
logging.error("Could not fetch {}".format(obj))
return []
def main() -> int:
args = parse_arguments()
@ -251,14 +272,14 @@ def main() -> int:
for table in DUMP_TRIVIAL:
output = args.output / (table + ".json")
data = api.get(table)
data = wrap_api_get(api, table)
if data:
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", "CONTAINS", "Political_Party"]])
data = wrap_api_get(api, "Contact", where=[["contact_sub_type", "CONTAINS", "Political_Party"]])
if data:
print("dumping parties")
with output.open("w") as of:

View File

@ -1,3 +1,3 @@
-e git+https://git.autonomic.zone/cas/civicrmapi4#egg=civicrmapi4
-e git+https://git.autonomic.zone/autonomic-cooperative/civicrmapi4#egg=civicrmapi4
phpserialize
mysqlclient~=1.4.6