email support
This commit is contained in:
parent
8a27076ae4
commit
f441f8d8d8
|
@ -3,6 +3,7 @@ from flask import Flask
|
||||||
from flask.ext.sqlalchemy import SQLAlchemy
|
from flask.ext.sqlalchemy import SQLAlchemy
|
||||||
from flask.ext.login import LoginManager
|
from flask.ext.login import LoginManager
|
||||||
from flask.ext.openid import OpenID
|
from flask.ext.openid import OpenID
|
||||||
|
from flask.ext.mail import Mail
|
||||||
from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD
|
from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
@ -12,6 +13,7 @@ lm = LoginManager()
|
||||||
lm.setup_app(app)
|
lm.setup_app(app)
|
||||||
lm.login_view = 'login'
|
lm.login_view = 'login'
|
||||||
oid = OpenID(app, os.path.join(basedir, 'tmp'))
|
oid = OpenID(app, os.path.join(basedir, 'tmp'))
|
||||||
|
mail = Mail(app)
|
||||||
|
|
||||||
if not app.debug:
|
if not app.debug:
|
||||||
import logging
|
import logging
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
def async(f):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
thr = Thread(target = f, args = args, kwargs = kwargs)
|
||||||
|
thr.start()
|
||||||
|
return wrapper
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
from flask import render_template
|
||||||
|
from flask.ext.mail import Message
|
||||||
|
from app import mail
|
||||||
|
from decorators import async
|
||||||
|
from config import ADMINS
|
||||||
|
|
||||||
|
@async
|
||||||
|
def send_async_email(msg):
|
||||||
|
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
|
||||||
|
send_async_email(msg)
|
||||||
|
#thr = threading.Thread(target = send_async_email, args = [msg])
|
||||||
|
#thr.start()
|
||||||
|
|
||||||
|
|
||||||
|
def follower_notification(followed, follower):
|
||||||
|
send_email("[microblog] %s is now following you!" % follower.nickname,
|
||||||
|
ADMINS[0],
|
||||||
|
[followed.email],
|
||||||
|
render_template("follower_email.txt",
|
||||||
|
user = followed, follower = follower),
|
||||||
|
render_template("follower_email.html",
|
||||||
|
user = followed, follower = follower))
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
<p>Dear {{user.nickname}},</p>
|
||||||
|
<p><a href="{{url_for("user", nickname = follower.nickname, _external = True)}}">{{follower.nickname}}</a> is now a follower.</p>
|
||||||
|
<table>
|
||||||
|
<tr valign="top">
|
||||||
|
<td><img src="{{follower.avatar(50)}}"></td>
|
||||||
|
<td>
|
||||||
|
<a href="{{url_for('user', nickname = follower.nickname, _external = True)}}">{{follower.nickname}}</a><br />
|
||||||
|
{{follower.about_me}}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<p>Regards,</p>
|
||||||
|
<p>The <code>microblog</code> admin</p>
|
|
@ -0,0 +1,9 @@
|
||||||
|
Dear {{user.nickname}},
|
||||||
|
|
||||||
|
{{follower.nickname}} is now a follower. Click on the following link to visit {{follower.nickname}}'s profile page:
|
||||||
|
|
||||||
|
{{url_for("user", nickname = follower.nickname, _external = True)}}
|
||||||
|
|
||||||
|
Regards,
|
||||||
|
|
||||||
|
The microblog admin
|
|
@ -4,6 +4,7 @@ from app import app, db, lm, oid
|
||||||
from forms import LoginForm, EditForm, PostForm, SearchForm
|
from forms import LoginForm, EditForm, PostForm, SearchForm
|
||||||
from models import User, ROLE_USER, ROLE_ADMIN, Post
|
from models import User, ROLE_USER, ROLE_ADMIN, Post
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from emails import follower_notification
|
||||||
from config import POSTS_PER_PAGE, MAX_SEARCH_RESULTS
|
from config import POSTS_PER_PAGE, MAX_SEARCH_RESULTS
|
||||||
|
|
||||||
@lm.user_loader
|
@lm.user_loader
|
||||||
|
@ -135,6 +136,7 @@ def follow(nickname):
|
||||||
db.session.add(u)
|
db.session.add(u)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash('You are now following ' + nickname + '!')
|
flash('You are now following ' + nickname + '!')
|
||||||
|
follower_notification(user, g.user)
|
||||||
return redirect(url_for('user', nickname = nickname))
|
return redirect(url_for('user', nickname = nickname))
|
||||||
|
|
||||||
@app.route('/unfollow/<nickname>')
|
@app.route('/unfollow/<nickname>')
|
||||||
|
|
10
config.py
10
config.py
|
@ -15,11 +15,13 @@ SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
|
||||||
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
|
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
|
||||||
WHOOSH_BASE = os.path.join(basedir, 'search.db')
|
WHOOSH_BASE = os.path.join(basedir, 'search.db')
|
||||||
|
|
||||||
# mail server settings
|
# email server
|
||||||
MAIL_SERVER = 'localhost'
|
MAIL_SERVER = 'your.mailserver.com'
|
||||||
MAIL_PORT = 25
|
MAIL_PORT = 25
|
||||||
MAIL_USERNAME = None
|
MAIL_USE_TLS = False
|
||||||
MAIL_PASSWORD = None
|
MAIL_USE_SSL = False
|
||||||
|
MAIL_USERNAME = 'you'
|
||||||
|
MAIL_PASSWORD = 'your-password'
|
||||||
|
|
||||||
# administrator list
|
# administrator list
|
||||||
ADMINS = ['you@example.com']
|
ADMINS = ['you@example.com']
|
||||||
|
|
Loading…
Reference in New Issue