-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vocabulary.py
41 lines (32 loc) · 1.11 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
39
40
41
## IMPORTS FIXED
import sqlalchemy as sa
import meta
import types
import tag
import domain_object
VOCABULARY_NAME_MIN_LENGTH = 2
VOCABULARY_NAME_MAX_LENGTH = 100
vocabulary_table = meta.Table(
'vocabulary', meta.metadata,
sa.Column('id', sa.types.UnicodeText, primary_key=True, default=types.make_uuid),
sa.Column('name', sa.types.Unicode(VOCABULARY_NAME_MAX_LENGTH), nullable=False,
unique=True),
)
class Vocabulary(domain_object.DomainObject):
def __init__(self, name):
self.id = types.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 = meta.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 meta.Session.query(tag.Tag).filter(tag.Tag.vocabulary_id==self.id)
meta.mapper(Vocabulary, vocabulary_table)