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

Add/Edit handling of 'one-to-one' relationship cases in resourceApi #5509

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const ResourceBase = Backbone.Model.extend({
{ createdBy: 'clone' }
);

newResource.specifyTable = this.specifyTable;
newResource.needsSaved = self.needsSaved;

await Promise.all(
Expand Down Expand Up @@ -215,6 +216,13 @@ export const ResourceBase = Backbone.Model.extend({
);
break;
}
case 'one-to-one': {
newResource.set(
fieldName,
await related?.clone(cloneAll, isBulkCarry)
);
break;
}
default: {
throw new Error('unhandled relationship type');
}
Expand Down Expand Up @@ -551,7 +559,18 @@ export const ResourceBase = Backbone.Model.extend({
* Needed for taxonTreeDef on discipline because field.isVirtual equals false
*/
case 'one-to-one': {
return value;
if (!value) {
if (field.isDependent()) this.storeDependent(field, null);
else this.storeIndependent(field, null);
return value;
}

const oneToOne = maybeMakeResource(value, relatedTable);
if (field.isDependent()) this.storeDependent(field, oneToOne);
else this.storeIndependent(field, oneToOne);
this.trigger(`change:${fieldName}`, this);
this.trigger('change', this);
return oneToOne.url();
}
}
if (!field.isVirtual)
Expand Down
29 changes: 15 additions & 14 deletions specifyweb/frontend/js_src/lib/components/Forms/Save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,25 +269,26 @@ export function SaveButton<SCHEMA extends AnySchema = AnySchema>({
carryForwardAmount > 1
? async (): Promise<RA<SpecifyResource<SCHEMA>>> => {
const formatter =
tables.CollectionObject.strictGetLiteralField(
'catalogNumber'
).getUiFormatter()!;
const wildCard = formatter.valueOrWild();

const clonePromises = Array.from(
{ length: carryForwardAmount },
async () => {
const clonedResource = await resource.clone(
false,
true
);
tables.CollectionObject.strictGetLiteralField(
'catalogNumber'
).getUiFormatter()!;
const wildCard =formatter === undefined ? null : formatter.valueOrWild();
const clonePromises = Array.from(
{ length: carryForwardAmount },
async () => {
const clonedResource = await resource.clone(
false,
true
);
if (wildCard !== null) {
clonedResource.set(
'catalogNumber',
wildCard as never
);
return clonedResource;
}
);
return clonedResource;
}
);

const clones = await Promise.all(clonePromises);

Expand Down
14 changes: 13 additions & 1 deletion specifyweb/specify/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ def create_obj(collection, agent, model, data: Dict[str, Any], parent_obj=None):
model = get_model_or_404(model)
data = cleanData(model, data, agent)
obj = model()
_handle_special_create_priors(obj, data, parent_obj)
handle_fk_fields(collection, agent, obj, data)
set_fields_from_data(obj, data)
set_field_if_exists(obj, 'createdbyagent', agent)
Expand Down Expand Up @@ -1084,4 +1085,15 @@ def _handle_special_save_priors(obj):

def _handle_special_update_priors(obj, data):
data = modify_update_of_interaction_sibling_preps(obj, data)
pass

def _save_parent_co_prior(obj, data, parent_obj):
if (
obj._meta.model_name == "collectionobjectgroupjoin"
and parent_obj._meta.model_name == "collectionobject"
and data["childco"] == "/api/specify/collectionobject/"
):
parent_obj.save() # temporary fix for saving co cojo.childco
data['childco'] = f"{data['childco']}{parent_obj.id}/"

def _handle_special_create_priors(obj, data, parent_obj):
_save_parent_co_prior(obj, data, parent_obj)
Loading