From: Jan Szejko Date: Tue, 11 Apr 2017 15:44:07 +0000 (+0200) Subject: tutorial and help texts for tags in new resource form X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/3263fb5558f69c727e61a6d4c0b3575a0f081ef6 tutorial and help texts for tags in new resource form --- diff --git a/apps/catalogue/forms.py b/apps/catalogue/forms.py index 6c704b96..e286a1d5 100644 --- a/apps/catalogue/forms.py +++ b/apps/catalogue/forms.py @@ -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, 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 index 00000000..26c07d7a --- /dev/null +++ b/apps/catalogue/migrations/0014_auto_20170411_1518.py @@ -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 index 00000000..f5ac7265 --- /dev/null +++ b/apps/catalogue/migrations/0015_auto_20170411_1545.py @@ -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), + ), + ] diff --git a/apps/catalogue/models/tag.py b/apps/catalogue/models/tag.py index 03dc1e56..b6341d01 100644 --- a/apps/catalogue/models/tag.py +++ b/apps/catalogue/models/tag.py @@ -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: diff --git a/apps/catalogue/templates/catalogue/document_create_missing.html b/apps/catalogue/templates/catalogue/document_create_missing.html index 4876d4ff..f86ce94b 100644 --- a/apps/catalogue/templates/catalogue/document_create_missing.html +++ b/apps/catalogue/templates/catalogue/document_create_missing.html @@ -27,61 +27,26 @@ {{ form.cover.errors }} {{ form.cover }} - - {{ form.language.errors }} - {{ form.publisher.errors }} - - {{ form.rights.errors }} - -
- - {{ form.audience.errors }} - - - {{ form.description.errors }} - {% for tag_form in tag_forms %} {% for tag_field in tag_form %} {{ tag_field.errors }} {{ tag_field }} +
{% endfor %} {% endfor %} + + {{ form.description.errors }} + + diff --git a/apps/catalogue/translation.py b/apps/catalogue/translation.py index 634a11bb..ae4b6c2a 100644 --- a/apps/catalogue/translation.py +++ b/apps/catalogue/translation.py @@ -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) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index d2e135c5..4c1a21ce 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -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, diff --git a/redakcja/static/js/catalogue/catalogue.js b/redakcja/static/js/catalogue/catalogue.js index a56afdb3..a750b77b 100755 --- a/redakcja/static/js/catalogue/catalogue.js +++ b/redakcja/static/js/catalogue/catalogue.js @@ -51,7 +51,15 @@ } }); - $('.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); + } + }); + });