Skip to content

Commit fecbd2f

Browse files
committed
activated tag functionality in the front-end
1 parent c2f31b2 commit fecbd2f

File tree

15 files changed

+209
-10
lines changed

15 files changed

+209
-10
lines changed

memorymap_toolkit/settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'mmt_api',
4040
# 3rd Party
4141
'easy_thumbnails',
42+
'rest_framework',
4243
'filer',
4344
'mptt',
4445
'ckeditor',

mmt_api/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
path('1.0/features/<str:source_layer>/<int:pk>', views.feature, name='feature'),
1313
path('1.0/features/search/', views.search_features, name='search_features'),
1414
path('1.0/features/theme/', views.get_features_by_theme, name='get_features_by_theme'),
15+
path('1.0/features/tag/', views.get_features_by_tag, name='get_features_by_tag'),
1516
path('1.0/features/attachments/documents/<int:pk>', views.document, name='document'),
1617
path('1.0/features/<str:source_layer>/<int:pk>/attachments/', views.feature_attachments, name='attachments'),
1718
path('1.0/pages/<str:slug>', views.page, name='page'),

mmt_api/views.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,56 @@ def get_features_by_theme(request):
202202

203203

204204

205+
@api_view()
206+
def get_features_by_tag(request):
207+
"""
208+
Returns a JSON list of features in a particular theme.
209+
"""
210+
211+
try:
212+
tags = request.GET['tags'].split(',')
213+
print(tags)
214+
except:
215+
return Response('No tags', status=status.HTTP_404_NOT_FOUND)
216+
217+
218+
219+
points = Point.objects.filter(tags__name__in=tags)
220+
lines = Line.objects.filter(tags__name__in=tags)
221+
polygons = Polygon.objects.filter(tags__name__in=tags)
222+
223+
points_serializer = PointSerializer(points, many=True)
224+
lines_serializer = LineSerializer(lines, many=True)
225+
polygons_serializer = PolygonSerializer(polygons, many=True)
226+
227+
points_data = points_serializer.data
228+
lines_data = lines_serializer.data
229+
polygons_data = polygons_serializer.data
230+
231+
features = points_data['features'] + lines_data['features'] + polygons_data['features']
232+
sorted_features = sorted(features, key=lambda x: x['properties']['name'])
233+
234+
paginator = Paginator(sorted_features, 20)
235+
236+
try:
237+
page = request.GET['page']
238+
239+
except:
240+
page = 1
241+
242+
if int(page) > paginator.num_pages:
243+
page = paginator.num_pages
244+
245+
features_list = {
246+
'page': page,
247+
'totalPages': paginator.num_pages,
248+
'features': paginator.page(page).object_list
249+
}
250+
251+
return Response(features_list)
252+
253+
254+
205255
@api_view()
206256
def feature_attachments(request, pk, source_layer):
207257
"""

mmt_map/admin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.contrib.gis import admin
33

44
# Memory Map Toolkit
5-
from .models import Theme, Point, Polygon, Line, Document, Image, AudioFile
5+
from .models import Theme, Point, Polygon, Line, Document, Image, AudioFile, TagList
66
from .forms import PointForm, PolygonForm, LineForm
77

88
# 3rd Party
@@ -137,8 +137,8 @@ class Meta:
137137

138138

139139

140-
141140
admin.site.register(Theme, ThemeAdmin)
142141
admin.site.register(Point, PointAdmin)
143142
admin.site.register(Polygon, PolygonAdmin)
144-
admin.site.register(Line, LineAdmin)
143+
admin.site.register(Line, LineAdmin)
144+
admin.site.register(TagList)

mmt_map/migrations/0029_taglist.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 3.1.13 on 2022-12-15 15:32
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('taggit', '0003_taggeditem_add_unique_index'),
10+
('mmt_map', '0028_auto_20200806_1153'),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='TagList',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('name', models.CharField(max_length=64)),
19+
('tags', models.ManyToManyField(to='taggit.Tag')),
20+
],
21+
),
22+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 3.1.13 on 2022-12-15 15:36
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('mmt_map', '0029_taglist'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='taglist',
15+
name='order',
16+
field=models.PositiveSmallIntegerField(default=0),
17+
),
18+
migrations.AddField(
19+
model_name='taglist',
20+
name='published',
21+
field=models.BooleanField(default=False),
22+
),
23+
]

mmt_map/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ def __str__(self):
3838
return self.name
3939

4040

41+
class TagList(models.Model):
42+
"""A list of tag instances for display in a dropdown in the frontend"""
43+
name = models.CharField(max_length=64)
44+
tags = models.ManyToManyField(Tag)
45+
published = models.BooleanField(default=False)
46+
order = models.PositiveSmallIntegerField(default=0)
47+
48+
def __str__(self):
49+
return self.name
50+
51+
4152
class AbstractFeature(models.Model):
4253
"""The model class from which all feature objects derive"""
4354
name = models.CharField(max_length=140)

mmt_map/views.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from constance import config
1818

1919
# Memory Map Toolkit
20-
from .models import Point, Line, Polygon, Theme, Document, Image, AudioFile
20+
from .models import Point, Line, Polygon, Theme, Document, Image, AudioFile, TagList
2121
from mmt_pages.models import Page
2222
from mmt_api.serializers import PointSerializer, PolygonSerializer, PointDetailSerializer, DocumentSerializer
2323
from .vector_tile_helpers import tileIsValid, tileToEnvelope
@@ -28,6 +28,7 @@ def index(request):
2828

2929
themes = Theme.objects.all()
3030
pages = Page.objects.all().order_by('order')
31+
tag_lists = TagList.objects.filter(published=True).order_by('order')
3132

3233
bounds = None
3334

@@ -36,15 +37,16 @@ def index(request):
3637
bounds = [[config.BOUNDS_SW_LONGITUDE,config.BOUNDS_SW_LATITUDE],[config.BOUNDS_NE_LONGITUDE,config.BOUNDS_NE_LATITUDE]]
3738

3839

39-
return render(request, 'mmt_map/index.html', {'themes': themes, 'bounds': bounds})
40+
return render(request, 'mmt_map/index.html', {'themes': themes, 'bounds': bounds, 'tag_lists': tag_lists})
4041

4142

4243
def text_only_feature_list(request):
4344
"""A text only representation of features to provide access to memory map content for blind and partially-sighted users"""
4445

4546
themes = Theme.objects.all()
47+
tag_lists = TagList.objects.filter(published=True).order_by('order')
4648

47-
return render(request, 'mmt_map/feature_list.html', {'themes': themes})
49+
return render(request, 'mmt_map/feature_list.html', {'themes': themes, 'tag_lists': tag_lists})
4850

4951

5052
# Vector tiles are optionally cached to stop the database being spammed to heavily.

project_static/js/accessibleFeatureListHandler.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ for (let i=0; i < MmtMap.themes.length; i++) {
88
$('#themes').append(theme);
99
}
1010

11-
12-
11+
// Add the tags to the menu bar, in the correct tag lists
12+
13+
for (let i=0; i < MmtMap.tagLists.length; i++) {
14+
let tl = MmtMap.tagLists[i];
15+
$(tl.id).append('<a href="#" class="tag dropdown-item">All</a>');
16+
tl.tags.forEach((tag) => {
17+
$(tl.id).append('<a href="#" class="tag dropdown-item" ' +'data-tag="' + tag +'">' + tag + '</a>');
18+
});
19+
}
1320

1421
MmtFeatureList.populateList = function(url, params) {
1522

project_static/js/accessibleFeatureSearchHandler.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,23 @@ $('.theme').click(function(e) {
5252
$('.results_title').html('Places in Theme ' + theme);
5353
MmtFeatureList.allSites = false;
5454
}
55+
});
56+
57+
/* Tag filtering */
58+
59+
$('.tag').click(function(e) {
60+
e.preventDefault();
61+
tag = $(this).data('tag');
62+
if (tag == undefined) {
63+
$('.feature_list').empty();
64+
MmtFeatureList.populateList(MmtFeatureList.listUrl, {page: 1});
65+
$('.results_title').html('Place Index');
66+
MmtFeatureList.allSites = true;
67+
} else {
68+
$('.feature_list').empty();
69+
url = MmtFeatureList.tagUrl;
70+
MmtFeatureList.populateList(url, {tags: tag, page: 1});
71+
$('.results_title').html('Places Tagged "' + tag + '"');
72+
MmtFeatureList.allSites = false;
73+
}
5574
});

project_static/js/filters.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,24 @@ $('.theme').click(function(e) {
3333
$(this).css('color', color);
3434
});
3535
}
36-
});
36+
});
37+
38+
// Filter features by tag
39+
40+
$('.tag').click(function(e) {
41+
e.preventDefault();
42+
let tag = $(this).data('tag');
43+
if (tag != undefined) {
44+
map.setFilter('polygons', ['in', tag, ['get', 'tag_str']]);
45+
map.setFilter('polygon_outlines', ['in', tag, ['get', 'tag_str']]);
46+
map.setFilter('points', ['in', tag, ['get', 'tag_str']]);
47+
map.setFilter('points_labels', tag, ['in', ['get', 'tag_str']]);
48+
map.setFilter('polygon_labels', ['in', tag, ['get', 'tag_str']]);
49+
} else {
50+
map.setFilter('polygons');
51+
map.setFilter('polygon_outlines');
52+
map.setFilter('points');
53+
map.setFilter('points_labels');
54+
map.setFilter('polygon_labels');
55+
}
56+
})

project_static/js/mapHandler.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
// Add the themes to the menu bar
22

3-
$('#themes').append('<a href="#" class="theme dropdown-item">All</a>')
3+
$('#themes').append('<a href="#" class="theme dropdown-item">All</a>');
44

55
for (let i=0; i < MmtMap.themes.length; i++) {
66
let themes = MmtMap.themes;
77
let theme = '<a href="#" class="theme dropdown-item ' + themes[i].name + '" data-key="' + themes[i].id + '" data-color="' + themes[i].color +'" style="color: '+ themes[i].color +'">' + themes[i].name + '</a>';
88
$('#themes').append(theme);
99
}
1010

11+
// Add the tags to the menu bar, in the correct tag lists
12+
13+
for (let i=0; i < MmtMap.tagLists.length; i++) {
14+
let tl = MmtMap.tagLists[i];
15+
$(tl.id).append('<a href="#" class="tag dropdown-item">All</a>');
16+
tl.tags.forEach((tag) => {
17+
$(tl.id).append('<a href="#" class="tag dropdown-item" ' +'data-tag="' + tag +'">' + tag + '</a>');
18+
});
19+
}
20+
21+
1122

1223
// If there are switchable layers, add a #maps dropdown to the menu bar
1324

templates/mmt_map/base.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@
9191
</div>
9292
</li>
9393
{% endif %}
94+
{% if tag_lists %}
95+
{% for tl in tag_lists %}
96+
<li class="nav-item dropdown">
97+
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
98+
{{ tl.name }}
99+
</a>
100+
<div class="dropdown-menu" id="taglist_{{ tl.id }}" aria-labelledby="navbarDropdown">
101+
</div>
102+
</li>
103+
{% endfor %}
104+
{% endif %}
94105
</ul>
95106
<form class="form-inline my-2 my-lg-0">
96107
<input class="form-control mr-sm-2" placeholder="Search" aria-label="Search" id="map_search">

templates/mmt_map/feature_list.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ <h1 class="results_title">Place Index</h1>
2727
listUrl: '{% url 'feature_list' %}',
2828
themeUrl: '{% url 'get_features_by_theme' %}',
2929
searchUrl: '{% url 'search_features' %}',
30+
tagUrl: '{% url 'get_features_by_tag' %}',
3031
allSites: true
3132
}
3233

@@ -39,6 +40,16 @@ <h1 class="results_title">Place Index</h1>
3940
'color': '{{ theme.color }}'
4041
},
4142
{% endfor %}
43+
],
44+
45+
tagLists: [
46+
{% for tl in tag_lists %}
47+
{
48+
'id': '#taglist_{{ tl.id }}',
49+
'name': '{{ tl.name }}',
50+
'tags': [{% for tag in tl.tags.all %}"{{ tag }}",{% endfor %}]
51+
},
52+
{% endfor %}
4253
]
4354
}
4455

templates/mmt_map/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444
{% endfor %}
4545
],
4646

47+
tagLists: [
48+
{% for tl in tag_lists %}
49+
{
50+
'id': '#taglist_{{ tl.id }}',
51+
'name': '{{ tl.name }}',
52+
'tags': [{% for tag in tl.tags.all %}"{{ tag }}",{% endfor %}]
53+
},
54+
{% endfor %}
55+
],
56+
4757
{% if themes %}
4858
themeStyleExpressions: [
4959
'case',

0 commit comments

Comments
 (0)