mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-21 15:11:08 -05:00
Refactor test utilities and enhance ASGI settings
- Cleaned up and standardized assertions in ApiTestMixin for API response validation. - Updated ASGI settings to use os.environ for setting the DJANGO_SETTINGS_MODULE. - Removed unused imports and improved formatting in settings.py. - Refactored URL patterns in urls.py for better readability and organization. - Enhanced view functions in views.py for consistency and clarity. - Added .flake8 configuration for linting and style enforcement. - Introduced type stubs for django-environ to improve type checking with Pylance.
This commit is contained in:
@@ -2,7 +2,6 @@ from django.http import JsonResponse
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.shortcuts import get_object_or_404
|
||||
import json
|
||||
import logging
|
||||
@@ -52,7 +51,8 @@ def upload_photo(request):
|
||||
)
|
||||
except ContentType.DoesNotExist:
|
||||
return JsonResponse(
|
||||
{"error": f"Invalid content type: {app_label}.{model}"}, status=400
|
||||
{"error": f"Invalid content type: {app_label}.{model}"},
|
||||
status=400,
|
||||
)
|
||||
|
||||
# Get the object instance
|
||||
@@ -61,7 +61,8 @@ def upload_photo(request):
|
||||
except Exception as e:
|
||||
return JsonResponse(
|
||||
{
|
||||
"error": f"Object not found: {app_label}.{model} with id {object_id}. Error: {str(e)}"
|
||||
"error": f"Object not found: {app_label}.{model} with id {object_id}. Error: {
|
||||
str(e)}"
|
||||
},
|
||||
status=404,
|
||||
)
|
||||
@@ -69,14 +70,20 @@ def upload_photo(request):
|
||||
# Check if user has permission to add photos
|
||||
if not request.user.has_perm("media.add_photo"):
|
||||
logger.warning(
|
||||
f"User {request.user} attempted to upload photo without permission"
|
||||
f"User {
|
||||
request.user} attempted to upload photo without permission"
|
||||
)
|
||||
return JsonResponse(
|
||||
{"error": "You do not have permission to upload photos"}, status=403
|
||||
{"error": "You do not have permission to upload photos"},
|
||||
status=403,
|
||||
)
|
||||
|
||||
# Determine if the photo should be auto-approved
|
||||
is_approved = request.user.is_superuser or request.user.is_staff or request.user.groups.filter(name='Moderators').exists()
|
||||
is_approved = (
|
||||
request.user.is_superuser
|
||||
or request.user.is_staff
|
||||
or request.user.groups.filter(name="Moderators").exists()
|
||||
)
|
||||
|
||||
# Create the photo
|
||||
photo = Photo.objects.create(
|
||||
@@ -87,7 +94,8 @@ def upload_photo(request):
|
||||
is_primary=not Photo.objects.filter(
|
||||
content_type=content_type, object_id=obj.pk
|
||||
).exists(),
|
||||
is_approved=is_approved # Auto-approve if the user is a moderator, admin, or superuser
|
||||
is_approved=is_approved,
|
||||
# Auto-approve if the user is a moderator, admin, or superuser
|
||||
)
|
||||
|
||||
return JsonResponse(
|
||||
@@ -118,7 +126,8 @@ def set_primary_photo(request, photo_id):
|
||||
# Check if user has permission to edit photos
|
||||
if not request.user.has_perm("media.change_photo"):
|
||||
return JsonResponse(
|
||||
{"error": "You do not have permission to edit photos"}, status=403
|
||||
{"error": "You do not have permission to edit photos"},
|
||||
status=403,
|
||||
)
|
||||
|
||||
# Set this photo as primary
|
||||
@@ -142,7 +151,8 @@ def update_caption(request, photo_id):
|
||||
# Check if user has permission to edit photos
|
||||
if not request.user.has_perm("media.change_photo"):
|
||||
return JsonResponse(
|
||||
{"error": "You do not have permission to edit photos"}, status=403
|
||||
{"error": "You do not have permission to edit photos"},
|
||||
status=403,
|
||||
)
|
||||
|
||||
# Update caption
|
||||
@@ -167,7 +177,8 @@ def delete_photo(request, photo_id):
|
||||
# Check if user has permission to delete photos
|
||||
if not request.user.has_perm("media.delete_photo"):
|
||||
return JsonResponse(
|
||||
{"error": "You do not have permission to delete photos"}, status=403
|
||||
{"error": "You do not have permission to delete photos"},
|
||||
status=403,
|
||||
)
|
||||
|
||||
photo.delete()
|
||||
|
||||
Reference in New Issue
Block a user