Skip to content

Add corpus categories

https://redmine.teklia.com/issues/11416

A new arkindex.documents.models.CorpusCategory model should be added, with the following fields:

  • id, an AutoField as the primary key;
  • created, a DateTimeField with auto_now_add set;
  • updated, a DateTimeField with auto_now set;
  • slug, a SlugField with db_index=False to disable the implicit and unnecessary database index that Django creates, and with a MinLengthValidator to ensure you can't use an empty slug;
  • display_name, a CharField with a MinLengthValidator;
  • color, a CharField with a regex validator to ensure it is always a 6 character hex code, just like in EntityType.color and ElementType.color, and a help_text to explain to instance administrators that this is a 6 character code (it will be shown automatically in the Django admin).

Two UniqueConstraint should be declared on this model to require unique values for both slugs and display names. Do not use unique=True on the fields themselves, as explained in the best practices.

A CorpusCategory.__str__ method should return the display_name.

A new Corpus.category foreign key assigns one CorpusCategory to each corpus. It is nullable for now.

The database migration should:

  1. Create CorpusCategory
  2. Create Corpus.category
  3. In a RunPython operation:
    1. Create a default CorpusCategory with the slug set to default, the display name to Default and the color to FF0000
    2. In a single update() call, assign this category to all existing corpora.

CorpusCategory should be added to the Django admin. The list shows slugs and display names, and orders by slug by default. Creating a CorpusCategory with a slug or a display name that already exist should result in a clean error message, not a crash.

Corpus.category should be editable in the Django admin. The corpora list must include the category column, which should show the category's display_name. Make sure there isn't one query per corpus to retrieve the category, which would make the admin page very slow; you might need to add a select_related manually to handle this. When creating or updating a single corpus, it should be possible to set a category.