Another stab at error-tolerant versioning

This commit is contained in:
2023-06-28 11:53:15 +01:00
parent 53b09e47b8
commit 29db3b5a0c

View File

@@ -104,6 +104,9 @@ class FieldComparison:
class ModelComparison: class ModelComparison:
def __init__(self, old=None, new=None, version=None, follow=False, excluded_keys=['date_joined']): def __init__(self, old=None, new=None, version=None, follow=False, excluded_keys=['date_joined']):
if new is None and old is None:
logging.warning("Both new and old are none, this won't work")
pass
# recieves two objects of the same model, and compares them. Returns an array of FieldCompare objects # recieves two objects of the same model, and compares them. Returns an array of FieldCompare objects
try: try:
self.fields = old._meta.get_fields() self.fields = old._meta.get_fields()
@@ -152,19 +155,20 @@ class ModelComparison:
from training.models import TrainingLevelQualification, TrainingItemQualification from training.models import TrainingLevelQualification, TrainingItemQualification
if self.follow and self.version.object is not None: if self.follow and self.version.object is not None:
item_type = ContentType.objects.get_for_model(self.version.object) item_type = ContentType.objects.get_for_model(self.version.object)
old_item_versions = self.version.parent.revision.version_set.exclude(content_type=item_type).exclude(content_type=ContentType.objects.get_for_model(TrainingItemQualification)) \
.exclude(content_type=ContentType.objects.get_for_model(TrainingLevelQualification))
new_item_versions = self.version.revision.version_set.exclude(content_type=item_type).exclude(content_type=ContentType.objects.get_for_model(EventAuthorisation)) new_item_versions = self.version.revision.version_set.exclude(content_type=item_type).exclude(content_type=ContentType.objects.get_for_model(EventAuthorisation))
comparisonParams = {'excluded_keys': ['id', 'event', 'order', 'checklist', 'level', '_order', 'date_joined']} comparisonParams = {'excluded_keys': ['id', 'event', 'order', 'checklist', 'level', '_order', 'date_joined']}
# Build some dicts of what we have # Build some dicts of what we have
item_dict = {} # build a list of items, key is the item_pk item_dict = {} # build a list of items, key is the item_pk
for version in old_item_versions: # put all the old versions in a list if self.version.parent is not None:
if version is None or version._object_version is None: old_item_versions = self.version.parent.revision.version_set.exclude(content_type=item_type).exclude(content_type=ContentType.objects.get_for_model(TrainingItemQualification)) \
logging.warning(f"Something was null when it really shouldn't be! {old_item_versions}") .exclude(content_type=ContentType.objects.get_for_model(TrainingLevelQualification))
compare = ModelComparison(old=version._object_version.object, **comparisonParams) for version in old_item_versions: # put all the old versions in a list
item_dict[version.object_id] = compare if version is None or version._object_version is None:
logging.warning(f"Something was null when it really shouldn't be! {old_item_versions}")
compare = ModelComparison(old=version._object_version.object, **comparisonParams)
item_dict[version.object_id] = compare
for version in new_item_versions: # go through the new versions for version in new_item_versions: # go through the new versions
try: try:
@@ -227,7 +231,7 @@ class RIGSVersion(Version):
try: try:
previousVersion = versions.filter(revision_id__lt=self.revision_id).latest('revision__date_created') previousVersion = versions.filter(revision_id__lt=self.revision_id).latest('revision__date_created')
except ObjectDoesNotExist: except ObjectDoesNotExist:
return False return None
return previousVersion return previousVersion
@@ -236,7 +240,7 @@ class RIGSVersion(Version):
return ModelComparison( return ModelComparison(
version=self, version=self,
new=self._object_version.object, new=self._object_version.object,
old=self.parent._object_version.object if self.parent else None, old=self.parent._object_version.object,
follow=True follow=True
) )