Files
J621/backend/apps/library/models.py
T
JakeBreath e5cc63b0cc Phase 3: J-IDs, ownership, roles, guest safety, adaptive detail, download
Backend:
- User.role (user/uploader/staff) with can_upload; uploads and downloads
  gated to uploader+; owners and staff can edit their items
- MediaItem.uploaded_by plus J-<id> identity (serializer, admin,
  scan_files --user, first superuser as default owner)
- API resolves J-<id>, bare numeric ids and MD5s; neighbors and lookup
  return j_ids
- Guest safety: mirror e621's anonymous default blacklist into Redis
  (parses comments, negations and wildcards), flag hidden_from_guests
  and filter lists, details and lookups for anonymous users
- POST /api/online/downloads/ writes an e621 file into the watched
  folder and indexes it for the uploader
- MariaDB + Redis via docker compose (host ports 3307/6380), PyMySQL
  driver shim, Redis cache replacing the file cache; SQLite data
  dumped and loaded into MariaDB

Frontend:
- Single /detail/:itemId route with an adaptive shell: J-<id> renders
  the library item, bare numbers render the e621 post
- Legacy /view/<md5> and /online/view/<id> redirect to canonical URLs
- Cards expose J-IDs; library custom-data editor is read-only for
  non-owners
- Role gating: Upload hidden/blocked for regular users, account shows
  the role, guest hint on the library
2026-09-17 10:16:45 -05:00

55 lines
1.7 KiB
Python

from django.conf import settings
from django.db import models
class MediaItem(models.Model):
"""A logical media file, identified by its MD5 fingerprint."""
md5 = models.CharField(max_length=32, unique=True)
size = models.BigIntegerField(default=0)
# Custom rating: "s" (safe), "q" (questionable), "e" (explicit) or "" (unrated).
rating = models.CharField(max_length=1, blank=True, default="")
tags = models.JSONField(default=list, blank=True)
notes = models.TextField(blank=True, default="")
uploaded_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="uploads",
)
# Guests never see items whose tags hit the mirrored default blacklist.
hidden_from_guests = models.BooleanField(default=False, db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["-created_at"]
def __str__(self):
return f"J-{self.pk} ({self.md5})"
def save(self, *args, **kwargs):
from .guest_filter import item_hidden_from_guests
self.hidden_from_guests = item_hidden_from_guests(self.tags)
super().save(*args, **kwargs)
class MediaLocation(models.Model):
"""A physical copy of a media item inside the watched folder."""
item = models.ForeignKey(
MediaItem, related_name="locations", on_delete=models.CASCADE
)
path = models.CharField(max_length=1024)
rel_path = models.CharField(max_length=1024)
mtime = models.FloatField(default=0)
class Meta:
unique_together = ("item", "path")
ordering = ["rel_path"]
def __str__(self):
return self.rel_path