-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vocabulary.py
42 lines (32 loc) · 1.2 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
42
# encoding: utf-8
from sqlalchemy import types, Column, Table
from ckan.model import meta, types as _types, domain_object
VOCABULARY_NAME_MIN_LENGTH = 2
VOCABULARY_NAME_MAX_LENGTH = 100
vocabulary_table = Table(
'vocabulary', meta.metadata,
Column('id', types.UnicodeText, primary_key=True,
default=_types.make_uuid),
Column('name', 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)
query = query.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):
from ckan.model import tag
query = meta.Session.query(tag.Tag)
return query.filter(tag.Tag.vocabulary_id == self.id)
meta.mapper(Vocabulary, vocabulary_table)