diff --git a/catalog/common/utils.py b/catalog/common/utils.py index 6fae443f..bd3f1eaa 100644 --- a/catalog/common/utils.py +++ b/catalog/common/utils.py @@ -27,3 +27,13 @@ def item_cover_path(item, filename): + filename.split(".")[-1] ) return "item/" + item.category + "/" + fn + + +def piece_cover_path(item, filename): + fn = ( + timezone.now().strftime("%Y/%m/%d/") + + str(uuid.uuid4()) + + "." + + filename.split(".")[-1] + ) + return f"user/{item.owner_id}/{fn}" diff --git a/journal/models.py b/journal/models.py index 8757ab01..cf99eb9e 100644 --- a/journal/models.py +++ b/journal/models.py @@ -16,7 +16,7 @@ from django.contrib.contenttypes.models import ContentType import django.dispatch import uuid import re -from catalog.common.utils import DEFAULT_ITEM_COVER, item_cover_path +from catalog.common.utils import DEFAULT_ITEM_COVER, piece_cover_path from django.utils.baseconv import base62 from django.db.models import Q from catalog.models import * @@ -661,7 +661,7 @@ class Collection(List): title = models.CharField(_("标题"), max_length=1000, default="") brief = models.TextField(_("简介"), blank=True, default="") cover = models.ImageField( - upload_to=item_cover_path, default=DEFAULT_ITEM_COVER, blank=True + upload_to=piece_cover_path, default=DEFAULT_ITEM_COVER, blank=True ) items = models.ManyToManyField( Item, through="CollectionMember", related_name="collections" diff --git a/journal/views.py b/journal/views.py index ff556c0e..44d6b987 100644 --- a/journal/views.py +++ b/journal/views.py @@ -288,7 +288,7 @@ def collection_edit(request, collection_uuid=None): ) elif request.method == "POST": form = ( - CollectionForm(request.POST, instance=collection) + CollectionForm(request.POST, request.FILES, instance=collection) if collection else CollectionForm(request.POST) )