Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple field json #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WorkbenchConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def get_media_types(self):
"webm",
"ogv",
"mpeg",
"m4v",
]
},
{"extracted_text": ["txt"]},
Expand Down
1 change: 1 addition & 0 deletions workbench
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def create():
if "field_member_of" in row.keys() and (
len(row["field_member_of"]) > 0
and value_is_numeric(row["field_member_of"]) is False
and config["subdelimiter"] not in row["field_member_of"]
):
field_member_of_value_for_message = copy.copy(row["field_member_of"])
row["field_member_of"] = get_nid_from_url_alias(
Expand Down
65 changes: 48 additions & 17 deletions workbench_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def create(self, config, field_definitions, entity, row, field_name):
config, field_definitions, field_name, subvalues
)
subvalues = self.dedupe_values(subvalues)

cardinality = int(field_definitions[field_name].get("cardinality", -1))
if -1 < cardinality < len(subvalues):
log_field_cardinality_violation(field_name, id_field, str(cardinality))
Expand All @@ -84,17 +83,25 @@ def create(self, config, field_definitions, entity, row, field_name):
"formatted_text" in field_definitions[field_name]
and field_definitions[field_name]["formatted_text"] is True
):
field_values.append({"value": subvalue, "format": text_format})
json_str = self.get_json(subvalue)
if json_str is False:
field_values.append({"value": subvalue, "format": text_format})
else:
field_values.append(json_str)
else:
if field_definitions[field_name][
"field_type"
] == "integer" and value_is_numeric(subvalue):
subvalue = int(subvalue)
if field_definitions[field_name][
"field_type"
] == "float" and value_is_numeric(subvalue, allow_decimals=True):
subvalue = float(subvalue)
field_values.append({"value": subvalue})
json_str = self.get_json(subvalue)
if json_str is False:
if field_definitions[field_name][
"field_type"
] == "integer" and value_is_numeric(subvalue):
subvalue = int(subvalue)
if field_definitions[field_name][
"field_type"
] == "float" and value_is_numeric(subvalue, allow_decimals=True):
subvalue = float(subvalue)
field_values.append({"value": subvalue})
else:
field_values.append(json_str)
field_values = self.dedupe_values(field_values)
entity[field_name] = field_values

Expand Down Expand Up @@ -162,13 +169,17 @@ def update(
field_definitions[field_name],
subvalue,
)
json_str = self.get_json(subvalue)
if (
"formatted_text" in field_definitions[field_name]
and field_definitions[field_name]["formatted_text"] is True
):
entity[field_name].append(
{"value": subvalue, "format": text_format}
)
if json_str is False:
entity[field_name].append(
{"value": subvalue, "format": text_format}
)
else:
entity[field_name].append(json_str)
else:
if field_definitions[field_name][
"field_type"
Expand All @@ -178,7 +189,10 @@ def update(
"field_type"
] == "float" and value_is_numeric(subvalue, allow_decimals=True):
subvalue = float(subvalue)
entity[field_name].append({"value": subvalue})
if json_str is False:
entity[field_name].append({"value": subvalue})
else:
entity[field_name].append(json_str)
entity[field_name] = self.dedupe_values(entity[field_name])
if -1 < cardinality < len(entity[field_name]):
log_field_cardinality_violation(
Expand All @@ -204,11 +218,15 @@ def update(
field_definitions[field_name],
subvalue,
)
json_str = self.get_json(subvalue)
if (
"formatted_text" in field_definitions[field_name]
and field_definitions[field_name]["formatted_text"] is True
):
field_values.append({"value": subvalue, "format": text_format})
if json_str is False:
field_values.append({"value": subvalue, "format": text_format})
else:
field_values.append(json_str)
else:
if field_definitions[field_name][
"field_type"
Expand All @@ -218,7 +236,10 @@ def update(
"field_type"
] == "float" and value_is_numeric(subvalue, allow_decimals=True):
subvalue = float(subvalue)
field_values.append({"value": subvalue})
if json_str is False:
field_values.append({"value": subvalue})
else:
field_values.append(json_str)
field_values = self.dedupe_values(field_values)
entity[field_name] = field_values

Expand Down Expand Up @@ -366,6 +387,16 @@ def serialize(self, config, field_definitions, field_name, field_data):
else:
return subvalues[0]

def get_json(self, json_str):
if value_is_numeric(json_str, allow_decimals=True):
return False

try:
j = json.loads(json_str)
return j
except ValueError:
return False


class GeolocationField:
"""Functions for handling fields with 'geolocation' Drupal field data type.
Expand Down
Loading