forked from orf/simple
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
78 lines (61 loc) · 3.05 KB
/
model.py
File metadata and controls
78 lines (61 loc) · 3.05 KB
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
# -*- coding: utf-8 -*-
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, Sequence, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
import datetime
import json
import markdown
Base = declarative_base()
class Author(Base):
__tablename__ = "authors"
id = Column(Integer(11), Sequence('author_id_sequence'), primary_key = True, nullable = False)
username = Column(String(255), unique = True, index = True, nullable = False)
password = Column(String(255), nullable = False)
email = Column(String(255), nullable = False)
gravatar = Column(String(255), nullable = False)
github = Column(String(255))
linkedin = Column(String(255))
stackoverflow = Column(Integer(11))
bio = Column(Text)
post = relationship('Post', backref='author')
created_at = Column(DateTime, default=datetime.datetime.utcnow(), index = True, nullable = False)
updated_at = Column(DateTime, default=datetime.datetime.utcnow(), onupdate=datetime.datetime.utcnow(), nullable = False)
def __init__(self, *args, **kwargs):
for key in kwargs:
setattr(self, key, kwargs[key])
def __unicode__(self):
return "%s %s %s %s" % (self.id, self.username, self.email, self.github)
public_field = ('id', 'username', 'email', 'github', 'bio', 'created_at')
def get_public(self):
data = {}
for field in self.public_field:
data[field] = getattr(self, field, '')
return data
def get_json(self):
return json.dumps(self.get_public())
class Post(Base):
__tablename__ = "posts"
id = Column(Integer(11), Sequence('post_id_sequence'), primary_key = True, nullable = False)
title = Column(String(255), nullable = False)
author_id = Column(ForeignKey(Author.id), nullable = False)
slug = Column(String(255), unique = True)
text = Column(String(255), default = "")
draft = Column(Boolean, index = True)
views = Column(Integer(11), default = 0)
created_at = Column(DateTime, default=datetime.datetime.utcnow(), index = True, nullable = False)
updated_at = Column(DateTime, default=datetime.datetime.utcnow(), onupdate = datetime.datetime.utcnow(), nullable = False)
def __init__(self, *args, **kwargs):
for key in kwargs:
setattr(self, key, kwargs[key])
def __unicode__(self):
return "%s %s %s" % (self.title, self.text, self.created_at)
public_field = ('id', 'title', 'author', 'slug', 'text', 'draft', 'views', 'created_at', 'updated_at')
def get_public(self):
data = {}
for field in self.public_field:
data[field] = getattr(self, field, '')
return data
def get_json(self):
return json.dumps(self.get_public())
def render_content(self):
return markdown.Markdown(extensions=['fenced_code'], output_format="html5", safe_mode=True).convert(self.text)