Skip to content

Commit

Permalink
Improve admin and store a smaller JSON diff
Browse files Browse the repository at this point in the history
  • Loading branch information
kemar committed Dec 30, 2024
1 parent 17bd3cb commit 56a450a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
37 changes: 27 additions & 10 deletions iaso/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def get_queryset(self, request):
@admin_attr_decorator
class SourceVersionAdmin(admin.ModelAdmin):
readonly_fields = ("created_at",)
list_display = ["__str__", "data_source", "number", "created_at", "updated_at"]
list_display = ["id", "data_source", "number", "created_at", "updated_at"]
list_filter = ["data_source", "created_at", "updated_at"]
search_fields = ["data_source__name", "number", "description"]
autocomplete_fields = ["data_source"]
Expand Down Expand Up @@ -826,7 +826,7 @@ class EntityDuplicateAnalyzisAdmin(admin.ModelAdmin):
class OrgUnitChangeRequestAdmin(admin.ModelAdmin):
list_display = ("pk", "org_unit", "created_at", "status")
list_display_links = ("pk", "org_unit")
list_filter = ("status", "kind")
list_filter = ("status", "kind", "data_source_synchronization")
readonly_fields = (
"uuid",
"created_at",
Expand All @@ -851,6 +851,7 @@ class OrgUnitChangeRequestAdmin(admin.ModelAdmin):
"new_reference_instances",
"payment",
"potential_payment",
"data_source_synchronization",
)
fieldsets = (
(
Expand Down Expand Up @@ -900,6 +901,7 @@ class OrgUnitChangeRequestAdmin(admin.ModelAdmin):
"updated_at",
"updated_by",
"rejection_comment",
"data_source_synchronization",
)
},
),
Expand All @@ -921,7 +923,7 @@ class OrgUnitChangeRequestAdmin(admin.ModelAdmin):
)

def get_queryset(self, request):
return super().get_queryset(request).select_related("org_unit__org_unit_type")
return super().get_queryset(request).select_related("org_unit__org_unit_type", "data_source_synchronization")


@admin.register(Config)
Expand Down Expand Up @@ -1081,14 +1083,29 @@ class Media:

@admin.register(DataSourceVersionsSynchronization)
class DataSourceVersionsSynchronizationAdmin(admin.ModelAdmin):
list_display = (
"pk",
"name",
"account",
"created_by",
"count_create",
"count_update",
)
list_display_links = ("pk", "name")
autocomplete_fields = ("account", "created_by", "source_version_to_update", "source_version_to_compare_with")
readonly_fields = ("created_at", "updated_at", "sync_task")
formfield_overrides = {
ArrayField: {
"form_class": ArrayFieldMultipleChoiceField,
"widget": forms.CheckboxSelectMultiple,
}
}
readonly_fields = ("json_diff", "count_create", "count_update", "created_at", "updated_at", "sync_task")

def get_queryset(self, request):
return (
super()
.get_queryset(request)
.select_related(
"source_version_to_update__data_source",
"source_version_to_compare_with__data_source",
"account",
"created_by",
)
)


admin.site.register(AccountFeatureFlag)
Expand Down
17 changes: 9 additions & 8 deletions iaso/models/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Meta:
]

def __str__(self):
return "%s %d" % (self.data_source, self.number)
return f"#{self.pk}{self.number} - Data source: {self.data_source}"

def as_dict(self):
return {
Expand Down Expand Up @@ -294,21 +294,22 @@ def create_json_diff(
"show_deleted_org_units": show_deleted_org_units,
"field_names": field_names,
}
diffs, fields = Differ(logger_to_use or logger).diff(**differ_params)
diffs, _ = Differ(logger_to_use or logger).diff(**differ_params)
diffs = [diff for diff in diffs if diff.status != "same"]

coun_status = {
count_status = {
"new": 0,
"modified": 0,
}
for diff in diffs:
if diff.status in coun_status:
coun_status[diff.status] += 1
if diff.status in count_status:
count_status[diff.status] += 1

self.count_create = coun_status["new"]
self.count_update = coun_status["modified"]
self.count_create = count_status["new"]
self.count_update = count_status["modified"]
self.json_diff = diffs_to_json(diffs)
self.diff_config = str(differ_params)
self.save(update_fields=["json_diff", "diff_config"])
self.save()

def synchronize_source_versions(self):
# Prevent a circular import.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ def test_create_json_diff(self):
ignore_groups=True,
field_names=["name"],
)
data_source_sync.refresh_from_db()

json_diff = json.loads(data_source_sync.json_diff)

comparisons = json_diff[0]["comparisons"]
Expand Down

0 comments on commit 56a450a

Please sign in to comment.