def add_tag(request, production_id): # Only used in AJAX calls. production = get_object_or_404(Production, id=production_id) if not production.editable_by_user(request.user): raise PermissionDenied tag_name = slugify_tag(request.POST.get('tag_name')) try: blacklisted_tag = BlacklistedTag.objects.get(tag=tag_name) tag_name = slugify_tag(blacklisted_tag.replacement) message = blacklisted_tag.message except BlacklistedTag.DoesNotExist: message = None if tag_name: # check whether it's already present existing_tag = production.tags.filter(name=tag_name) if not existing_tag: production.tags.add(tag_name) Edit.objects.create(action_type='production_add_tag', focus=production, description=u"Added tag '%s'" % tag_name, user=request.user) tags_list_html = render_to_string('productions/_tags_list.html', { 'tags': production.tags.order_by('name') }) return JsonResponse({ 'tags_list_html': tags_list_html, 'clean_tag_name': tag_name, 'message': message, })
def post(self, request, subject_id): # Only used in AJAX calls. subject = get_object_or_404(self.subject_model, id=subject_id) if not self.can_edit(subject): raise PermissionDenied tag_name = slugify_tag(request.POST.get('tag_name')) try: blacklisted_tag = BlacklistedTag.objects.get(tag=tag_name) tag_name = slugify_tag(blacklisted_tag.replacement) message = blacklisted_tag.message except BlacklistedTag.DoesNotExist: message = None if tag_name: # check whether it's already present existing_tag = subject.tags.filter(name=tag_name) if not existing_tag: subject.tags.add(tag_name) Edit.objects.create( action_type=self.action_type, focus=subject, description=u"Added tag '%s'" % tag_name, user=request.user ) tags_list_html = render_to_string(self.template_name, { 'tags': subject.tags.order_by('name') }) return JsonResponse({ 'tags_list_html': tags_list_html, 'clean_tag_name': tag_name, 'message': message, })
def clean_tags(self): clean_tags = [] for name in self.cleaned_data['tags']: name = slugify_tag(name) try: blacklisted_tag = BlacklistedTag.objects.get(tag=name) name = slugify_tag(blacklisted_tag.replacement) except BlacklistedTag.DoesNotExist: pass if name: clean_tags.append(name) return clean_tags
def handle_noargs(self, **options): for tag in Tag.objects.all(): new_name = slugify_tag(tag.name) try: existing_tag = Tag.objects.exclude(id=tag.id).get(name=new_name) # if tag with that name already exists, move all tagged items to existing_tag # and delete this tag TaggedItem.objects.filter(tag=tag).update(tag=existing_tag) tag.delete() except Tag.DoesNotExist: # keep this tag, just rewrite the name tag.name = new_name tag.save()
def handle_noargs(self, **options): for tag in Tag.objects.all(): new_name = slugify_tag(tag.name) try: existing_tag = Tag.objects.exclude(id=tag.id).get( name=new_name) # if tag with that name already exists, move all tagged items to existing_tag # and delete this tag TaggedItem.objects.filter(tag=tag).update(tag=existing_tag) tag.delete() except Tag.DoesNotExist: # keep this tag, just rewrite the name tag.name = new_name tag.save()
def add_tag(request, production_id): # Only used in AJAX calls. production = get_object_or_404(Production, id=production_id) if request.method == 'POST': tag_name = slugify_tag(request.POST.get('tag_name')) # check whether it's already present existing_tag = production.tags.filter(name=tag_name) if not existing_tag: production.tags.add(tag_name) Edit.objects.create(action_type='production_add_tag', focus=production, description=u"Added tag '%s'" % tag_name, user=request.user) return render(request, 'productions/_tags_list.html', { 'tags': production.tags.order_by('name'), })
def remove_tag(request, production_id): # Only used in AJAX calls. production = get_object_or_404(Production, id=production_id) if request.method == 'POST': tag_name = slugify_tag(request.POST.get('tag_name')) existing_tag = production.tags.filter(name=tag_name) if existing_tag: production.tags.remove(tag_name) Edit.objects.create(action_type='production_remove_tag', focus=production, description=u"Removed tag '%s'" % tag_name, user=request.user) if not existing_tag[0].taggit_taggeditem_items.count(): # no more items use this tag - delete it existing_tag[0].delete() return render(request, 'productions/_tags_list.html', { 'tags': production.tags.order_by('name'), })
def post(self, request, subject_id): # Only used in AJAX calls. subject = get_object_or_404(self.subject_model, id=subject_id) if not self.can_edit(subject): raise PermissionDenied if request.method == 'POST': tag_name = slugify_tag(request.POST.get('tag_name')) existing_tag = subject.tags.filter(name=tag_name) if existing_tag: subject.tags.remove(tag_name) Edit.objects.create( action_type=self.action_type, focus=subject, description=u"Removed tag '%s'" % tag_name, user=request.user ) if not existing_tag[0].taggit_taggeditem_items.count(): # no more items use this tag - delete it existing_tag[0].delete() return render(request, self.template_name, { 'tags': subject.tags.order_by('name'), })
def clean_tags(self): return [slugify_tag(name) for name in self.cleaned_data['tags']]