tutorial and help texts for tags in new resource form
authorJan Szejko <janek37@gmail.com>
Tue, 11 Apr 2017 15:44:07 +0000 (17:44 +0200)
committerJan Szejko <janek37@gmail.com>
Tue, 11 Apr 2017 15:44:07 +0000 (17:44 +0200)
apps/catalogue/forms.py
apps/catalogue/migrations/0014_auto_20170411_1518.py [new file with mode: 0644]
apps/catalogue/migrations/0015_auto_20170411_1545.py [new file with mode: 0644]
apps/catalogue/models/tag.py
apps/catalogue/templates/catalogue/document_create_missing.html
apps/catalogue/translation.py
apps/catalogue/views.py
redakcja/static/js/catalogue/catalogue.js

index 6c704b9..e286a1d 100644 (file)
@@ -3,6 +3,10 @@
 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
+from django.utils.encoding import force_text
+from django.utils.html import format_html
+from django.utils.safestring import mark_safe
+
 from catalogue.models import Category
 from catalogue.models import Tag
 from django import forms
@@ -38,12 +42,19 @@ class DocumentCreateForm(forms.Form):
 
 
 class TagForm(forms.Form):
-    def __init__(self, category, instance=None, *args, **kwargs):
+    def __init__(self, category, instance=None, tutorial_no=None, *args, **kwargs):
         super(TagForm, self).__init__(*args, **kwargs)
         self.category = category
         self.instance = instance
         self.field().queryset = Tag.objects.filter(category=self.category)
         self.field().label = self.category.label.capitalize()
+        if tutorial_no and category.tutorial:
+            self.field().widget.attrs.update({
+                'data-toggle': 'tutorial',
+                'data-tutorial': str(tutorial_no),
+                'data-placement': 'bottom',
+                'data-content': category.tutorial,
+            })
         if self.instance:
             self.field().initial = self.initial()
 
@@ -62,10 +73,32 @@ class TagForm(forms.Form):
         raise NotImplementedError
 
 
+class TagSelect(forms.Select):
+    def render_option(self, selected_choices, option_value, option_label):
+        if option_value is None:
+            option_value = ''
+        help_html = ''
+        if option_value:
+            tag = Tag.objects.get(id=int(option_value))
+            if tag.help_text:
+                help_html = mark_safe(' data-help="%s"' % tag.help_text)
+        option_value = force_text(option_value)
+        if option_value in selected_choices:
+            selected_html = mark_safe(' selected="selected"')
+            if not self.allow_multiple_selected:
+                # Only allow for a single selection.
+                selected_choices.remove(option_value)
+        else:
+            selected_html = ''
+        return format_html(
+            u'<option value="{}"{}{}>{}</option>',
+            option_value, selected_html, help_html, force_text(option_label))
+
+
 class TagSingleForm(TagForm):
     tag = forms.ModelChoiceField(
         Tag.objects.none(),
-        widget=forms.Select(attrs={
+        widget=TagSelect(attrs={
             'class': 'form-control',
         })
     )
diff --git a/apps/catalogue/migrations/0014_auto_20170411_1518.py b/apps/catalogue/migrations/0014_auto_20170411_1518.py
new file mode 100644 (file)
index 0000000..26c07d7
--- /dev/null
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.6 on 2017-04-11 15:18
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('catalogue', '0013_auto_20170207_1434'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='category',
+            name='tutorial',
+            field=models.CharField(blank=True, max_length=250),
+        ),
+        migrations.AddField(
+            model_name='category',
+            name='tutorial_en',
+            field=models.CharField(blank=True, max_length=250, null=True),
+        ),
+        migrations.AddField(
+            model_name='category',
+            name='tutorial_pl',
+            field=models.CharField(blank=True, max_length=250, null=True),
+        ),
+    ]
diff --git a/apps/catalogue/migrations/0015_auto_20170411_1545.py b/apps/catalogue/migrations/0015_auto_20170411_1545.py
new file mode 100644 (file)
index 0000000..f5ac726
--- /dev/null
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.6 on 2017-04-11 15:45
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('catalogue', '0014_auto_20170411_1518'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='tag',
+            name='help_text',
+            field=models.CharField(blank=True, max_length=250),
+        ),
+        migrations.AddField(
+            model_name='tag',
+            name='help_text_en',
+            field=models.CharField(blank=True, max_length=250, null=True),
+        ),
+        migrations.AddField(
+            model_name='tag',
+            name='help_text_pl',
+            field=models.CharField(blank=True, max_length=250, null=True),
+        ),
+    ]
index 03dc1e5..b6341d0 100644 (file)
@@ -13,6 +13,7 @@ class Category(models.Model):
     label = models.CharField(max_length=64, verbose_name=_('category'))
     dc_tag = models.CharField(max_length=32)
     multiple = models.BooleanField(default=False, verbose_name=_('multiple choice'))
+    tutorial = models.CharField(max_length=250, blank=True)
     index = models.IntegerField()
 
     class Meta:
@@ -28,6 +29,7 @@ class Tag(models.Model):
     label = models.CharField(max_length=64, verbose_name=_('tag'))
     dc_value = models.CharField(max_length=32)
     category = models.ForeignKey(Category)
+    help_text = models.CharField(max_length=250, blank=True)
     index = models.IntegerField()
 
     class Meta:
