-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.py
114 lines (89 loc) · 3.09 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# encoding: utf-8
import os.path
from datetime import datetime
from collections import defaultdict
from flask import json
from flask.ext.sqlalchemy import SQLAlchemy
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
prop_defs = {
'phone': u"Telefon",
'email': u"Email",
'website': u"Website",
'facebook': u"Facebook",
'twitter': u"Twitter",
'address': u"Adresa poștală",
}
meta_defs = ['office', 'college', 'hpol_id']
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
openid_url = db.Column(db.Text())
name = db.Column(db.Text())
email = db.Column(db.Text())
time_create = db.Column(db.DateTime)
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text())
def get_content(self):
if self.versions:
version = sorted(self.versions, key=lambda v: v.time)[-1]
return version.get_content()
else:
return {}
def save_content_version(self, new_content, user):
utcnow = datetime.utcnow()
version = ContentVersion(person=self, user=user, time=utcnow)
version.content = json.dumps(new_content)
db.session.add(version)
log.info("Content update for person id=%r version_id=%r",
self.id, version.id)
def get_meta(self, key):
for meta in self.meta:
if meta.key == key:
return meta.value
else:
return None
@classmethod
def objects_current(cls):
return cls.query.filter(
db.not_(
cls.meta.any(
PersonMeta.key == 'removed' and
PersonMeta.value == 'true'
)
)
)
class ContentVersion(db.Model):
id = db.Column(db.Integer, primary_key=True)
person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
person = db.relationship('Person', backref=db.backref('versions'))
content = db.Column(db.LargeBinary)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship('User')
time = db.Column(db.DateTime)
def get_content(self):
return json.loads(self.content)
class PersonMeta(db.Model):
id = db.Column(db.Integer, primary_key=True)
person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
person = db.relationship('Person', backref=db.backref('meta'))
key = db.Column(db.Text)
value = db.Column(db.Text)
def get_user(openid_url):
return User.query.filter_by(openid_url=openid_url).first()
def get_update_user(openid_url, name, email):
user = get_user(openid_url)
if user is None:
utcnow = datetime.utcnow()
user = User(openid_url=openid_url, time_create=utcnow)
log.info("New user, openid_url=%r", openid_url)
if (name, email) != (user.name, user.email):
user.name = name
user.email = email
db.session.add(user)
db.session.commit()
log.info("User data modified for openid_url=%r: name=%r, email=%r",
openid_url, name, email)
return user