Commit 9f60450c authored by BERJON Matthieu's avatar BERJON Matthieu
Browse files

Updating the tag views

I updated the tag views by importing the `Tag` model provided by the
Taggit plugin, deleted the `WebappTag` model and `TagForm` form that are
I updated the `WebappUpdate` view to follow the recommendation given by
the Taggit plugin documentation. The `TagList` now uses directly the
`Tag` model and send both the tag fields and a count of each tag
I removed the `TagAdd` list that is obsolete. I updated the
`TagWebappList` that returns simply a list of all `Webapp`objects for a
given tag.
Signed-off-by: BERJON Matthieu's avatarMatthieu Berjon <>
parent 2215f661
......@@ -27,6 +27,7 @@ from django.contrib.auth.models import User
from django.contrib.messages.views import SuccessMessageMixin
from django.core.urlresolvers import reverse
from django.db import transaction
from django.db.models import Count
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse, reverse_lazy
......@@ -45,6 +46,7 @@ from django.views.generic import (
import natsort
import redis
from taggit.models import Tag
# Local imports
from .mixins import GroupRequiredMixin
......@@ -56,7 +58,6 @@ from .models import (
from .forms import (
......@@ -65,7 +66,6 @@ from .forms import (
......@@ -198,9 +198,8 @@ class WebappUpdate(SuccessMessageMixin, LoginRequiredMixin, UpdateView):
user = User.objects.get(username=form.cleaned_data['owner'])
obj.user_id =
# Add the tage to the database (specific because it's a many to
# many relationship
obj.tags = form.cleaned_data['tags']
# Add the tag to the database (specific because it's a many to
# many relationship)
if user != self.request.user:
messages.success(self.request, self.success_message)
......@@ -420,36 +419,19 @@ class TagList(ListView):
template_name: template filename.
model = WebappTag
model = Tag
context_object_name = 'tags'
template_name = 'tag_list.html'
def get_queryset(self):
"""Return all available tags
Each tag return as well the number of webapps attached to it
tags = WebappTag.objects.annotate(num_tag=Count('webapptags'))
tags = Tag.objects.annotate(num_tag=Count('taggit_taggeditem_items'))
return tags
class TagAdd(LoginRequiredMixin, CreateView):
"""Add a tag
model: database model
form_class: form used
success_url: URL to redirect to if successful
success_message: message displayed to the user if successful
template_name: template filename
model = WebappTag
form_class = TagForm
success_url = reverse_lazy('main:home')
success_message = 'Tag created successfully.'
template_name = 'tag_add_update.html'
class TagWebappList(ListView):
"""List all available webapps for a given tag
......@@ -460,28 +442,14 @@ class TagWebappList(ListView):
paginated_by: number of occurences per page.
template_name: template filename.
model = WebappTag
model = Webapp
context_object_name = 'webapps'
paginated_by = 10
template_name = 'tag_webapp_list.html'
def get_queryset(self):
"""Return all available webapp for a given tag (slug)
slug = self.kwargs['slug']
queryset = Webapp.objects.filter(tags__slug=slug)
return queryset
def get_context_data(self, **kwargs):
"""Recover data to pass on to the template context
return Webapp.objects.filter(tags__slug=self.kwargs['slug'])
return the tag object used in the breadcrumbs and title tag of the
context = super().get_context_data(**kwargs)
context['tag'] = WebappTag.objects.get(slug=self.kwargs['slug'])
return context
# -----------------------------------------------------------------------------
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment