Skip to content

Commit

Permalink
Merge pull request #71 from ProjectTIER/software-ex-tag
Browse files Browse the repository at this point in the history
Add software tag to exercises section, fixes #68
  • Loading branch information
alexgleason committed Nov 5, 2018
2 parents f76d585 + f34c119 commit 6c8ab1b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
33 changes: 33 additions & 0 deletions project_tier/exercises/migrations/0008_auto_20181105_1140.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.0.4 on 2018-11-05 16:40

from django.db import migrations, models
import django.db.models.deletion
import modelcluster.contrib.taggit
import modelcluster.fields


class Migration(migrations.Migration):

dependencies = [
('taggit', '0002_auto_20150616_2121'),
('exercises', '0007_exerciseindexpage_notes'),
]

operations = [
migrations.CreateModel(
name='SoftwareTag',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content_object', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='software_tags_relationship', to='exercises.ExercisePage')),
('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='exercises_softwaretag_items', to='taggit.Tag')),
],
options={
'abstract': False,
},
),
migrations.AddField(
model_name='exercisepage',
name='software_tags',
field=modelcluster.contrib.taggit.ClusterTaggableManager(blank=True, help_text='A comma-separated list of tags.', related_name='+', through='exercises.SoftwareTag', to='taggit.Tag', verbose_name='software tags'),
),
]
23 changes: 22 additions & 1 deletion project_tier/exercises/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
changing the actual tagging API within Wagtail or django-taggit.
"""


class DisciplineTag(TaggedItemBase):
content_object = ParentalKey(
'exercises.ExercisePage',
Expand Down Expand Up @@ -59,6 +60,15 @@ class ProtocolTag(TaggedItemBase):
)


class SoftwareTag(TaggedItemBase):
content_object = ParentalKey(
'exercises.ExercisePage',
# django-modelcluster requires this to be set
related_name='software_tags_relationship',
on_delete=models.CASCADE
)


class ExercisePage(Page):
cover_sheet = models.ForeignKey(
'wagtaildocs.Document',
Expand Down Expand Up @@ -120,6 +130,13 @@ class ExercisePage(Page):
blank=True,
verbose_name="protocol tags"
)
software_tags = ClusterTaggableManager(
through=SoftwareTag,
# disabling reverse accessors solves a naming clash
related_name="+",
blank=True,
verbose_name="software tags"
)

parent_page_types = ['exercises.ExerciseIndexPage']

Expand All @@ -130,6 +147,7 @@ class ExercisePage(Page):
FieldPanel('discipline_tags'),
FieldPanel('course_level_tags'),
FieldPanel('protocol_tags'),
FieldPanel('software_tags'),
],
heading="tags",
),
Expand Down Expand Up @@ -170,13 +188,16 @@ def get_context(self, request):
exercises_courseleveltag_items__isnull=False).distinct()
context['protocol_tags'] = Tag.objects.filter(
exercises_protocoltag_items__isnull=False).distinct()
context['software_tags'] = Tag.objects.filter(
exercises_softwaretag_items__isnull=False).distinct()

# Filters exercises by the tag name in the URL
tag_groups = [
# (The name in the URL, the name of the Python model attribute)
('disciplines', 'discipline'),
('course-levels', 'course_level'),
('protocols', 'protocol')
('protocols', 'protocol'),
('softwares', 'software'),
]
# Loop through the given tag groups from above
for tag_group in tag_groups:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ <h6>Protocol</h6>
</div>
{% endif %}

{% if software_tags %}
<div class="tag-group">
<h6>Software</h6>
{% for tag in software_tags %}
<div class="tag">
<label>
<input class="tag-checkbox" data-tag="softwares" name="{{ tag }}" type="checkbox"{% if tag.name in software_tags_checked %} checked{% endif %}>
<span class="tag-name">{{ tag }}</span>
</label>
</div>
{% endfor %}
</div>
{% endif %}

{% if results.filtered %}
<a class="clear-button" href="{{ page.url }}">
<i class="fa fa-times-circle"></i> Clear filters
Expand Down Expand Up @@ -88,6 +102,12 @@ <h6>Protocol</h6>
{% endfor %}
{% endif %}

{% if software_tags_checked %}
{% for tag in software_tags_checked %}
<span class="tag-indicator">{{ tag }}</span>
{% endfor %}
{% endif %}

<a href="{{ page.url }}">Clear all filters <i class="fa fa-times"></i></a>
</p>

Expand Down Expand Up @@ -118,6 +138,9 @@ <h2>{{ exercise.title }}</h2>
{% for tag in exercise.specific.protocol_tags.all %}
<span class="tag" href="?protocols={{ tag }}">{{ tag }}</span>
{% endfor %}
{% for tag in exercise.specific.software_tags.all %}
<span class="tag" href="?softwares={{ tag }}">{{ tag }}</span>
{% endfor %}
</div>
<p class="excerpt">{{ exercise.specific.listing_excerpt|truncatechars_html:100 }}</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion project_tier/static/js/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function serializeFilters() {
var tags = $('.exercise-filter input[type="checkbox"]:checked');
// Loop each tag type and build the query string
var queryString = '?';
var tagTypes = ['disciplines', 'course-levels', 'protocols'];
var tagTypes = ['disciplines', 'course-levels', 'protocols', 'softwares'];
for(var i=0; i<tagTypes.length; i++) {
var tagType = tagTypes[i];
var filteredTags = tags.filter('[data-tag="' + tagType + '"]');
Expand Down

0 comments on commit 6c8ab1b

Please sign in to comment.