This repository has been archived by the owner on Jan 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·104 lines (82 loc) · 2.88 KB
/
main.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
#!/usr/bin/env python
# flask
from flask import Flask, g, session, url_for
# config
import settings
from models.interface import DatabaseInterface
# apps
from apps.posts import posts
from apps.accounts import accounts
from apps.frontpage import frontpage
from apps.pages import pages
from apps.upload import upload
# common
from datetime import timedelta, datetime
from pytz import timezone as load_timezone
app = Flask(__name__)
app.config.from_object(settings)
database = DatabaseInterface(settings.DATABASE_ENGINE, settings.DATABASE_CONNECTION_STRING)
@app.template_filter('formatdatetime')
def format_datetime_filter(dt, format="%Y-%m-%d %H:%M %Z"):
timezone = load_timezone(app.config['TIME_LOCALE'])
dt = timezone.fromutc(dt)
return dt.strftime(format)
@app.template_filter('agedatetime')
def format_datetime_filter(dt, default="just now"):
"""
Returns string representing "time since" e.g.
3 days ago, 5 hours ago etc.
"""
now = datetime.utcnow()
diff = now - dt
periods = (
(diff.days / 365, "year", "years"),
(diff.days / 30, "month", "months"),
(diff.days / 7, "week", "weeks"),
(diff.days, "day", "days"),
(diff.seconds / 3600, "hour", "hours"),
(diff.seconds / 60, "minute", "minutes"),
(diff.seconds, "second", "seconds"),
)
for period, singular, plural in periods:
if period:
return "%d %s ago" % (period, singular if period == 1 else plural)
return default
@app.template_filter("parse")
def set_parser(s):
parser_name = ".".join(["parsers", settings.PARSER])
parser_module = __import__(parser_name, globals(), locals(), [settings.PARSER])
parser = getattr(parser_module, "parser")
return parser(s)
@app.context_processor
def get_page():
def get_page(name, published=True):
page = g.database.engine.get_page(name)
if page and published and page.published:
return page
return {"get_page": get_page}
@app.context_processor
def latest_posts():
posts = g.database.engine.get_published_posts()
return {"latest_posts": posts}
@app.context_processor
def add_user():
user = g.database.engine.get_user(session.get('user'))
return {"user": user}
@app.before_request
def setup_database():
g.database = DatabaseInterface(settings.DATABASE_ENGINE, settings.DATABASE_CONNECTION_STRING)
app.url_map.strict_slashes = False
app.register_blueprint(frontpage)
app.register_blueprint(pages)
app.register_blueprint(posts, url_prefix="/posts")
app.register_blueprint(accounts, url_prefix="/accounts")
app.register_blueprint(upload, url_prefix="/upload")
if not app.debug:
import logging
from logging import FileHandler
file_handler = FileHandler(app.config['LOG_FILE'])
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
if __name__ == "__main__":
app.run()