summaryrefslogtreecommitdiff
path: root/watchnews/data.py
blob: 125e3d1c1b8ea2564a3f854b5f920a16a7db4bad (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
import peewee as p

import os
import datetime
import re

db = p.Proxy()

class SqliteDatabase(p.SqliteDatabase):
    foreign_keys = True
    def _add_conn_hooks(self, conn):
        p.SqliteDatabase._add_conn_hooks(self, conn)
        conn.execute('PRAGMA foreign_keys = ON;').fetchone()

def init_sqlite(path):
    db.initialize(SqliteDatabase(path))

    db.connect()
    db.create_tables(filter(lambda t: not t.table_exists(),
                                 BaseModel.__subclasses__()))


class BaseModel(p.Model):
    class Meta:
        database = db
    def __json__(self):
        return self._data
    def __repr__(self):
        return '<{.__class__.__name__} {}>'.format(self, ' '.join(
            map(lambda kv: '{0[0]}={0[1]}'.format(kv), self._data.items())))

class Feed(BaseModel):
    created_date = p.DateTimeField(default=datetime.datetime.now)
    url = p.CharField(unique=True)
    title = p.CharField(null=True)

class Item(BaseModel):
    created_date = p.DateTimeField(default=datetime.datetime.now)
    feed = p.ForeignKeyField(Feed, related_name='items', on_delete='CASCADE')
    uid = p.CharField()
    title = p.CharField(null=True)
   
class Version(BaseModel):
    created_date = p.DateTimeField(default=datetime.datetime.now)
    item = p.ForeignKeyField(Item, related_name='versions', on_delete='CASCADE')
    url = p.CharField()
    title = p.CharField()
    authors = p.CharField()
    text = p.CharField()