Skip to content
Snippets Groups Projects
Commit ca7262f5 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Merge branch 'merge/update-upload-helper' into 'master'

Move path_and_rename to core_helpers.py and delete old helper.py

See merge request AlekSIS/AlekSIS!125
parents 0472fab4 2507ecc2
No related branches found
No related tags found
1 merge request!125Move path_and_rename to core_helpers.py and delete old helper.py
Pipeline #676 failed
import os
import pkgutil import pkgutil
from importlib import import_module from importlib import import_module
from typing import Any, Callable, Sequence, Union from typing import Any, Callable, Sequence, Union
from uuid import uuid4
from django.conf import settings from django.conf import settings
from django.db.models import Model from django.db.models import Model
...@@ -106,7 +108,7 @@ def has_person(obj: Union[HttpRequest, Model]) -> bool: ...@@ -106,7 +108,7 @@ def has_person(obj: Union[HttpRequest, Model]) -> bool:
def celery_optional(orig: Callable) -> Callable: def celery_optional(orig: Callable) -> Callable:
""" Decorator that makes Celery optional for a function. """ Decorator that makes Celery optional for a function.
If Celery is configured and available, it wraps the function in a Task If Celery is configured and available, it wraps the function in a Task
and calls its delay method when invoked; if not, it leaves it untouched and calls its delay method when invoked; if not, it leaves it untouched
and it is executed synchronously. and it is executed synchronously.
...@@ -121,3 +123,18 @@ def celery_optional(orig: Callable) -> Callable: ...@@ -121,3 +123,18 @@ def celery_optional(orig: Callable) -> Callable:
return wrapped return wrapped
else: else:
return orig return orig
def path_and_rename(instance, filename: str, upload_to: str = "files") -> str:
""" Updates path of an uploaded file and renames it to a random UUID in Django FileField """
_, ext = os.path.splitext(filename)
# set filename as random string
new_filename = '{}.{}'.format(uuid4().hex, ext)
# Create upload directory if necessary
os.makedirs(os.path.join(settings.MEDIA_ROOT, upload_to), exist_ok=True)
# return the whole path to the file
return os.path.join(upload_to, new_filename)
import os
from uuid import uuid4
from django.template.loader_tags import register
def path_and_rename(instance, filename):
upload_to = 'menus'
ext = filename.split('.')[-1].lower()
# get filename
if instance.pk:
filename = '{}.{}'.format(instance.pk, ext)
else:
# set filename as random string
filename = '{}.{}'.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(upload_to, filename)
@register.inclusion_tag("components/msgbox.html")
def msg_box(msg, status="success", icon="info"):
return {"msg": msg, "status": status, "icon": icon}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment