From 7ed1f6f8e41535028951c151bd531c849d05e683 Mon Sep 17 00:00:00 2001 From: Jonathan Weth <git@jonathanweth.de> Date: Thu, 16 Jan 2020 19:43:09 +0100 Subject: [PATCH] Move path_and_rename to core_helpers.py and delete old helper.py --- aleksis/core/util/core_helpers.py | 17 ++++++++++++++++- aleksis/core/util/helper.py | 22 ---------------------- 2 files changed, 16 insertions(+), 23 deletions(-) delete mode 100644 aleksis/core/util/helper.py diff --git a/aleksis/core/util/core_helpers.py b/aleksis/core/util/core_helpers.py index 566932361..ec114c5c0 100644 --- a/aleksis/core/util/core_helpers.py +++ b/aleksis/core/util/core_helpers.py @@ -1,6 +1,8 @@ +import os import pkgutil from importlib import import_module from typing import Any, Callable, Sequence, Union +from uuid import uuid4 from django.conf import settings from django.db.models import Model @@ -106,7 +108,7 @@ def has_person(obj: Union[HttpRequest, Model]) -> bool: def celery_optional(orig: Callable) -> Callable: """ Decorator that makes Celery optional for a function. - + 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 it is executed synchronously. @@ -121,3 +123,16 @@ def celery_optional(orig: Callable) -> Callable: return wrapped else: 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) diff --git a/aleksis/core/util/helper.py b/aleksis/core/util/helper.py deleted file mode 100644 index 04b910255..000000000 --- a/aleksis/core/util/helper.py +++ /dev/null @@ -1,22 +0,0 @@ -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} -- GitLab