Ошибки...
This commit is contained in:
parent
df75b30da1
commit
8da8e71640
@ -1 +1,4 @@
|
||||
FLASK_APP=microblog.py
|
||||
FLASK_ENV=development
|
||||
FLASK_DEBUG=true
|
||||
FLASK_RUN_RELOAD=true
|
||||
|
||||
@ -1,2 +1,8 @@
|
||||
Код приложения по серии туториалов
|
||||
https://habr.com/ru/articles/804245/
|
||||
|
||||
Запуск отладочного мэйл сервера
|
||||
|
||||
```commandline
|
||||
aiosmtpd -n -c aiosmtpd.handlers.Debugging -l localhost:8025
|
||||
```
|
||||
11
app/errors.py
Normal file
11
app/errors.py
Normal file
@ -0,0 +1,11 @@
|
||||
from flask import render_template
|
||||
from app import app, db
|
||||
|
||||
@app.errorhandler(404)
|
||||
def not_found_error(error):
|
||||
return render_template('404.html'), 404
|
||||
|
||||
@app.errorhandler(500)
|
||||
def internal_error(error):
|
||||
db.session.rollback()
|
||||
return render_template('500.html'), 500
|
||||
@ -4,8 +4,6 @@ from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
|
||||
import sqlalchemy as sa
|
||||
from app import db
|
||||
from app.models import User
|
||||
from wtforms import TextAreaField
|
||||
from wtforms.validators import Length
|
||||
|
||||
|
||||
class LoginForm(FlaskForm):
|
||||
@ -34,8 +32,3 @@ class RegistrationForm(FlaskForm):
|
||||
User.email == email.data))
|
||||
if user is not None:
|
||||
raise ValidationError('Please use a different email address.')
|
||||
|
||||
class EditProfileForm(FlaskForm):
|
||||
username = StringField('Username', validators=[DataRequired()])
|
||||
about_me = TextAreaField('About me', validators=[Length(min=0, max=140)])
|
||||
submit = SubmitField('Submit')
|
||||
@ -5,7 +5,6 @@ import sqlalchemy.orm as so
|
||||
from flask_login import UserMixin
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
from app import db, login
|
||||
from hashlib import md5
|
||||
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
@ -18,9 +17,6 @@ class User(UserMixin, db.Model):
|
||||
|
||||
posts: so.WriteOnlyMapped['Post'] = so.relationship(
|
||||
back_populates='author')
|
||||
about_me: so.Mapped[Optional[str]] = so.mapped_column(sa.String(140))
|
||||
last_seen: so.Mapped[Optional[datetime]] = so.mapped_column(
|
||||
default=lambda: datetime.now(timezone.utc))
|
||||
|
||||
def __repr__(self):
|
||||
return '<User {}>'.format(self.username)
|
||||
@ -31,10 +27,6 @@ class User(UserMixin, db.Model):
|
||||
def check_password(self, password):
|
||||
return check_password_hash(self.password_hash, password)
|
||||
|
||||
def avatar(self, size):
|
||||
digest = md5(self.email.lower().encode('utf-8')).hexdigest()
|
||||
return f'https://www.gravatar.com/avatar/{digest}?d=identicon&s={size}'
|
||||
|
||||
|
||||
@login.user_loader
|
||||
def load_user(id):
|
||||
|
||||
@ -4,15 +4,8 @@ from flask_login import login_user, logout_user, current_user, login_required
|
||||
import sqlalchemy as sa
|
||||
from app import app, db
|
||||
from app.forms import LoginForm, RegistrationForm
|
||||
from app.forms import EditProfileForm
|
||||
from app.models import User
|
||||
from datetime import datetime, timezone
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
if current_user.is_authenticated:
|
||||
current_user.last_seen = datetime.now(timezone.utc)
|
||||
db.session.commit()
|
||||
|
||||
@app.route('/')
|
||||
@app.route('/index')
|
||||
@ -69,29 +62,3 @@ def register():
|
||||
flash('Congratulations, you are now a registered user!')
|
||||
return redirect(url_for('login'))
|
||||
return render_template('register.html', title='Register', form=form)
|
||||
|
||||
@app.route('/user/<username>')
|
||||
@login_required
|
||||
def user(username):
|
||||
user = db.first_or_404(sa.select(User).where(User.username == username))
|
||||
posts = [
|
||||
{'author': user, 'body': 'Test post #1'},
|
||||
{'author': user, 'body': 'Test post #2'}
|
||||
]
|
||||
return render_template('user.html', user=user, posts=posts)
|
||||
|
||||
@app.route('/edit_profile', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_profile():
|
||||
form = EditProfileForm()
|
||||
if form.validate_on_submit():
|
||||
current_user.username = form.username.data
|
||||
current_user.about_me = form.about_me.data
|
||||
db.session.commit()
|
||||
flash('Your changes have been saved.')
|
||||
return redirect(url_for('edit_profile'))
|
||||
elif request.method == 'GET':
|
||||
form.username.data = current_user.username
|
||||
form.about_me.data = current_user.about_me
|
||||
return render_template('edit_profile.html', title='Edit Profile',
|
||||
form=form)
|
||||
6
app/templates/404.html
Normal file
6
app/templates/404.html
Normal file
@ -0,0 +1,6 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>File Not Found</h1>
|
||||
<p><a href="{{ url_for('index') }}">Back</a></p>
|
||||
{% endblock %}
|
||||
7
app/templates/500.html
Normal file
7
app/templates/500.html
Normal file
@ -0,0 +1,7 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>An unexpected error has occurred</h1>
|
||||
<p>The administrator has been notified. Sorry for the inconvenience!</p>
|
||||
<p><a href="{{ url_for('index') }}">Back</a></p>
|
||||
{% endblock %}
|
||||
@ -14,7 +14,6 @@
|
||||
{% if current_user.is_anonymous %}
|
||||
<a href="{{ url_for('login') }}">Login</a>
|
||||
{% else %}
|
||||
<a href="{{ url_for('user', username=current_user.username) }}">Profile</a>
|
||||
<a href="{{ url_for('logout') }}">Logout</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
@ -6,3 +6,9 @@ class Config:
|
||||
SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
|
||||
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
|
||||
'sqlite:///' + os.path.join(basedir, 'app.db')
|
||||
MAIL_SERVER = os.environ.get('MAIL_SERVER')
|
||||
MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25)
|
||||
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None
|
||||
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
|
||||
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
|
||||
ADMINS = ['your-email@example.com']
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user