From c50b5c4f3bc3cdec945c13ecb61b260f8cd1e483 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Mon, 22 Apr 2024 02:27:50 +0100 Subject: [PATCH 1/2] perf(coordinator): remove avatar generator --- api/admin.py | 24 ++---------------------- api/views.py | 4 ---- requirements.txt | 1 - robosats/middleware.py | 21 --------------------- robosats/settings.py | 13 +++---------- 5 files changed, 5 insertions(+), 58 deletions(-) diff --git a/api/admin.py b/api/admin.py index 751a2a07..bbfdee19 100644 --- a/api/admin.py +++ b/api/admin.py @@ -18,20 +18,9 @@ admin.site.unregister(User) admin.site.unregister(TokenProxy) -class RobotInline(admin.StackedInline): - model = Robot - can_delete = False - fields = ("avatar_tag",) - readonly_fields = ["avatar_tag"] - show_change_link = True - - -# extended users with avatars @admin.register(User) class EUserAdmin(AdminChangeLinksMixin, UserAdmin): - inlines = [RobotInline] list_display = ( - "avatar_tag", "id", "robot_link", "username", @@ -43,25 +32,18 @@ class EUserAdmin(AdminChangeLinksMixin, UserAdmin): change_links = ("robot",) ordering = ("-id",) - def avatar_tag(self, obj): - return obj.robot.avatar_tag() - -# extended tokens with raw id fields and avatars +# extended tokens with raw id fields @admin.register(TokenProxy) class ETokenAdmin(AdminChangeLinksMixin, TokenAdmin): raw_id_fields = ["user"] list_display = ( - "avatar_tag", "key", "user_link", ) list_display_links = ("key",) change_links = ("user",) - def avatar_tag(self, obj): - return obj.user.robot.avatar_tag() - @admin.register(Order) class OrderAdmin(AdminChangeLinksMixin, admin.ModelAdmin): @@ -446,7 +428,6 @@ class OnchainPaymentAdmin(AdminChangeLinksMixin, admin.ModelAdmin): @admin.register(Robot) class UserRobotAdmin(AdminChangeLinksMixin, admin.ModelAdmin): list_display = ( - "avatar_tag", "id", "user_link", "telegram_enabled", @@ -459,9 +440,8 @@ class UserRobotAdmin(AdminChangeLinksMixin, admin.ModelAdmin): ) raw_id_fields = ("user",) list_editable = ["earned_rewards"] - list_display_links = ("avatar_tag", "id") + list_display_links = ["id"] change_links = ["user"] - readonly_fields = ["avatar_tag"] search_fields = ["user__username", "id"] readonly_fields = ("hash_id", "public_key", "encrypted_private_key") diff --git a/api/views.py b/api/views.py index 53e15292..3a76dc7a 100644 --- a/api/views.py +++ b/api/views.py @@ -1,5 +1,4 @@ from datetime import datetime, timedelta -from pathlib import Path from decouple import config from django.conf import settings @@ -55,9 +54,6 @@ from control.models import AccountingDay, BalanceLog EXP_MAKER_BOND_INVOICE = int(config("EXP_MAKER_BOND_INVOICE")) RETRY_TIME = int(config("RETRY_TIME")) -avatar_path = Path(settings.AVATAR_ROOT) -avatar_path.mkdir(parents=True, exist_ok=True) - class MakerView(CreateAPIView): serializer_class = MakeOrderSerializer diff --git a/requirements.txt b/requirements.txt index 3fdd9276..acb9eb8d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,7 +16,6 @@ Pillow==10.1.0 python-decouple==3.8 requests==2.31.0 ring==0.10.1 -git+https://github.com/RoboSats/Robohash.git gunicorn==22.0.0 psycopg2==2.9.9 SQLAlchemy==2.0.16 diff --git a/robosats/middleware.py b/robosats/middleware.py index f75a82aa..47f9bf2a 100644 --- a/robosats/middleware.py +++ b/robosats/middleware.py @@ -1,16 +1,13 @@ import hashlib from datetime import timedelta -from pathlib import Path from channels.db import database_sync_to_async from channels.middleware import BaseMiddleware -from django.conf import settings from django.contrib.auth.models import AnonymousUser, User, update_last_login from django.utils import timezone from django.utils.deprecation import MiddlewareMixin from rest_framework.authtoken.models import Token from rest_framework.exceptions import AuthenticationFailed -from robohash import Robohash from api.nick_generator.nick_generator import NickGenerator from api.utils import base91_to_hex, hex_to_base91, is_valid_token, validate_pgp_keys @@ -19,9 +16,6 @@ NickGen = NickGenerator( lang="English", use_adv=False, use_adj=True, use_noun=True, max_num=999 ) -avatar_path = Path(settings.AVATAR_ROOT) -avatar_path.mkdir(parents=True, exist_ok=True) - class DisableCSRFMiddleware(object): def __init__(self, get_response): @@ -158,21 +152,6 @@ class RobotTokenSHA256AuthenticationMiddleWare: if not user.robot.encrypted_private_key: user.robot.encrypted_private_key = encrypted_private_key - # Generate avatar. Does not replace if existing. - image_path = avatar_path.joinpath(nickname + ".webp") - if not image_path.exists(): - rh = Robohash(hash) - rh.assemble(roboset="set1", bgset="any") # for backgrounds ON - with open(image_path, "wb") as f: - rh.img.save(f, format="WEBP", quality=80) - - image_small_path = avatar_path.joinpath(nickname + ".small.webp") - with open(image_small_path, "wb") as f: - resized_img = rh.img.resize((80, 80)) - resized_img.save(f, format="WEBP", quality=80) - - user.robot.avatar = "static/assets/avatars/" + nickname + ".webp" - update_last_login(None, user) user.save() diff --git a/robosats/settings.py b/robosats/settings.py index b25a6bf2..555061f0 100644 --- a/robosats/settings.py +++ b/robosats/settings.py @@ -23,8 +23,6 @@ from .celery.conf import * # noqa # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent -STATIC_URL = "/static/" - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ @@ -32,8 +30,10 @@ STATIC_URL = "/static/" SECRET_KEY = config("SECRET_KEY") DEBUG = False + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ STATIC_URL = "static/" -STATIC_ROOT = "/usr/src/static/" # RoboSats version with open("version.json") as f: @@ -42,9 +42,6 @@ with open("version.json") as f: # SECURITY WARNING: don't run with debug turned on in production! if config("DEVELOPMENT", default=False): DEBUG = True - STATIC_ROOT = "frontend/static/" - -AVATAR_ROOT = STATIC_ROOT + "assets/avatars/" ALLOWED_HOSTS = [ config("HOST_NAME"), @@ -228,10 +225,6 @@ USE_I18N = True USE_TZ = True -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/4.0/howto/static-files/ - -STATIC_URL = "static/" ASGI_APPLICATION = "robosats.routing.application" CHANNEL_LAYERS = { From 520a245f828201c09f4f967b1eec5f99cb491834 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Thu, 2 May 2024 03:13:37 +0100 Subject: [PATCH 2/2] Add complete robot inline --- api/admin.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/admin.py b/api/admin.py index bbfdee19..a788eef8 100644 --- a/api/admin.py +++ b/api/admin.py @@ -18,8 +18,15 @@ admin.site.unregister(User) admin.site.unregister(TokenProxy) +class RobotInline(admin.StackedInline): + model = Robot + can_delete = False + show_change_link = True + + @admin.register(User) class EUserAdmin(AdminChangeLinksMixin, UserAdmin): + inlines = [RobotInline] list_display = ( "id", "robot_link",