-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
107 lines (88 loc) · 2.66 KB
/
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# ---------------
# Imports
# ---------------
from flask import Flask, make_response, request, jsonify, render_template, url_for, redirect
import json, os
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.ext.mutable import Mutable
from sqlalchemy import types, desc
from dictalchemy import make_class_dictable
from urllib import quote_plus
# ---------------
# Init
# ---------------
app = Flask(__name__)
if os.getenv('DEBUG') == 'false':
app.debug = False
else:
app.debug = True
app.secret_key = "ukl\xab\xb7\xc9\x10\xf7\xf1\x03\x087\x0by\x88X'v\xc9\x8c\xc4\xc8\xfe+"
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL', 'sqlite:///db.sqlite3')
db = SQLAlchemy(app)
make_class_dictable(db.Model)
# -------------------
# Models
# -------------------
class Message(db.Model):
'''
A unique lovey-dove message
'''
# Columns
id = db.Column(db.Integer(), primary_key=True)
password = db.Column(db.Unicode())
text = db.Column(db.Unicode())
sender = db.Column(db.Unicode())
receiver = db.Column(db.Unicode())
phone = db.Column(db.Unicode())
email = db.Column(db.Unicode())
__table_args__ = ( db.UniqueConstraint('password'), { } )
def __init__(self, password, text, sender, receiver, phone, email):
self.password = password
self.text = text
self.sender = sender
self.receiver = receiver
self.phone = phone
self.email = email
def asdict(self):
'''
Returns the documentation as a dictionary
'''
doc_dict = db.Model.asdict(self)
return doc_dict
def __repr__(self):
return "%s to %s at %s" % (self.sender, self.receiver, self.phone)
# -------------------
# Routes
# -------------------
@app.route('/')
def index():
return render_template('index.html')
@app.route('/message', methods=['GET', 'POST'])
def message():
if request.method == 'POST':
data = {
'password': request.form['password'],
'text': request.form['text'],
'sender': request.form['sender'],
'receiver': request.form['receiver'],
'phone': request.form['phone'],
'email': request.form['email']
}
new_message = Message(**data)
db.session.add(new_message)
db.session.commit()
return 'Mensaje guardado con amor <3. Espera al 14 ;)'
else:
return 'un mensaje'
@app.route('/love', methods=['GET', 'POST'])
def love():
if request.method != 'POST':
return render_template('love.html')
else:
m = Message.query.filter_by(password=request.form['password']).first()
if m:
return render_template('love.html', message=m.text)
else:
return render_template('love.html', message='No message for that password :( </3')
if __name__ == "__main__":
app.run()