Skip to content

Commit

Permalink
add ! name and description to DeduplicationSet
Browse files Browse the repository at this point in the history
del ! error from DeduplicationSet
chg ! admin panel
  • Loading branch information
vitali-yanushchyk-valor committed Sep 20, 2024
1 parent abf647b commit 9496b7f
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 35 deletions.
13 changes: 0 additions & 13 deletions src/hope_dedup_engine/apps/api/admin.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/hope_dedup_engine/apps/api/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .deduplicationset import DeduplicationSetAdmin # noqa
from .duplicate import DuplicateAdmin # noqa
from .hdetoken import HDETokenAdmin # noqa
from .image import ImageAdmin # noqa
45 changes: 45 additions & 0 deletions src/hope_dedup_engine/apps/api/admin/deduplicationset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.contrib.admin import ModelAdmin, register

from adminfilters.dates import DateRangeFilter
from adminfilters.filters import ChoicesFieldComboFilter, DjangoLookupFilter
from adminfilters.mixin import AdminFiltersMixin

from hope_dedup_engine.apps.api.models import DeduplicationSet


@register(DeduplicationSet)
class DeduplicationSetAdmin(AdminFiltersMixin, ModelAdmin):
list_display = (
"id",
"short_name",
"reference_pk",
"state",
"created_at",
"updated_at",
"deleted",
)
readonly_fields = (
"id",
"state",
"external_system",
"created_at",
"created_by",
"updated_at",
"updated_by",
"deleted",
)
search_fields = ("name",)
list_filter = (
("state", ChoicesFieldComboFilter),
("created_at", DateRangeFilter),
("updated_at", DateRangeFilter),
DjangoLookupFilter,
)

def short_name(self, obj):
if obj.name:
return (obj.name[:50] + "...") if len(obj.name) > 50 else obj.name
return "-"

def has_add_permission(self, request):
return False
35 changes: 35 additions & 0 deletions src/hope_dedup_engine/apps/api/admin/duplicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.contrib.admin import ModelAdmin, register

from adminfilters.filters import (
DjangoLookupFilter,
NumberFilter,
RelatedFieldComboFilter,
)
from adminfilters.mixin import AdminFiltersMixin

from hope_dedup_engine.apps.api.models import Duplicate


@register(Duplicate)
class DuplicateAdmin(AdminFiltersMixin, ModelAdmin):
list_display = (
"id",
"deduplication_set",
"score",
"first_reference_pk",
"second_reference_pk",
)
list_filter = (
("deduplication_set", RelatedFieldComboFilter),
("score", NumberFilter),
DjangoLookupFilter,
)

def has_add_permission(self, request):
return False

def has_delete_permission(self, request, obj=None):
return False

def has_change_permission(self, request, obj=None):
return False
8 changes: 8 additions & 0 deletions src/hope_dedup_engine/apps/api/admin/hdetoken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib.admin import ModelAdmin, register

from hope_dedup_engine.apps.api.models import HDEToken


@register(HDEToken)
class HDETokenAdmin(ModelAdmin):
pass
32 changes: 32 additions & 0 deletions src/hope_dedup_engine/apps/api/admin/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.contrib.admin import ModelAdmin, register

from adminfilters.dates import DateRangeFilter
from adminfilters.filters import DjangoLookupFilter, RelatedFieldComboFilter
from adminfilters.mixin import AdminFiltersMixin

from hope_dedup_engine.apps.api.models import Image


@register(Image)
class ImageAdmin(AdminFiltersMixin, ModelAdmin):
list_display = (
"id",
"filename",
"deduplication_set",
"created_at",
)

list_filter = (
("deduplication_set", RelatedFieldComboFilter),
("created_at", DateRangeFilter),
DjangoLookupFilter,
)

def has_add_permission(self, request):
return False

def has_delete_permission(self, request, obj=None):
return False

def has_change_permission(self, request, obj=None):
return False
3 changes: 1 addition & 2 deletions src/hope_dedup_engine/apps/api/deduplication/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ def find_duplicates(deduplication_set_id: str, serialized_lock: str) -> None:
if lock_enabled:
lock.release()

except Exception as e:
except Exception:
deduplication_set.state = DeduplicationSet.State.ERROR
deduplication_set.error = str(e)
deduplication_set.save()
raise
finally:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.0.7 on 2024-09-20 08:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("api", "0002_remove_duplicate_first_filename_and_more"),
]

operations = [
migrations.RemoveField(
model_name="deduplicationset",
name="error",
),
migrations.AddField(
model_name="deduplicationset",
name="description",
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name="deduplicationset",
name="name",
field=models.CharField(
blank=True, db_index=True, max_length=128, null=True, unique=True
),
),
]

This file was deleted.

24 changes: 21 additions & 3 deletions src/hope_dedup_engine/apps/api/models/deduplication.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from typing import Any, override
from typing import Any, Final, override
from uuid import uuid4

from django.conf import settings
from django.db import models

from hope_dedup_engine.apps.security.models import ExternalSystem

REFERENCE_PK_LENGTH = 100
REFERENCE_PK_LENGTH: Final[int] = 100
DELIMITER: Final[str] = "|"
EMPTY_PLACEHOLDER: Final[str] = "-"
MAX_STR_DISPLAY_LENGTH: Final[int] = 25


class DeduplicationSet(models.Model):
Expand All @@ -24,14 +27,17 @@ class State(models.IntegerChoices):
ERROR = 3, "Error" # Error occurred

id = models.UUIDField(primary_key=True, default=uuid4)
name = models.CharField(
max_length=128, unique=True, null=True, blank=True, db_index=True
)
description = models.TextField(null=True, blank=True)
reference_pk = models.CharField(max_length=REFERENCE_PK_LENGTH) # source_id
state = models.IntegerField(
choices=State.choices,
default=State.CLEAN,
)
deleted = models.BooleanField(null=False, blank=False, default=False)
external_system = models.ForeignKey(ExternalSystem, on_delete=models.CASCADE)
error = models.CharField(max_length=255, null=True, blank=True)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
Expand All @@ -50,6 +56,18 @@ class State(models.IntegerChoices):
updated_at = models.DateTimeField(auto_now=True)
notification_url = models.CharField(max_length=255, null=True, blank=True)

def __str__(self) -> str:
def shorten(text: str, trim: int = MAX_STR_DISPLAY_LENGTH) -> str:
return (
(text[:trim] + "...")
if text and len(text) > trim
else text or EMPTY_PLACEHOLDER
)

if not self.name and not self.description:
return f"ID: {self.pk}"
return f"{shorten(self.name)} {DELIMITER} {shorten(self.description)}"


class Image(models.Model):
"""
Expand Down

0 comments on commit 9496b7f

Please sign in to comment.