From 28d2f5a68ef1d7295e5f57c3896087ada175b001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Sat, 9 Apr 2022 23:02:22 +0200 Subject: [PATCH] Add markdown support --- dcc/dcc/__init__.py | 21 +------------------ dcc/dcc/urls.py | 17 +++++++-------- dcc/dcc/utils.py | 20 ++++++++++++++++++ ...move_siteconfiguration_homepage_content.py | 17 +++++++++++++++ dcc/portal/models.py | 2 -- dcc/portal/templates/footer.html | 6 ++++-- dcc/portal/templates/home.html | 8 ++----- dcc/portal/templatetags/__init__.py | 0 dcc/portal/templatetags/markdown.py | 12 +++++++++++ dcc/portal/urls.py | 7 +++++++ 10 files changed, 71 insertions(+), 39 deletions(-) create mode 100644 dcc/portal/migrations/0005_remove_siteconfiguration_homepage_content.py create mode 100644 dcc/portal/templatetags/__init__.py create mode 100644 dcc/portal/templatetags/markdown.py create mode 100644 dcc/portal/urls.py diff --git a/dcc/dcc/__init__.py b/dcc/dcc/__init__.py index fab61a8..70c2159 100644 --- a/dcc/dcc/__init__.py +++ b/dcc/dcc/__init__.py @@ -1,23 +1,4 @@ -import os -import subprocess - - -def git_suffix(fname): - """ - :returns: `` if Git repository found - """ - try: - gh = subprocess.check_output( - ['git', 'rev-parse', '--short', 'HEAD'], - stderr=open(os.devnull, 'w')).strip() - gh = "-git" + gh.decode() if gh else '' - except Exception: - # trapping everything on purpose; git may not be installed or it - # may not work properly - gh = '' - - return gh - +from dcc.utils import git_suffix __version__ = '0.0.1' __version__ += git_suffix(__file__) diff --git a/dcc/dcc/urls.py b/dcc/dcc/urls.py index 278fe87..5e1471c 100644 --- a/dcc/dcc/urls.py +++ b/dcc/dcc/urls.py @@ -18,21 +18,20 @@ from django.conf.urls.static import static from django.contrib import admin from django.urls import include, path +from portal.utils import get_site_conf from portal.views import GetHome -from consist import urls as consist_urls -from roster import urls as roster_urls -from driver import urls as driver_urls -admin.site.site_header = "Trains assets manager" +site_conf = get_site_conf() +admin.site.site_header = site_conf.site_name urlpatterns = [ - path('', GetHome.as_view(), name='index'), - path('page/', GetHome.as_view(), name='index_pagination'), + path("", GetHome.as_view(), name="index"), + path("page/", include("portal.urls")), path("ht/", include("health_check.urls")), path("admin/", admin.site.urls), - path("api/v1/consist/", include(consist_urls)), - path("api/v1/roster/", include(roster_urls)), - path("api/v1/dcc/", include(driver_urls)), + path("api/v1/consist/", include("consist.urls")), + path("api/v1/roster/", include("roster.urls")), + path("api/v1/dcc/", include("driver.urls")), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # if settings.DEBUG: diff --git a/dcc/dcc/utils.py b/dcc/dcc/utils.py index e97769e..47266fb 100644 --- a/dcc/dcc/utils.py +++ b/dcc/dcc/utils.py @@ -1,7 +1,27 @@ +import os +import subprocess + from django.utils.html import format_html from django.utils.text import slugify as django_slugify +def git_suffix(fname): + """ + :returns: `` if Git repository found + """ + try: + gh = subprocess.check_output( + ['git', 'rev-parse', '--short', 'HEAD'], + stderr=open(os.devnull, 'w')).strip() + gh = "-git" + gh.decode() if gh else '' + except Exception: + # trapping everything on purpose; git may not be installed or it + # may not work properly + gh = '' + + return gh + + def get_image_preview(url): return format_html( '

Back to top

-

© {% now "Y" %} {{ site_conf.footer }}

-

{{ site_conf.footer_extended }}

+

© {% now "Y" %} {{ site_conf.footer | markdown | safe }}

+

{{ site_conf.footer_extended | markdown | safe }}

{% if site_conf.show_version %}
diff --git a/dcc/portal/templates/home.html b/dcc/portal/templates/home.html index 77c8dbf..97f4002 100644 --- a/dcc/portal/templates/home.html +++ b/dcc/portal/templates/home.html @@ -1,5 +1,6 @@ {% load static %} {% load solo_tags %} +{% load markdown %} {% get_solo 'portal.SiteConfiguration' as site_conf %} @@ -11,11 +12,8 @@ {{ site_conf.site_name }} - - - @@ -35,8 +33,6 @@ } } - - @@ -57,7 +53,7 @@

About

-

{{ site_conf.about | safe }}

+

{{ site_conf.about | markdown | safe }}

diff --git a/dcc/portal/templatetags/__init__.py b/dcc/portal/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dcc/portal/templatetags/markdown.py b/dcc/portal/templatetags/markdown.py new file mode 100644 index 0000000..690b818 --- /dev/null +++ b/dcc/portal/templatetags/markdown.py @@ -0,0 +1,12 @@ +from django import template +from django.template.defaultfilters import stringfilter + +import markdown as md + +register = template.Library() + + +@register.filter +@stringfilter +def markdown(value): + return md.markdown(value, extensions=['markdown.extensions.fenced_code']) diff --git a/dcc/portal/urls.py b/dcc/portal/urls.py new file mode 100644 index 0000000..1b0df49 --- /dev/null +++ b/dcc/portal/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from portal.views import GetHome + +urlpatterns = [ + path("", GetHome.as_view(), name='index_pagination'), +]