Skip to content

Commit

Permalink
Simplify tag urls
Browse files Browse the repository at this point in the history
  • Loading branch information
HSZemi committed Nov 12, 2024
1 parent 3f2c961 commit d7d7259
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 43 deletions.
13 changes: 3 additions & 10 deletions mapsapp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,11 @@ def maps2json(maps):
return objects


def tags(request, url_fragment):
if url_fragment == '':
def tags(request, tag):
if tag == '':
return JsonResponse({"maps": []})

items = url_fragment.split('/')
taglist = []
for item in items:
if item.isnumeric():
taglist.append(get_object_or_404(Tag, pk=int(item)))
resultset = Rms.objects.filter(newer_version=None, archived=False)
for tag in taglist:
resultset = resultset.filter(tags=tag)
resultset = Rms.objects.filter(newer_version=None, archived=False).filter(tags=tag)
objects = maps2json(resultset)

return JsonResponse({"maps": objects})
Expand Down
2 changes: 1 addition & 1 deletion mapsapp/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
path('collection/<uuid:collection_id>', api.collection, name='collection'),
path('modifycollection/', api.modifycollection, name='modifycollection'),
path('version/<version_name>', api.versiontag, name='version'),
re_path(r'^tags/(?P<url_fragment>(\d+/)*)$', api.tags, name='tags'),
path('tags/<tag>', api.tags, name='tags'),
path('vote/<uuid:rms_id>/add', api.add_vote, name='add_vote'),
path('vote/<uuid:rms_id>/remove', api.remove_vote, name='remove_vote'),
]
2 changes: 1 addition & 1 deletion mapsapp/templates/mapsapp/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ <h5 class="card-subtitle mb-2 text-muted">by {{ rms.authors }}</h5>
<div class="col-12 align-self-end">
<div class="tags">Tags:
{% for tag in rms.tags.all %}
<a href="{% url 'tags' url_fragment=tag.id|stringformat:"s/" %}"
<a href="{% url 'tags' tag=tag.id %}"
class="badge badge-secondary">{{ tag.name }}</a>
{% endfor %}
</div>
Expand Down
11 changes: 3 additions & 8 deletions mapsapp/templates/mapsapp/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
{% block pagetitle %}Tags – {% endblock %}

{% block subtitle %}
<h4 class="text-center">Maps tagged with ›{% for tag in tagset %}{{ tag }}{% if not forloop.last %},
{% endif %}{% endfor %}‹</h4>
<h4 class="text-center">Maps tagged with ›{{ tag }}‹</h4>
{% endblock %}

{% block content %}
<div class="row alltags">
<div class="col-md-8 offset-md-2">
{% for tag in alltags %}
{% if tag in tagset %}
<a href="./remove/{{ tag.id }}/" class="badge badge-primary">{{ tag }}</a>
{% else %}
<a href="./{{ tag.id }}/" class="badge badge-secondary">{{ tag }}</a>
{% endif %}
{% for single_tag in alltags %}
<a href="./{{ single_tag.id }}" class="badge badge-secondary">{{ single_tag }}</a>
{% endfor %}
</div>
</div>
Expand Down
3 changes: 1 addition & 2 deletions mapsapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def to_url(self, value):
path('newcollection', views.editcollection, name='newcollection'),
path('newcollection/<uuid:rms_id>', views.editcollection, name='newcollection'),
path('version/<version_name>', views.version, name='version'),
re_path(r'^tags/(?P<url_fragment>(\d+/)*)$', views.tags, name='tags'),
re_path(r'^tags/(?P<url_fragment>(\d+/)+)remove/(?P<id_to_remove>\d+)/$', views.tags_remove, name='tags_remove'),
path('tags/<tag>', views.tags, name='tags'),
path('email_verification_sent', views.email_verification_sent, name='email_verification_sent'),
path('verify/<uidb64>/<token>', views.verify_email, name='verify_email'),
path('password_reset', PasswordResetViewWithCustomDomain.as_view(), name='password_reset'),
Expand Down
25 changes: 4 additions & 21 deletions mapsapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,12 @@ def email_verification_sent(request):
return render(request, 'mapsapp/email_verification_sent.html', context=context)


def tags(request, url_fragment):
items = url_fragment.split('/')
tagset = set()
for item in items:
if item.isnumeric():
tagset.add(get_object_or_404(Tag, pk=int(item)))
def tags(request, tag):
tag_object = Tag.objects.filter(id=tag).first()
context = {
"tagset": tagset,
"tag": tag_object.name if tag_object else '?',
"alltags": Tag.objects.all(),
API_URL: reverse('api:tags', kwargs={'url_fragment': url_fragment})
API_URL: reverse('api:tags', kwargs={'tag': tag})
}
return render(request, 'mapsapp/tags.html', context)

Expand All @@ -501,19 +497,6 @@ def version(request, version_name):
return render(request, 'mapsapp/version.html', context)


def tags_remove(request, url_fragment, id_to_remove):
items = url_fragment.split('/')
tag_id_set = set()
for item in items:
if item != "":
tag_id_set.add(item)
tag_id_set.remove(id_to_remove)
new_url_fragment = '/'.join(tag_id_set)
if new_url_fragment != '':
new_url_fragment += '/'
return redirect('tags', url_fragment=new_url_fragment)


def info(request):
context = {'contact': SiteSettings.load().contact}
return render(request, 'mapsapp/info.html', context)

0 comments on commit d7d7259

Please sign in to comment.