def post(self, project_id, slug): """A project has been identified as new. Send them a welcome.""" project = Project.get_by_id(project_id) program = Program.get_config(project.program_label) org = Organization.get_by_id(project.organization_id) # The Org liaison is preferred, but is not guaranteed to be set (users # choose their org liaison explicitly as one of the org tasks). Default # to the Project liaison, which is set on creation in # add_program.controller.js@joinProgram org_liaison = User.get_by_id(org.liaison_id) project_liaison = User.get_by_id(project.liaison_id) liaison = org_liaison or project_liaison email = Email.create( to_address=liaison.email, mandrill_template=slug, mandrill_template_content={ 'program_name': program['name'], 'organization_name': org.name, 'liaison_name': liaison.name, 'join_date': util.datelike_to_iso_string(project.created), }, ) email.put()
def test_spam_with_two_emails_to_same_address(self): # Create a single email to_address = '*****@*****.**' email = Email.create(to_address=to_address) email.put() second_email = Email.create(to_address=to_address) second_email.put() Email.send_pending_email() fetched_email = Email.query().fetch() self.assertEqual(len(fetched_email), 2) self.assertIsInstance(fetched_email[0], Email) self.assertIsInstance(fetched_email[1], Email) # Test if email was attempted to send self.assertEqual(fetched_email[0].was_attempted, True) self.assertEqual(fetched_email[1].was_attempted, True) # ...and has been sent self.assertEqual(fetched_email[0].was_sent, True) self.assertEqual(fetched_email[1].was_sent, False)
def index(): if request.method == "POST": if not Email.get_or_none(Email.email == request.form['email']): if EMAIL_REGEX.match(request.form["email"]): Email.create( email=request.form["email"], timestamp=datetime.datetime.now(), ) flash("The email address you entered ({}) \ \nis a VALID email address! Thank you!".format( request.form['email'])) return redirect("/success") else: flash("Email is not valid!") else: flash("Email already exists!") return render_template('index.html')
def index(): if request.method == "POST": if not Email.get_or_none(Email.email == request.form['email']): if EMAIL_REGEX.match(request.form["email"]): Email.create( email=request.form["email"], timestamp=datetime.datetime.now(), ) flash("The email address you entered ({}) \ \nis a VALID email address! Thank you!".format( request.form['email'])) return redirect("/success") else: flash("Email is not valid!") else: flash("Email already exists!") return render_template('index.html')
def test_spam_with_recent_email_to_same_addresses(self): # Create a sent and not sent email to same address # Note: Cannot be to a '@mindsetkit.org' address to_address = '*****@*****.**' email_subject = 'Email subject' sent_email = Email.create( to_address=to_address, was_attempted=True, was_sent=True) sent_email.put() email = Email.create( to_address=to_address, subject=email_subject) email.put() Email.send_pending_email() fetched_email = Email.query(Email.subject == email_subject).get() self.assertIsInstance(fetched_email, Email) # Test if email was attempted to send self.assertEqual(fetched_email.was_attempted, True) # ...and has been sent self.assertEqual(fetched_email.was_sent, False)
def test_spam_with_single_email(self): # Create a single email to_address = '*****@*****.**' email = Email.create(to_address=to_address) email.put() Email.send_pending_email() fetched_email = Email.query(Email.to_address == to_address).get() self.assertIsInstance(fetched_email, Email) # Test if email was attempted to send self.assertEqual(fetched_email.was_attempted, True) # ...and has been sent self.assertEqual(fetched_email.was_sent, True)
def after_put(self, init_kwargs, *args, **kwargs): is_using = self.meta.get('artifact_use', None) == 'true' url = self.meta.get('artifact_url', None) is_different = init_kwargs['meta'].get('artifact_url', None) != url if is_using and url and is_different: team = Team.get_by_id(self.team_id) team_url = 'https://copilot.perts.net/teams/{}'.format( team.short_uid) Email.create( to_address=config.from_server_email_address, subject=(u"Message IT artifact URL needs review: {}".format( team.name)), html=u''' <p>Team name: <a href="{team_url}">{team_name}</a></p> <p> Artifact URL: <a href="{artifact_url}">{artifact_url}</a> </p> '''.format( team_name=team.name, artifact_url=self.meta['artifact_url'], team_url=team_url, ), ).put()
def create_ep_email( program_label, recipient, users, team, classrooms, responses, cycle, pct_complete_by_id, ): """Render a cycle reminder email. Args: program_label, e.g. 'ep19' recipient - User users - all Users on the team team - Team classrooms - Classrooms, all on the team responses - All responses from the team, with no bodies cycle - Cycle, current pct_complete_by_id - dict, {classroom_id: int} Returns: html string """ if team.captain_id == recipient.uid: user_classrooms = classrooms else: user_classrooms = [ c for c in classrooms if recipient.uid == c.contact_id ] # EP Journals are always user-level. ids_with_responses = [r.user_id for r in responses if r.user_id] journal_statuses = [{ 'user_id': u.uid, 'user_name': u.name, 'status': ('Complete' if u.uid in ids_with_responses else 'Incomplete'), } for u in users] own_journal_status = next(d for d in journal_statuses if d['user_id'] == recipient.uid)['status'] params = { 'user': recipient, 'is_captain': recipient.uid == team.captain_id, 'team': team, 'classrooms': user_classrooms, 'copilot_url': 'https://{}'.format(os.environ['HOSTING_DOMAIN']), 'cycle': cycle, 'pct_complete_by_id': pct_complete_by_id, 'journal_complete': own_journal_status == 'Complete', 'journal_statuses': journal_statuses, } # Why not send these the "normal" way, where we pass the template # file name and the template params into the Email entity? Because this # is a case where the parameters are quite large and non-trivial to # serialize to JSON for storage. Simpler to reduce the incoming data # to the rendered html body and store that in the entity. template = jinja_env.get_template( '{}/cycle_progress.html'.format(program_label)) return Email.create(to_address=recipient.email, subject="Engagement Project: Progress Update", html=template.render(**params))
def get_email(self, to_address): template = jinja_env.get_template('notifications/digest_email.html') return Email.create(to_address=to_address, subject=self.subject, html=template.render(body=self.body))