2
from epsilon.extime import Time
4
from axiom.item import Item
5
from axiom.attributes import text, reference, integer, AND, timestamp
12
The short string which is being applied as a tag to an Item.
15
created = timestamp(doc="""
16
When this tag was applied to the Item to which it applies.
19
object = reference(doc="""
20
The Item to which this tag applies.
23
catalog = reference(doc="""
24
The L{Catalog} item in which this tag was created.
27
tagger = reference(doc="""
28
An optional reference to the Item which is responsible for this tag's
36
Helper class to make Catalog.tagNames very fast. One of these is created
37
for each distinct tag name that is created. _TagName Items are never
38
deleted from the database.
43
The short string which uniquely represents this tag.
46
catalog = reference(doc="""
47
The L{Catalog} item in which this tag exists.
54
typeName = 'tag_catalog'
57
tagCount = integer(default=0)
59
def tag(self, obj, tagName, tagger=None):
62
# check to see if that tag exists. Put the object attribute first,
63
# since each object should only have a handful of tags and the object
64
# reference is indexed. As long as this is the case, it doesn't matter
65
# whether the name or catalog attributes are indexed because selecting
66
# from a small set of results is fast even without an index.
67
if self.store.findFirst(Tag,
68
AND(Tag.object == obj,
70
Tag.catalog == self)):
73
# if the tag doesn't exist, maybe we need to create a new tagname object
74
self.store.findOrCreate(_TagName, name=tagName, catalog=self)
76
# Increment only if we are creating a new tag
78
Tag(store=self.store, object=obj,
79
name=tagName, catalog=self,
80
created=Time(), tagger=tagger)
85
Return an iterator of unicode strings - the unique tag names which have
86
been applied objects in this catalog.
88
return self.store.query(_TagName, _TagName.catalog == self).getColumn("name")
91
def tagsOf(self, obj):
93
Return an iterator of unicode strings - the tag names which apply to
96
return self.store.query(
98
AND(Tag.catalog == self,
99
Tag.object == obj)).getColumn("name")
102
def objectsIn(self, tagName):
103
return self.store.query(
105
AND(Tag.catalog == self,
106
Tag.name == tagName)).getColumn("object")
110
def upgradeCatalog1to2(oldCatalog):
112
Create _TagName instances which version 2 of Catalog automatically creates
113
for use in determining the tagNames result, but which version 1 of Catalog
116
newCatalog = oldCatalog.upgradeVersion('tag_catalog', 1, 2,
117
tagCount=oldCatalog.tagCount)
118
tags = newCatalog.store.query(Tag, Tag.catalog == newCatalog)
119
tagNames = tags.getColumn("name").distinct()
121
_TagName(store=newCatalog.store, catalog=newCatalog, name=t)
124
from axiom.upgrade import registerUpgrader
125
registerUpgrader(upgradeCatalog1to2, 'tag_catalog', 1, 2)