do create_multiple correctly
This commit is contained in:
parent
62d1773bb0
commit
44671a12dc
10 changed files with 86 additions and 53 deletions
|
@ -655,10 +655,19 @@ class ChangedObjectCollection(BaseSchema):
|
|||
# construct new situation
|
||||
new_situation = situation.model_copy(deep=True)
|
||||
|
||||
if isinstance(new_operation, CreateObjectOperation) and new_situation.operations:
|
||||
model = apps.get_model('mapdata', new_operation.obj.model)
|
||||
for parent in model._meta.get_parent_list():
|
||||
if parent._meta.concrete_model is not model._meta.concrete_model:
|
||||
is_multi_inheritance = True
|
||||
break
|
||||
else:
|
||||
is_multi_inheritance = False
|
||||
|
||||
if (isinstance(new_operation, CreateObjectOperation)
|
||||
and new_situation.operations and not is_multi_inheritance):
|
||||
last_operation = new_situation.operations[-1]
|
||||
if (isinstance(last_operation, CreateObjectOperation) and
|
||||
last_operation.obj.model == new_operation.obj.model):
|
||||
if (isinstance(last_operation, CreateObjectOperation)
|
||||
and last_operation.obj.model == new_operation.obj.model):
|
||||
new_situation.operations[-1] = CreateMultipleObjectsOperation(
|
||||
objects=[last_operation, new_operation],
|
||||
)
|
||||
|
|
|
@ -87,33 +87,19 @@ class CreateObjectOperation(BaseOperation):
|
|||
fields: FieldValuesDict
|
||||
|
||||
def get_data(self):
|
||||
model = apps.get_model('mapdata', self.obj.model)
|
||||
data = []
|
||||
if issubclass(model, LocationSlug):
|
||||
data.append({
|
||||
"model": f"mapdata.locationslug",
|
||||
"pk": self.obj.id,
|
||||
"fields": {
|
||||
"slug": self.fields.get("slug", None)
|
||||
},
|
||||
})
|
||||
values = {key: val for key, val in self.fields.items() if key != "slug"}
|
||||
else:
|
||||
values = self.fields
|
||||
data.append({
|
||||
return [{
|
||||
"model": f"mapdata.{self.obj.model}",
|
||||
"pk": self.obj.id,
|
||||
"fields": values,
|
||||
})
|
||||
return data
|
||||
"fields": self.fields,
|
||||
}]
|
||||
|
||||
def apply_create(self) -> dict[ObjectReference, Model]:
|
||||
data = self.get_data()
|
||||
instances = list(serializers.deserialize("json", json.dumps(data)))
|
||||
for instance in instances:
|
||||
instances = [item.object for item in serializers.deserialize("json", json.dumps(data))]
|
||||
for instance in instances[-1:]:
|
||||
# .object. to make sure our own .save() function is called!
|
||||
instance.object.save()
|
||||
return {self.obj: instances[-1].object}
|
||||
instance.save()
|
||||
return {self.obj: instances[-1]}
|
||||
|
||||
|
||||
class CreateMultipleObjectsOperation(BaseSchema):
|
||||
|
@ -126,11 +112,12 @@ class CreateMultipleObjectsOperation(BaseSchema):
|
|||
for obj in self.objects:
|
||||
data.extend(obj.get_data())
|
||||
indexes[obj.obj] = len(data)-1
|
||||
instances = list(serializers.deserialize("json", json.dumps(data)))
|
||||
# todo: actually do a create_multiple!, let's not forget about register_changed_geometries etc
|
||||
for instance in instances:
|
||||
# .object. to make sure our own .save() function is called!
|
||||
instance.object.save()
|
||||
instances = [item.object for item in serializers.deserialize("json", json.dumps(data))]
|
||||
if hasattr(instances[-1], "pre_save_changed_geometries"):
|
||||
for instance in instances:
|
||||
instance.pre_save_changed_geometries()
|
||||
model = apps.get_model('mapdata', self.objects[0].obj.model)
|
||||
model.objects.bulk_create(instances)
|
||||
return {ref: instances[i] for ref, i in indexes.items()}
|
||||
|
||||
|
||||
|
|
|
@ -81,6 +81,9 @@
|
|||
{% if changeset.description %}
|
||||
<p>{{ changeset.description }}</p>
|
||||
{% endif %}
|
||||
{% if changed_objects %}
|
||||
<p><em>{% blocktranslate count counter=operations|length %}({{ counter }} operation in total){% plural %}({{ counter }} operations in total){% endblocktranslate %}</em></p>
|
||||
{% endif %}
|
||||
{% for obj in changed_objects %}
|
||||
<table class="table table-condensed table-h-bordered change-group">
|
||||
<thead>
|
||||
|
|
|
@ -233,6 +233,7 @@ def changeset_detail(request, pk):
|
|||
'can_unreject': changeset.can_unreject(request),
|
||||
'can_apply': changeset.can_apply(request),
|
||||
'active': active,
|
||||
'operations': changeset.as_operations,
|
||||
}
|
||||
|
||||
cache_key = '%s:%s:%s:view_data' % (changeset.cache_key_by_changes,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue