lck.django 0.8.5 documentation

This Page

lck.django.tags.models

lck.django.tags.models

Tagging-related models. Wouldn’t it be great if we just had to:

obj.tags.all()
obj.tag('nice', Language.en_gb, author)
obj.untag('co za asy', Language.pl, author)

Classes

class TaggableBase(*args, **kwargs)

Bases: django.db.models.base.Model

Provides the tags generic relation to prettify the API.

tag(name, language, author)

Tags this object using a name in a specific language. The tag will be marked as authored by author.

The name can be a list of comma-separated tags. Double quotes can be used to escape values with spaces or commas. One special case: if there are no commas in the input, spaces are treated as tag delimiters.

untag(name, language, author)

Untags this object from tags in a specific language, authored by author.

The name can be a list of comma-separated tags. Double quotes can be used to escape values with spaces or commas. One special case: if there are no commas in the input, spaces are treated as tag delimiters.

untag_all([name][, language][, author])

Untags this object from all tags in a specific language or authored by author.

similar_objects([same_type, official]) → [(obj, distance), (obj, distance), ...]

Returns a sorted list of similar objects in tuples (the object itself, the distance to the self object). If there are no similar objects, the list returned is empty. Searching for similar objects can be constrained to the objects of the same type (if same_type is True).

Objects are similar when they share the same tags. Distance is the number of tags that are not shared by the two objects (specifically, the object has distance 0 to itself). Distance calculation by default uses all tags present on the object. If official is True, only the official tags are taken into account.

get_tags([official, author, language]) → [TagStem, TagStem, ...]

A convenience getter for tags on the current taggable. By default gets the official tags by no specific author and in any language.

class Taggable(*args, **kwargs)

Bases: lck.django.tags.models.TaggableBase

Provides the tags generic relation and default tags that can be edited straight from the admin.

NoDefaultTags

alias of TaggableBase

class TagStem(*args, **kwargs)

Bases: lck.django.common.models.NonUnique, lck.django.common.models.Localized, lck.django.tags.models.TaggableBase

A taggable stem of an existing tag (just the name in a specific language).

inc_count()

Increases the reported tag count.

dec_count()

Decreases the reported tag count. If it reaches zero, deletes itself.

class TagStemManager

Bases: django.db.models.manager.Manager

A regular manager but with a couple additional methods for easier stems discovery.

get_dictionary(model=None, content_type=None, stem=None, stems=None, official=False, author=None, language=None)

Returns a dictionary of all tagged objects with values being sets of raw stems (strings) for the specified object.

This is basically an overly complex implementation that avoids duplicating SQL queries. A straight forward version would be:

{obj: set(TagStem.objects.get_queryset_for_model(obj.__class__, obj))
    for obj in {t.content_object for t in Tag.objects.all()}
}
get_content_objects(model=None, content_type=None, stem=None, stems=None, official=False, author=None, language=None, order_by=None)

Returns a set of tagged objects.

get_queryset_for_model(model, instance=None, official=False, author=None, language=None)

Returns a flat QuerySet of distinct tag stems for the given model, optionally for a specific instance which can be filtered only to official tags and tags by a specific author. The QuerySet can be further filtered for instance to sort by name or -tag_count.

class Tag(*args, **kwargs)

Bases: lck.django.common.models.NonUnique, lck.django.common.models.Localized, lck.django.common.models.TimeTrackable

A tag to a generic object done by an author. If the author is a staff member, the tag is official.

update_stem()

Sets the correct stem on the object and updates tag counts for stems. Automatically invoked during each save() for this model.