def notify_signup_accepted(event_name, signup): """Send an email to a user, that his signup was accepted""" id_field = current_app.config['ID_FIELD'] if signup.get('user'): lookup = {id_field: signup['user']} user = current_app.data.find_one('users', None, **lookup) name = user['firstname'] email = user['email'] else: name = 'Guest of AMIV' email = signup['email'] token = Signer(get_token_secret()).sign( str(signup[id_field]).encode('utf-8')) if current_app.config.get('SERVER_NAME') is None: current_app.logger.warning("SERVER_NAME is not set. E-Mail links " "will not work!") deletion_link = url_for('emails.on_delete_signup', token=token, _external=True) mail( current_app.config['API_MAIL'], email, '[AMIV] Eventsignup accepted', 'Hello %s!\n' '\n' 'We are happy to inform you that your signup for %s was accepted and ' 'you can come to the event! If you do not have time to attend the ' 'event please click this link to free your spot for someone else:\n' '\n%s\n\n' 'Best Regards,\n' 'The AMIV event bot' % (name, event_name, deletion_link))
def send_confirmmail_to_unregistered_users(items): """Send a confirmation email for external signups(email only) Args: item: The item, which was just inserted into the database """ for item in items: if 'user' not in item: event = current_app.data.find_one( 'events', None, **{current_app.config['ID_FIELD']: item['event']}) if 'title_en' in event: title = event['title_en'] else: title = event['title_de'] token = Signer(get_token_secret()).sign( str(item['_id']).encode('utf-8')) if current_app.config.get('SERVER_NAME') is None: current_app.logger.warning("SERVER_NAME is not set. E-Mail " "links will not work!") fields = { 'link': url_for('emails.on_confirm_email', token=token, _external=True), 'title': title } email_content = current_app.config['CONFIRM_EMAIL_TEXT'] % fields mail(current_app.config['API_MAIL'], # from [item['email']], # receivers list 'Registration for AMIV event %s' % title, email_content)
def on_confirm_email(token): """Email confirmation endpoint. We try to confirm the specified signup and redirect to a webpage. """ try: s = URLSafeSerializer(get_token_secret()) signup_id = ObjectId(s.loads(token)) except BadSignature: return "Unknown token" patch_internal('eventsignups', {'confirmed': True}, skip_validation=True, concurrency_check=False, **{current_app.config['ID_FIELD']: signup_id}) # Now the user may be able to get accepted, so update the events waiting # list lookup = {current_app.config['ID_FIELD']: signup_id} signup = current_app.data.find_one('eventsignups', None, **lookup) update_waiting_list(signup['event']) redirect_url = current_app.config.get('EMAIL_CONFIRMED_REDIRECT') if redirect_url: return redirect(redirect_url) else: return current_app.config['CONFIRM_TEXT']
def send_confirmmail_to_unregistered_users(items): """Send a confirmation email for external signups(email only) Args: item: The item, which was just inserted into the database """ for item in items: if item.get('user') is None: event = current_app.data.find_one( 'events', None, **{current_app.config['ID_FIELD']: item['event']}) title = event.get('title_en') or event.get('title_de') s = URLSafeSerializer(get_token_secret()) token = s.dumps(str(item['_id'])) if current_app.config.get('SERVER_NAME') is None: current_app.logger.warning("SERVER_NAME is not set. E-Mail " "links will not work!") confirm_link = url_for('emails.on_confirm_email', token=token, _external=True) mail([item['email']], 'Registration for %s' % title, current_app.config['CONFIRM_EMAIL_TEXT'].format( title=title, link=confirm_link))
def send_confirmmail_to_unregistered_users(items): """Send a confirmation email for external signups(email only) Args: item: The item, which was just inserted into the database """ for item in items: if 'user' not in item: event = current_app.data.find_one( 'events', None, **{current_app.config['ID_FIELD']: item['event']}) title = event.get('title_en') or event.get('title_de') s = URLSafeSerializer(get_token_secret()) token = s.dumps(str(item['_id'])) if current_app.config.get('SERVER_NAME') is None: current_app.logger.warning("SERVER_NAME is not set. E-Mail " "links will not work!") confirm_link = url_for('emails.on_confirm_email', token=token, _external=True) mail([item['email']], 'Registration for %s' % title, current_app.config['CONFIRM_EMAIL_TEXT'].format( title=title, link=confirm_link))
def notify_signup_accepted(event, signup): """Send an email to a user that his signup was accepted""" id_field = current_app.config['ID_FIELD'] if signup.get('user'): lookup = {id_field: signup['user']} user = current_app.data.find_one('users', None, **lookup) name = user['firstname'] email = user['email'] else: name = 'Guest of AMIV' email = signup['email'] s = URLSafeSerializer(get_token_secret()) token = s.dumps(str(signup[id_field])) if current_app.config.get('SERVER_NAME') is None: current_app.logger.warning("SERVER_NAME is not set. E-Mail links " "will not work!") deletion_link = url_for('emails.on_delete_signup', token=token, _external=True) mail([email], 'Eventsignup accepted', current_app.config['ACCEPT_EMAIL_TEXT'].format( name=name, title=event.get('title_en') or event.get('title_de'), link=deletion_link, deadline=event['time_register_end'].strftime('%H.%M %d.%m.%Y')))
def on_delete_signup(token): """Endpoint to delete signups via email""" try: s = Signer(get_token_secret()) signup_id = ObjectId(s.unsign(token).decode('utf-8')) except BadSignature: return "Unknown token" deleteitem_internal('eventsignups', concurrency_check=False, **{current_app.config['ID_FIELD']: signup_id}) redirect_url = current_app.config.get('SIGNUP_DELETED_REDIRECT') if redirect_url: return redirect(redirect_url) else: return current_app.config['SIGNUP_DELETED_TEXT']
def on_delete_confirmed(token): try: s = URLSafeSerializer(get_token_secret()) signup_id = ObjectId(s.loads(token)) except BadSignature: return "Unknown token" deleteitem_internal('eventsignups', concurrency_check=False, **{current_app.config['ID_FIELD']: signup_id}) redirect_url = current_app.config.get('SIGNUP_DELETED_REDIRECT') if redirect_url: return redirect(redirect_url) else: return current_app.config['SIGNUP_DELETED_TEXT']
def on_delete_signup(token): """Endpoint to delete signups via email""" try: s = URLSafeSerializer(get_token_secret()) signup_id = ObjectId(s.loads(token)) except BadSignature: return "Unknown token" # Verify if user confirmed # definitive = request.args.get('DEFINITIVE_DELETE') # Get first name for personal greeting error_msg = '' query = {'_id': signup_id} data_signup = current_app.data.driver.db['eventsignups'].find_one(query) if data_signup is None: error_msg = "This event might not exist anymore or the link is broken." user = data_signup['user'] if user is None: user = data_signup['email'] else: query = {'_id': user} data_user = current_app.data.driver.db['users'].find_one(query) user = data_user["firstname"] event = data_signup['event'] query = {'_id': event} data_event = current_app.data.driver.db['events'].find_one(query) event_name = data_event["title_en"] if event_name is None: event_name = data_event["title_en"] if data_event["time_start"] is None: event_date = "a yet undefined day." else: event_date = datetime.strftime(data_event["time_start"], '%Y-%m-%d %H:%M') # Serve the unregister_event page response = make_response( render_template("unregister_event.html", user=user, event=event_name, event_date=event_date, error_msg=error_msg, token=token)) response.set_cookie('token', token) return response
def test_existing_secret(self): """Test that a secret from the database is not overwritten.""" # We need to run the setup to be able to use an app context super().setUp() old_secret = 'Trololololo' # Set the secret in the database with self.app.app_context(): self.db['config'].update_one( {SECRET_KEY: {'$exists': True}}, {'$set': {SECRET_KEY: old_secret}} ) # This should now not change the token create_token_secret_on_startup(self.app) with self.app.app_context(): self.assertEqual(get_token_secret(), old_secret)