Skip to content
Snippets Groups Projects
Verified Commit 7ed1f6f8 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

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

parent 0472fab4
Branches
Tags
1 merge request!125Move path_and_rename to core_helpers.py and delete old helper.py
Pipeline #671 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,16 @@ def celery_optional(orig: Callable) -> Callable: ...@@ -121,3 +123,16 @@ def celery_optional(orig: Callable) -> Callable:
return wrapped return wrapped
else: else:
return orig return orig
def path_and_rename(instance, filename):
upload_to = 'files'
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)
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.
Please register or to comment