fix now card width
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
"""Central access checks and association/reporting services for field notes."""
|
||||
import uuid
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core import signing
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from django.http import Http404
|
||||
|
||||
from .models import Event, PendingEvent, FieldNote, FieldNoteContext, FieldNoteAssociation
|
||||
|
||||
|
||||
def draft_token(user):
|
||||
if not user.is_authenticated:
|
||||
return ""
|
||||
return signing.dumps({"author": user.pk, "id": str(uuid.uuid4())}, salt="field-notes")
|
||||
|
||||
|
||||
def token_id(user, token):
|
||||
try:
|
||||
data = signing.loads(token, salt="field-notes", max_age=7 * 86400)
|
||||
if data["author"] != user.pk:
|
||||
raise ValueError
|
||||
return uuid.UUID(data["id"])
|
||||
except (signing.BadSignature, ValueError, KeyError, TypeError):
|
||||
raise PermissionDenied("Invalid or expired note context. Reload the reporting page.")
|
||||
|
||||
|
||||
def accessible_pending(user, session_ids=()):
|
||||
if not user.is_authenticated:
|
||||
return PendingEvent.objects.none()
|
||||
if user.is_staff and user.has_perm("fireworks_detect.view_pendingevent"):
|
||||
return PendingEvent.objects.all()
|
||||
return PendingEvent.objects.filter(Q(note_contexts__author=user) | Q(sensor__owner=user) | Q(pk__in=session_ids)).distinct()
|
||||
|
||||
|
||||
def target_snapshot(context):
|
||||
if context.event_id:
|
||||
return {"kind": "event", "id": context.event_id}
|
||||
if context.pending_event_id:
|
||||
return {"kind": "pending", "id": context.pending_event_id}
|
||||
return {}
|
||||
|
||||
|
||||
def associate(context, *, event=None, pending=None, actor=None, reason):
|
||||
previous = target_snapshot(context)
|
||||
context.event = event
|
||||
context.pending_event = pending
|
||||
context.full_clean()
|
||||
context.save(update_fields=["event", "pending_event"])
|
||||
FieldNoteAssociation.objects.create(context=context, actor=actor, previous=previous,
|
||||
target=target_snapshot(context), reason=reason)
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def context_for_token(user, token):
|
||||
# Serialize draft-note creation and report binding for the same author.
|
||||
get_user_model().objects.select_for_update().get(pk=user.pk)
|
||||
context, _ = FieldNoteContext.objects.get_or_create(id=token_id(user, token), defaults={"author": user})
|
||||
if context.author_id != user.pk:
|
||||
raise PermissionDenied
|
||||
return context
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def bind_report(user, token, pending):
|
||||
if not user.is_authenticated or not token:
|
||||
return
|
||||
context = context_for_token(user, token)
|
||||
if context.event_id or context.pending_event_id:
|
||||
raise PermissionDenied("This note context already belongs to a report. Reload before reporting again.")
|
||||
associate(context, pending=pending, actor=user, reason="submission")
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def link_standalone(user, context_id, event):
|
||||
# Approved events are publicly accessible in the existing application.
|
||||
context = FieldNoteContext.objects.select_for_update().filter(pk=context_id, author=user).first()
|
||||
if not context or context.event_id or context.pending_event_id:
|
||||
raise Http404
|
||||
associate(context, event=event, actor=user, reason="linked")
|
||||
return context
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def regroup_event_notes(user, source, target):
|
||||
"""Hook for future event-merging code; does not modify detection records."""
|
||||
if not (user.is_staff and user.has_perm("fireworks_detect.change_event") and
|
||||
user.has_perm("fireworks_detect.view_all_fieldnotes")):
|
||||
raise PermissionDenied
|
||||
if source.pk == target.pk:
|
||||
return
|
||||
for context in FieldNoteContext.objects.select_for_update().filter(event=source):
|
||||
associate(context, event=target, actor=user, reason="regrouped")
|
||||
|
||||
|
||||
def reportable_notes(user):
|
||||
"""All exports, including future PDF reports, must start with this queryset."""
|
||||
return FieldNote.objects.reportable_to(user).select_related("author", "context")
|
||||
Reference in New Issue
Block a user