dms/jci/models.py

114 lines
3.5 KiB
Python

import re
from django.db import models
from django.utils.html import mark_safe
from markdown import markdown
class Section(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Meta:
ordering = ('name',)
class Chapter(models.Model):
name = models.CharField(max_length=255)
section = models.ForeignKey(Section, related_name='headlines')
acronym = models.CharField(max_length=10)
overview = models.TextField(max_length=2000, null=True, blank=True)
def __str__(self):
return '{} - {}'.format(self.acronym, self.name)
def number_of_associated_standards(self):
"""Returns the number of standards that are associated to the current chapter.
Returns:
An integer that counts the number of associated standards.
"""
return Standard.objects.filter(headline=self).count()
class Meta:
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 + ')'
def create_markdown_from_content(content):
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, content)
return mark_safe(markdown(modified_content, safe_mode='escape'))
class Intent(models.Model):
content = models.TextField(max_length=16000)
def __str__(self):
if len(self.content) > 50:
return self.content[0:50] + '...'
return None
def get_message_as_markdown(self):
return create_markdown_from_content(self.content)
class Standard(models.Model):
name = models.CharField(max_length=255)
headline = models.ForeignKey(Chapter, related_name='standards')
order = models.CharField(max_length=50)
parent = models.ForeignKey('self', null=True, blank=True)
require_written_procedure = models.BooleanField(default=False)
intent = models.ForeignKey(Intent, null=True, blank=True, related_name='standards')
@property
def structure(self):
return self.order
def number_of_measurables(self):
return self.measurables.count()
def __str__(self):
return '{}.{}'.format(self.headline.acronym, self.structure)
class Meta:
ordering = ('headline__acronym', 'order')
class Measurable(models.Model):
standard = models.ForeignKey(Standard, related_name='measurables')
order = models.IntegerField()
content = models.TextField(max_length=500)
def __str__(self):
return '{} ME {}'.format(self.standard, self.order)
def get_message_as_markdown(self):
return create_markdown_from_content(self.content)
class Meta:
unique_together = ('standard', 'order')
ordering = ('standard__headline__acronym', 'standard__order', 'order')