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

1155 add a column to track the reindexing curator #1180

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.9 on 2025-01-09 05:07

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("sde_collections", "0075_alter_collection_reindexing_status_and_more"),
]

operations = [
migrations.AddField(
model_name="collection",
name="reindexing_curated_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="reindexing_curated_by",
to=settings.AUTH_USER_MODEL,
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.9 on 2025-01-09 05:12

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("sde_collections", "0076_collection_reindexing_curated_by"),
]

operations = [
migrations.AlterField(
model_name="collection",
name="reindexing_curated_by",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="reindexing_curated_by",
to=settings.AUTH_USER_MODEL,
),
),
]
27 changes: 27 additions & 0 deletions sde_collections/migrations/0078_reindexinghistory_old_curator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.9 on 2025-01-29 05:28

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("sde_collections", "0077_alter_collection_reindexing_curated_by"),
]

operations = [
migrations.AddField(
model_name="reindexinghistory",
name="old_curator",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="old_curator",
to=settings.AUTH_USER_MODEL,
),
),
]
16 changes: 14 additions & 2 deletions sde_collections/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class Collection(models.Model):
tracker = FieldTracker(fields=["workflow_status", "reindexing_status"])

curated_by = models.ForeignKey(User, on_delete=models.DO_NOTHING, null=True, blank=True)
reindexing_curated_by = models.ForeignKey(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write tests as well.

User, on_delete=models.DO_NOTHING, null=True, blank=True, default=None, related_name="reindexing_curated_by"
)
curation_started = models.DateTimeField("Curation Started", null=True, blank=True)

class Meta:
Expand Down Expand Up @@ -550,6 +553,7 @@ def _create_from_json(cls, json_results):
for collection in json_results:
print("Creating collection: ", collection["name"])
collection.pop("curated_by")
collection.pop("reindexing_curated_by")
cls.objects.create(**collection)

@classmethod
Expand Down Expand Up @@ -662,6 +666,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.old_workflow_status = self.workflow_status
self.old_reindexing_status = self.reindexing_status
self.old_reindexing_curated_by = self.reindexing_curated_by


class RequiredUrls(models.Model):
Expand Down Expand Up @@ -742,12 +747,16 @@ def log_workflow_history(sender, instance, created, **kwargs):
old_status=instance.old_workflow_status,
)

if instance.reindexing_status != instance.old_reindexing_status:
if (
instance.reindexing_status != instance.old_reindexing_status
or instance.reindexing_curated_by != instance.old_reindexing_curated_by
):
ReindexingHistory.objects.create(
collection=instance,
reindexing_status=instance.reindexing_status,
curated_by=instance.curated_by,
curated_by=instance.reindexing_curated_by,
old_status=instance.old_reindexing_status,
old_curator=instance.old_reindexing_curated_by,
)


Expand All @@ -759,6 +768,9 @@ class ReindexingHistory(models.Model):
)
old_status = models.IntegerField(choices=ReindexingStatusChoices.choices, null=True)
curated_by = models.ForeignKey(User, on_delete=models.DO_NOTHING, null=True, blank=True)
old_curator = models.ForeignKey(
User, on_delete=models.DO_NOTHING, null=True, blank=True, related_name="old_curator"
)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
Expand Down
1 change: 1 addition & 0 deletions sde_collections/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Meta:
"workflow_status_display",
"reindexing_status_display",
"curated_by",
"reindexing_curated_by",
"division",
"document_type",
"name",
Expand Down
85 changes: 85 additions & 0 deletions sde_collections/tests/test_reindexing_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# docker-compose -f local.yml run --rm django pytest -s sde_collections/tests/test_reindexing_history.py

import pytest
from django.contrib.auth import get_user_model

from sde_collections.models.collection import Collection, ReindexingHistory
from sde_collections.models.collection_choice_fields import ReindexingStatusChoices
from sde_collections.tests.factories import CollectionFactory, UserFactory

User = get_user_model()


@pytest.mark.django_db
class TestReindexingHistory:
"""Test suite for ReindexingHistory functionality"""

def setup_method(self):
"""Setup test data"""
self.collection = CollectionFactory()
self.user1 = UserFactory()
self.user2 = UserFactory()

def test_reindexing_history_status_change(self):
"""Should create history entry when reindexing status changes"""
self.collection.reindexing_status = ReindexingStatusChoices.REINDEXING_NEEDED_ON_DEV
self.collection.save()

history = ReindexingHistory.objects.filter(collection=self.collection)
assert history.count() == 1
assert history.first().reindexing_status == ReindexingStatusChoices.REINDEXING_NEEDED_ON_DEV
assert history.first().old_status == ReindexingStatusChoices.REINDEXING_NOT_NEEDED

def test_reindexing_history_curator_change(self):
"""Should create history entry when curator changes"""
self.collection.reindexing_curated_by = self.user1
self.collection.save()

history = ReindexingHistory.objects.filter(collection=self.collection)
assert history.count() == 1
assert history.first().curated_by == self.user1
assert history.first().old_curator is None

def test_reindexing_history_both_changes(self):
"""Should create history entry when both status and curator change"""
self.collection.reindexing_status = ReindexingStatusChoices.REINDEXING_NEEDED_ON_DEV
self.collection.reindexing_curated_by = self.user1
self.collection.save()

history = ReindexingHistory.objects.filter(collection=self.collection)
assert history.count() == 1
entry = history.first()
assert entry.reindexing_status == ReindexingStatusChoices.REINDEXING_NEEDED_ON_DEV
assert entry.old_status == ReindexingStatusChoices.REINDEXING_NOT_NEEDED
assert entry.curated_by == self.user1
assert entry.old_curator is None

def test_reindexing_history_multiple_changes(self):
"""Should create multiple history entries for sequential changes"""
# First change
self.collection.reindexing_status = ReindexingStatusChoices.REINDEXING_NEEDED_ON_DEV
self.collection.reindexing_curated_by = self.user1
self.collection.save()

# Re-fetch the object
self.collection = Collection.objects.get(id=self.collection.id)

# Second change
self.collection.reindexing_status = ReindexingStatusChoices.REINDEXING_FINISHED_ON_DEV
self.collection.reindexing_curated_by = self.user2
self.collection.save()

history = ReindexingHistory.objects.filter(collection=self.collection).order_by("created_at")
assert history.count() == 2

first_entry = history[0]
assert first_entry.reindexing_status == ReindexingStatusChoices.REINDEXING_NEEDED_ON_DEV
assert first_entry.old_status == ReindexingStatusChoices.REINDEXING_NOT_NEEDED
assert first_entry.curated_by == self.user1
assert first_entry.old_curator is None

second_entry = history[1]
assert second_entry.reindexing_status == ReindexingStatusChoices.REINDEXING_FINISHED_ON_DEV
assert second_entry.old_status == ReindexingStatusChoices.REINDEXING_NEEDED_ON_DEV
assert second_entry.curated_by == self.user2
assert second_entry.old_curator == self.user1
Loading
Loading