-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vocabulary.py
38 lines (30 loc) · 1.09 KB
/
vocabulary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from meta import Table, types, Session
from core import metadata, Column, DomainObject, mapper
from types import make_uuid
import tag
VOCABULARY_NAME_MIN_LENGTH = 2
VOCABULARY_NAME_MAX_LENGTH = 100
vocabulary_table = Table(
'vocabulary', metadata,
Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
Column('name', types.Unicode(VOCABULARY_NAME_MAX_LENGTH), nullable=False,
unique=True),
)
class Vocabulary(DomainObject):
def __init__(self, name):
self.id = make_uuid()
self.name = name
@classmethod
def get(cls, id_or_name):
"""Return a Vocabulary object referenced by its id or name, or None if
there is no vocabulary with the given id or name.
"""
query = Session.query(Vocabulary).filter(Vocabulary.id==id_or_name)
vocab = query.first()
if vocab is None:
vocab = Vocabulary.by_name(id_or_name)
return vocab
@property
def tags(self):
return Session.query(tag.Tag).filter(tag.Tag.vocabulary_id==self.id)
mapper(Vocabulary, vocabulary_table)