Adding Commonmark and a template tag for that. We're going to use it in the shop app as well, so it's in its own app now (utils).

This commit is contained in:
Víðir Valberg Guðmundsson 2016-05-30 21:51:17 +02:00 committed by Víðir Valberg Guðmundsson
parent e8bda33ac3
commit a4b578642d
12 changed files with 41 additions and 14 deletions

View file

@ -28,6 +28,7 @@ INSTALLED_APPS = [
'camps',
'shop',
'news',
'utils',
'allauth',
'allauth.account',

View file

@ -2,7 +2,7 @@ import datetime
from django.db import models
from django.utils.translation import ugettext_lazy as _
from bornhack.utils import CreatedUpdatedModel, UUIDModel
from utils.models import UUIDModel, CreatedUpdatedModel
from .managers import CampQuerySet

View file

@ -2,7 +2,7 @@ from __future__ import unicode_literals
from django.db import models
from bornhack.utils import CreatedUpdatedModel
from utils.models import CreatedUpdatedModel
from news.managers import NewsItemQuerySet

View file

@ -1,11 +1,12 @@
{% extends 'base.html' %}
{% load commonmark %}
{% block content %}
{% for item in news_items %}
<div>
<h3>{{ item.title }} <small>{{ item.published_at|date:"Y-m-d" }}</small></h3>
</div>
{{ item.content }}
{{ item.content|commonmark }}
{% if not forloop.last %}
<hr />
{% endif %}

View file

@ -4,7 +4,7 @@ from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
from bornhack.utils import CreatedUpdatedModel, UUIDModel
from utils.models import UUIDModel, CreatedUpdatedModel
class Profile(CreatedUpdatedModel, UUIDModel):

View file

@ -6,4 +6,5 @@ psycopg2>=2.6.1
PyPDF2>=1.25.1
django-wkhtmltopdf>=3.0.0
Pillow==3.2.0
qrcode==5.3
qrcode==5.3
CommonMark==0.6.4

View file

@ -6,7 +6,7 @@ from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.core.urlresolvers import reverse_lazy
from bornhack.utils import CreatedUpdatedModel, UUIDModel
from utils.models import UUIDModel, CreatedUpdatedModel
from .managers import ProductQuerySet
import hashlib, io, base64, qrcode

0
utils/__init__.py Normal file
View file

7
utils/apps.py Normal file
View file

@ -0,0 +1,7 @@
from __future__ import unicode_literals
from django.apps import AppConfig
class UtilsConfig(AppConfig):
name = 'utils'

View file

@ -3,14 +3,6 @@ import uuid
from django.db import models
class CreatedUpdatedModel(models.Model):
class Meta:
abstract = True
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class UUIDModel(models.Model):
class Meta:
abstract = True
@ -20,3 +12,11 @@ class UUIDModel(models.Model):
default=uuid.uuid4,
editable=False,
)
class CreatedUpdatedModel(models.Model):
class Meta:
abstract = True
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

View file

View file

@ -0,0 +1,17 @@
import CommonMark
from django import template
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter(is_safe=True)
def commonmark(value):
parser = CommonMark.Parser()
renderer = CommonMark.HtmlRenderer()
ast = parser.parse(force_text(value))
return mark_safe(
force_text(renderer.render(ast))
)