mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2025-02-19 22:40:31 +03:00
minor BaseImageModel refactor
This commit is contained in:
parent
91b851805f
commit
1a2665cdb4
|
@ -1,57 +1,14 @@
|
|||
import os
|
||||
|
||||
from django.db import models
|
||||
|
||||
from akarpov.common.tasks import crop_model_image
|
||||
from akarpov.common.signals import (
|
||||
create_cropped_model_image,
|
||||
delete_cropped_model_image,
|
||||
update_cropped_model_image,
|
||||
)
|
||||
from akarpov.utils.files import user_file_upload_mixin
|
||||
from akarpov.utils.generators import generate_charset
|
||||
|
||||
|
||||
def create_cropped_model_image(sender, instance, created, **kwargs):
|
||||
model = sender
|
||||
if created:
|
||||
if instance.image:
|
||||
crop_model_image.apply_async(
|
||||
kwargs={
|
||||
"pk": instance.pk,
|
||||
"app_label": model._meta.app_label,
|
||||
"model_name": model._meta.model_name,
|
||||
},
|
||||
countdown=2,
|
||||
)
|
||||
|
||||
|
||||
def update_cropped_model_image(sender, instance, **kwargs):
|
||||
model = sender
|
||||
if instance.id:
|
||||
previous = model.objects.get(id=instance.id)
|
||||
if previous.image != instance.image:
|
||||
# delete previous cropped image
|
||||
if instance.image_cropped:
|
||||
if os.path.isfile(instance.image_cropped.path):
|
||||
os.remove(instance.image_cropped.path)
|
||||
# run task to create new cropped image
|
||||
if kwargs["update_fields"] != frozenset({"image_cropped"}) and instance:
|
||||
if instance.image:
|
||||
crop_model_image.apply_async(
|
||||
kwargs={
|
||||
"pk": instance.pk,
|
||||
"app_label": model._meta.app_label,
|
||||
"model_name": model._meta.model_name,
|
||||
},
|
||||
countdown=2,
|
||||
)
|
||||
else:
|
||||
instance.image_cropped = None
|
||||
|
||||
|
||||
def delete_cropped_model_image(sender, instance, **kwargs):
|
||||
print(instance.image_cropped)
|
||||
if instance.image_cropped:
|
||||
if os.path.isfile(instance.image_cropped.path):
|
||||
os.remove(instance.image_cropped.path)
|
||||
|
||||
|
||||
class BaseImageModel(models.Model):
|
||||
"""
|
||||
stores user's images in their media folder, creates, updates and deletes preview
|
||||
|
|
47
akarpov/common/signals.py
Normal file
47
akarpov/common/signals.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
import os
|
||||
|
||||
from akarpov.common.tasks import crop_model_image
|
||||
|
||||
|
||||
def create_cropped_model_image(sender, instance, created, **kwargs):
|
||||
model = sender
|
||||
if created:
|
||||
if instance.image:
|
||||
crop_model_image.apply_async(
|
||||
kwargs={
|
||||
"pk": instance.pk,
|
||||
"app_label": model._meta.app_label,
|
||||
"model_name": model._meta.model_name,
|
||||
},
|
||||
countdown=2,
|
||||
)
|
||||
|
||||
|
||||
def update_cropped_model_image(sender, instance, **kwargs):
|
||||
model = sender
|
||||
if instance.id:
|
||||
previous = model.objects.get(id=instance.id)
|
||||
if previous.image != instance.image:
|
||||
# delete previous cropped image
|
||||
if instance.image_cropped:
|
||||
if os.path.isfile(instance.image_cropped.path):
|
||||
os.remove(instance.image_cropped.path)
|
||||
# run task to create new cropped image
|
||||
if kwargs["update_fields"] != frozenset({"image_cropped"}) and instance:
|
||||
if instance.image:
|
||||
crop_model_image.apply_async(
|
||||
kwargs={
|
||||
"pk": instance.pk,
|
||||
"app_label": model._meta.app_label,
|
||||
"model_name": model._meta.model_name,
|
||||
},
|
||||
countdown=2,
|
||||
)
|
||||
else:
|
||||
instance.image_cropped = None
|
||||
|
||||
|
||||
def delete_cropped_model_image(sender, instance, **kwargs):
|
||||
if instance.image_cropped:
|
||||
if os.path.isfile(instance.image_cropped.path):
|
||||
os.remove(instance.image_cropped.path)
|
Loading…
Reference in New Issue
Block a user