Skip to content

Commit

Permalink
add book's map creation
Browse files Browse the repository at this point in the history
  • Loading branch information
astynax committed May 27, 2020
1 parent c92e64e commit fad6805
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 47 deletions.
8 changes: 7 additions & 1 deletion djaif/book/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

from djaif.book import models


class Admin(admin.ModelAdmin):
filter_horizontal = ('items',)

admin.site.register(models.Book)

class BookAdmin(admin.ModelAdmin):
change_form_template = 'admin/book_change_form.html'


admin.site.register(models.Book, BookAdmin)
admin.site.register(models.BookPage, Admin)
admin.site.register(models.PageLink, Admin)
admin.site.register(models.BookProgress)
Expand Down
7 changes: 7 additions & 0 deletions djaif/book/templates/admin/book_change_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "admin/change_form.html" %}
{% load i18n admin_urls %}
{% block after_related_objects %}
<a target="_blank" href="/book/{{ original.pk }}/map.svg">
<img src="/book/{{ original.pk }}/map.svg"/>
</a>
{% endblock %}
1 change: 1 addition & 0 deletions djaif/book/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
views.take_item,
name='take',
),
path('book/<int:book_id>/map.svg', views.view_book_map),
]
38 changes: 38 additions & 0 deletions djaif/book/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from functools import wraps

from graphviz import Digraph

from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.http import FileResponse

from djaif.book import models

Expand Down Expand Up @@ -83,3 +86,38 @@ def take_item(request, progress, book_id, page_id, item_id):
return redirect(
reverse('page', kwargs={'book_id': book_id, 'page_id': page_id}),
)


def view_book_map(request, book_id):
book = get_object_or_404(models.Book, id=book_id)

g = Digraph('Map', filename='map.gv', directory='/tmp')

def pid(page):
return 'page_{id}'.format(id=page.id)

for page in book.bookpage_set.all():
g.node(
pid(page),
label='\n'.join(
[str(page.id), page.title] + [
i.name for i in page.items.all()
]
),
tooltip=page.body,
href='/admin/book/bookpage/{}/change'.format(page.id),
)

for link in models.PageLink.objects.filter(
from_page__book_id=book_id,
).all():
g.edge(pid(link.from_page), pid(link.to_page), label='\n'.join(
[str(link.id), link.name[:10]] + [
i.name for i in link.items.all()
]),
labeltooltip=link.name,
labelhref='/admin/book/pagelink/{}/change'.format(link.id),
)

g.render(quiet=True, view=False, format='svg')
return FileResponse(open('/tmp/map.gv.svg', 'rb'))
63 changes: 18 additions & 45 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ authors = ["Your Name <[email protected]>"]
python = "^3.6"
django = "^3.0.5"
Pillow = "^7.1.1"
graphviz = "^0.14"

[tool.poetry.dev-dependencies]
wemake-python-styleguide = "^0.14.0"
Expand Down
8 changes: 7 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ exclude =
*.egg
**/migrations/**

ignore = D100, D101, D103, D105, D106, WPS326, WPS306, WPS317
ignore =
# documentation isn't so important for now
D100, D101, D102, D103, D105, D106,
# yes, we have bad names here and there
WPS110, WPS111,
# annoying stuff
WPS326, WPS306, WPS317,

per-file-ignores =
settings.py: WPS407, E501, C812, WPS221, WPS226

0 comments on commit fad6805

Please sign in to comment.