-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
201 lines (152 loc) · 5.8 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from flask import Flask, request, render_template, url_for, redirect
from flask_login import *
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from forms import NoteForm, CopyForm, LoginForm, RegisterForm
from werkzeug.security import generate_password_hash, check_password_hash
import secrets
import config
import os
# ===== Flask application, connect with database, login manager =====
app = Flask(__name__)
# uri = os.getenv("DATABASE_URL")
# if uri.startswith("postgres://"):
# uri = uri.replace("postgres://", "postgresql://", 1)
app.config['SECRET_KEY'] = config.__DB_SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
Bootstrap(app)
db = SQLAlchemy(app)
# ===== Login set up =====
class Users(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
login = db.Column(db.String(300))
password = db.Column(db.String(300))
notes = db.relationship('Notes', backref='users')
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return Users.query.get(user_id)
# ====== SQLAlchemy class model =====
class Notes(db.Model):
id = db.Column('note_id', db.Integer, primary_key=True)
title = db.Column(db.String(100))
content = db.Column(db.String(1000))
url = db.Column(db.String(100))
recipient = db.Column(db.String(100))
user_id = db.Column(db.ForeignKey('users.id'))
# db.create_all()
#
# new_user = Users(
# login='pawel',
# password='pawel',
# )
# db.session.add(new_user)
# db.session.commit()
# ===== MAIN PAGE - SEND A MESSAGE =====
@app.route('/', methods=['GET', 'POST'])
def main():
if request.method == "POST":
title = request.form["title"]
content = request.form["content"]
url = secrets.token_urlsafe(6)
new_note = Notes(
title=title,
content=content,
url=url,
)
db.session.add(new_note)
db.session.commit()
return redirect(url_for('url_path', url=url))
return render_template('message.html', show_addressee=0)
# ===== READ A MESSAGE =====
@app.route('/<url>')
def note(url):
note_in_db = Notes.query.filter_by(url=url).first()
if note_in_db is not None:
my_note = [note_in_db.title, note_in_db.content]
db.session.delete(note_in_db)
db.session.commit()
return render_template('note.html', data=my_note)
return render_template('note.html', data=None)
@app.route('/copy_url/<url>', methods=['GET', 'POST'])
def url_path(url):
path = request.host_url + url
# copy = CopyForm()
# if copy.is_submitted():
# pyperclip.copy(path)
return render_template('url_path.html', path=path, correctly_shipped=0)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == "POST":
user_name = request.form['username']
user_password = request.form['password']
user = Users.query.filter_by(login=user_name).first()
if user is not None and check_password_hash(user.password, user_password):
login_user(user)
return redirect(url_for('main'))
else:
error = 'Invalid username or password'
return render_template('log.html', error=error)
return render_template('log.html', error=None)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('main'))
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == "POST":
user_name = request.form['username']
user_password = request.form['password']
print(Users.query.filter_by(login=user_name).all())
if not Users.query.filter_by(login=user_name).all():
new_user = Users(
login=user_name,
password=generate_password_hash(user_password),
)
db.session.add(new_user)
db.session.commit()
login_user(new_user)
return redirect(url_for('main'))
else:
error = 'User with this login already exists.'
return render_template('register.html', error=error)
return render_template('register.html', error=None)
@app.route('/direct_msg', methods=['GET', 'POST'])
@login_required
def direct_msg():
if request.method == "POST":
addressee = Users.query.filter_by(login=request.form["addressee"]).first()
if addressee is not None:
title = request.form["title"]
content = request.form["content"]
url = secrets.token_urlsafe(6)
new_note = Notes(
title=title,
content=content,
url=url,
recipient=current_user.login,
user_id=addressee.id
)
db.session.add(new_note)
db.session.commit()
return render_template('url_path.html', correctly_shipped=1)
else:
error = 'Addressee doesn\'t exist. Your message has not been sent.'
return render_template('message.html', show_addressee=1, error=error)
return render_template('message.html', show_addressee=1)
@app.route('/mailbox')
@login_required
def mailbox():
messages = Notes.query.filter_by(user_id=current_user.id).all()
return render_template('mailbox.html', messages=messages)
@app.route('/info')
def info():
return render_template('info.html')
if __name__ == '__main__':
# app.run(debug=True)
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)