rascunho

ref: master

rascunho/app.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
from flask import Flask, abort, render_template, request
from jinja2 import Markup
import locale
from rascunho.config import read_from_config, env
from rascunho.database import db, init_database
import mistune
import humanize
from datetime import datetime, timedelta
from pytz import timezone

class Rascunho(Flask):
    def __init__(self, *args, **kwargs):
        super().__init__(__name__, *args, **kwargs)

        init_database()

        try:
            # TODO: Use the locale config, from my local package it doesn't
            # work as the package is out-of-date on Arch's Community Repository
            humanize.i18n.activate("pt_BR")
            locale.setlocale(locale.LC_ALL, read_from_config("locale"))
            locale.setlocale(locale.LC_TIME, read_from_config("locale"))
        except:
            pass


        from rascunho.blueprints.basic import basic
        from rascunho.blueprints.api import api

        self.register_blueprint(basic)
        self.register_blueprint(api)

        @self.template_filter()
        def human_date(d):
            if not d:
                return 'Nunca'

            local_tz = timezone(str(read_from_config("timezone")))
            d = d.replace(tzinfo=timezone('UTC'))
            local_date = d.astimezone(local_tz)

            if isinstance(d, timedelta):
                return Markup('<span title="{}">{}</span>'.format(
                    f'{d.seconds} segundos', humanize.naturaldelta(d)))

            return Markup('<span title="{}">{}</span>'.format(
                d.strftime('%Y-%m-%d %H:%M:%S UTC'),
                humanize.naturaltime(
                    datetime.now().astimezone(local_tz) - local_date
                )))

        @self.template_filter()
        def md(text):
            return mistune.markdown(text)

        @self.errorhandler(422)
        def handle_unprocessable_entity(e):
            if request.path.startswith("/api"):
                return { "errors": [ { "reason": "422 Unprocessable Entity" } ] }, 422

            return render_template("unprocessable_entity.html", 422)

        @self.errorhandler(500)
        def handle_internal_server_error(e):
            if request.path.startswith("/api"):
                return { "errors": [ { "reason": "500 Internal Server Error" } ] }, 500

            return render_template("internal_error.html", 500)

        @self.errorhandler(404)
        def handle_not_found(e):
            if request.path.startswith("/api"):
                return { "errors": [ { "reason": "404 Not Found" } ] }, 404

            return render_template("not_found.html"), 404

        @self.context_processor
        def inject():
            return {
                "env": env
            }


app = Rascunho()