-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
84 lines (71 loc) · 2.59 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
import os
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
'''
setup_db(app):
binds a flask application and a SQLAlchemy service
'''
def setup_db(app):
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://' + os.environ.get('DATABASE_URL')[len('postgresql/'):]
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
'''
drops the database tables and starts fresh
can be used to initialize a clean database
'''
def db_drop_and_create_all():
db.drop_all()
db.create_all()
def dump_datetime(value):
"""Deserialize datetime object into string form for JSON processing."""
if value is None:
return None
return value.strftime("%d %b %Y, %H:%M")
# return value.isoformat()
class Article(db.Model):
__tablename__='articles'
id = db.Column(db.Integer, primary_key=True)
articleId = db.Column(db.String, unique=True)
title = db.Column(db.String)
description = db.Column(db.Text)
datePublished = db.Column(db.DateTime)
bodyText = db.Column(db.Text)
def __init__(self, articleId, title, description, datePublished, bodyText):
self.articleId = articleId
self.title = title
self.description = description
self.datePublished = datePublished
self.bodyText = bodyText
def __repr__(self):
return 'Article Link(' + self.articleId + ',' + str(self.title) + ',' + str(self.description) + ',' + str(self.datePublished) + ',' + str(self.bodyText) + ')'
@property
def serialize(self):
"""Return object data in easily serializable format"""
idArticle = self.articleId
if idArticle.startswith('www.gov.sg') : idArticle = "https://" + idArticle
return {
'articleId': idArticle,
'title': self.title,
'description': self.description,
'datePublished': dump_datetime(self.datePublished),
'bodyText': self.bodyText
}
class EventType(db.Model):
__tablename__ = 'tags'
id = db.Column(db.Integer, primary_key=True)
eventTypeName = db.Column(db.String, unique=True)
currString = db.Column(db.String)
def __init__(self, tagName, currString):
self.eventTypeName = tagName
self.currString = currString
def __repr__(self):
return '<Event Type %s : %s>' % self.eventTypeName, self.currString
@property
def serialize(self):
"""Return object data in easily serializable format"""
return {
'id': self.id,
'eventTypeName': self.eventTypeName,
'currString': self.currString,
}