regex for intent display

This commit is contained in:
Fred Pauchet 2018-02-24 21:13:53 +01:00
parent 43ed342bcd
commit 93cdb8d31f
11 changed files with 62 additions and 17 deletions

View File

@ -1,3 +1,5 @@
import re
from django.db import models
from django.utils.html import mark_safe
from markdown import markdown
@ -34,6 +36,25 @@ class Chapter(models.Model):
ordering = ('name',)
def create_link(matchobj):
splitted = [x.strip() for x in matchobj.group(0).split(',')]
acrronym = splitted[0].split('.')[0]
order = '.'.join(splitted[0].split('.')[1:])
print('searching', acrronym, order)
try:
std_id = Standard.objects.get(headline__acronym=acrronym, order=order).pk
except Standard.DoesNotExist:
return matchobj.group(0)
if len(splitted) > 1:
measurable = splitted[1].replace('ME', '').strip()
else:
measurable = ''
return '[' + matchobj.group(0) + '](/jci/standard/' + str(std_id) + '?measurable=' + measurable + ')'
class Intent(models.Model):
content = models.TextField(max_length=16000)
@ -43,7 +64,11 @@ class Intent(models.Model):
return None
def get_message_as_markdown(self):
return mark_safe(markdown(self.content, safe_mode='escape'))
str_list = [x for x in Chapter.objects.values_list('acronym', flat=True)]
regex_query = "(" + '|'.join(str_list) + ")(\.[0-9])*(\, ME [0-9])?"
modified_content = re.sub(regex_query, create_link, self.content)
return mark_safe(markdown(modified_content, safe_mode='escape'))
class Standard(models.Model):

View File

@ -1,7 +1,7 @@
{% extends "../base.html" %}
{% block "content" %}
{{ intent.content }}
{{ intent.get_message_as_markdown | safe }}
{% if intent.standards.all %}
<ul>

View File

@ -1,16 +1,19 @@
{% extends "base.html" %}
{% load jci_extras %}
{% block "content" %}
{% if sections %}
{% for section in sections %}
<h2>{{section}}</h2>
{% if section.headlines.all %}
{% for headline in section.headlines.all %}
<h2>{{ headline }}</h2>
<h3>{{ headline }}</h3>
{% if headline.standards.all %}
<table>
{% for std in headline.standards.all %}
<tr>
<td>{{std}}</td>
<td><a href="{% url 'standard_details' std.pk %}">{{std}}</a></td>
<td>{{std.name}}</td>
<td><a href="{% url 'standard_documents' std.pk %}">{% count_documents std %}</a></td>
</tr>
@ -20,4 +23,6 @@
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endblock %}

View File

@ -1,8 +1,10 @@
{% extends "../base.html" %}
{% block "content" %}
{{ standard }}
<h2>{{ standard }}</h2>
{% if standard.intent %}
<h2><a href="{% url 'intent_details' standard.intent.id %}">Intent</a></h2>
{{ standard.intent.content }}
{% endif %}
{% endblock %}

View File

@ -0,0 +1,5 @@
<ul>
{% for section in sections %}
<li><a href="{% url 'edit_document_section' document_id section.order%}">{{ section }}</a></li>
{% endfor %}
</ul>

View File

@ -1,7 +1,8 @@
{% load document_edition %}
{% extends "../base.html" %}
{% block "navigation" %}
{% display_sections %}
{% endblock %}
{% block "content" %}

View File

@ -1,4 +1,4 @@
{% extends "../base.html" %}
{% extends "base.html" %}
{% block "content" %}
<h2>Edit section {{ section }}</h2>

View File

@ -1,10 +1,6 @@
{% extends "../base.html" %}
{% extends "base.html" %}
{% block "content" %}
<h2>Sections</h2>
<ul>
{% for section in sections %}
<li><a href="{% url 'edit_document_section' document_id section.order%}">{{ section }}</a></li>
{% endfor %}
</ul>
Meh
{% endblock %}

View File

View File

@ -0,0 +1,11 @@
from django import template
from ..models import Section
register = template.Library()
@register.inclusion_tag('templatetags/writer_sections.html')
def display_sections(document_id):
sections = Section.objects.filter(document_id=document_id)
return {'sections_list': sections}

View File

@ -4,6 +4,6 @@ from .views import list_sections, edit_section
urlpatterns = [
url(r'document/(?P<document_id>\d+)/section/(?P<section_id>\d+)', edit_section, name='edit_document_section'),
url(r'document/(?P<document_id>\d+)', list_sections, name='document_sections'),
url(r'doc/(?P<document_id>\d+)/section/(?P<section_id>\d+)', edit_section, name='edit_document_section'),
url(r'doc/(?P<document_id>\d+)', list_sections, name='document_sections'),
]