feat(submissions): base submissions frontend + api
This commit is contained in:
@@ -39,14 +39,14 @@ class SubmissionFileInline(admin.TabularInline):
|
||||
|
||||
@admin.register(Submission)
|
||||
class SubmissionAdmin(admin.ModelAdmin):
|
||||
list_display = ["id_short", "user", "notes_preview", "created_at"]
|
||||
list_filter = ["created_at", "updated_at"]
|
||||
search_fields = ["id", "user__email", "notes"]
|
||||
list_display = ["id_short", "game", "user", "notes_preview", "created_at"]
|
||||
list_filter = ["game", "created_at", "updated_at"]
|
||||
search_fields = ["id", "user__email", "notes", "game__name"]
|
||||
readonly_fields = ["id", "created_at", "updated_at"]
|
||||
inlines = [SubmissionFileInline]
|
||||
|
||||
def get_queryset(self, request):
|
||||
return super().get_queryset(request).select_related("user")
|
||||
return super().get_queryset(request).select_related("user", "game")
|
||||
|
||||
@admin.display(description="ID")
|
||||
def id_short(self, obj):
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
from typing import List, Optional
|
||||
from django.http import HttpRequest
|
||||
from ninja import Router, File, Form
|
||||
from ninja.files import UploadedFile
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from games.models import Game
|
||||
from .models import Submission, SubmissionFile
|
||||
from .schemas import SubmissionOut
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.post("/create", response=SubmissionOut)
|
||||
def create_submission(
|
||||
request: HttpRequest,
|
||||
game_path: str,
|
||||
files: List[UploadedFile] = File(...),
|
||||
notes: Optional[str] = Form(None),
|
||||
):
|
||||
game = get_object_or_404(Game, path=game_path, enabled=True)
|
||||
|
||||
submission = Submission.objects.create(
|
||||
game=game,
|
||||
user=request.user if request.user.is_authenticated else None,
|
||||
notes=notes or None,
|
||||
)
|
||||
|
||||
for file in files:
|
||||
SubmissionFile.objects.create(
|
||||
submission=submission,
|
||||
file=file,
|
||||
original_filename=file.name,
|
||||
content_type=file.content_type or "application/octet-stream",
|
||||
)
|
||||
|
||||
return submission
|
||||
|
||||
|
||||
@router.get("/{submission_id}", response=SubmissionOut)
|
||||
def get_submission(request: HttpRequest, submission_id: str):
|
||||
submission = get_object_or_404(Submission, id=submission_id)
|
||||
|
||||
if submission.user != request.user and not request.user.is_staff:
|
||||
return 403, {"detail": "Not authorized"}
|
||||
|
||||
return submission
|
||||
|
||||
|
||||
@router.get("/game/{game_path}")
|
||||
def list_submissions_by_game(request: HttpRequest, game_path: str):
|
||||
game = get_object_or_404(Game, path=game_path)
|
||||
|
||||
if not request.user.is_staff:
|
||||
return 403, {"detail": "Admin access required"}
|
||||
|
||||
submissions = Submission.objects.filter(game=game)
|
||||
return [SubmissionOut.from_orm(s) for s in submissions]
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
# Generated by Django 6.0.5 on 2026-05-24 22:57
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("games", "0004_game_image_alter_game_steam_app_id"),
|
||||
("submissions", "0001_initial"),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="submission",
|
||||
name="game",
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="submissions",
|
||||
to="games.game",
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="submission",
|
||||
name="user",
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="User who made the submission (null for anonymous)",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="user_submissions",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -4,6 +4,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
import uuid
|
||||
|
||||
from accounts.models import CustomUser
|
||||
from games.models import Game
|
||||
|
||||
|
||||
class OCRStatus(models.TextChoices):
|
||||
@@ -33,13 +34,21 @@ class BaseModel(models.Model):
|
||||
|
||||
|
||||
class Submission(BaseModel):
|
||||
game = models.ForeignKey(
|
||||
Game,
|
||||
on_delete=models.CASCADE,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="submissions",
|
||||
)
|
||||
|
||||
user = models.ForeignKey(
|
||||
CustomUser,
|
||||
on_delete=models.CASCADE,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="User who made the submission (null for anonymous)",
|
||||
related_name="submissions",
|
||||
related_name="user_submissions",
|
||||
)
|
||||
|
||||
# Submission metadata
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
from typing import Optional
|
||||
from ninja import Schema
|
||||
|
||||
|
||||
class SubmissionFileOut(Schema):
|
||||
id: str
|
||||
original_filename: str
|
||||
file_size: int
|
||||
content_type: str
|
||||
file_url: Optional[str] = None
|
||||
ocr_status: str
|
||||
|
||||
|
||||
class SubmissionOut(Schema):
|
||||
id: str
|
||||
game_id: Optional[int] = None
|
||||
notes: Optional[str] = None
|
||||
files: list[SubmissionFileOut] = []
|
||||
created_at: str
|
||||
updated_at: str
|
||||
@@ -1 +1,17 @@
|
||||
# Create your views here.
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
from games.models import Game
|
||||
|
||||
|
||||
@login_required
|
||||
def submission_home(request: HttpRequest, game: str):
|
||||
game_obj = get_object_or_404(Game, path=f"/submissions/{game}", enabled=True)
|
||||
return render(
|
||||
request,
|
||||
"submissions.html",
|
||||
{
|
||||
"game": game_obj,
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user