This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
82 lines (61 loc) · 1.77 KB
/
database.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports
from os import path
from peewee import *
from playhouse.sqlite_ext import SqliteExtDatabase, FTSModel, RowIDField, SearchField
# from playhouse.sqliteq import SqliteQueueDatabase
from playhouse.hybrid import hybrid_property
import config
# Database
db = SqliteExtDatabase(config.DATABASE_FILE, **{
'pragmas': {
'journal_mode': 'off',
# 'cache_size': 10000, # 10000 pages, or ~40MB
# 'foreign_keys': 1, # Enforce foreign-key constraints
},
# 'check_same_thread': False
})
# Model: Base
class BaseModel(Model):
class Meta:
database = db
# Model: Podcast
class Podcast(BaseModel):
name = CharField(max_length=50)
short_name = CharField(max_length=3, unique=True)
feed_url = CharField(max_length=125, unique=True)
color = CharField(max_length=6, unique=True)
@hybrid_property
def episode_count(self):
return self.episodes.count()
# Model: Episode
class Episode(BaseModel):
podcast = ForeignKeyField(Podcast, backref='episodes')
title = CharField()
description = TextField()
pubdate = DateTimeField()
class Meta:
indexes = (
(('podcast', 'title', 'pubdate'), True),
)
# Model: EpisodeIndex
class EpisodeIndex(FTSModel):
# Full-text search index.
rowid = RowIDField()
title = SearchField()
description = SearchField()
class Meta:
database = db
options = {'tokenize': 'porter'}
# Connect
db.connect()
if __name__ == '__main__':
db.create_tables([Podcast, Episode, EpisodeIndex])
p1 = {
'name': 'Film Junk Podcast',
'short_name': 'fjp',
'feed_url': 'http://feeds.feedburner.com/filmjunk',
'color': '00C3E2'
}
Podcast.create(**p1)