def notify_blog_users(blog_id, project_id, queue='high'): """Send email with new blog post.""" from sqlalchemy.sql import text from pybossa.core import db from pybossa.core import blog_repo from pybossa.pro_features import ProFeatureHandler blog = blog_repo.get(blog_id) users = 0 feature_handler = ProFeatureHandler(current_app.config.get('PRO_FEATURES')) only_pros = feature_handler.only_for_pro('notify_blog_updates') timeout = current_app.config.get('TIMEOUT') if blog.project.featured or (blog.project.owner.pro or not only_pros): sql = text(''' SELECT email_addr, name from "user", task_run WHERE task_run.project_id=:project_id AND task_run.user_id="user".id AND "user".subscribed=true AND "user".restrict=false GROUP BY email_addr, name, subscribed; ''') results = db.slave_session.execute(sql, dict(project_id=project_id)) for row in results: subject = "Project Update: %s by %s" % (blog.project.name, blog.project.owner.fullname) body = render_template('/account/email/blogupdate.md', user_name=row.name, blog=blog, config=current_app.config) html = render_template('/account/email/blogupdate.html', user_name=row.name, blog=blog, config=current_app.config) mail_dict = dict(recipients=[row.email_addr], subject=subject, body=body, html=html) job = dict(name=send_mail, args=[mail_dict], kwargs={}, timeout=timeout, queue=queue) enqueue_job(job) users += 1 msg = "%s users notified by email" % users return msg
def notify_blog_users(blog_id, project_id, queue='high'): """Send email with new blog post.""" from sqlalchemy.sql import text from pybossa.core import db from pybossa.core import blog_repo from pybossa.pro_features import ProFeatureHandler blog = blog_repo.get(blog_id) users = 0 feature_handler = ProFeatureHandler(current_app.config.get('PRO_FEATURES')) only_pros = feature_handler.only_for_pro('notify_blog_updates') timeout = current_app.config.get('TIMEOUT') if blog.project.featured or (blog.project.owner.pro or not only_pros): sql = text(''' SELECT email_addr, name from "user", task_run WHERE task_run.project_id=:project_id AND task_run.user_id="user".id AND "user".subscribed=true AND "user".restrict=false GROUP BY email_addr, name, subscribed; ''') results = db.slave_session.execute(sql, dict(project_id=project_id)) for row in results: subject = "Project Update: %s by %s" % (blog.project.name, blog.project.owner.fullname) body = render_template('/account/email/blogupdate.md', user_name=row.name, blog=blog, config=current_app.config) html = render_template('/account/email/blogupdate.html', user_name=row.name, blog=blog, config=current_app.config) mail_dict = dict(recipients=[row.email_addr], subject=subject, body=body, html=html) job = dict(name=send_mail, args=[mail_dict], kwargs={}, timeout=timeout, queue=queue) enqueue_job(job) users += 1 msg = "%s users notified by email" % users return msg