|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from django.db import models |
| 4 | + |
| 5 | +from dandiapi.api.models.dandiset import Dandiset |
| 6 | + |
| 7 | + |
| 8 | +class DataciteEventType(models.TextChoices): |
| 9 | + CREATE_DOI = 'CREATE_DOI', 'Create DOI' |
| 10 | + UPDATE_DOI = 'UPDATE_DOI', 'Update DOI' |
| 11 | + HIDE_DOI = 'HIDE_DOI', 'Hide DOI' |
| 12 | + DELETE_DOI = 'DELETE_DOI', 'Delete DOI' |
| 13 | + |
| 14 | + |
| 15 | +class DataciteEvent(models.Model): |
| 16 | + timestamp = models.DateTimeField() |
| 17 | + event_type = models.CharField( |
| 18 | + max_length=max(len(choice[0]) for choice in DataciteEventType.choices), |
| 19 | + choices=DataciteEventType.choices, |
| 20 | + ) |
| 21 | + |
| 22 | + # Since the deletion of a dandiset will set the foreign key relation to null, |
| 23 | + # also store the dandiset ID in a separate field |
| 24 | + dandiset_identifier = models.PositiveIntegerField() |
| 25 | + dandiset = models.ForeignKey( |
| 26 | + Dandiset, |
| 27 | + related_name='datacite_events', |
| 28 | + on_delete=models.SET_NULL, |
| 29 | + null=True, |
| 30 | + ) |
| 31 | + |
| 32 | + class Meta: |
| 33 | + ordering = ['timestamp'] |
| 34 | + constraints = [ |
| 35 | + models.CheckConstraint( |
| 36 | + condition=models.Q(dandiset_id=models.F('dandiset_identifier')), |
| 37 | + name='dandiset_fields_equal', |
| 38 | + ) |
| 39 | + ] |
| 40 | + |
| 41 | + def __str__(self) -> str: |
| 42 | + return f'Datacite Event: {self.event_type} @ {self.timestamp}' |
0 commit comments