forked from ZPXD/feed.xd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
164 lines (114 loc) · 3.92 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
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
from flask import Flask, render_template, request, redirect, url_for
import flask_wtf
from flask_wtf import FlaskForm
from flask_login import UserMixin
from wtforms import StringField, TextAreaField, SubmitField, PasswordField, DateField, SelectField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager
from flask_login import login_required, current_user, login_user, logout_user
import os
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/xddd.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.secret_key = ':)'
db = SQLAlchemy(app)
login_manager = LoginManager(app)
# Main
@app.route('/')
def index():
return render_template("index.html")
# Login
@login_manager.user_loader
def load_user(user_id):
user = User.query.get(user_id)
return user
@app.route('/login', methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
email = form.email.data
password = form.password.data
user = User.query.filter_by(email=email).first()
if user:
if user.check_password(password):
login_user(user, force=True)
return redirect( url_for('index'))
else:
return "user not found"
return render_template("login.html", form=form)
@app.route('/signup', methods=["GET", "POST"])
def signup():
form = SignupForm()
if form.validate_on_submit():
name = form.name.data
email = form.email.data
password = form.password.data
confirm_password = form.confirm_password.data
if User.query.filter_by(email=email).first():
return 'taki email już istnieje'
user = User(name=name, email=email, password=password)
user.set_password(password)
db.session.add(user)
db.session.commit()
return redirect( url_for('index'))
return render_template("signup.html", form=form)
@login_required
@app.route('/logout')
def logout():
logout_user()
return render_template("logout.html")
@login_required
@app.route('/profile/<name>')
def user(name):
if name == current_user.name:
return "ok"
else:
return "no"
# DB Models
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(120), unique=True, nullable=False)
def set_password(self,password):
self.password = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password, password)
def __repr__(self):
return '<User {}>'.format(self.name)
@app.before_first_request
def create_all():
db.create_all()
# Forms
class LoginForm(FlaskForm):
email = StringField('email', validators=[DataRequired()])
password = StringField('hasło', validators=[DataRequired()])
submit = SubmitField('zaloguj się')
class SignupForm(FlaskForm):
name = StringField('nazwa użytkownika', validators=[DataRequired()])
email = StringField('email', validators=[DataRequired()])
password = StringField('hasło', validators=[DataRequired()])
confirm_password = StringField('powtórz hasło', validators=[DataRequired()])
submit = SubmitField('załóż konto')
# Errors
@app.errorhandler(404)
def handle_404(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def handle_500(e):
return render_template('500.html'), 500
if __name__=="__main__":
app.run(debug=True)
# Server
# Server side:
# - klucze
# - download_wall (swoj)
# - lista_zrodel
# - appka wyswietlajaca walle
# - szyfrowanie
# - przeslanie
# -