Compare commits
21 Commits
Author | SHA1 | Date |
---|---|---|
|
8b4993d2fc | |
|
a25d6f2f19 | |
|
7f32336d21 | |
|
b577aa6b62 | |
|
5cfb75def5 | |
|
94b40cf4b7 | |
|
868ad4b410 | |
|
f4a401e320 | |
|
7f7c8ef5d7 | |
|
8a56c34ce2 | |
|
a650e6f989 | |
|
542fa42312 | |
|
14f6f99ea9 | |
|
fdeb27d052 | |
|
d1363e0326 | |
|
8f43aadd41 | |
|
f9ba2d28cf | |
|
e7b1227a1c | |
|
68368ea5c5 | |
|
3172e0955c | |
|
5d4559227a |
|
@ -0,0 +1,23 @@
|
|||
FROM python:slim
|
||||
|
||||
RUN useradd microblog
|
||||
|
||||
WORKDIR /home/microblog
|
||||
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN python -m venv venv
|
||||
RUN venv/bin/pip install -r requirements.txt
|
||||
RUN venv/bin/pip install gunicorn pymysql cryptography
|
||||
|
||||
COPY app app
|
||||
COPY migrations migrations
|
||||
COPY microblog.py config.py boot.sh ./
|
||||
RUN chmod a+x boot.sh
|
||||
|
||||
ENV FLASK_APP microblog.py
|
||||
|
||||
RUN chown -R microblog:microblog ./
|
||||
USER microblog
|
||||
|
||||
EXPOSE 5000
|
||||
ENTRYPOINT ["./boot.sh"]
|
|
@ -0,0 +1 @@
|
|||
web: flask db upgrade; flask translate compile; gunicorn microblog:app
|
|
@ -0,0 +1,7 @@
|
|||
Vagrant.configure("2") do |config|
|
||||
config.vm.box = "ubuntu/focal64"
|
||||
config.vm.network "private_network", ip: "192.168.33.10"
|
||||
config.vm.provider "virtualbox" do |vb|
|
||||
vb.memory = "2048"
|
||||
end
|
||||
end
|
|
@ -1,5 +1,92 @@
|
|||
from flask import Flask
|
||||
import logging
|
||||
from logging.handlers import SMTPHandler, RotatingFileHandler
|
||||
import os
|
||||
from flask import Flask, request, current_app
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
from flask_login import LoginManager
|
||||
from flask_mail import Mail
|
||||
from flask_bootstrap import Bootstrap
|
||||
from flask_moment import Moment
|
||||
from flask_babel import Babel, lazy_gettext as _l
|
||||
from elasticsearch import Elasticsearch
|
||||
from config import Config
|
||||
|
||||
app = Flask(__name__)
|
||||
db = SQLAlchemy()
|
||||
migrate = Migrate()
|
||||
login = LoginManager()
|
||||
login.login_view = 'auth.login'
|
||||
login.login_message = _l('Please log in to access this page.')
|
||||
mail = Mail()
|
||||
bootstrap = Bootstrap()
|
||||
moment = Moment()
|
||||
babel = Babel()
|
||||
|
||||
from app import routes
|
||||
|
||||
def create_app(config_class=Config):
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(config_class)
|
||||
|
||||
db.init_app(app)
|
||||
migrate.init_app(app, db)
|
||||
login.init_app(app)
|
||||
mail.init_app(app)
|
||||
bootstrap.init_app(app)
|
||||
moment.init_app(app)
|
||||
babel.init_app(app)
|
||||
app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) \
|
||||
if app.config['ELASTICSEARCH_URL'] else None
|
||||
|
||||
from app.errors import bp as errors_bp
|
||||
app.register_blueprint(errors_bp)
|
||||
|
||||
from app.auth import bp as auth_bp
|
||||
app.register_blueprint(auth_bp, url_prefix='/auth')
|
||||
|
||||
from app.main import bp as main_bp
|
||||
app.register_blueprint(main_bp)
|
||||
|
||||
if not app.debug and not app.testing:
|
||||
if app.config['MAIL_SERVER']:
|
||||
auth = None
|
||||
if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
|
||||
auth = (app.config['MAIL_USERNAME'],
|
||||
app.config['MAIL_PASSWORD'])
|
||||
secure = None
|
||||
if app.config['MAIL_USE_TLS']:
|
||||
secure = ()
|
||||
mail_handler = SMTPHandler(
|
||||
mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
|
||||
fromaddr='no-reply@' + app.config['MAIL_SERVER'],
|
||||
toaddrs=app.config['ADMINS'], subject='Microblog Failure',
|
||||
credentials=auth, secure=secure)
|
||||
mail_handler.setLevel(logging.ERROR)
|
||||
app.logger.addHandler(mail_handler)
|
||||
|
||||
if app.config['LOG_TO_STDOUT']:
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setLevel(logging.INFO)
|
||||
app.logger.addHandler(stream_handler)
|
||||
else:
|
||||
if not os.path.exists('logs'):
|
||||
os.mkdir('logs')
|
||||
file_handler = RotatingFileHandler('logs/microblog.log',
|
||||
maxBytes=10240, backupCount=10)
|
||||
file_handler.setFormatter(logging.Formatter(
|
||||
'%(asctime)s %(levelname)s: %(message)s '
|
||||
'[in %(pathname)s:%(lineno)d]'))
|
||||
file_handler.setLevel(logging.INFO)
|
||||
app.logger.addHandler(file_handler)
|
||||
|
||||
app.logger.setLevel(logging.INFO)
|
||||
app.logger.info('Microblog startup')
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@babel.localeselector
|
||||
def get_locale():
|
||||
return request.accept_languages.best_match(current_app.config['LANGUAGES'])
|
||||
|
||||
|
||||
from app import models
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
from flask import Blueprint
|
||||
|
||||
bp = Blueprint('auth', __name__)
|
||||
|
||||
from app.auth import routes
|
|
@ -0,0 +1,14 @@
|
|||
from flask import render_template, current_app
|
||||
from flask_babel import _
|
||||
from app.email import send_email
|
||||
|
||||
|
||||
def send_password_reset_email(user):
|
||||
token = user.get_reset_password_token()
|
||||
send_email(_('[Microblog] Reset Your Password'),
|
||||
sender=current_app.config['ADMINS'][0],
|
||||
recipients=[user.email],
|
||||
text_body=render_template('email/reset_password.txt',
|
||||
user=user, token=token),
|
||||
html_body=render_template('email/reset_password.html',
|
||||
user=user, token=token))
|
|
@ -0,0 +1,45 @@
|
|||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, PasswordField, BooleanField, SubmitField
|
||||
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
|
||||
from flask_babel import _, lazy_gettext as _l
|
||||
from app.models import User
|
||||
|
||||
|
||||
class LoginForm(FlaskForm):
|
||||
username = StringField(_l('Username'), validators=[DataRequired()])
|
||||
password = PasswordField(_l('Password'), validators=[DataRequired()])
|
||||
remember_me = BooleanField(_l('Remember Me'))
|
||||
submit = SubmitField(_l('Sign In'))
|
||||
|
||||
|
||||
class RegistrationForm(FlaskForm):
|
||||
username = StringField(_l('Username'), validators=[DataRequired()])
|
||||
email = StringField(_l('Email'), validators=[DataRequired(), Email()])
|
||||
password = PasswordField(_l('Password'), validators=[DataRequired()])
|
||||
password2 = PasswordField(
|
||||
_l('Repeat Password'), validators=[DataRequired(),
|
||||
EqualTo('password')])
|
||||
submit = SubmitField(_l('Register'))
|
||||
|
||||
def validate_username(self, username):
|
||||
user = User.query.filter_by(username=username.data).first()
|
||||
if user is not None:
|
||||
raise ValidationError(_('Please use a different username.'))
|
||||
|
||||
def validate_email(self, email):
|
||||
user = User.query.filter_by(email=email.data).first()
|
||||
if user is not None:
|
||||
raise ValidationError(_('Please use a different email address.'))
|
||||
|
||||
|
||||
class ResetPasswordRequestForm(FlaskForm):
|
||||
email = StringField(_l('Email'), validators=[DataRequired(), Email()])
|
||||
submit = SubmitField(_l('Request Password Reset'))
|
||||
|
||||
|
||||
class ResetPasswordForm(FlaskForm):
|
||||
password = PasswordField(_l('Password'), validators=[DataRequired()])
|
||||
password2 = PasswordField(
|
||||
_l('Repeat Password'), validators=[DataRequired(),
|
||||
EqualTo('password')])
|
||||
submit = SubmitField(_l('Request Password Reset'))
|
|
@ -0,0 +1,82 @@
|
|||
from flask import render_template, redirect, url_for, flash, request
|
||||
from werkzeug.urls import url_parse
|
||||
from flask_login import login_user, logout_user, current_user
|
||||
from flask_babel import _
|
||||
from app import db
|
||||
from app.auth import bp
|
||||
from app.auth.forms import LoginForm, RegistrationForm, \
|
||||
ResetPasswordRequestForm, ResetPasswordForm
|
||||
from app.models import User
|
||||
from app.auth.email import send_password_reset_email
|
||||
|
||||
|
||||
@bp.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
form = LoginForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(username=form.username.data).first()
|
||||
if user is None or not user.check_password(form.password.data):
|
||||
flash(_('Invalid username or password'))
|
||||
return redirect(url_for('auth.login'))
|
||||
login_user(user, remember=form.remember_me.data)
|
||||
next_page = request.args.get('next')
|
||||
if not next_page or url_parse(next_page).netloc != '':
|
||||
next_page = url_for('main.index')
|
||||
return redirect(next_page)
|
||||
return render_template('auth/login.html', title=_('Sign In'), form=form)
|
||||
|
||||
|
||||
@bp.route('/logout')
|
||||
def logout():
|
||||
logout_user()
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
|
||||
@bp.route('/register', methods=['GET', 'POST'])
|
||||
def register():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
form = RegistrationForm()
|
||||
if form.validate_on_submit():
|
||||
user = User(username=form.username.data, email=form.email.data)
|
||||
user.set_password(form.password.data)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
flash(_('Congratulations, you are now a registered user!'))
|
||||
return redirect(url_for('auth.login'))
|
||||
return render_template('auth/register.html', title=_('Register'),
|
||||
form=form)
|
||||
|
||||
|
||||
@bp.route('/reset_password_request', methods=['GET', 'POST'])
|
||||
def reset_password_request():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
form = ResetPasswordRequestForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(email=form.email.data).first()
|
||||
if user:
|
||||
send_password_reset_email(user)
|
||||
flash(
|
||||
_('Check your email for the instructions to reset your password'))
|
||||
return redirect(url_for('auth.login'))
|
||||
return render_template('auth/reset_password_request.html',
|
||||
title=_('Reset Password'), form=form)
|
||||
|
||||
|
||||
@bp.route('/reset_password/<token>', methods=['GET', 'POST'])
|
||||
def reset_password(token):
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
user = User.verify_reset_password_token(token)
|
||||
if not user:
|
||||
return redirect(url_for('main.index'))
|
||||
form = ResetPasswordForm()
|
||||
if form.validate_on_submit():
|
||||
user.set_password(form.password.data)
|
||||
db.session.commit()
|
||||
flash(_('Your password has been reset.'))
|
||||
return redirect(url_for('auth.login'))
|
||||
return render_template('auth/reset_password.html', form=form)
|
|
@ -0,0 +1,35 @@
|
|||
import os
|
||||
import click
|
||||
|
||||
|
||||
def register(app):
|
||||
@app.cli.group()
|
||||
def translate():
|
||||
"""Translation and localization commands."""
|
||||
pass
|
||||
|
||||
@translate.command()
|
||||
@click.argument('lang')
|
||||
def init(lang):
|
||||
"""Initialize a new language."""
|
||||
if os.system('pybabel extract -F babel.cfg -k _l -o messages.pot .'):
|
||||
raise RuntimeError('extract command failed')
|
||||
if os.system(
|
||||
'pybabel init -i messages.pot -d app/translations -l ' + lang):
|
||||
raise RuntimeError('init command failed')
|
||||
os.remove('messages.pot')
|
||||
|
||||
@translate.command()
|
||||
def update():
|
||||
"""Update all languages."""
|
||||
if os.system('pybabel extract -F babel.cfg -k _l -o messages.pot .'):
|
||||
raise RuntimeError('extract command failed')
|
||||
if os.system('pybabel update -i messages.pot -d app/translations'):
|
||||
raise RuntimeError('update command failed')
|
||||
os.remove('messages.pot')
|
||||
|
||||
@translate.command()
|
||||
def compile():
|
||||
"""Compile all languages."""
|
||||
if os.system('pybabel compile -d app/translations'):
|
||||
raise RuntimeError('compile command failed')
|
|
@ -0,0 +1,17 @@
|
|||
from threading import Thread
|
||||
from flask import current_app
|
||||
from flask_mail import Message
|
||||
from app import mail
|
||||
|
||||
|
||||
def send_async_email(app, msg):
|
||||
with app.app_context():
|
||||
mail.send(msg)
|
||||
|
||||
|
||||
def send_email(subject, sender, recipients, text_body, html_body):
|
||||
msg = Message(subject, sender=sender, recipients=recipients)
|
||||
msg.body = text_body
|
||||
msg.html = html_body
|
||||
Thread(target=send_async_email,
|
||||
args=(current_app._get_current_object(), msg)).start()
|
|
@ -0,0 +1,5 @@
|
|||
from flask import Blueprint
|
||||
|
||||
bp = Blueprint('errors', __name__)
|
||||
|
||||
from app.errors import handlers
|
|
@ -0,0 +1,14 @@
|
|||
from flask import render_template
|
||||
from app import db
|
||||
from app.errors import bp
|
||||
|
||||
|
||||
@bp.app_errorhandler(404)
|
||||
def not_found_error(error):
|
||||
return render_template('errors/404.html'), 404
|
||||
|
||||
|
||||
@bp.app_errorhandler(500)
|
||||
def internal_error(error):
|
||||
db.session.rollback()
|
||||
return render_template('errors/500.html'), 500
|
|
@ -0,0 +1,5 @@
|
|||
from flask import Blueprint
|
||||
|
||||
bp = Blueprint('main', __name__)
|
||||
|
||||
from app.main import routes
|
|
@ -0,0 +1,49 @@
|
|||
from flask import request
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, SubmitField, TextAreaField
|
||||
from wtforms.validators import ValidationError, DataRequired, Length
|
||||
from flask_babel import _, lazy_gettext as _l
|
||||
from app.models import User
|
||||
|
||||
|
||||
class EditProfileForm(FlaskForm):
|
||||
username = StringField(_l('Username'), validators=[DataRequired()])
|
||||
about_me = TextAreaField(_l('About me'),
|
||||
validators=[Length(min=0, max=140)])
|
||||
submit = SubmitField(_l('Submit'))
|
||||
|
||||
def __init__(self, original_username, *args, **kwargs):
|
||||
super(EditProfileForm, self).__init__(*args, **kwargs)
|
||||
self.original_username = original_username
|
||||
|
||||
def validate_username(self, username):
|
||||
if username.data != self.original_username:
|
||||
user = User.query.filter_by(username=self.username.data).first()
|
||||
if user is not None:
|
||||
raise ValidationError(_('Please use a different username.'))
|
||||
|
||||
|
||||
class EmptyForm(FlaskForm):
|
||||
submit = SubmitField('Submit')
|
||||
|
||||
|
||||
class PostForm(FlaskForm):
|
||||
post = TextAreaField(_l('Say something'), validators=[DataRequired()])
|
||||
submit = SubmitField(_l('Submit'))
|
||||
|
||||
|
||||
class SearchForm(FlaskForm):
|
||||
q = StringField(_l('Search'), validators=[DataRequired()])
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
if 'formdata' not in kwargs:
|
||||
kwargs['formdata'] = request.args
|
||||
if 'meta' not in kwargs:
|
||||
kwargs['meta'] = {'csrf': False}
|
||||
super(SearchForm, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class MessageForm(FlaskForm):
|
||||
message = TextAreaField(_l('Message'), validators=[
|
||||
DataRequired(), Length(min=1, max=140)])
|
||||
submit = SubmitField(_l('Submit'))
|
|
@ -0,0 +1,224 @@
|
|||
from datetime import datetime
|
||||
from flask import render_template, flash, redirect, url_for, request, g, \
|
||||
jsonify, current_app
|
||||
from flask_login import current_user, login_required
|
||||
from flask_babel import _, get_locale
|
||||
from langdetect import detect, LangDetectException
|
||||
from app import db
|
||||
from app.main.forms import EditProfileForm, EmptyForm, PostForm, SearchForm, \
|
||||
MessageForm
|
||||
from app.models import User, Post, Message, Notification
|
||||
from app.translate import translate
|
||||
from app.main import bp
|
||||
|
||||
|
||||
@bp.before_app_request
|
||||
def before_request():
|
||||
if current_user.is_authenticated:
|
||||
current_user.last_seen = datetime.utcnow()
|
||||
db.session.commit()
|
||||
g.search_form = SearchForm()
|
||||
g.locale = str(get_locale())
|
||||
|
||||
|
||||
@bp.route('/', methods=['GET', 'POST'])
|
||||
@bp.route('/index', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def index():
|
||||
form = PostForm()
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
language = detect(form.post.data)
|
||||
except LangDetectException:
|
||||
language = ''
|
||||
post = Post(body=form.post.data, author=current_user,
|
||||
language=language)
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
flash(_('Your post is now live!'))
|
||||
return redirect(url_for('main.index'))
|
||||
page = request.args.get('page', 1, type=int)
|
||||
posts = current_user.followed_posts().paginate(
|
||||
page=page, per_page=current_app.config['POSTS_PER_PAGE'],
|
||||
error_out=False)
|
||||
next_url = url_for('main.index', page=posts.next_num) \
|
||||
if posts.has_next else None
|
||||
prev_url = url_for('main.index', page=posts.prev_num) \
|
||||
if posts.has_prev else None
|
||||
return render_template('index.html', title=_('Home'), form=form,
|
||||
posts=posts.items, next_url=next_url,
|
||||
prev_url=prev_url)
|
||||
|
||||
|
||||
@bp.route('/explore')
|
||||
@login_required
|
||||
def explore():
|
||||
page = request.args.get('page', 1, type=int)
|
||||
posts = Post.query.order_by(Post.timestamp.desc()).paginate(
|
||||
page=page, per_page=current_app.config['POSTS_PER_PAGE'],
|
||||
error_out=False)
|
||||
next_url = url_for('main.explore', page=posts.next_num) \
|
||||
if posts.has_next else None
|
||||
prev_url = url_for('main.explore', page=posts.prev_num) \
|
||||
if posts.has_prev else None
|
||||
return render_template('index.html', title=_('Explore'),
|
||||
posts=posts.items, next_url=next_url,
|
||||
prev_url=prev_url)
|
||||
|
||||
@bp.route('/about')
|
||||
def about_page():
|
||||
return render_template('about.html', title=_('About me'))
|
||||
|
||||
@bp.route('/user/<username>')
|
||||
@login_required
|
||||
def user(username):
|
||||
user = User.query.filter_by(username=username).first_or_404()
|
||||
page = request.args.get('page', 1, type=int)
|
||||
posts = user.posts.order_by(Post.timestamp.desc()).paginate(
|
||||
page=page, per_page=current_app.config['POSTS_PER_PAGE'],
|
||||
error_out=False)
|
||||
next_url = url_for('main.user', username=user.username,
|
||||
page=posts.next_num) if posts.has_next else None
|
||||
prev_url = url_for('main.user', username=user.username,
|
||||
page=posts.prev_num) if posts.has_prev else None
|
||||
form = EmptyForm()
|
||||
return render_template('user.html', user=user, posts=posts.items,
|
||||
next_url=next_url, prev_url=prev_url, form=form)
|
||||
|
||||
|
||||
@bp.route('/user/<username>/popup')
|
||||
@login_required
|
||||
def user_popup(username):
|
||||
user = User.query.filter_by(username=username).first_or_404()
|
||||
form = EmptyForm()
|
||||
return render_template('user_popup.html', user=user, form=form)
|
||||
|
||||
|
||||
@bp.route('/edit_profile', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_profile():
|
||||
form = EditProfileForm(current_user.username)
|
||||
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('main.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)
|
||||
|
||||
|
||||
@bp.route('/follow/<username>', methods=['POST'])
|
||||
@login_required
|
||||
def follow(username):
|
||||
form = EmptyForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user is None:
|
||||
flash(_('User %(username)s not found.', username=username))
|
||||
return redirect(url_for('main.index'))
|
||||
if user == current_user:
|
||||
flash(_('You cannot follow yourself!'))
|
||||
return redirect(url_for('main.user', username=username))
|
||||
current_user.follow(user)
|
||||
db.session.commit()
|
||||
flash(_('You are following %(username)s!', username=username))
|
||||
return redirect(url_for('main.user', username=username))
|
||||
else:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
|
||||
@bp.route('/unfollow/<username>', methods=['POST'])
|
||||
@login_required
|
||||
def unfollow(username):
|
||||
form = EmptyForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user is None:
|
||||
flash(_('User %(username)s not found.', username=username))
|
||||
return redirect(url_for('main.index'))
|
||||
if user == current_user:
|
||||
flash(_('You cannot unfollow yourself!'))
|
||||
return redirect(url_for('main.user', username=username))
|
||||
current_user.unfollow(user)
|
||||
db.session.commit()
|
||||
flash(_('You are not following %(username)s.', username=username))
|
||||
return redirect(url_for('main.user', username=username))
|
||||
else:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
|
||||
@bp.route('/translate', methods=['POST'])
|
||||
@login_required
|
||||
def translate_text():
|
||||
return jsonify({'text': translate(request.form['text'],
|
||||
request.form['source_language'],
|
||||
request.form['dest_language'])})
|
||||
|
||||
|
||||
@bp.route('/search')
|
||||
@login_required
|
||||
def search():
|
||||
if not g.search_form.validate():
|
||||
return redirect(url_for('main.explore'))
|
||||
page = request.args.get('page', 1, type=int)
|
||||
posts, total = Post.search(g.search_form.q.data, page,
|
||||
current_app.config['POSTS_PER_PAGE'])
|
||||
next_url = url_for('main.search', q=g.search_form.q.data, page=page + 1) \
|
||||
if total > page * current_app.config['POSTS_PER_PAGE'] else None
|
||||
prev_url = url_for('main.search', q=g.search_form.q.data, page=page - 1) \
|
||||
if page > 1 else None
|
||||
return render_template('search.html', title=_('Search'), posts=posts,
|
||||
next_url=next_url, prev_url=prev_url)
|
||||
|
||||
|
||||
@bp.route('/send_message/<recipient>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def send_message(recipient):
|
||||
user = User.query.filter_by(username=recipient).first_or_404()
|
||||
form = MessageForm()
|
||||
if form.validate_on_submit():
|
||||
msg = Message(author=current_user, recipient=user,
|
||||
body=form.message.data)
|
||||
db.session.add(msg)
|
||||
user.add_notification('unread_message_count', user.new_messages())
|
||||
db.session.commit()
|
||||
flash(_('Your message has been sent.'))
|
||||
return redirect(url_for('main.user', username=recipient))
|
||||
return render_template('send_message.html', title=_('Send Message'),
|
||||
form=form, recipient=recipient)
|
||||
|
||||
|
||||
@bp.route('/messages')
|
||||
@login_required
|
||||
def messages():
|
||||
current_user.last_message_read_time = datetime.utcnow()
|
||||
current_user.add_notification('unread_message_count', 0)
|
||||
db.session.commit()
|
||||
page = request.args.get('page', 1, type=int)
|
||||
messages = current_user.messages_received.order_by(
|
||||
Message.timestamp.desc()).paginate(
|
||||
page=page, per_page=current_app.config['POSTS_PER_PAGE'],
|
||||
error_out=False)
|
||||
next_url = url_for('main.messages', page=messages.next_num) \
|
||||
if messages.has_next else None
|
||||
prev_url = url_for('main.messages', page=messages.prev_num) \
|
||||
if messages.has_prev else None
|
||||
return render_template('messages.html', messages=messages.items,
|
||||
next_url=next_url, prev_url=prev_url)
|
||||
|
||||
|
||||
@bp.route('/notifications')
|
||||
@login_required
|
||||
def notifications():
|
||||
since = request.args.get('since', 0.0, type=float)
|
||||
notifications = current_user.notifications.filter(
|
||||
Notification.timestamp > since).order_by(Notification.timestamp.asc())
|
||||
return jsonify([{
|
||||
'name': n.name,
|
||||
'data': n.get_data(),
|
||||
'timestamp': n.timestamp
|
||||
} for n in notifications])
|
|
@ -0,0 +1,181 @@
|
|||
from datetime import datetime
|
||||
from hashlib import md5
|
||||
import json
|
||||
from time import time
|
||||
from flask import current_app
|
||||
from flask_login import UserMixin
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
import jwt
|
||||
from app import db, login
|
||||
from app.search import add_to_index, remove_from_index, query_index
|
||||
|
||||
|
||||
class SearchableMixin(object):
|
||||
@classmethod
|
||||
def search(cls, expression, page, per_page):
|
||||
ids, total = query_index(cls.__tablename__, expression, page, per_page)
|
||||
if total == 0:
|
||||
return cls.query.filter_by(id=0), 0
|
||||
when = []
|
||||
for i in range(len(ids)):
|
||||
when.append((ids[i], i))
|
||||
return cls.query.filter(cls.id.in_(ids)).order_by(
|
||||
db.case(when, value=cls.id)), total
|
||||
|
||||
@classmethod
|
||||
def before_commit(cls, session):
|
||||
session._changes = {
|
||||
'add': list(session.new),
|
||||
'update': list(session.dirty),
|
||||
'delete': list(session.deleted)
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def after_commit(cls, session):
|
||||
for obj in session._changes['add']:
|
||||
if isinstance(obj, SearchableMixin):
|
||||
add_to_index(obj.__tablename__, obj)
|
||||
for obj in session._changes['update']:
|
||||
if isinstance(obj, SearchableMixin):
|
||||
add_to_index(obj.__tablename__, obj)
|
||||
for obj in session._changes['delete']:
|
||||
if isinstance(obj, SearchableMixin):
|
||||
remove_from_index(obj.__tablename__, obj)
|
||||
session._changes = None
|
||||
|
||||
@classmethod
|
||||
def reindex(cls):
|
||||
for obj in cls.query:
|
||||
add_to_index(cls.__tablename__, obj)
|
||||
|
||||
|
||||
db.event.listen(db.session, 'before_commit', SearchableMixin.before_commit)
|
||||
db.event.listen(db.session, 'after_commit', SearchableMixin.after_commit)
|
||||
|
||||
|
||||
followers = db.Table(
|
||||
'followers',
|
||||
db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
|
||||
db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))
|
||||
)
|
||||
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(64), index=True, unique=True)
|
||||
email = db.Column(db.String(120), index=True, unique=True)
|
||||
password_hash = db.Column(db.String(128))
|
||||
posts = db.relationship('Post', backref='author', lazy='dynamic')
|
||||
about_me = db.Column(db.String(140))
|
||||
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
followed = db.relationship(
|
||||
'User', secondary=followers,
|
||||
primaryjoin=(followers.c.follower_id == id),
|
||||
secondaryjoin=(followers.c.followed_id == id),
|
||||
backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')
|
||||
messages_sent = db.relationship('Message',
|
||||
foreign_keys='Message.sender_id',
|
||||
backref='author', lazy='dynamic')
|
||||
messages_received = db.relationship('Message',
|
||||
foreign_keys='Message.recipient_id',
|
||||
backref='recipient', lazy='dynamic')
|
||||
last_message_read_time = db.Column(db.DateTime)
|
||||
notifications = db.relationship('Notification', backref='user',
|
||||
lazy='dynamic')
|
||||
|
||||
def __repr__(self):
|
||||
return '<User {}>'.format(self.username)
|
||||
|
||||
def set_password(self, password):
|
||||
self.password_hash = generate_password_hash(password)
|
||||
|
||||
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 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(
|
||||
digest, size)
|
||||
|
||||
def follow(self, user):
|
||||
if not self.is_following(user):
|
||||
self.followed.append(user)
|
||||
|
||||
def unfollow(self, user):
|
||||
if self.is_following(user):
|
||||
self.followed.remove(user)
|
||||
|
||||
def is_following(self, user):
|
||||
return self.followed.filter(
|
||||
followers.c.followed_id == user.id).count() > 0
|
||||
|
||||
def followed_posts(self):
|
||||
followed = Post.query.join(
|
||||
followers, (followers.c.followed_id == Post.user_id)).filter(
|
||||
followers.c.follower_id == self.id)
|
||||
own = Post.query.filter_by(user_id=self.id)
|
||||
return followed.union(own).order_by(Post.timestamp.desc())
|
||||
|
||||
def get_reset_password_token(self, expires_in=600):
|
||||
return jwt.encode(
|
||||
{'reset_password': self.id, 'exp': time() + expires_in},
|
||||
current_app.config['SECRET_KEY'], algorithm='HS256')
|
||||
|
||||
@staticmethod
|
||||
def verify_reset_password_token(token):
|
||||
try:
|
||||
id = jwt.decode(token, current_app.config['SECRET_KEY'],
|
||||
algorithms=['HS256'])['reset_password']
|
||||
except:
|
||||
return
|
||||
return User.query.get(id)
|
||||
|
||||
def new_messages(self):
|
||||
last_read_time = self.last_message_read_time or datetime(1900, 1, 1)
|
||||
return Message.query.filter_by(recipient=self).filter(
|
||||
Message.timestamp > last_read_time).count()
|
||||
|
||||
def add_notification(self, name, data):
|
||||
self.notifications.filter_by(name=name).delete()
|
||||
n = Notification(name=name, payload_json=json.dumps(data), user=self)
|
||||
db.session.add(n)
|
||||
return n
|
||||
|
||||
|
||||
@login.user_loader
|
||||
def load_user(id):
|
||||
return User.query.get(int(id))
|
||||
|
||||
|
||||
class Post(SearchableMixin, db.Model):
|
||||
__searchable__ = ['body']
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
body = db.Column(db.String(140))
|
||||
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
language = db.Column(db.String(5))
|
||||
|
||||
def __repr__(self):
|
||||
return '<Post {}>'.format(self.body)
|
||||
|
||||
|
||||
class Message(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
sender_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
body = db.Column(db.String(140))
|
||||
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
|
||||
|
||||
def __repr__(self):
|
||||
return '<Message {}>'.format(self.body)
|
||||
|
||||
|
||||
class Notification(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(128), index=True)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
timestamp = db.Column(db.Float, index=True, default=time)
|
||||
payload_json = db.Column(db.Text)
|
||||
|
||||
def get_data(self):
|
||||
return json.loads(str(self.payload_json))
|
|
@ -1,7 +0,0 @@
|
|||
from app import app
|
||||
|
||||
|
||||
@app.route('/')
|
||||
@app.route('/index')
|
||||
def index():
|
||||
return "Hello, World!"
|
|
@ -0,0 +1,27 @@
|
|||
from flask import current_app
|
||||
|
||||
|
||||
def add_to_index(index, model):
|
||||
if not current_app.elasticsearch:
|
||||
return
|
||||
payload = {}
|
||||
for field in model.__searchable__:
|
||||
payload[field] = getattr(model, field)
|
||||
current_app.elasticsearch.index(index=index, id=model.id, body=payload)
|
||||
|
||||
|
||||
def remove_from_index(index, model):
|
||||
if not current_app.elasticsearch:
|
||||
return
|
||||
current_app.elasticsearch.delete(index=index, id=model.id)
|
||||
|
||||
|
||||
def query_index(index, query, page, per_page):
|
||||
if not current_app.elasticsearch:
|
||||
return [], 0
|
||||
search = current_app.elasticsearch.search(
|
||||
index=index,
|
||||
body={'query': {'multi_match': {'query': query, 'fields': ['*']}},
|
||||
'from': (page - 1) * per_page, 'size': per_page})
|
||||
ids = [int(hit['_id']) for hit in search['hits']['hits']]
|
||||
return ids, search['hits']['total']['value']
|
Binary file not shown.
After Width: | Height: | Size: 673 B |
|
@ -0,0 +1,32 @@
|
|||
<table class="table table-hover">
|
||||
<tr>
|
||||
<td width="70px">
|
||||
<a href="{{ url_for('main.user', username=post.author.username) }}">
|
||||
<img src="{{ post.author.avatar(70) }}" />
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
{% set user_link %}
|
||||
<span class="user_popup">
|
||||
<a href="{{ url_for('main.user', username=post.author.username) }}">
|
||||
{{ post.author.username }}
|
||||
</a>
|
||||
</span>
|
||||
{% endset %}
|
||||
{{ _('%(username)s said %(when)s',
|
||||
username=user_link, when=moment(post.timestamp).fromNow()) }}
|
||||
<br>
|
||||
<span id="post{{ post.id }}">{{ post.body }}</span>
|
||||
{% if post.language and post.language != g.locale %}
|
||||
<br><br>
|
||||
<span id="translation{{ post.id }}">
|
||||
<a href="javascript:translate(
|
||||
'#post{{ post.id }}',
|
||||
'#translation{{ post.id }}',
|
||||
'{{ post.language }}',
|
||||
'{{ g.locale }}');">{{ _('Translate') }}</a>
|
||||
</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
|
@ -0,0 +1,10 @@
|
|||
{% extends "base.html" %} {% block app_content %}
|
||||
|
||||
<h1>A propos</h1>
|
||||
<p>
|
||||
Bonjour, je m'appelle Gaëtan et j'ai modifié
|
||||
<a href="https://github.com/miguelgrinberg/microblog"
|
||||
>l'application Microblog de Miguel Grinberg pour ce TP</a
|
||||
>.
|
||||
</p>
|
||||
{% endblock %}
|
|
@ -0,0 +1,17 @@
|
|||
{% extends 'base.html' %}
|
||||
{% import 'bootstrap/wtf.html' as wtf %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Sign In') }}</h1>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{{ wtf.quick_form(form) }}
|
||||
</div>
|
||||
</div>
|
||||
<br>
|
||||
<p>{{ _('New User?') }} <a href="{{ url_for('auth.register') }}">{{ _('Click to Register!') }}</a></p>
|
||||
<p>
|
||||
{{ _('Forgot Your Password?') }}
|
||||
<a href="{{ url_for('auth.reset_password_request') }}">{{ _('Click to Reset It') }}</a>
|
||||
</p>
|
||||
{% endblock %}
|
|
@ -0,0 +1,11 @@
|
|||
{% extends "base.html" %}
|
||||
{% import 'bootstrap/wtf.html' as wtf %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Register') }}</h1>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{{ wtf.quick_form(form) }}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -0,0 +1,11 @@
|
|||
{% extends "base.html" %}
|
||||
{% import 'bootstrap/wtf.html' as wtf %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Reset Your Password') }}</h1>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{{ wtf.quick_form(form) }}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -0,0 +1,11 @@
|
|||
{% extends "base.html" %}
|
||||
{% import 'bootstrap/wtf.html' as wtf %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Reset Password') }}</h1>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{{ wtf.quick_form(form) }}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -0,0 +1,150 @@
|
|||
{% extends 'bootstrap/base.html' %}
|
||||
|
||||
{% block title %}
|
||||
{% if title %}{{ title }} - Microblog{% else %}{{ _('Welcome to Microblog') }}{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block navbar %}
|
||||
<nav class="navbar navbar-default">
|
||||
<div class="container">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
|
||||
<span class="sr-only">Toggle navigation</span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="{{ url_for('main.index') }}">Microblog</a>
|
||||
</div>
|
||||
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
|
||||
<ul class="nav navbar-nav">
|
||||
<li><a href="{{ url_for('main.index') }}">{{ _('Home') }}</a></li>
|
||||
<li><a href="{{ url_for('main.explore') }}">{{ _('Explore') }}</a></li>
|
||||
<li><a href="{{ url_for('main.about_page') }}">{{ _('About') }}</a></li>
|
||||
</ul>
|
||||
{% if g.search_form %}
|
||||
<form class="navbar-form navbar-left" method="get" action="{{ url_for('main.search') }}">
|
||||
<div class="form-group">
|
||||
{{ g.search_form.q(size=20, class='form-control', placeholder=g.search_form.q.label.text) }}
|
||||
</div>
|
||||
</form>
|
||||
{% endif %}
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
{% if current_user.is_anonymous %}
|
||||
<li><a href="{{ url_for('auth.login') }}">{{ _('Login') }}</a></li>
|
||||
{% else %}
|
||||
<li>
|
||||
<a href="{{ url_for('main.messages') }}">{{ _('Messages') }}
|
||||
{% set new_messages = current_user.new_messages() %}
|
||||
<span id="message_count" class="badge"
|
||||
style="visibility: {% if new_messages %}visible
|
||||
{% else %}hidden{% endif %};">
|
||||
{{ new_messages }}
|
||||
</span>
|
||||
</a>
|
||||
</li>
|
||||
<li><a href="{{ url_for('main.user', username=current_user.username) }}">{{ _('Profile') }}</a></li>
|
||||
<li><a href="{{ url_for('auth.logout') }}">{{ _('Logout') }}</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
{% with messages = get_flashed_messages() %}
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-info" role="alert">{{ message }}</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
{# application content needs to be provided in the app_content block #}
|
||||
{% block app_content %}{% endblock %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
{{ super() }}
|
||||
{{ moment.include_moment() }}
|
||||
{{ moment.lang(g.locale) }}
|
||||
<script>
|
||||
function translate(sourceElem, destElem, sourceLang, destLang) {
|
||||
$(destElem).html('<img src="{{ url_for('static', filename='loading.gif') }}">');
|
||||
$.post('/translate', {
|
||||
text: $(sourceElem).text(),
|
||||
source_language: sourceLang,
|
||||
dest_language: destLang
|
||||
}).done(function(response) {
|
||||
$(destElem).text(response['text'])
|
||||
}).fail(function() {
|
||||
$(destElem).text("{{ _('Error: Could not contact server.') }}");
|
||||
});
|
||||
}
|
||||
$(function () {
|
||||
var timer = null;
|
||||
var xhr = null;
|
||||
$('.user_popup').hover(
|
||||
function(event) {
|
||||
// mouse in event handler
|
||||
var elem = $(event.currentTarget);
|
||||
timer = setTimeout(function() {
|
||||
timer = null;
|
||||
xhr = $.ajax(
|
||||
'/user/' + elem.first().text().trim() + '/popup').done(
|
||||
function(data) {
|
||||
xhr = null;
|
||||
elem.popover({
|
||||
trigger: 'manual',
|
||||
html: true,
|
||||
animation: false,
|
||||
container: elem,
|
||||
content: data
|
||||
}).popover('show');
|
||||
flask_moment_render_all();
|
||||
}
|
||||
);
|
||||
}, 1000);
|
||||
},
|
||||
function(event) {
|
||||
// mouse out event handler
|
||||
var elem = $(event.currentTarget);
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
else if (xhr) {
|
||||
xhr.abort();
|
||||
xhr = null;
|
||||
}
|
||||
else {
|
||||
elem.popover('destroy');
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
function set_message_count(n) {
|
||||
$('#message_count').text(n);
|
||||
$('#message_count').css('visibility', n ? 'visible' : 'hidden');
|
||||
}
|
||||
{% if current_user.is_authenticated %}
|
||||
$(function() {
|
||||
var since = 0;
|
||||
setInterval(function() {
|
||||
$.ajax('{{ url_for('main.notifications') }}?since=' + since).done(
|
||||
function(notifications) {
|
||||
for (var i = 0; i < notifications.length; i++) {
|
||||
if (notifications[i].name == 'unread_message_count')
|
||||
set_message_count(notifications[i].data);
|
||||
since = notifications[i].timestamp;
|
||||
}
|
||||
}
|
||||
);
|
||||
}, 10000);
|
||||
});
|
||||
{% endif %}
|
||||
</script>
|
||||
{% endblock %}
|
|
@ -0,0 +1,11 @@
|
|||
{% extends "base.html" %}
|
||||
{% import 'bootstrap/wtf.html' as wtf %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Edit Profile') }}</h1>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{{ wtf.quick_form(form) }}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -0,0 +1,12 @@
|
|||
<p>Dear {{ user.username }},</p>
|
||||
<p>
|
||||
To reset your password
|
||||
<a href="{{ url_for('auth.reset_password', token=token, _external=True) }}">
|
||||
click here
|
||||
</a>.
|
||||
</p>
|
||||
<p>Alternatively, you can paste the following link in your browser's address bar:</p>
|
||||
<p>{{ url_for('auth.reset_password', token=token, _external=True) }}</p>
|
||||
<p>If you have not requested a password reset simply ignore this message.</p>
|
||||
<p>Sincerely,</p>
|
||||
<p>The Microblog Team</p>
|
|
@ -0,0 +1,11 @@
|
|||
Dear {{ user.username }},
|
||||
|
||||
To reset your password click on the following link:
|
||||
|
||||
{{ url_for('auth.reset_password', token=token, _external=True) }}
|
||||
|
||||
If you have not requested a password reset simply ignore this message.
|
||||
|
||||
Sincerely,
|
||||
|
||||
The Microblog Team
|
|
@ -0,0 +1,6 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Not Found') }}</h1>
|
||||
<p><a href="{{ url_for('main.index') }}">{{ _('Back') }}</a></p>
|
||||
{% endblock %}
|
|
@ -0,0 +1,7 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('An unexpected error has occurred') }}</h1>
|
||||
<p>{{ _('The administrator has been notified. Sorry for the inconvenience!') }}</p>
|
||||
<p><a href="{{ url_for('main.index') }}">{{ _('Back') }}</a></p>
|
||||
{% endblock %}
|
|
@ -0,0 +1,27 @@
|
|||
{% extends "base.html" %}
|
||||
{% import 'bootstrap/wtf.html' as wtf %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Hi, %(username)s!', username=current_user.username) }}</h1>
|
||||
{% if form %}
|
||||
{{ wtf.quick_form(form) }}
|
||||
<br>
|
||||
{% endif %}
|
||||
{% for post in posts %}
|
||||
{% include '_post.html' %}
|
||||
{% endfor %}
|
||||
<nav aria-label="...">
|
||||
<ul class="pager">
|
||||
<li class="previous{% if not prev_url %} disabled{% endif %}">
|
||||
<a href="{{ prev_url or '#' }}">
|
||||
<span aria-hidden="true">←</span> {{ _('Newer posts') }}
|
||||
</a>
|
||||
</li>
|
||||
<li class="next{% if not next_url %} disabled{% endif %}">
|
||||
<a href="{{ next_url or '#' }}">
|
||||
{{ _('Older posts') }} <span aria-hidden="true">→</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
{% endblock %}
|
|
@ -0,0 +1,22 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Messages') }}</h1>
|
||||
{% for post in messages %}
|
||||
{% include '_post.html' %}
|
||||
{% endfor %}
|
||||
<nav aria-label="...">
|
||||
<ul class="pager">
|
||||
<li class="previous{% if not prev_url %} disabled{% endif %}">
|
||||
<a href="{{ prev_url or '#' }}">
|
||||
<span aria-hidden="true">←</span> {{ _('Newer messages') }}
|
||||
</a>
|
||||
</li>
|
||||
<li class="next{% if not next_url %} disabled{% endif %}">
|
||||
<a href="{{ next_url or '#' }}">
|
||||
{{ _('Older messages') }} <span aria-hidden="true">→</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
{% endblock %}
|
|
@ -0,0 +1,22 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Search Results') }}</h1>
|
||||
{% for post in posts %}
|
||||
{% include '_post.html' %}
|
||||
{% endfor %}
|
||||
<nav aria-label="...">
|
||||
<ul class="pager">
|
||||
<li class="previous{% if not prev_url %} disabled{% endif %}">
|
||||
<a href="{{ prev_url or '#' }}">
|
||||
<span aria-hidden="true">←</span> {{ _('Previous results') }}
|
||||
</a>
|
||||
</li>
|
||||
<li class="next{% if not next_url %} disabled{% endif %}">
|
||||
<a href="{{ next_url or '#' }}">
|
||||
{{ _('Next results') }} <span aria-hidden="true">→</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
{% endblock %}
|
|
@ -0,0 +1,11 @@
|
|||
{% extends "base.html" %}
|
||||
{% import 'bootstrap/wtf.html' as wtf %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>{{ _('Send Message to %(recipient)s', recipient=recipient) }}</h1>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{{ wtf.quick_form(form) }}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -0,0 +1,54 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block app_content %}
|
||||
<table class="table table-hover">
|
||||
<tr>
|
||||
<td width="256px"><img src="{{ user.avatar(256) }}"></td>
|
||||
<td>
|
||||
<h1>{{ _('User') }}: {{ user.username }}</h1>
|
||||
{% if user.about_me %}<p>{{ user.about_me }}</p>{% endif %}
|
||||
{% if user.last_seen %}
|
||||
<p>{{ _('Last seen on') }}: {{ moment(user.last_seen).format('LLL') }}</p>
|
||||
{% endif %}
|
||||
<p>{{ _('%(count)d followers', count=user.followers.count()) }}, {{ _('%(count)d following', count=user.followed.count()) }}</p>
|
||||
{% if user == current_user %}
|
||||
<p><a href="{{ url_for('main.edit_profile') }}">{{ _('Edit your profile') }}</a></p>
|
||||
{% elif not current_user.is_following(user) %}
|
||||
<p>
|
||||
<form action="{{ url_for('main.follow', username=user.username) }}" method="post">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.submit(value=_('Follow'), class_='btn btn-default') }}
|
||||
</form>
|
||||
</p>
|
||||
{% else %}
|
||||
<p>
|
||||
<form action="{{ url_for('main.unfollow', username=user.username) }}" method="post">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.submit(value=_('Unfollow'), class_='btn btn-default') }}
|
||||
</form>
|
||||
</p>
|
||||
{% endif %}
|
||||
{% if user != current_user %}
|
||||
<p><a href="{{ url_for('main.send_message', recipient=user.username) }}">{{ _('Send private message') }}</a></p>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% for post in posts %}
|
||||
{% include '_post.html' %}
|
||||
{% endfor %}
|
||||
<nav aria-label="...">
|
||||
<ul class="pager">
|
||||
<li class="previous{% if not prev_url %} disabled{% endif %}">
|
||||
<a href="{{ prev_url or '#' }}">
|
||||
<span aria-hidden="true">←</span> {{ _('Newer posts') }}
|
||||
</a>
|
||||
</li>
|
||||
<li class="next{% if not next_url %} disabled{% endif %}">
|
||||
<a href="{{ next_url or '#' }}">
|
||||
{{ _('Older posts') }} <span aria-hidden="true">→</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
{% endblock %}
|
|
@ -0,0 +1,32 @@
|
|||
<table class="table">
|
||||
<tr>
|
||||
<td width="64" style="border: 0px;"><img src="{{ user.avatar(64) }}"></td>
|
||||
<td style="border: 0px;">
|
||||
<p><a href="{{ url_for('main.user', username=user.username) }}">{{ user.username }}</a></p>
|
||||
<small>
|
||||
{% if user.about_me %}<p>{{ user.about_me }}</p>{% endif %}
|
||||
{% if user.last_seen %}
|
||||
<p>{{ _('Last seen on') }}: {{ moment(user.last_seen).format('lll') }}</p>
|
||||
{% endif %}
|
||||
<p>{{ _('%(count)d followers', count=user.followers.count()) }}, {{ _('%(count)d following', count=user.followed.count()) }}</p>
|
||||
{% if user != current_user %}
|
||||
{% if not current_user.is_following(user) %}
|
||||
<p>
|
||||
<form action="{{ url_for('main.follow', username=user.username) }}" method="post">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.submit(value=_('Follow'), class_='btn btn-default btn-sm') }}
|
||||
</form>
|
||||
</p>
|
||||
{% else %}
|
||||
<p>
|
||||
<form action="{{ url_for('main.unfollow', username=user.username) }}" method="post">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.submit(value=_('Unfollow'), class_='btn btn-default btm-sm') }}
|
||||
</form>
|
||||
</p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</small>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
|
@ -0,0 +1,21 @@
|
|||
import json
|
||||
import requests
|
||||
from flask import current_app
|
||||
from flask_babel import _
|
||||
|
||||
|
||||
def translate(text, source_language, dest_language):
|
||||
if 'MS_TRANSLATOR_KEY' not in current_app.config or \
|
||||
not current_app.config['MS_TRANSLATOR_KEY']:
|
||||
return _('Error: the translation service is not configured.')
|
||||
auth = {
|
||||
'Ocp-Apim-Subscription-Key': current_app.config['MS_TRANSLATOR_KEY'],
|
||||
'Ocp-Apim-Subscription-Region': 'westus2'}
|
||||
r = requests.post(
|
||||
'https://api.cognitive.microsofttranslator.com'
|
||||
'/translate?api-version=3.0&from={}&to={}'.format(
|
||||
source_language, dest_language), headers=auth, json=[
|
||||
{'Text': text}])
|
||||
if r.status_code != 200:
|
||||
return _('Error: the translation service failed.')
|
||||
return r.json()[0]['translations'][0]['text']
|
|
@ -0,0 +1,308 @@
|
|||
# Spanish translations for PROJECT.
|
||||
# Copyright (C) 2017 ORGANIZATION
|
||||
# This file is distributed under the same license as the PROJECT project.
|
||||
# FIRST AUTHOR <EMAIL@ADDRESS>, 2017.
|
||||
#
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PROJECT VERSION\n"
|
||||
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
|
||||
"POT-Creation-Date: 2017-11-25 18:26-0800\n"
|
||||
"PO-Revision-Date: 2017-09-29 23:25-0700\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language: es\n"
|
||||
"Language-Team: es <LL@li.org>\n"
|
||||
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=utf-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
"Generated-By: Babel 2.5.1\n"
|
||||
|
||||
#: app/__init__.py:18
|
||||
msgid "Please log in to access this page."
|
||||
msgstr "Por favor ingrese para acceder a esta página."
|
||||
|
||||
#: app/translate.py:10
|
||||
msgid "Error: the translation service is not configured."
|
||||
msgstr "Error: el servicio de traducciones no está configurado."
|
||||
|
||||
#: app/translate.py:18
|
||||
msgid "Error: the translation service failed."
|
||||
msgstr "Error el servicio de traducciones ha fallado."
|
||||
|
||||
#: app/auth/email.py:8
|
||||
msgid "[Microblog] Reset Your Password"
|
||||
msgstr "[Microblog] Nueva Contraseña"
|
||||
|
||||
#: app/auth/forms.py:10 app/auth/forms.py:17 app/main/forms.py:10
|
||||
msgid "Username"
|
||||
msgstr "Nombre de usuario"
|
||||
|
||||
#: app/auth/forms.py:11 app/auth/forms.py:19 app/auth/forms.py:42
|
||||
msgid "Password"
|
||||
msgstr "Contraseña"
|
||||
|
||||
#: app/auth/forms.py:12
|
||||
msgid "Remember Me"
|
||||
msgstr "Recordarme"
|
||||
|
||||
#: app/auth/forms.py:13 app/templates/auth/login.html:5
|
||||
msgid "Sign In"
|
||||
msgstr "Ingresar"
|
||||
|
||||
#: app/auth/forms.py:18 app/auth/forms.py:37
|
||||
msgid "Email"
|
||||
msgstr "Email"
|
||||
|
||||
#: app/auth/forms.py:21 app/auth/forms.py:44
|
||||
msgid "Repeat Password"
|
||||
msgstr "Repetir Contraseña"
|
||||
|
||||
#: app/auth/forms.py:23 app/templates/auth/register.html:5
|
||||
msgid "Register"
|
||||
msgstr "Registrarse"
|
||||
|
||||
#: app/auth/forms.py:28 app/main/forms.py:23
|
||||
msgid "Please use a different username."
|
||||
msgstr "Por favor use un nombre de usuario diferente."
|
||||
|
||||
#: app/auth/forms.py:33
|
||||
msgid "Please use a different email address."
|
||||
msgstr "Por favor use una dirección de email diferente."
|
||||
|
||||
#: app/auth/forms.py:38 app/auth/forms.py:46
|
||||
msgid "Request Password Reset"
|
||||
msgstr "Pedir una nueva contraseña"
|
||||
|
||||
#: app/auth/routes.py:20
|
||||
msgid "Invalid username or password"
|
||||
msgstr "Nombre de usuario o contraseña inválidos"
|
||||
|
||||
#: app/auth/routes.py:46
|
||||
msgid "Congratulations, you are now a registered user!"
|
||||
msgstr "¡Felicitaciones, ya eres un usuario registrado!"
|
||||
|
||||
#: app/auth/routes.py:61
|
||||
msgid "Check your email for the instructions to reset your password"
|
||||
msgstr "Busca en tu email las instrucciones para crear una nueva contraseña"
|
||||
|
||||
#: app/auth/routes.py:78
|
||||
msgid "Your password has been reset."
|
||||
msgstr "Tu contraseña ha sido cambiada."
|
||||
|
||||
#: app/main/forms.py:11
|
||||
msgid "About me"
|
||||
msgstr "Acerca de mí"
|
||||
|
||||
#: app/main/forms.py:13 app/main/forms.py:28 app/main/forms.py:44
|
||||
msgid "Submit"
|
||||
msgstr "Enviar"
|
||||
|
||||
#: app/main/forms.py:27
|
||||
msgid "Say something"
|
||||
msgstr "Dí algo"
|
||||
|
||||
#: app/main/forms.py:32
|
||||
msgid "Search"
|
||||
msgstr "Buscar"
|
||||
|
||||
#: app/main/forms.py:43
|
||||
msgid "Message"
|
||||
msgstr "Mensaje"
|
||||
|
||||
#: app/main/routes.py:36
|
||||
msgid "Your post is now live!"
|
||||
msgstr "¡Tu artículo ha sido publicado!"
|
||||
|
||||
#: app/main/routes.py:94
|
||||
msgid "Your changes have been saved."
|
||||
msgstr "Tus cambios han sido salvados."
|
||||
|
||||
#: app/main/routes.py:99 app/templates/edit_profile.html:5
|
||||
msgid "Edit Profile"
|
||||
msgstr "Editar Perfil"
|
||||
|
||||
#: app/main/routes.py:108 app/main/routes.py:124
|
||||
#, python-format
|
||||
msgid "User %(username)s not found."
|
||||
msgstr "El usuario %(username)s no ha sido encontrado."
|
||||
|
||||
#: app/main/routes.py:111
|
||||
msgid "You cannot follow yourself!"
|
||||
msgstr "¡No te puedes seguir a tí mismo!"
|
||||
|
||||
#: app/main/routes.py:115
|
||||
#, python-format
|
||||
msgid "You are following %(username)s!"
|
||||
msgstr "¡Ahora estás siguiendo a %(username)s!"
|
||||
|
||||
#: app/main/routes.py:127
|
||||
msgid "You cannot unfollow yourself!"
|
||||
msgstr "¡No te puedes dejar de seguir a tí mismo!"
|
||||
|
||||
#: app/main/routes.py:131
|
||||
#, python-format
|
||||
msgid "You are not following %(username)s."
|
||||
msgstr "No estás siguiendo a %(username)s."
|
||||
|
||||
#: app/main/routes.py:170
|
||||
msgid "Your message has been sent."
|
||||
msgstr "Tu mensaje ha sido enviado."
|
||||
|
||||
#: app/main/routes.py:172
|
||||
msgid "Send Message"
|
||||
msgstr "Enviar Mensaje"
|
||||
|
||||
#: app/templates/_post.html:16
|
||||
#, python-format
|
||||
msgid "%(username)s said %(when)s"
|
||||
msgstr "%(username)s dijo %(when)s"
|
||||
|
||||
#: app/templates/_post.html:27
|
||||
msgid "Translate"
|
||||
msgstr "Traducir"
|
||||
|
||||
#: app/templates/base.html:4
|
||||
msgid "Welcome to Microblog"
|
||||
msgstr "Bienvenido a Microblog"
|
||||
|
||||
#: app/templates/base.html:21
|
||||
msgid "Home"
|
||||
msgstr "Inicio"
|
||||
|
||||
#: app/templates/base.html:22
|
||||
msgid "Explore"
|
||||
msgstr "Explorar"
|
||||
|
||||
#: app/templates/base.html:33
|
||||
msgid "Login"
|
||||
msgstr "Ingresar"
|
||||
|
||||
#: app/templates/base.html:36 app/templates/messages.html:4
|
||||
msgid "Messages"
|
||||
msgstr "Mensajes"
|
||||
|
||||
#: app/templates/base.html:45
|
||||
msgid "Profile"
|
||||
msgstr "Perfil"
|
||||
|
||||
#: app/templates/base.html:46
|
||||
msgid "Logout"
|
||||
msgstr "Salir"
|
||||
|
||||
#: app/templates/base.html:83
|
||||
msgid "Error: Could not contact server."
|
||||
msgstr "Error: el servidor no pudo ser contactado."
|
||||
|
||||
#: app/templates/index.html:5
|
||||
#, python-format
|
||||
msgid "Hi, %(username)s!"
|
||||
msgstr "¡Hola, %(username)s!"
|
||||
|
||||
#: app/templates/index.html:17 app/templates/user.html:34
|
||||
msgid "Newer posts"
|
||||
msgstr "Artículos siguientes"
|
||||
|
||||
#: app/templates/index.html:22 app/templates/user.html:39
|
||||
msgid "Older posts"
|
||||
msgstr "Artículos previos"
|
||||
|
||||
#: app/templates/messages.html:12
|
||||
msgid "Newer messages"
|
||||
msgstr "Mensajes siguientes"
|
||||
|
||||
#: app/templates/messages.html:17
|
||||
msgid "Older messages"
|
||||
msgstr "Mensajes previos"
|
||||
|
||||
#: app/templates/search.html:4
|
||||
msgid "Search Results"
|
||||
msgstr ""
|
||||
|
||||
#: app/templates/search.html:12
|
||||
msgid "Previous results"
|
||||
msgstr ""
|
||||
|
||||
#: app/templates/search.html:17
|
||||
msgid "Next results"
|
||||
msgstr ""
|
||||
|
||||
#: app/templates/send_message.html:5
|
||||
#, python-format
|
||||
msgid "Send Message to %(recipient)s"
|
||||
msgstr "Enviar Mensaje a %(recipient)s"
|
||||
|
||||
#: app/templates/user.html:8
|
||||
msgid "User"
|
||||
msgstr "Usuario"
|
||||
|
||||
#: app/templates/user.html:11 app/templates/user_popup.html:9
|
||||
msgid "Last seen on"
|
||||
msgstr "Última visita"
|
||||
|
||||
#: app/templates/user.html:13 app/templates/user_popup.html:11
|
||||
#, python-format
|
||||
msgid "%(count)d followers"
|
||||
msgstr "%(count)d seguidores"
|
||||
|
||||
#: app/templates/user.html:13 app/templates/user_popup.html:11
|
||||
#, python-format
|
||||
msgid "%(count)d following"
|
||||
msgstr "siguiendo a %(count)d"
|
||||
|
||||
#: app/templates/user.html:15
|
||||
msgid "Edit your profile"
|
||||
msgstr "Editar tu perfil"
|
||||
|
||||
#: app/templates/user.html:17 app/templates/user_popup.html:14
|
||||
msgid "Follow"
|
||||
msgstr "Seguir"
|
||||
|
||||
#: app/templates/user.html:19 app/templates/user_popup.html:16
|
||||
msgid "Unfollow"
|
||||
msgstr "Dejar de seguir"
|
||||
|
||||
#: app/templates/user.html:22
|
||||
msgid "Send private message"
|
||||
msgstr "Enviar mensaje privado"
|
||||
|
||||
#: app/templates/auth/login.html:12
|
||||
msgid "New User?"
|
||||
msgstr "¿Usuario Nuevo?"
|
||||
|
||||
#: app/templates/auth/login.html:12
|
||||
msgid "Click to Register!"
|
||||
msgstr "¡Haz click aquí para registrarte!"
|
||||
|
||||
#: app/templates/auth/login.html:14
|
||||
msgid "Forgot Your Password?"
|
||||
msgstr "¿Te olvidaste tu contraseña?"
|
||||
|
||||
#: app/templates/auth/login.html:15
|
||||
msgid "Click to Reset It"
|
||||
msgstr "Haz click aquí para pedir una nueva"
|
||||
|
||||
#: app/templates/auth/reset_password.html:5
|
||||
msgid "Reset Your Password"
|
||||
msgstr "Nueva Contraseña"
|
||||
|
||||
#: app/templates/auth/reset_password_request.html:5
|
||||
msgid "Reset Password"
|
||||
msgstr "Nueva Contraseña"
|
||||
|
||||
#: app/templates/errors/404.html:4
|
||||
msgid "Not Found"
|
||||
msgstr "Página No Encontrada"
|
||||
|
||||
#: app/templates/errors/404.html:5 app/templates/errors/500.html:6
|
||||
msgid "Back"
|
||||
msgstr "Atrás"
|
||||
|
||||
#: app/templates/errors/500.html:4
|
||||
msgid "An unexpected error has occurred"
|
||||
msgstr "Ha ocurrido un error inesperado"
|
||||
|
||||
#: app/templates/errors/500.html:5
|
||||
msgid "The administrator has been notified. Sorry for the inconvenience!"
|
||||
msgstr "El administrador ha sido notificado. ¡Lamentamos la inconveniencia!"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
[python: app/**.py]
|
||||
[jinja2: app/templates/**.html]
|
||||
extensions=jinja2.ext.autoescape,jinja2.ext.with_
|
|
@ -0,0 +1,13 @@
|
|||
#!/bin/bash
|
||||
# this script is used to boot a Docker container
|
||||
source venv/bin/activate
|
||||
while true; do
|
||||
flask db upgrade
|
||||
if [[ "$?" == "0" ]]; then
|
||||
break
|
||||
fi
|
||||
echo Deploy command failed, retrying in 5 secs...
|
||||
sleep 5
|
||||
done
|
||||
flask translate compile
|
||||
exec gunicorn -b :5000 --access-logfile - --error-logfile - microblog:app
|
|
@ -0,0 +1,24 @@
|
|||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
load_dotenv(os.path.join(basedir, '.env'))
|
||||
|
||||
|
||||
class Config(object):
|
||||
SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
|
||||
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', '').replace(
|
||||
'postgres://', 'postgresql://') or \
|
||||
'sqlite:///' + os.path.join(basedir, 'app.db')
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
LOG_TO_STDOUT = os.environ.get('LOG_TO_STDOUT')
|
||||
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']
|
||||
LANGUAGES = ['en', 'es']
|
||||
MS_TRANSLATOR_KEY = os.environ.get('MS_TRANSLATOR_KEY')
|
||||
ELASTICSEARCH_URL = os.environ.get('ELASTICSEARCH_URL')
|
||||
POSTS_PER_PAGE = 25
|
|
@ -0,0 +1,37 @@
|
|||
server {
|
||||
# listen on port 80 (http)
|
||||
listen 80;
|
||||
server_name _;
|
||||
location / {
|
||||
# redirect any requests to the same URL but on https
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
}
|
||||
server {
|
||||
# listen on port 443 (https)
|
||||
listen 443 ssl;
|
||||
server_name _;
|
||||
|
||||
# location of the self-signed SSL certificate
|
||||
ssl_certificate /home/ubuntu/microblog/certs/cert.pem;
|
||||
ssl_certificate_key /home/ubuntu/microblog/certs/key.pem;
|
||||
|
||||
# write access and error logs to /var/log
|
||||
access_log /var/log/microblog_access.log;
|
||||
error_log /var/log/microblog_error.log;
|
||||
|
||||
location / {
|
||||
# forward application requests to the gunicorn server
|
||||
proxy_pass http://localhost:8000;
|
||||
proxy_redirect off;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
}
|
||||
|
||||
location /static {
|
||||
# handle static files directly, without forwarding to the application
|
||||
alias /home/ubuntu/microblog/app/static;
|
||||
expires 30d;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[program:microblog]
|
||||
command=/home/ubuntu/microblog/venv/bin/gunicorn -b localhost:8000 -w 4 microblog:app
|
||||
directory=/home/ubuntu/microblog
|
||||
user=ubuntu
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stopasgroup=true
|
||||
killasgroup=true
|
12
microblog.py
12
microblog.py
|
@ -1 +1,11 @@
|
|||
from app import app
|
||||
from app import create_app, db, cli
|
||||
from app.models import User, Post, Message, Notification
|
||||
|
||||
app = create_app()
|
||||
cli.register(app)
|
||||
|
||||
|
||||
@app.shell_context_processor
|
||||
def make_shell_context():
|
||||
return {'db': db, 'User': User, 'Post': Post, 'Message': Message,
|
||||
'Notification': Notification}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Generic single-database configuration.
|
|
@ -0,0 +1,45 @@
|
|||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
|
@ -0,0 +1,87 @@
|
|||
from __future__ import with_statement
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
from logging.config import fileConfig
|
||||
import logging
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
from flask import current_app
|
||||
config.set_main_option('sqlalchemy.url',
|
||||
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
|
||||
target_metadata = current_app.extensions['migrate'].db.metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(url=url)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
# this callback is used to prevent an auto-migration from being generated
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
script = directives[0]
|
||||
if script.upgrade_ops.is_empty():
|
||||
directives[:] = []
|
||||
logger.info('No changes in schema detected.')
|
||||
|
||||
engine = engine_from_config(config.get_section(config.config_ini_section),
|
||||
prefix='sqlalchemy.',
|
||||
poolclass=pool.NullPool)
|
||||
|
||||
connection = engine.connect()
|
||||
context.configure(connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
process_revision_directives=process_revision_directives,
|
||||
**current_app.extensions['migrate'].configure_args)
|
||||
|
||||
try:
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
|
@ -0,0 +1,24 @@
|
|||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
|
@ -0,0 +1,28 @@
|
|||
"""add language to posts
|
||||
|
||||
Revision ID: 2b017edaa91f
|
||||
Revises: ae346256b650
|
||||
Create Date: 2017-10-04 22:48:34.494465
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '2b017edaa91f'
|
||||
down_revision = 'ae346256b650'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('post', sa.Column('language', sa.String(length=5), nullable=True))
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('post', 'language')
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,30 @@
|
|||
"""new fields in user model
|
||||
|
||||
Revision ID: 37f06a334dbf
|
||||
Revises: 780739b227a7
|
||||
Create Date: 2017-09-14 10:54:13.865401
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '37f06a334dbf'
|
||||
down_revision = '780739b227a7'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('user', sa.Column('about_me', sa.String(length=140), nullable=True))
|
||||
op.add_column('user', sa.Column('last_seen', sa.DateTime(), nullable=True))
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('user', 'last_seen')
|
||||
op.drop_column('user', 'about_me')
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,37 @@
|
|||
"""posts table
|
||||
|
||||
Revision ID: 780739b227a7
|
||||
Revises: e517276bb1c2
|
||||
Create Date: 2017-09-11 12:23:25.496587
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '780739b227a7'
|
||||
down_revision = 'e517276bb1c2'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('post',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('body', sa.String(length=140), nullable=True),
|
||||
sa.Column('timestamp', sa.DateTime(), nullable=True),
|
||||
sa.Column('user_id', sa.Integer(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_post_timestamp'), 'post', ['timestamp'], unique=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_post_timestamp'), table_name='post')
|
||||
op.drop_table('post')
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,33 @@
|
|||
"""followers
|
||||
|
||||
Revision ID: ae346256b650
|
||||
Revises: 37f06a334dbf
|
||||
Create Date: 2017-09-17 15:41:30.211082
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'ae346256b650'
|
||||
down_revision = '37f06a334dbf'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('followers',
|
||||
sa.Column('follower_id', sa.Integer(), nullable=True),
|
||||
sa.Column('followed_id', sa.Integer(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['followed_id'], ['user.id'], ),
|
||||
sa.ForeignKeyConstraint(['follower_id'], ['user.id'], )
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('followers')
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,41 @@
|
|||
"""private messages
|
||||
|
||||
Revision ID: d049de007ccf
|
||||
Revises: 834b1a697901
|
||||
Create Date: 2017-11-12 23:30:28.571784
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'd049de007ccf'
|
||||
down_revision = '2b017edaa91f'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('message',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('sender_id', sa.Integer(), nullable=True),
|
||||
sa.Column('recipient_id', sa.Integer(), nullable=True),
|
||||
sa.Column('body', sa.String(length=140), nullable=True),
|
||||
sa.Column('timestamp', sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['recipient_id'], ['user.id'], ),
|
||||
sa.ForeignKeyConstraint(['sender_id'], ['user.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_message_timestamp'), 'message', ['timestamp'], unique=False)
|
||||
op.add_column('user', sa.Column('last_message_read_time', sa.DateTime(), nullable=True))
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('user', 'last_message_read_time')
|
||||
op.drop_index(op.f('ix_message_timestamp'), table_name='message')
|
||||
op.drop_table('message')
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,38 @@
|
|||
"""users table
|
||||
|
||||
Revision ID: e517276bb1c2
|
||||
Revises:
|
||||
Create Date: 2017-09-11 11:23:05.566844
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'e517276bb1c2'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('user',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('username', sa.String(length=64), nullable=True),
|
||||
sa.Column('email', sa.String(length=120), nullable=True),
|
||||
sa.Column('password_hash', sa.String(length=128), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_user_email'), 'user', ['email'], unique=True)
|
||||
op.create_index(op.f('ix_user_username'), 'user', ['username'], unique=True)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_user_username'), table_name='user')
|
||||
op.drop_index(op.f('ix_user_email'), table_name='user')
|
||||
op.drop_table('user')
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,40 @@
|
|||
"""notifications
|
||||
|
||||
Revision ID: f7ac3d27bb1d
|
||||
Revises: d049de007ccf
|
||||
Create Date: 2017-11-22 19:48:39.945858
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'f7ac3d27bb1d'
|
||||
down_revision = 'd049de007ccf'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('notification',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=128), nullable=True),
|
||||
sa.Column('user_id', sa.Integer(), nullable=True),
|
||||
sa.Column('timestamp', sa.Float(), nullable=True),
|
||||
sa.Column('payload_json', sa.Text(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_notification_name'), 'notification', ['name'], unique=False)
|
||||
op.create_index(op.f('ix_notification_timestamp'), 'notification', ['timestamp'], unique=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_notification_timestamp'), table_name='notification')
|
||||
op.drop_index(op.f('ix_notification_name'), table_name='notification')
|
||||
op.drop_table('notification')
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,42 @@
|
|||
alembic==1.6.5
|
||||
Babel==2.9.1
|
||||
blinker==1.4
|
||||
certifi==2021.5.30
|
||||
chardet==4.0.0
|
||||
click==8.0.1
|
||||
dnspython==2.1.0
|
||||
dominate==2.6.0
|
||||
elasticsearch==7.13.3
|
||||
email-validator==1.1.3
|
||||
Flask==2.0.1
|
||||
Flask-Babel==2.0.0
|
||||
Flask-Bootstrap==3.3.7.1
|
||||
Flask-Login==0.5.0
|
||||
Flask-Mail==0.9.1
|
||||
Flask-Migrate==3.0.1
|
||||
Flask-Moment==1.0.1
|
||||
Flask-SQLAlchemy==2.5.1
|
||||
Flask-WTF==0.15.1
|
||||
greenlet==1.1.0
|
||||
idna==2.10
|
||||
itsdangerous==2.0.1
|
||||
Jinja2==3.0.1
|
||||
langdetect==1.0.9
|
||||
Mako==1.1.4
|
||||
MarkupSafe==2.0.1
|
||||
PyJWT==2.1.0
|
||||
python-dateutil==2.8.1
|
||||
python-dotenv==0.18.0
|
||||
python-editor==1.0.4
|
||||
pytz==2021.1
|
||||
requests==2.25.1
|
||||
six==1.16.0
|
||||
SQLAlchemy==1.4.20
|
||||
urllib3==1.26.6
|
||||
visitor==0.1.3
|
||||
Werkzeug==2.0.1
|
||||
WTForms==2.3.3
|
||||
|
||||
# requirements for Heroku
|
||||
#psycopg2==2.9.1
|
||||
#gunicorn==20.1.0
|
|
@ -0,0 +1,102 @@
|
|||
#!/usr/bin/env python
|
||||
from datetime import datetime, timedelta
|
||||
import unittest
|
||||
from app import create_app, db
|
||||
from app.models import User, Post
|
||||
from config import Config
|
||||
|
||||
|
||||
class TestConfig(Config):
|
||||
TESTING = True
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite://'
|
||||
ELASTICSEARCH_URL = None
|
||||
|
||||
|
||||
class UserModelCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = create_app(TestConfig)
|
||||
self.app_context = self.app.app_context()
|
||||
self.app_context.push()
|
||||
db.create_all()
|
||||
|
||||
def tearDown(self):
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
self.app_context.pop()
|
||||
|
||||
def test_password_hashing(self):
|
||||
u = User(username='susan')
|
||||
u.set_password('cat')
|
||||
self.assertFalse(u.check_password('dog'))
|
||||
self.assertTrue(u.check_password('cat'))
|
||||
|
||||
def test_avatar(self):
|
||||
u = User(username='john', email='john@example.com')
|
||||
self.assertEqual(u.avatar(128), ('https://www.gravatar.com/avatar/'
|
||||
'd4c74594d841139328695756648b6bd6'
|
||||
'?d=identicon&s=128'))
|
||||
|
||||
def test_follow(self):
|
||||
u1 = User(username='john', email='john@example.com')
|
||||
u2 = User(username='susan', email='susan@example.com')
|
||||
db.session.add(u1)
|
||||
db.session.add(u2)
|
||||
db.session.commit()
|
||||
self.assertEqual(u1.followed.all(), [])
|
||||
self.assertEqual(u1.followers.all(), [])
|
||||
|
||||
u1.follow(u2)
|
||||
db.session.commit()
|
||||
self.assertTrue(u1.is_following(u2))
|
||||
self.assertEqual(u1.followed.count(), 1)
|
||||
self.assertEqual(u1.followed.first().username, 'susan')
|
||||
self.assertEqual(u2.followers.count(), 1)
|
||||
self.assertEqual(u2.followers.first().username, 'john')
|
||||
|
||||
u1.unfollow(u2)
|
||||
db.session.commit()
|
||||
self.assertFalse(u1.is_following(u2))
|
||||
self.assertEqual(u1.followed.count(), 0)
|
||||
self.assertEqual(u2.followers.count(), 0)
|
||||
|
||||
def test_follow_posts(self):
|
||||
# create four users
|
||||
u1 = User(username='john', email='john@example.com')
|
||||
u2 = User(username='susan', email='susan@example.com')
|
||||
u3 = User(username='mary', email='mary@example.com')
|
||||
u4 = User(username='david', email='david@example.com')
|
||||
db.session.add_all([u1, u2, u3, u4])
|
||||
|
||||
# create four posts
|
||||
now = datetime.utcnow()
|
||||
p1 = Post(body="post from john", author=u1,
|
||||
timestamp=now + timedelta(seconds=1))
|
||||
p2 = Post(body="post from susan", author=u2,
|
||||
timestamp=now + timedelta(seconds=4))
|
||||
p3 = Post(body="post from mary", author=u3,
|
||||
timestamp=now + timedelta(seconds=3))
|
||||
p4 = Post(body="post from david", author=u4,
|
||||
timestamp=now + timedelta(seconds=2))
|
||||
db.session.add_all([p1, p2, p3, p4])
|
||||
db.session.commit()
|
||||
|
||||
# setup the followers
|
||||
u1.follow(u2) # john follows susan
|
||||
u1.follow(u4) # john follows david
|
||||
u2.follow(u3) # susan follows mary
|
||||
u3.follow(u4) # mary follows david
|
||||
db.session.commit()
|
||||
|
||||
# check the followed posts of each user
|
||||
f1 = u1.followed_posts().all()
|
||||
f2 = u2.followed_posts().all()
|
||||
f3 = u3.followed_posts().all()
|
||||
f4 = u4.followed_posts().all()
|
||||
self.assertEqual(f1, [p2, p4, p1])
|
||||
self.assertEqual(f2, [p2, p3])
|
||||
self.assertEqual(f3, [p3, p4])
|
||||
self.assertEqual(f4, [p4])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(verbosity=2)
|
Loading…
Reference in New Issue