# 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
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()
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',
})
)
--- /dev/null
+# -*- 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),
+ ),
+ ]
--- /dev/null
+# -*- 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),
+ ),
+ ]
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:
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:
<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>
class TagTranslationOptions(TranslationOptions):
- fields = ('label',)
+ fields = ('label', 'help_text')
class CategoryTranslationOptions(TranslationOptions):
- fields = ('label',)
+ fields = ('label', 'tutorial')
translator.register(Tag, TagTranslationOptions)
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,
}
});
- $('.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);
+ }
+ });
+ });