fix now card width
This commit is contained in:
+118
-1
@@ -265,7 +265,7 @@ class PendingEvent(ClassifiedAudio):
|
||||
verbose_name_plural = _("Pending")
|
||||
|
||||
@transaction.atomic
|
||||
def approve(self):
|
||||
def approve(self, actor=None):
|
||||
event = Event.objects.create(
|
||||
submitted_at=self.created_at,
|
||||
audio_file=self.audio_file.name,
|
||||
@@ -289,6 +289,9 @@ class PendingEvent(ClassifiedAudio):
|
||||
direction_accuracy_degrees=self.direction_accuracy_degrees,
|
||||
direction_source=self.direction_source,
|
||||
)
|
||||
from .field_notes import associate
|
||||
for context in self.note_contexts.select_for_update():
|
||||
associate(context, event=event, actor=actor, reason="approved")
|
||||
self.delete()
|
||||
return event
|
||||
|
||||
@@ -334,3 +337,117 @@ class SensorSubmission(models.Model):
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=["sensor", "event_id"], name="unique_sensor_event_id")]
|
||||
|
||||
|
||||
class FieldNoteContext(models.Model):
|
||||
"""An author's note association, including drafts before a report exists.
|
||||
|
||||
This stores no detection/incident data; Event/PendingEvent remain canonical.
|
||||
"""
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
|
||||
event = models.ForeignKey(Event, null=True, blank=True, on_delete=models.SET_NULL, related_name="note_contexts")
|
||||
pending_event = models.ForeignKey(PendingEvent, null=True, blank=True, on_delete=models.SET_NULL, related_name="note_contexts")
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.CheckConstraint(condition=~(models.Q(event__isnull=False) & models.Q(pending_event__isnull=False)), name="note_context_one_target")]
|
||||
|
||||
|
||||
class FieldNoteQuerySet(models.QuerySet):
|
||||
def visible_to(self, user):
|
||||
if not user.is_authenticated or not user.is_active:
|
||||
return self.none()
|
||||
if user.is_active and user.has_perm("fireworks_detect.view_all_fieldnotes"):
|
||||
return self
|
||||
return self.filter(author=user)
|
||||
|
||||
def reportable_to(self, user):
|
||||
if not user.is_authenticated or not user.is_active:
|
||||
return self.none()
|
||||
notes = self.filter(visibility="reportable")
|
||||
if user.is_active and user.has_perm("fireworks_detect.export_reportable_fieldnotes"):
|
||||
return notes
|
||||
return notes.filter(author=user)
|
||||
|
||||
|
||||
class FieldNote(models.Model):
|
||||
class Category(models.TextChoices):
|
||||
GENERAL = "general", _("General")
|
||||
WITNESS = "witness", _("Witness")
|
||||
PEOPLE = "people_vehicles", _("People / vehicles")
|
||||
CCTV = "cctv", _("CCTV")
|
||||
LOCATION = "location_direction", _("Location / direction")
|
||||
FOLLOW_UP = "follow_up", _("Follow-up")
|
||||
OTHER = "other", _("Other")
|
||||
|
||||
class Source(models.TextChoices):
|
||||
FIRST_HAND = "first_hand", _("First-hand")
|
||||
SECOND_HAND = "second_hand", _("Second-hand")
|
||||
UNKNOWN = "unknown", _("Other / unknown")
|
||||
|
||||
class Verification(models.TextChoices):
|
||||
UNVERIFIED = "unverified", _("Unverified")
|
||||
CORROBORATED = "corroborated", _("Corroborated")
|
||||
DISPUTED = "disputed", _("Disputed")
|
||||
|
||||
class Visibility(models.TextChoices):
|
||||
PRIVATE = "private", _("Private")
|
||||
REPORTABLE = "reportable", _("Reportable")
|
||||
|
||||
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="field_notes")
|
||||
context = models.ForeignKey(FieldNoteContext, on_delete=models.PROTECT, related_name="notes")
|
||||
content = models.TextField(max_length=10000)
|
||||
category = models.CharField(max_length=32, choices=Category.choices, default=Category.GENERAL)
|
||||
source_type = models.CharField(max_length=24, choices=Source.choices, default=Source.UNKNOWN)
|
||||
verification_status = models.CharField(max_length=24, choices=Verification.choices, default=Verification.UNVERIFIED)
|
||||
visibility = models.CharField(max_length=16, choices=Visibility.choices, default=Visibility.PRIVATE)
|
||||
observed_at = models.DateTimeField(null=True, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
objects = FieldNoteQuerySet.as_manager()
|
||||
|
||||
class Meta:
|
||||
ordering = ["created_at", "pk"]
|
||||
indexes = [models.Index(fields=["author", "created_at"], name="fieldnote_author_time")]
|
||||
permissions = [
|
||||
("view_all_fieldnotes", "Read all private field notes"),
|
||||
("export_reportable_fieldnotes", "Export reportable field notes from all authors"),
|
||||
]
|
||||
constraints = [
|
||||
models.CheckConstraint(condition=~models.Q(content=""), name="fieldnote_nonempty"),
|
||||
models.CheckConstraint(condition=models.Q(visibility__in=["private", "reportable"]), name="fieldnote_visibility"),
|
||||
models.CheckConstraint(condition=models.Q(source_type__in=["first_hand", "second_hand", "unknown"]), name="fieldnote_source"),
|
||||
models.CheckConstraint(condition=models.Q(verification_status__in=["unverified", "corroborated", "disputed"]), name="fieldnote_verification"),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if not self.content.strip():
|
||||
raise ValidationError({"content": _("Enter a note.")})
|
||||
if self.context_id and self.author_id != self.context.author_id:
|
||||
raise ValidationError(_("The note and its context must have the same author."))
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self._state.adding:
|
||||
raise ValidationError(_("Field notes are append-only. Add a follow-up to correct an earlier note."))
|
||||
self.full_clean()
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class FieldNoteAssociation(models.Model):
|
||||
"""Immutable provenance retained even when the original event is deleted."""
|
||||
context = models.ForeignKey(FieldNoteContext, on_delete=models.PROTECT, related_name="associations")
|
||||
actor = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
|
||||
previous = models.JSONField(default=dict)
|
||||
target = models.JSONField(default=dict)
|
||||
reason = models.CharField(max_length=32)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["created_at", "pk"]
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self._state.adding:
|
||||
raise ValidationError("Association history is append-only.")
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user