This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
/
models.py
85 lines (64 loc) · 2.4 KB
/
models.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''\
Search Engine and Tagging models. Just two of them, one for storing Words and
one for linking other objects to Words.
'''
from inspect import isclass
from stdnet import orm
from stdnet.utils import range, to_string
class WordItemManager(orm.Manager):
def for_model(self, model):
q = self.query()
if not isclass(model):
return q.filter(model_type = model.__class__,
object_id = model.id)
else:
return q.filter(model_type = model)
class Word(orm.StdModel):
'''Model which hold a word as primary key'''
id = orm.SymbolField(primary_key = True)
tag = orm.BooleanField(default = False)
def __unicode__(self):
return self.id
class Meta:
ordering = -orm.autoincrement()
class WordItem(orm.StdModel):
'''A model for associating :class:`Word` instances with general
:class:`stdnet.orm.StdModel` instances.'''
word = orm.ForeignKey(Word, related_name = 'items')
'''tag instance'''
model_type = orm.ModelField()
'''Model type'''
object_id = orm.SymbolField()
'''Model instance id'''
count = orm.IntegerField(index = False, default = 1)
def __unicode__(self):
return self.word.__unicode__()
objects = WordItemManager()
class Meta:
ordering = -orm.autoincrement()
unique_together = ('word', 'model_type', 'object_id')
@property
def object(self):
'''Instance of :attr:`model_type` with id :attr:`object_id`.'''
if not hasattr(self,'_object'):
self._object = self.model_type.objects.get(id = self.object_id)
return self._object
class Tag(Word):
pass
class TagItem():
'''A model for associating :class:`Word` instances with general
:class:`stdnet.orm.StdModel` instances.'''
tag = orm.ForeignKey(Tag, related_name = 'items')
'''tag instance'''
model_type = orm.ModelField()
'''Model type'''
object_id = orm.SymbolField()
'''Model instance id'''
def __unicode__(self):
return self.word.__unicode__()
@property
def object(self):
'''Instance of :attr:`model_type` with id :attr:`object_id`.'''
if not hasattr(self,'_object'):
self._object = self.model_type.objects.get(id = self.object_id)
return self._object