Skip to content

Commit

Permalink
Merge branch 'feature/898-podglad-edycji-opisu-bib' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mpasternak committed Jun 14, 2022
2 parents 3c31b4e + 7469716 commit 5b8324b
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dev
* umożliwiaj wygenerowanie kodu JSON wysyłanego do PBN API z linii
poleceń -- polecenie ``pbn_show_json`` (b/n),
* poprawnie wysyłaj strony do PBN API (#1176),
* podgląd edycji schematu opisu bibliograficznego (#898),

202205.1086
-----------
Expand Down
66 changes: 66 additions & 0 deletions src/bpp/admin/templates.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import sys
import traceback
from datetime import datetime, timedelta

from dbtemplates.admin import TemplateAdmin, TemplateAdminForm
from dbtemplates.models import Template
from dbtemplates.utils.cache import remove_cached_template
from dbtemplates.utils.template import check_template_syntax
from django.conf.urls import url
from django.core.exceptions import ValidationError
from django.template import Context, TemplateSyntaxError
from django.template.engine import Engine
from django.template.loaders.cached import Loader as CachedLoader
from django.template.response import TemplateResponse

from django.contrib import admin, messages

from bpp.util import rebuild_instances_of_models

admin.site.unregister(Template)

ILE_OSTATNICH_PRAC = 25


class BppTemplateAdminForm(TemplateAdminForm):
def clean_content(self):
Expand Down Expand Up @@ -95,3 +102,62 @@ def delete_model(self, request, obj):
super(TemplateAdmin, self).delete_model(request, obj)
self.invalidate_cache(request, Template.objects.filter(pk=obj.pk))
self.template_updated(request, obj)

def get_urls(self):
urls = super().get_urls()

preview_urls = [
url(
r"^preview/$",
self.admin_site.admin_view(self.opis_bibliograficzny_preview),
)
# Add here more urls if you want following same logic
]

return preview_urls + urls

# Your view definition fn
def opis_bibliograficzny_preview(self, request):
template = request.GET.get("template")

from django.template import Template as DjangoTemplate

template_okay = True
exception = None
lista_prac = []
tb = None

try:
django_template = DjangoTemplate(template)
except TemplateSyntaxError as e:
template_okay = False
exception = e
type, value, tb = sys.exc_info()

if template_okay:
from bpp.models.cache import Rekord

ostatnie_prace = Rekord.objects.all().order_by("-ostatnio_zmieniony")[
:ILE_OSTATNICH_PRAC
]

for elem in ostatnie_prace:
context = Context({"praca": elem.original})
lista_prac.append(django_template.render(context))

#
context = dict(
self.admin_site.each_context(request),
template=template,
lista_prac=lista_prac,
template_okay=template_okay,
exception=exception,
ile_ostatnich_prac=ILE_OSTATNICH_PRAC,
is_popup=True,
traceback="\n".join(traceback.format_tb(tb)),
tytul="Szybki podgląd szablonu",
)

return TemplateResponse(
request, "admin/opis_bibliograficzny_preview.html", context
)
40 changes: 40 additions & 0 deletions src/bpp/templates/admin/opis_bibliograficzny_preview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% extends "bare.html" %}

{% block extratitle %}
Szybki podgląd
{% endblock %}

{% block body %}
<p></p>
<div class="grid-container">
<div class="grid-x">
<div class="large-12 cell">

<div style="float: right">
<input type="button" onclick="window.close();" value="Zamknij"/>
</div>

{% if template_okay %}
<p>Szablon przeanalizowany poprawnie. Poniżej znajdziesz listę {{ ile_ostatnich_prac }} ostatnio zmienionych rekordów
w BPP z opisem bibliograficznym wyrenderowanym za pomocą nowego szablonu. Po zweryfikowaniu,
czy wszystko działa zgodnie z zamierzeniami możesz zamknąć to okno i zapisać szablon --
wówczas nastąpi stopniowe przebudowanie wszystkich opisów bibliograficznych.
</p>
<ol>
{% for elem in lista_prac %}
<li>{{ elem }}</li>
{% endfor %}
</ol>
{% else %}
<p>
Wystąpił błąd podczas analizy szablonu.
</p><p>
Przyczyna: <strong>{{ exception }}</strong>
</p><p>
Popraw szablon i spróbuj ponownie.
</p>
{% endif %}
</div>
</div>
</div>
{% endblock %}
34 changes: 34 additions & 0 deletions src/bpp/tests/test_admin/test_templateadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,37 @@ def test_BppTemplateAdmin_zmiana_szablonu_zmienia_rekordy(

wydawnictwo_ciagle.refresh_from_db()
assert wydawnictwo_ciagle.opis_bibliograficzny_cache == WERSJA_2


@pytest.fixture
def typowy_szablon_opisu():
t, _ign = Template.objects.update_or_create(
name="opis_bibliograficzny.html",
defaults={"content": "123"},
)
SzablonDlaOpisuBibliograficznego.objects.update_or_create(
model=None,
template=t,
)
return t


@pytest.mark.django_db
def test_dbtemplates_TemplateAdmin_preview_good(
typowy_szablon_opisu, admin_client, wydawnictwo_ciagle
):

TEST_STR = b"456"
res = admin_client.get(
"/admin/dbtemplates/template/preview/", {"template": TEST_STR}, follow=True
)
assert TEST_STR in res.content


@pytest.mark.django_db
def test_dbtemplates_TemplateAdmin_preview_bad(typowy_szablon_opisu, admin_client):

res = admin_client.get(
"/admin/dbtemplates/template/preview/", {"template": "{% if foobar %}"}
)
assert "Wystąpił błąd" in res.rendered_content
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "admin/change_form.html" %}

{% block object-tools-items %}
{% if original.name == "opis_bibliograficzny.html" %}
<li>
<a onclick="window.open('../../preview/?template='+encodeURIComponent(django.jQuery('#id_content').val()), 'preview', 'width=600,height=400'); return false;"
>Szybki podgląd</a></li>
{% endif %}
{{ block.super }}
{% endblock %}

0 comments on commit 5b8324b

Please sign in to comment.