series of tests added with built-in django test support

This commit is contained in:
pacnpal
2024-11-05 18:40:39 +00:00
parent ba226c861a
commit 2e8a725933
60 changed files with 2108 additions and 274 deletions

View File

@@ -2,6 +2,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.contenttypes.models import ContentType
from django.http import JsonResponse, HttpResponseForbidden
from django.core.exceptions import PermissionDenied
from django.views.generic import DetailView
from django.utils import timezone
import json
from .models import EditSubmission, PhotoSubmission
@@ -49,7 +50,7 @@ class EditSubmissionMixin:
# Auto-approve for moderators and above
if request.user.role in ['MODERATOR', 'ADMIN', 'SUPERUSER']:
obj = submission.auto_approve()
obj = submission.approve(request.user)
return JsonResponse({
'status': 'success',
'message': 'Changes saved successfully.',
@@ -119,13 +120,20 @@ class PhotoSubmissionMixin:
'message': 'You must be logged in to upload photos.'
}, status=403)
try:
obj = self.get_object()
except (AttributeError, self.model.DoesNotExist):
return JsonResponse({
'status': 'error',
'message': 'Invalid object.'
}, status=400)
if not request.FILES.get('photo'):
return JsonResponse({
'status': 'error',
'message': 'No photo provided.'
}, status=400)
obj = self.get_object()
content_type = ContentType.objects.get_for_model(obj)
submission = PhotoSubmission(
@@ -184,10 +192,10 @@ class InlineEditMixin:
"""Add inline editing context to views"""
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.request.user.is_authenticated:
if hasattr(self, 'request') and self.request.user.is_authenticated:
context['can_edit'] = True
context['can_auto_approve'] = self.request.user.role in ['MODERATOR', 'ADMIN', 'SUPERUSER']
if hasattr(self, 'get_object'):
if isinstance(self, DetailView):
obj = self.get_object()
context['pending_edits'] = EditSubmission.objects.filter(
content_type=ContentType.objects.get_for_model(obj),
@@ -200,18 +208,21 @@ class HistoryMixin:
"""Add edit history context to views"""
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
obj = self.get_object()
# Get historical records ordered by date
context['history'] = obj.history.all().select_related('history_user').order_by('-history_date')
# Get related edit submissions
content_type = ContentType.objects.get_for_model(obj)
context['edit_submissions'] = EditSubmission.objects.filter(
content_type=content_type,
object_id=obj.id
).exclude(
status='NEW'
).select_related('user', 'handled_by').order_by('-created_at')
# Only add history context for DetailViews
if isinstance(self, DetailView):
obj = self.get_object()
# Get historical records ordered by date
context['history'] = obj.history.all().select_related('history_user').order_by('-history_date')
# Get related edit submissions
content_type = ContentType.objects.get_for_model(obj)
context['edit_submissions'] = EditSubmission.objects.filter(
content_type=content_type,
object_id=obj.id
).exclude(
status='NEW'
).select_related('user', 'handled_by').order_by('-created_at')
return context