Browse Source

Handle description/notes as markdown

Jocelyn Delande 9 years ago
parent
commit
3a6354c879

+ 2 - 0
costs/admin.py

@@ -10,11 +10,13 @@ from .models import (
 class GoodInline(admin.TabularInline):
     model = Good
     extra = 0
+    exclude = ['description_html']
 
 
 class CostInline(admin.TabularInline):
     model = Cost
     extra = 0
+    exclude = ['description_html']
 
 
 class ServiceInline(admin.TabularInline):

+ 52 - 0
costs/migrations/0011_auto_20160209_1032.py

@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.2 on 2016-02-09 09:32
+from __future__ import unicode_literals
+
+import costs.models
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('costs', '0010_service_reusable'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='document',
+            name='comment_html',
+            field=models.TextField(blank=True, editable=False),
+        ),
+        migrations.AlterField(
+            model_name='cost',
+            name='capacity_unit',
+            field=models.CharField(blank=True, choices=[('a', 'A'), ('mbps', 'Mbps'), ('u', 'U'), ('ipv4', 'IPv4'), ('eth', 'ports'), ('services', 'abonnement')], max_length=10),
+        ),
+        migrations.AlterField(
+            model_name='document',
+            name='comment',
+            field=models.TextField(blank=True, help_text='Texte brut ou markdown'),
+        ),
+        migrations.AlterField(
+            model_name='good',
+            name='capacity_unit',
+            field=models.CharField(blank=True, choices=[('a', 'A'), ('mbps', 'Mbps'), ('u', 'U'), ('ipv4', 'IPv4'), ('eth', 'ports'), ('services', 'abonnement')], max_length=10),
+        ),
+        migrations.AlterField(
+            model_name='service',
+            name='capacity_unit',
+            field=models.CharField(blank=True, choices=[('a', 'A'), ('mbps', 'Mbps'), ('u', 'U'), ('ipv4', 'IPv4'), ('eth', 'ports'), ('services', 'abonnement')], max_length=10),
+        ),
+        migrations.AlterField(
+            model_name='service',
+            name='reusable',
+            field=models.BooleanField(default=False, help_text="Peut-être utilisé par d'autres services"),
+        ),
+        migrations.AlterField(
+            model_name='serviceuse',
+            name='resource',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='dependent_services', to='costs.Service', validators=[costs.models.validate_reusable_service]),
+        ),
+    ]

+ 30 - 0
costs/migrations/0012_auto_20160209_1043.py

@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.2 on 2016-02-09 09:43
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('costs', '0011_auto_20160209_1032'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='cost',
+            name='description_html',
+            field=models.TextField(blank=True),
+        ),
+        migrations.AddField(
+            model_name='good',
+            name='description_html',
+            field=models.TextField(blank=True),
+        ),
+        migrations.AddField(
+            model_name='service',
+            name='description_html',
+            field=models.TextField(blank=True),
+        ),
+    ]

+ 12 - 1
costs/models.py

@@ -5,6 +5,7 @@ from django.conf import settings
 from django.core.exceptions import ValidationError
 from django.core.urlresolvers import reverse
 from django.db import models, transaction
+import markdown
 
 
 class Document(models.Model):
@@ -14,7 +15,8 @@ class Document(models.Model):
     TYPE_PLAN = 'plan'
 
     name = models.CharField(max_length=130)
-    comment = models.TextField(blank=True)
+    comment = models.TextField(blank=True, help_text="Texte brut ou markdown")
+    comment_html = models.TextField(blank=True, editable=False)
     date = models.DateField(default=datetime.datetime.now)
     type = models.CharField(max_length=10, choices=(
         (TYPE_FACT, 'relevé'),
@@ -24,6 +26,10 @@ class Document(models.Model):
     def __str__(self):
         return self.name
 
+    def save(self, *args, **kwargs):
+        self.comment_html = markdown.markdown(self.comment)
+        super().save(*args, **kwargs)
+
     def get_absolute_url(self):
         return reverse('detail-document', kwargs={'pk': self.pk})
 
@@ -74,11 +80,16 @@ class Document(models.Model):
 class AbstractItem(models.Model):
     name = models.CharField(max_length=130)
     description = models.TextField(blank=True)
+    description_html = models.TextField(blank=True)
     document = models.ForeignKey(Document)
 
     def __str__(self):
         return self.name
 
+    def save(self, *args, **kwargs):
+        self.description_html = markdown.markdown(self.description)
+        super().save(*args, **kwargs)
+
     class Meta:
         abstract = True
 

+ 2 - 2
costs/templates/costs/document_detail.html

@@ -5,12 +5,12 @@
 
 {% block rail %}
 
-{% if document.comment %}
+{% if document.comment_html %}
 
 <div class="ui segment">
   <p>
     <a class="ui ribbon left label">Notes</a>
-{{ document.comment|linebreaks }}
+{{ document.comment_html|safe }}
   </p>
 </div>
 {% endif %}

+ 1 - 1
costs/templates/costs/service_detail.html

@@ -26,7 +26,7 @@
   <div class="ui segment" style="width: 100%">
   <p>
     <a class="ui ribbon left label">Notes</a>
-{{ service.description|linebreaks }}
+{{ service.description_html|safe }}
   </p>
   </div>
   {% if request.user.is_staff %}

+ 1 - 0
requirements.txt

@@ -1,2 +1,3 @@
 Django==1.9.2
 PyYAML==3.11
+Markdown==2.6.5