Browse Source

Merge branch 'feat-AbstractUse-help-text' of daimrod/transparency into master

jocelyn 6 years ago
parent
commit
05cf25034c
4 changed files with 83 additions and 3 deletions
  1. 12 3
      costs/admin.py
  2. 49 0
      costs/migrations/0014_auto_20190406_2027.py
  3. 3 0
      costs/models.py
  4. 19 0
      costs/static/edit_help.js

+ 12 - 3
costs/admin.py

@@ -4,8 +4,9 @@ from django.utils.encoding import force_text
 from django.utils.safestring import mark_safe
 
 from .models import (
-    Document, Cost, Good, CostUse, GoodUse, Service, ServiceUse)
+    Document, Cost, Good, AbstractUse, CostUse, GoodUse, Service, ServiceUse)
 
+import markdown
 
 class GoodInline(admin.TabularInline):
     model = Good
@@ -58,10 +59,19 @@ class DocumentAdmin(admin.ModelAdmin):
                         new, edit_url)))
     copy.short_description = 'Copier'
 
-
 class AbstractUseInline(admin.TabularInline):
     """ An inline with some knowledge of the currently edited Document
     """
+    fields = ['share', 'help', 'view_help', 'resource']
+    readonly_fields = ['view_help']
+
+    class Media:
+        js = ("edit_help.js",)
+
+    def view_help(self, obj):
+        return mark_safe(markdown.markdown(getattr(obj, 'help', '')
+                         + ''' <a class='edit_help' title='Editer le text'><img src="/static/admin/img/icon-changelink.svg" alt="Modifier"></a>'''))
+
     def formfield_for_foreignkey(self, db_field, request, **kwargs):
         if db_field.name == "resource" and getattr(request, 'document', None):
             kwargs["queryset"] = db_field.related_model.objects.filter(
@@ -69,7 +79,6 @@ class AbstractUseInline(admin.TabularInline):
         return super().formfield_for_foreignkey(
             db_field, request, **kwargs)
 
-
 class GoodUseInline(AbstractUseInline):
     model = GoodUse
     extra = 1

+ 49 - 0
costs/migrations/0014_auto_20190406_2027.py

@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.2 on 2019-04-06 18:27
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('costs', '0013_auto_20161107_1333'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='document',
+            options={'ordering': ['-date'], 'verbose_name': 'Rapport'},
+        ),
+        migrations.AddField(
+            model_name='costuse',
+            name='help',
+            field=models.TextField(blank=True, help_text='Texte brut ou markdown', verbose_name='Aide'),
+        ),
+        migrations.AddField(
+            model_name='gooduse',
+            name='help',
+            field=models.TextField(blank=True, help_text='Texte brut ou markdown', verbose_name='Aide'),
+        ),
+        migrations.AddField(
+            model_name='serviceuse',
+            name='help',
+            field=models.TextField(blank=True, help_text='Texte brut ou markdown', verbose_name='Aide'),
+        ),
+        migrations.AlterField(
+            model_name='document',
+            name='type',
+            field=models.CharField(choices=[('fact', 'Rapport public'), ('plan', 'Rapport brouillon')], help_text="Un rapport brouillon n'est pas visible publiquement", max_length=10),
+        ),
+        migrations.AlterField(
+            model_name='service',
+            name='internal',
+            field=models.BooleanField(default=False, help_text="Ne peut être vendu tel quel, n'apparaît pas dans la liste des services", verbose_name='Service interne'),
+        ),
+        migrations.AlterField(
+            model_name='service',
+            name='reusable',
+            field=models.BooleanField(default=False, help_text="Peut-être utilisé par d'autres services", verbose_name="Ré-utilisable par d'autres services"),
+        ),
+    ]

+ 3 - 0
costs/models.py

@@ -247,6 +247,9 @@ class Good(AbstractResource):
 class AbstractUse(models.Model):
     share = models.FloatField()
     service = models.ForeignKey('Service')
+    help = models.TextField(
+        'Aide',
+        blank=True, help_text="Texte brut ou markdown")
 
     class Meta:
         abstract = True

+ 19 - 0
costs/static/edit_help.js

@@ -0,0 +1,19 @@
+function edit_help_event(event) {
+    var tr = django.jQuery(event.target).closest('tr');
+    var view_help = tr.find('.field-view_help');
+    view_help.hide();
+    var help = tr.find('.field-help');
+    help.show();
+}
+
+django.jQuery().ready(function() {
+    // Hide all help field
+    django.jQuery(".field-help").hide();
+
+    // Hide header of help field
+    django.jQuery("body").find(".field-view_help").closest("table").find("th:nth-child(3)").hide();
+
+    // Add handler on edit button
+    django.jQuery(".edit_help").click(edit_help_event);
+});
+