From 6080b38fb2b6b3c1017bdd34bb7552bc7e26a4a0 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sun, 22 Nov 2015 00:25:56 +0100 Subject: project restructure make it a proper python package --- watchnews/data.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 watchnews/data.py (limited to 'watchnews/data.py') diff --git a/watchnews/data.py b/watchnews/data.py new file mode 100644 index 0000000..e4ecc24 --- /dev/null +++ b/watchnews/data.py @@ -0,0 +1,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() + -- cgit v1.2.1