diff --git a/admin_panel/bd_models/templates/generate_ball_preview.html b/admin_panel/bd_models/templates/generate_ball_preview.html index b0f8a968..3d219da3 100644 --- a/admin_panel/bd_models/templates/generate_ball_preview.html +++ b/admin_panel/bd_models/templates/generate_ball_preview.html @@ -5,7 +5,7 @@ \ No newline at end of file diff --git a/admin_panel/preview/urls.py b/admin_panel/preview/urls.py index 1c8a668c..c2ab2f6c 100644 --- a/admin_panel/preview/urls.py +++ b/admin_panel/preview/urls.py @@ -1,7 +1,8 @@ from django.urls import path -from .views import render_image +from .views import render_ballinstance, render_special urlpatterns = [ - path("ball/generate/", render_image), + path("ball/generate/", render_ballinstance), + path("special/generate/", render_special), ] diff --git a/admin_panel/preview/views.py b/admin_panel/preview/views.py index 748f833d..077d078f 100644 --- a/admin_panel/preview/views.py +++ b/admin_panel/preview/views.py @@ -1,9 +1,11 @@ import os +from django.contrib import messages from django.http import HttpRequest, HttpResponse from tortoise import Tortoise from ballsdex.__main__ import init_tortoise +from ballsdex.core.image_generator.image_gen import draw_card from ballsdex.core.models import ( Ball, BallInstance, @@ -17,9 +19,14 @@ ) -async def render_image(request: HttpRequest, ball_pk: int) -> HttpResponse: - from ballsdex.core.image_generator.image_gen import draw_card +async def _refresh_cache(): + """ + Similar to the bot's `load_cache` function without the fancy display. Also handles + initializing the connection to Tortoise. + This must be called on every request, since the image generation relies on cache and we + do *not* want caching in the admin panel to happen (since we're actively editing stuff). + """ if not Tortoise._inited: await init_tortoise(os.environ["BALLSDEXBOT_DB_URL"], skip_migrations=True) balls.clear() @@ -38,8 +45,32 @@ async def render_image(request: HttpRequest, ball_pk: int) -> HttpResponse: for special in await Special.all(): specials[special.pk] = special + +async def render_ballinstance(request: HttpRequest, ball_pk: int) -> HttpResponse: + await _refresh_cache() + ball = await Ball.get(pk=ball_pk) - instance = BallInstance(ball=ball, count=1) + instance = BallInstance(ball=ball) + image = draw_card(instance, media_path="./media/") + + response = HttpResponse(content_type="image/png") + image.save(response, "PNG") # type: ignore + return response + + +async def render_special(request: HttpRequest, special_pk: int) -> HttpResponse: + await _refresh_cache() + + ball = await Ball.first() + if ball is None: + messages.warning( + request, + "You must create a countryball before being able to generate a special's preview.", + ) + return HttpResponse(status_code=422) + + special = await Special.get(pk=special_pk) + instance = BallInstance(ball=ball, special=special) image = draw_card(instance, media_path="./media/") response = HttpResponse(content_type="image/png") diff --git a/admin_panel/templates/admin/bd_models/special/change_form.html b/admin_panel/templates/admin/bd_models/special/change_form.html new file mode 100644 index 00000000..dfc03162 --- /dev/null +++ b/admin_panel/templates/admin/bd_models/special/change_form.html @@ -0,0 +1,73 @@ + + +{% extends "admin/change_form.html" %} +{% load i18n admin_urls static admin_modify %} + +{% block content %} +{% if request.resolver_match.url_name == "bd_models_special_change" %} + +
+ {% block object-tools %} + {% if change and not is_popup %} +
    + {% block object-tools-items %} + {% change_form_object_tools %} + {% endblock %} +
+ {% endif %} + {% endblock %} +
+
{% csrf_token %}{% block form_top %}{% endblock %} +
+ {% if is_popup %}{% endif %} + {% if to_field %}{% endif %} + {% if save_on_top %}{% block submit_buttons_top %}{% submit_row %}{% endblock %}{% endif %} + {% if errors %} +

+ {% blocktranslate count counter=errors|length %}Please correct the error below.{% plural %}Please + correct the errors below.{% endblocktranslate %} +

+ {{ adminform.form.non_field_errors }} + {% endif %} + + {% block field_sets %} + {% for fieldset in adminform %} + {% include "admin/includes/fieldset.html" with heading_level=2 prefix="fieldset" id_prefix=0 id_suffix=forloop.counter0 %} + {% endfor %} + {% endblock %} + + {% block after_field_sets %}{% endblock %} + + {% block inline_field_sets %} + {% for inline_admin_formset in inline_admin_formsets %} + {% include inline_admin_formset.opts.template %} + {% endfor %} + {% endblock %} + + {% block after_related_objects %}{% endblock %} + + {% block submit_buttons_bottom %}{% submit_row %}{% endblock %} + + {% block admin_change_form_document_ready %} + + {% endblock %} + + {# JavaScript for prepopulated fields #} + {% prepopulated_fields_js %} + +
+
+ {% include "generate_ball_preview.html" %} +
+
+{% else %} + {{ block.super }} +{% endif %} +{% endblock %} \ No newline at end of file