summaryrefslogtreecommitdiff
path: root/archivist/model.py
blob: b281edcbd08546767aba43ae9ba6daa83eabdd3b (plain)
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
from peewee import *
from playhouse.fields import CompressedField

import datetime
 
db = SqliteDatabase('test.db', pragmas=[('foreign_keys', 'ON')])

__tables__ = []
__all__ = ['create_tables', 'drop_tables']

def table(cls):
    __tables__.append(cls)
    __all__.append(cls.__name__)
    return cls

def create_tables():
    db.create_tables(__tables__, True)

def drop_tables():
    db.drop_tables(__tables__, True)

class BaseModel(Model):
    class Meta:
        database = db

@table
class Document(BaseModel):
    content = CompressedField()
    created = DateTimeField(default=datetime.datetime.now)
    description = CharField(null=True)
    direction = BooleanField(null=True)
    original_path = CharField(null=True)

@table
class Prefix(BaseModel):
    name = CharField(null=True, unique=True)
    builtin = BooleanField(default = False)
    pseudo = BooleanField(default = False)

@table
class Tag(BaseModel):
    name = CharField()
    prefix = ForeignKeyField(Prefix, null=True, related_name = 'tag')

@table
class DocumentTag(BaseModel):
    document = ForeignKeyField(Document, related_name = 'tags')
    tag = ForeignKeyField(Tag)

    class Meta:
        primary_key = CompositeKey('document', 'tag')

@table
class TagImplications(BaseModel):
    tag = ForeignKeyField(Tag)
    implies_tag = ForeignKeyField(Tag, related_name = 'implications')
    
    class Meta:
        primary_key = CompositeKey('tag', 'implies_tag')