2020-06-13 22:36:46 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2020-06-01 11:03:21 +02:00
|
|
|
from django.db import models
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.text import slugify
|
|
|
|
from simple_history.models import HistoricalRecords
|
|
|
|
|
|
|
|
from acros.models import Tag
|
2020-06-01 20:26:00 +02:00
|
|
|
from acros.utils.conversion import md_to_html
|
2020-06-01 11:03:21 +02:00
|
|
|
|
|
|
|
|
2020-06-13 22:36:46 +02:00
|
|
|
def valid_acronym(value: str):
|
|
|
|
if ":" in value:
|
|
|
|
raise ValidationError('Acronyms are not allowed to contain a ":"')
|
|
|
|
|
|
|
|
|
2020-06-01 11:03:21 +02:00
|
|
|
class Acronym(models.Model):
|
2020-06-13 22:36:46 +02:00
|
|
|
name = models.CharField(max_length=100, validators=[valid_acronym])
|
2020-06-01 11:03:21 +02:00
|
|
|
full_name = models.CharField(max_length=1000)
|
|
|
|
slug = models.SlugField(null=False, unique=True)
|
|
|
|
description_md = models.TextField(blank=True)
|
|
|
|
description_html = models.TextField(editable=False)
|
2020-06-08 20:33:29 +02:00
|
|
|
history = HistoricalRecords(excluded_fields=["created_date"])
|
2020-06-01 11:03:21 +02:00
|
|
|
tags = models.ManyToManyField(Tag, related_name="acronyms")
|
2020-07-18 19:21:01 +02:00
|
|
|
stub = models.BooleanField(default=True, help_text="check if this is a minimal entry that should be extended")
|
2020-06-08 20:33:29 +02:00
|
|
|
created_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified_date = models.DateTimeField(auto_now=True)
|
2020-06-01 11:03:21 +02:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.description_html = md_to_html(self.description_md)
|
|
|
|
if not self.slug:
|
|
|
|
self.slug = slugify(self.name)
|
|
|
|
super(Acronym, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('detail', args=[str(self.slug)])
|
|
|
|
|
2020-06-08 20:33:29 +02:00
|
|
|
def first_letter(self):
|
|
|
|
return self.name and self.name[0] or ''
|
|
|
|
|
2020-06-01 11:03:21 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ["name"]
|
2020-06-13 22:36:46 +02:00
|
|
|
|
|
|
|
def clean(self):
|
2020-06-14 16:21:39 +02:00
|
|
|
if not self.id:
|
|
|
|
new_slug = slugify(self.name)
|
|
|
|
try:
|
|
|
|
found = Acronym.objects.get(slug=new_slug)
|
|
|
|
except Acronym.DoesNotExist:
|
|
|
|
found = False
|
|
|
|
if found:
|
|
|
|
raise ValidationError(f"slug '{new_slug}' already exists")
|