def rename(request, tag): """ Renames a tag using a RenameTagForm If successfull, outputs a JSON object with "obj", "type" and "pooled" properties. "obj" is the JSON object of the tag that was renamed, while "type" will always be "tag". pooled is true when the tag has been renamed to that of an existing tag, in which case "obj" will be that tag. If it fails, a JSON object with an "error" value will be returned. """ tagObj = get_object_or_404(Tag, owner=request.user, slug=tag) form = RenameTagForm(request.POST, instance=tagObj) if not form.is_valid(): return HttpResponse('{"error":"Form invalid"}', content_type="application/json", status=422) if Tag.objects.filter(owner=request.user, slug=makeslug(form.data["name"])).exclude(pk=form.instance.pk).exists(): # Tag exists, pool them existing = Tag.objects.get(owner=request.user, slug=makeslug(form.data["name"])) form.instance.pool_into(existing) form.instance.delete() tagObj = existing pooled = "true" else: form.save() pooled = "false" return HttpResponse('{"obj":'+tagObj.to_json()+', "type":"tag", "pooled":'+pooled+'}', content_type="application/json")
def suggest(request, value): """ Returns a JSON object containing tag suggestions for the sepecified value The JSON object contains two values: - yours: The string that was submitted to this page - tags: An array of strings for the suggestions """ tags = Tag.objects.filter(owner=request.user, slug__startswith=makeslug(value))[:10] return TemplateResponse(request, "tags/suggest.json", {"tags":tags, "value":value}, "application/json")
def get_or_create_with_slug(owner, instance): """ Checks if a tag exists for a given user, and returns it if it does If it doesn't, the owner of the instance is set to the provided one, it is saved and then returned. """ try: return Tag.objects.get(owner=owner, slug=makeslug(instance.name)) except Tag.DoesNotExist: instance.owner = owner instance.save() return instance
def untag(self, tag): """ Untags this object from a tag specified by a tag name, primary key or Tag model instance """ if isinstance(tag, six.integer_types): try: tag = Tag.objects.get(pk=tag, owner=self.owner) except Tag.DoesNotExist: return if isinstance(tag, six.string_types): try: tag = Tag.objects.get(slug=makeslug(tag), owner=self.owner) except Tag.DoesNotExist: return self.tags.remove(tag)
def filter(request, tag): """ Given a slug, uses filter.html to display all the things tagged with that specific tag """ tag = get_object_or_404(Tag, owner=request.user, slug=makeslug(tag)) ctx = {} if not tag.pinned: # Don't display "tags" thing as active when the tag is pinned ctx["area"] = "tags" ctx["tag"] = tag ctx["pin_form"] = PinTagForm(instance=tag) ctx["bookmarks"] = make_page(Bookmark.get_by_tag(tag), request.GET.get("p")) return TemplateResponse(request, "tags/filter.html", ctx)
def tag(self, tag): """ Tags this object with a tag specified by a tag name, primary key or Tag model instance """ if isinstance(tag, six.integer_types): try: tag = Tag.objects.get(pk=tag, owner=self.owner) except Tag.DoesNotExist: #Handle this better? return if isinstance(tag, six.string_types): tname = tag try: tag = Tag(owner=self.owner, name=tag) tag.save() except IntegrityError: tag = Tag.objects.get(slug=makeslug(tname), owner=self.owner) tag.save() # If this isn't here there are crashes for some reason self.tags.add(tag)
def tag(request): """ Tags a thing using an AddTagForm Returns a JSON response with an "obj" and "key" porperty. "obj" is the object that was tagged, while "type" is it's type. If there is an error, a JSON object with an "error" key is returned instead. """ f = AddTagForm(request.POST) if not f.is_valid(): return HttpResponse('{"error":"Form invalid"}', content_type="application/json", status=422) taggable = tags.lookup_taggable(f.cleaned_data["type"]) if taggable is None: return HttpResponse('{"error":"Taggable type invalid"}', content_type="application/json", status=422) try: obj = taggable.objects.get(owner=request.user, pk=f.cleaned_data["pk"]) except taggable.DoesNotExist: return HttpResponse('{"error":"Taggable not found"}', content_type="application/json", status=422) tag_names = map(lambda x: x.strip(), f.cleaned_data["name"].split(",")) for n in tag_names: if not n: continue try: tag = Tag.objects.get(owner=request.user, slug=makeslug(n)) except Tag.DoesNotExist: tag = Tag(owner=request.user, name=n, colour=f.instance.colour) tag.save() obj.tag(tag) return HttpResponse( '{{"obj":{}, "type":"{}"}}'.format(obj.to_json(), f.cleaned_data["type"]), content_type="application/json" )
def save(self, *args, **kwargs): self.slug = makeslug(self.name) super(Taggable, self).save(*args, **kwargs)