index 4876d4f..f86ce94 100644 (file)
         <label for="title">{% trans "Cover image" %}</label>
             {{ form.cover.errors }}
             {{ form.cover }}
-        <label for="language">{% trans "Language" %}</label>
-            {{ form.language.errors }}
-            <input class="form-control" name="language" id="language" type="text" value='{{ form.language.value|default:"" }}'>
         <label for="publisher">{% trans "Publisher" %}</label>
             {{ form.publisher.errors }}
             <input class="form-control" name="publisher" id="publisher" type="text" value='{{ form.publisher.value|default:"" }}'>
-        <label for="rights">{% trans "Rights" %}</label>
-            {{ form.rights.errors }}
-            <select class="form-control" name="rights" id="rights"
-                data-toggle="tutorial" data-tutorial="2" data-placement="bottom"
-                data-content="{% trans 'You should choose a free license for your resource. We recommend using Creative Commons Attribution - Share Alike.' %}"
-            >
-                <option value=''>–</option>
-                <option name='pd' 
-                    data-help="{% trans 'Only set for resources that are not restricted with copyright.' %}"
-                    >{% trans "public domain" %}</option>
-                <option name='cc-by'
-                    data-help="{% trans "Non-copyleft free culture license. See <a target='_blank' href='//creativecommons.org/choose/'>creativecommons.org</a>" %}"
-                    >{% trans "Creative Commons Attribution" %}</option>
-                <option name='cc-by-sa'
-                    data-help="{% trans "Copyleft free culture license. See <a target='_blank' href='//creativecommons.org/choose/'>creativecommons.org</a>" %}"
-                    >{% trans "Creative Commons Attribution – Share Alike" %}</option>
-                <option name='fal'
-                    data-help="{% trans "Copyleft free culture license. See <a target='_blank' href='http://artlibre.org/'>artlibre.org</a>" %}"
-                    >{% trans "Free Art License" %}</option>
-            </select>
-            <div class="help-text" style="text-align: right;"></div>
-        <label for="audience">{% trans "Audience" %}</label>
-            {{ form.audience.errors }}
-            <select class="form-control" name="audience" id="audience"
-                data-toggle="tutorial" data-tutorial="3" data-placement="bottom"
-                data-content="{% trans 'Choose primary audience for your resource.' %}"
-            >
-                <option>3-6</option>
-                <option>6-9</option>
-                <option>9-12</option>
-                <option>12-18</option>
-                <option>18+</option>
-                <option>Adults</option>
-            </select>
 
-        <label for="description">{% trans "Summary" %}</label>
-            {{ form.description.errors }}
-            <textarea class="form-control" name="description" id="description"
-                data-toggle="tutorial" data-tutorial="4" data-placement="top"
-                data-content="{% trans 'You can provide a short description of the document here.' %}"
-            >{{ form.description.value|default:"" }}</textarea>
         {% for tag_form in tag_forms %}
             {% for tag_field in tag_form %}
                 <label for="id_{{ tag_form.prefix }}-{{ tag_field.name }}">{{ tag_field.label }}</label>
                 {{ tag_field.errors }}
                 {{ tag_field }}
+                <div class="help-text" style="text-align: right;"></div>
             {% endfor %}
         {% endfor %}
 
+        <label for="description">{% trans "Summary" %}</label>
+            {{ form.description.errors }}
+            <textarea class="form-control" name="description" id="description"
+                data-toggle="tutorial" data-tutorial="100" data-placement="top"
+                data-content="{% trans 'You can provide a short description of the document here.' %}"
+            >{{ form.description.value|default:"" }}</textarea>
+
         <button style="margin-top:1em;" class="btn btn-default" type="submit">{% trans "Create resource" %}</button>
     </form>
 
index 634a11b..ae4b6c2 100644 (file)
@@ -9,11 +9,11 @@ from catalogue.models import Tag, Category
 
 
 class TagTranslationOptions(TranslationOptions):
-    fields = ('label',)
+    fields = ('label', 'help_text')
 
 
 class CategoryTranslationOptions(TranslationOptions):
-    fields = ('label',)
+    fields = ('label', 'tutorial')
 
 
 translator.register(Tag, TagTranslationOptions)
index d2e135c..4c1a21c 100644 (file)
@@ -148,8 +148,9 @@ def create_missing(request):
         form = forms.DocumentCreateForm(initial={'owner_organization': org})
 
         tag_forms = [
-            (TagMultipleForm if category.multiple else TagSingleForm)(category=category, prefix=category.dc_tag)
-            for category in Category.objects.all()]
+            (TagMultipleForm if category.multiple else TagSingleForm)(
+                category=category, tutorial_no=i, prefix=category.dc_tag)
+            for i, category in enumerate(Category.objects.all(), start=2)]
 
     return render(request, "catalogue/document_create_missing.html", {
         "form": form,
index a56afdb..a750b77 100755 (executable)
             }
         });
 
-        $('.chosen-select').chosen();
+        $('.chosen-select').chosen().each(function() {
+            var widget = $(this.nextSibling), $t = $(this);
+            $.each($.merge([], this.attributes), function() {
+                if (this.name.substr(0, 5) === 'data-') {
+                    $t.removeAttr(this.name);
+                    widget.attr(this.name, this.value);
+                }
+            });
+        });