diff --git a/project/accounts/api.py b/project/accounts/api.py index 9b31969db..9d5494614 100644 --- a/project/accounts/api.py +++ b/project/accounts/api.py @@ -1,4 +1,3 @@ -from django.apps import AppConfig from rest_framework.viewsets import ModelViewSet from rest_framework.decorators import action from rest_framework.response import Response diff --git a/project/accounts/models.py b/project/accounts/models.py index baa05efaf..e6f1f9b8b 100644 --- a/project/accounts/models.py +++ b/project/accounts/models.py @@ -1,20 +1,17 @@ -from django.db import models from django.contrib.auth.models import AbstractUser import os -import uuid import io -from django.utils.deconstruct import deconstructible from django.core.files.storage import default_storage from django.conf import settings from django.db import models from PIL import Image, ImageOps from django.core.files.uploadedfile import InMemoryUploadedFile from django.contrib.auth import get_user_model -from core.constants import US_STATES from taggit.managers import TaggableManager from api.models.category import Category +from common.utils import PathAndRename class User(AbstractUser): @@ -78,8 +75,7 @@ def card_summarize(self, account, request_account): "username": account.user.username, "first_name": account.first_name, "last_name": account.last_name, - "about_me": account.about_me[:about_me_truncate_length] - + (ellipsis_if_too_long), + "about_me": account.about_me[:about_me_truncate_length] + ellipsis_if_too_long, "profile_image": account.profile_image_url, "follow_state": True if account in request_account.following.all() @@ -95,18 +91,6 @@ def following(self, account): return [self.chip_summarize(following) for following in account.following.all()] -@deconstructible -class PathAndRename(object): - def __init__(self, sub_path): - self.sub_path = sub_path - - def __call__(self, instance, filename): - extension = filename.split(".")[-1] - new_filename = str(uuid.uuid4()) - filename = "{}.{}".format(new_filename, extension) - return os.path.join(self.sub_path, filename) - - profile_upload_path = PathAndRename("") @@ -141,12 +125,9 @@ class Account(models.Model): @property def full_name(self): - "Returns the person's full name." + """Returns the person's full name.""" - full_name = "{first_name} {last_name}".format( - first_name=self.first_name, last_name=self.last_name - ) - return full_name + return f"{self.first_name} {self.last_name}" @property def profile_image_url(self): @@ -174,9 +155,6 @@ def profile_image_thumb_url(self): return "/static/img/no_image_md.png" - def __init__(self, *args, **kwargs): - super(Account, self).__init__(*args, **kwargs) - def save(self, *args, **kwargs): """Image crop/resize and thumbnail creation""" @@ -240,17 +218,3 @@ def is_full_account(self): return True else: return False - -@deconstructible -class PathAndRename(object): - def __init__(self, sub_path): - self.sub_path = sub_path - - def __call__(self, instance, filename): - extension = filename.split(".")[-1] - new_filename = str(uuid.uuid4()) - filename = "{}.{}".format(new_filename, extension) - return os.path.join(self.sub_path, filename) - - -profile_upload_path = PathAndRename("") \ No newline at end of file diff --git a/project/api/models/civi.py b/project/api/models/civi.py index e19cfacf4..eb3d9c561 100644 --- a/project/api/models/civi.py +++ b/project/api/models/civi.py @@ -7,20 +7,19 @@ import json import datetime import math -import uuid from calendar import month_name from django.core.files.storage import default_storage from django.core.serializers.json import DjangoJSONEncoder from django.db import models from django.conf import settings -from django.utils.deconstruct import deconstructible from accounts.models import Account from .thread import Thread from taggit.managers import TaggableManager from .thread import Thread from core.constants import CIVI_TYPES +from common.utils import PathAndRename class CiviManager(models.Manager): @@ -278,18 +277,6 @@ def dict_with_score(self, req_acct_id=None): return data -@deconstructible -class PathAndRename(object): - def __init__(self, sub_path): - self.sub_path = sub_path - - def __call__(self, instance, filename): - extension = filename.split(".")[-1] - new_filename = str(uuid.uuid4()) - filename = "{}.{}".format(new_filename, extension) - return os.path.join(self.sub_path, filename) - - image_upload_path = PathAndRename("") diff --git a/project/api/models/thread.py b/project/api/models/thread.py index fe5042aee..06c04cb48 100644 --- a/project/api/models/thread.py +++ b/project/api/models/thread.py @@ -4,19 +4,18 @@ """ import os -import uuid from calendar import month_name from django.core.files.storage import default_storage from django.conf import settings from django.db import models -from django.utils.deconstruct import deconstructible from accounts.models import Account from .category import Category from .fact import Fact from taggit.managers import TaggableManager from core.constants import US_STATES +from common.utils import PathAndRename class ThreadManager(models.Manager): @@ -67,18 +66,6 @@ def filter_by_category(self, categories): return self.all().filter(category__in=categories) -@deconstructible -class PathAndRename(object): - def __init__(self, sub_path): - self.sub_path = sub_path - - def __call__(self, instance, filename): - extension = filename.split(".")[-1] - new_filename = str(uuid.uuid4()) - filename = "{}.{}".format(new_filename, extension) - return os.path.join(self.sub_path, filename) - - image_upload_path = PathAndRename("") diff --git a/project/api/write.py b/project/api/write.py index 2967775a6..9c617d8f4 100644 --- a/project/api/write.py +++ b/project/api/write.py @@ -1,7 +1,6 @@ import json import PIL import urllib -import uuid from notifications.signals import notify @@ -18,13 +17,10 @@ from django.core.files import File # need this for image file handling from django.contrib.auth.decorators import login_required -from django.contrib.auth.decorators import user_passes_test -from django.contrib.sites.shortcuts import get_current_site # civi packages from api.forms import UpdateProfileImage from api.models import Thread -from accounts.utils import send_mass_email from .models import Activity, Category, Civi, CiviImage from accounts.models import Account from core.custom_decorators import require_post_params diff --git a/project/common/__init__.py b/project/common/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/project/common/utils.py b/project/common/utils.py new file mode 100644 index 000000000..14ae19ba0 --- /dev/null +++ b/project/common/utils.py @@ -0,0 +1,15 @@ +import os +import uuid +from django.utils.deconstruct import deconstructible + + +@deconstructible +class PathAndRename(object): + def __init__(self, sub_path): + self.sub_path = sub_path + + def __call__(self, instance, filename): + extension = filename.split(".")[-1] + new_filename = str(uuid.uuid4()) + filename = "{}.{}".format(new_filename, extension) + return os.path.join(self.sub_path, filename)