-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
143 lines (114 loc) · 3.56 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
SQLAlchemy declarations.
"""
import datetime
import hashlib
import sha
from flask.ext.login import UserMixin
import markdown
import os
from sqlalchemy_utils.types.password import PasswordType
from sqlalchemy import create_engine, Integer, Column, String, Date, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import deferred
from os import environ
from sqlalchemy import create_engine, update, ForeignKey
conn_string = None
try:
conn_string = environ['SQL_ALCHEMY_CONN_STRING']
except KeyError:
print 'You need to set SQL_ALCHEMY_CONN_STRING'
exit(1)
# This is the creation of the db, stored in memory vs. disk vs. remote (uses db drivers, so must connect through socket)
engine = create_engine(conn_string, echo=True)
Base = declarative_base()
class BlogPost(Base):
"""
A model representing a Blog Post.
"""
__tablename__ = 'post'
# On class construction all of these Column objects are replaced with
# python descriptors. This is known as instrumentation and provides the class
# with the means to move in and out of SQL and python objects.
id = Column(Integer, primary_key=True)
title = Column(String, nullable=False)
content = deferred(Column(String))
created_at = Column(DateTime, default=datetime.datetime.now)
updated_at = Column(DateTime, default=datetime.datetime.now,
onupdate=datetime.datetime.now)
summary = Column(String)
# type = Column(String)
type = Column(String, nullable=False, index=True)
__mapper_args__ = {
'polymorphic_on': type,
'polymorphic_identity': 'post'
}
@property
def html(self):
# converts the content (which is in markdown), into html
# html contains the content as html, the function is rendering markdown as html...
html = markdown.markdown(self.content)
return html
@property
def date(self):
return self.created_at.strftime("%A %d %B %Y")
# @todo: finish this property if we want to include an abbreviation of the summary
# @property
# def abbreviated(self):
class Daily(BlogPost):
# __tablename__ = "daily"
__mapper_args__ = {
'polymorphic_identity': 'daily',
}
class Weekly(BlogPost):
# __tablename__ = "weekly"
__mapper_args__ = {
'polymorphic_identity': 'weekly',
}
class Project(BlogPost):
# __tablename__ = "project"
__mapper_args__ = {
'polymorphic_identity': 'project',
}
class Todo(BlogPost):
# __tablename__ = "todo"
__mapper_args__ = {
'polymorphic_identity': 'todo',
}
# class Type(object):
# daily = 1
# weekly = 2
# project = 3
# todo = 4
class User(Base, UserMixin):
__tablename__ = 'User'
id = Column(Integer, primary_key=True)
user_name = Column(String, unique=True)
password = Column(PasswordType(
schemes=[
'pbkdf2_sha512'
]
))
# self.active = active
class Project(Base):
__tablename__ = 'Project'
id = Column(Integer, primary_key=True)
name = Column(String)
image_path = Column(String)
url = Column(String)
Base.metadata.create_all(engine)
# class User(Base, UserMixin):
# password = Column(String)
#
# def set_password(self, password):
# hashlib.
# self.password = sha5(password)
# self.save()
#
# def check_password(self, password):
# sha = sha5(password)
# return self.password == sha
# user_datastore = SQLAlchemyUserDatastore(db, User, Role)
# security = Security(app, user_datastore)
#
# db.create_all()