def export_tasks(current_user_email_addr, short_name, ty, expanded, filetype, **filters): """Export tasks/taskruns from a project.""" from pybossa.core import task_csv_exporter, task_json_exporter from pybossa.cache import projects as cached_projects project = cached_projects.get_project(short_name) try: # Export data and upload .zip file locally if filetype == 'json': path = task_json_exporter.make_zip(project, ty, expanded, **filters) elif filetype == 'csv': path = task_csv_exporter.make_zip(project, ty, expanded, **filters) else: path = None # Construct message if path is not None: # Success email subject = 'Data exported for your project: {0}'.format(project.name) msg = 'Your exported data is attached.' else: # Failure email subject = 'Data export failed for your project: {0}'.format(project.name) msg = 'There was an issue with your export. ' + \ 'Please try again or report this issue ' + \ 'to a {0} administrator.' msg = msg.format(current_app.config.get('BRAND')) body = 'Hello,\n\n' + msg + '\n\nThe {0} team.' body = body.format(current_app.config.get('BRAND')) mail_dict = dict(recipients=[current_user_email_addr], subject=subject, body=body) message = Message(**mail_dict) # Attach export file to message if path is not None: with current_app.open_resource(path) as fp: message.attach(path.split('/')[-1], "application/zip", fp.read()) mail.send(message) job_response = '{0} {1} file was successfully exported for: {2}' return job_response.format( ty.capitalize(), filetype.upper(), project.name) except: current_app.logger.exception( 'Export email failed - Project: {0}' .format(project.name)) subject = 'Email delivery failed for your project: {0}'.format(project.name) msg = 'There was an error when attempting to deliver your data export via email.' body = 'Hello,\n\n' + msg + '\n\nThe {0} team.' body = body.format(current_app.config.get('BRAND')) mail_dict = dict(recipients=[current_user_email_addr], subject=subject, body=body) message = Message(**mail_dict) mail.send(message) raise
def register(): """ Register method for creating a PyBossa account. Returns a Jinja2 template """ form = RegisterForm(request.form) if request.method == 'POST' and form.validate(): account = dict(fullname=form.fullname.data, name=form.name.data, email_addr=form.email_addr.data, password=form.password.data) key = signer.dumps(account, salt='account-validation') confirm_url = url_for('.confirm_account', key=key, _external=True) if current_app.config.get('ACCOUNT_CONFIRMATION_DISABLED'): return redirect(confirm_url) msg = Message(subject='Welcome to %s!' % current_app.config.get('BRAND'), recipients=[account['email_addr']]) msg.body = render_template('/account/email/validate_account.md', user=account, confirm_url=confirm_url) msg.html = markdown(msg.body) mail.send(msg) return render_template('account/account_validation.html') if request.method == 'POST' and not form.validate(): flash(gettext('Please correct the errors'), 'error') return render_template('account/register.html', title=gettext("Register"), form=form)
def register(): """ Register method for creating a PyBossa account. Returns a Jinja2 template """ form = RegisterForm(request.form) if request.method == 'POST' and form.validate(): account = dict(fullname=form.fullname.data, name=form.name.data, email_addr=form.email_addr.data, password=form.password.data) key = signer.dumps(account, salt='account-validation') confirm_url = url_for('.confirm_account', key=key, _external=True) msg = Message(subject='Welcome to %s!' % current_app.config.get('BRAND'), recipients=[account['email_addr']]) msg.body = render_template('/account/email/validate_account.md', user=account, confirm_url=confirm_url) msg.html = markdown(msg.body) mail.send(msg) return render_template('account/account_validation.html') if request.method == 'POST' and not form.validate(): flash(gettext('Please correct the errors'), 'error') return render_template('account/register.html', title=gettext("Register"), form=form)
def forgot_password(): form = ForgotPasswordForm(request.form) if form.validate_on_submit(): user = model.User.query.filter_by(email_addr=form.email_addr.data).first() if user and user.email_addr: msg = Message(subject="Account Recovery", recipients=[user.email_addr]) if user.twitter_user_id: msg.body = render_template( "/account/email/forgot_password_openid.md", user=user, account_name="Twitter" ) elif user.facebook_user_id: msg.body = render_template( "/account/email/forgot_password_openid.md", user=user, account_name="Facebook" ) elif user.google_user_id: msg.body = render_template("/account/email/forgot_password_openid.md", user=user, account_name="Google") else: userdict = {"user": user.name, "password": user.passwd_hash} key = signer.dumps(userdict, salt="password-reset") recovery_url = url_for(".reset_password", key=key, _external=True) msg.body = render_template("/account/email/forgot_password.md", user=user, recovery_url=recovery_url) msg.html = markdown(msg.body) mail.send(msg) flash("We've send you email with account recovery instructions!", "success") else: flash( "We don't have this email in our records. You may have" " signed up with a different email or used Twitter, " "Facebook, or Google to sign-in", "error", ) if request.method == "POST" and not form.validate(): flash("Something went wrong, please correct the errors on the " "form", "error") return render_template("/account/password_forgot.html", form=form)
def forgot_password(): """ Request a forgotten password for a user. Returns a Jinja2 template. """ form = ForgotPasswordForm(request.form) if form.validate_on_submit(): user = model.user.User.query\ .filter_by(email_addr=form.email_addr.data)\ .first() if user and user.email_addr: msg = Message(subject='Account Recovery', recipients=[user.email_addr]) if user.twitter_user_id: msg.body = render_template( '/account/email/forgot_password_openid.md', user=user, account_name='Twitter') elif user.facebook_user_id: msg.body = render_template( '/account/email/forgot_password_openid.md', user=user, account_name='Facebook') elif user.google_user_id: msg.body = render_template( '/account/email/forgot_password_openid.md', user=user, account_name='Google') else: userdict = {'user': user.name, 'password': user.passwd_hash} key = signer.dumps(userdict, salt='password-reset') recovery_url = url_for('.reset_password', key=key, _external=True) msg.body = render_template('/account/email/forgot_password.md', user=user, recovery_url=recovery_url) msg.html = markdown(msg.body) mail.send(msg) flash( gettext("We've send you email with account " "recovery instructions!"), 'success') else: flash( gettext("We don't have this email in our records. " "You may have signed up with a different " "email or used Twitter, Facebook, or " "Google to sign-in"), 'error') if request.method == 'POST' and not form.validate(): flash( gettext('Something went wrong, please correct the errors on the ' 'form'), 'error') return render_template('/account/password_forgot.html', form=form)
def send_mail(message_dict): """Send email.""" message = Message(**message_dict) spam = False for r in message_dict['recipients']: acc, domain = r.split('@') if domain in current_app.config.get('SPAM'): spam = True break if not spam: mail.send(message)
def forgot_password(): """ Request a forgotten password for a user. Returns a Jinja2 template. """ form = ForgotPasswordForm(request.form) if form.validate_on_submit(): user = model.user.User.query\ .filter_by(email_addr=form.email_addr.data)\ .first() if user and user.email_addr: msg = Message(subject='Account Recovery', recipients=[user.email_addr]) if user.twitter_user_id: msg.body = render_template( '/account/email/forgot_password_openid.md', user=user, account_name='Twitter') elif user.facebook_user_id: msg.body = render_template( '/account/email/forgot_password_openid.md', user=user, account_name='Facebook') elif user.google_user_id: msg.body = render_template( '/account/email/forgot_password_openid.md', user=user, account_name='Google') else: userdict = {'user': user.name, 'password': user.passwd_hash} key = signer.dumps(userdict, salt='password-reset') recovery_url = url_for('.reset_password', key=key, _external=True) msg.body = render_template( '/account/email/forgot_password.md', user=user, recovery_url=recovery_url) msg.html = markdown(msg.body) mail.send(msg) flash(gettext("We've send you email with account " "recovery instructions!"), 'success') else: flash(gettext("We don't have this email in our records. " "You may have signed up with a different " "email or used Twitter, Facebook, or " "Google to sign-in"), 'error') if request.method == 'POST' and not form.validate(): flash(gettext('Something went wrong, please correct the errors on the ' 'form'), 'error') return render_template('/account/password_forgot.html', form=form)
def send_mail(message_dict, user_id=None): """Send email.""" from pybossa.core import db from pybossa.model.user import User message = Message(**message_dict) spam = False for r in message_dict['recipients']: acc, domain = r.split('@') if domain in current_app.config.get('SPAM'): spam = True break if not spam: mail.send(message) if user_id: user = User.query.get(user_id) user.notified_at = datetime.now() db.session.add(user) db.session.commit()
def send_newsletter(): """ Endpoint to send newsletter to all subscribersIL """ from ..model.form.newsletter import NewsletterForm form = NewsletterForm() if request.method == "POST": try: if request.form.get('debug_mode'): SUBJECT = "DEBUG :: "+request.form['subject'] EMAIL_LIST = current_app.config['GEOTAGX_NEWSLETTER_DEBUG_EMAIL_LIST'] else: EMAIL_REGEX = "([^@|\s]+@[^@]+\.[^@|\s]+)" SUBJECT = request.form['subject'] user_list = User.query.with_entities(User.email_addr).filter(User.subscribed==True).all() EMAIL_LIST = [] for _user_email in user_list: if re.match(EMAIL_REGEX, _user_email[0]): EMAIL_LIST.append(_user_email[0]) mail_dict = dict( subject=SUBJECT, html=markdown.markdown(request.form['message']), # recipients=['*****@*****.**'],#The "To" field of the email always points to the geotagx e-group. Also helps in archiving. ) for _email in EMAIL_LIST: mail_dict['recipients'] = [_email] message = Message(**mail_dict) mail.send(message) flash("Newsletter sent successfully", "success") except: flash("Unable to send newsletter. Please contact the systems administrator.", "error") debug_emails = current_app.config["GEOTAGX_NEWSLETTER_DEBUG_EMAIL_LIST"] return render_template("/geotagx/newsletter/newsletter.html", form=form, debug_emails=debug_emails)
def send_mail(message_dict): """Send email.""" message = Message(**message_dict) mail.send(message)
def user_add(name,user=None): ''' Add Current User to a team ''' team = cached_teams.get_team(name) title = gettext('Add User to a Team') if not require.team.read(): abort(403) if request.method == 'GET': return render_template( '/team/user_add.html', title=title, team=team, user=user ) if user: user_search = User.query.filter_by(name=user).first() if not user_search: flash(gettext('This user don\t exists!!!'), 'error') return redirect(url_for('team.myteams', name=team.name )) else: ''' Check to see if the current_user is the owner or admin ''' if current_user.admin is True or team.owner_id == current_user.id: user_id = user_search.id else: flash(gettext('You do not have right to add to this team!!!'), 'error') return redirect(url_for('team.myteams', name=team.name )) else: user_search= current_user '''user_id = current_user.id''' ''' Search relationship ''' user2team = db.session.query(User2Team)\ .filter(User2Team.user_id == user_search.id )\ .filter(User2Team.team_id == team.id )\ .first() if user2team: flash(gettext('This user is already in this team'), 'error') return redirect(url_for('team.search_users', name=team.name )) else: if team.public == True: cached_teams.delete_team_members() user2team = User2Team( user_id = user_search.id, team_id = team.id ) db.session.add(user2team) db.session.commit() flash(gettext('Association to the team created'), 'success') return redirect(url_for('team.myteams' )) else: msg = Message(subject='Invitation to a Team', recipients=[user_search.email_addr]) userdict = {'user': user_search.name, 'team': team.name } key = signer.dumps(userdict, salt='join-private-team') join_url = url_for('.join_private_team', key=key, _external=True) msg.body = render_template( '/team/email/send_invitation.md', user=user_search, team=team, join_url=join_url) msg.html = markdown(msg.body) mail.send(msg) return render_template('./team/message.html')
def send_mail(message_dict): message = Message(**message_dict) mail.send(message)
def user_add(name, user=None): ''' Add Current User to a team ''' team = cached_teams.get_team(name) title = gettext('Add User to a Team') if not require.team.read(): abort(403) if request.method == 'GET': return render_template('/team/user_add.html', title=title, team=team, user=user) if user: user_search = User.query.filter_by(name=user).first() if not user_search: flash(gettext('This user don\t exists!!!'), 'error') return redirect(url_for('team.myteams', name=team.name)) else: ''' Check to see if the current_user is the owner or admin ''' if current_user.admin is True or team.owner_id == current_user.id: user_id = user_search.id else: flash(gettext('You do not have right to add to this team!!!'), 'error') return redirect(url_for('team.myteams', name=team.name)) else: user_search = current_user '''user_id = current_user.id''' ''' Search relationship ''' user2team = db.session.query(User2Team)\ .filter(User2Team.user_id == user_search.id )\ .filter(User2Team.team_id == team.id )\ .first() if user2team: flash(gettext('This user is already in this team'), 'error') return redirect(url_for('team.search_users', name=team.name)) else: if team.public == True: cached_teams.delete_team_members() user2team = User2Team(user_id=user_search.id, team_id=team.id) db.session.add(user2team) db.session.commit() flash(gettext('Association to the team created'), 'success') return redirect(url_for('team.myteams')) else: msg = Message(subject='Invitation to a Team', recipients=[user_search.email_addr]) userdict = {'user': user_search.name, 'team': team.name} key = signer.dumps(userdict, salt='join-private-team') join_url = url_for('.join_private_team', key=key, _external=True) msg.body = render_template('/team/email/send_invitation.md', user=user_search, team=team, join_url=join_url) msg.html = markdown(msg.body) mail.send(msg) return render_template('./team/message.html')
def export_tasks(current_user_email_addr, short_name, ty, expanded, filetype, filters=None): """Export tasks/taskruns from a project.""" from pybossa.core import (task_csv_exporter, task_json_exporter, project_repo) import pybossa.exporter.consensus_exporter as export_consensus project = project_repo.get_by_shortname(short_name) try: # Export data and upload .zip file locally if ty == 'consensus': export_fn = getattr(export_consensus, 'export_consensus_{}'.format(filetype)) elif filetype == 'json': export_fn = task_json_exporter.make_zip elif filetype == 'csv': export_fn = task_csv_exporter.make_zip else: export_fn = None # Construct message if export_fn is not None: # Success email subject = u'Data exported for your project: {0}'.format(project.name) msg = u'Your exported data is attached.' else: # Failure email subject = u'Data export failed for your project: {0}'.format(project.name) msg = u'There was an issue with your export. ' + \ u'Please try again or report this issue ' + \ u'to a {0} administrator.' msg = msg.format(current_app.config.get('BRAND')) body = u'Hello,\n\n' + msg + '\n\nThe {0} team.' body = body.format(current_app.config.get('BRAND')) mail_dict = dict(recipients=[current_user_email_addr], subject=subject, body=body) message = Message(**mail_dict) # Attach export file to message if export_fn is not None: with export_fn(project, ty, expanded, filters) as fp: message.attach(fp.filename, "application/zip", fp.read()) mail.send(message) job_response = u'{0} {1} file was successfully exported for: {2}' return job_response.format( ty.capitalize(), filetype.upper(), project.name) except: current_app.logger.exception( u'Export email failed - Project: {0}' .format(project.name)) subject = u'Email delivery failed for your project: {0}'.format(project.name) msg = u'There was an error when attempting to deliver your data export via email.' body = u'Hello,\n\n' + msg + u'\n\nThe {0} team.' body = body.format(current_app.config.get('BRAND')) mail_dict = dict(recipients=[current_user_email_addr], subject=subject, body=body) message = Message(**mail_dict) mail.send(message) raise
def send_mail(message_dict, mail_all=False): """Send email.""" if mail_all or mail_with_enabled_users(message_dict): message = Message(**message_dict) mail.send(message)