def emailUnauthenticatedUser(email, authGuid): """ Send unauthenticated user a link to authenticate. Using template: auth_user @type email: string @param email: Email address to send to @rtype: * @returns: Emailer send response. """ # Create values for template. emailAccount = Config.get('email') subject = "Please authenticate your account" link = "%sjoin/auth/%s" % (Config.get('default_host'), authGuid) template_values = { 'link': link, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/auth_user', template_values, suffix = 'txt') # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send authenticate user email") log.error(e) return False
def emailProjectEndorsement(email, title, leaderName): """ Email project admins about endorsements. Using template: project_endorsement @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') subject = "%s liked your project!" % leaderName template_values = { 'title': title, 'leader_name': leaderName, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/project_endorsement', template_values, suffix = 'txt') # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send endorsement email") log.error(e) return False
def emailTempPassword(email, password): """ Email temporary password. Using template: forgot_password @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') subject = "Your password has been reset" link = "%slogin" % Config.get('default_host') link = "%stou" % Config.get('default_host') template_values = { 'password': password, 'link': link, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/forgot_password', template_values, suffix = 'txt') # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send forgot password email") log.error(e) return False
def emailResourceApproval(email, title): """ Email resource owner on approval. Using template: resource_approval @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') subject = "Your resource has been approved" template_values = { 'link': Config.get('default_host'), 'title': title, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/resource_approval', template_values, suffix = 'txt') # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send resource approval email") log.error(e) return False
def emailAccountDeactivation(email): """ Email deleted users. Using template: account_deactivation @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') subject = "Your account has been deactivated" link = "%stou" % Config.get('default_host') template_values = { 'link': link, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/account_deactivation', template_values, suffix = 'txt') # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send account deactivation email") log.error(e) return False
def test_get_all(self): """ Test `get_all` method """ data = Config.get_all() self.assertIsNotNone(data) self.assertIsInstance(data, dict)
def test_manualSetConfig(self): """ Test manually setting a value. """ # Assert that value is not there self.assertNotIn('testing', Config.data) # Set new value and assert it is there Config.data['testing'] = True data = Config.get_all() self.assertTrue(data['testing'])
def directMessageUser(db, toUserId, toName, toEmail, fromUserId, fromName, message): """ Email user about direct message. Using template: direct_message @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') #email = "%s <%s>" % (toName, toEmail) email = toEmail subject = "Change By Us message from %s" % fromName link = "%suseraccount/%s" % (Config.get('default_host'), fromUserId) template_values = { 'name': fromName, 'message': message, 'link': link, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/direct_message', template_values, suffix = 'txt') # Send email. try: isSent = Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) if (isSent): db.insert('direct_message', message = message, to_user_id = toUserId, from_user_id = fromUserId) return True else: log.info("*** couldn't log direct message") # Not sure if best to return False return False except Exception, e: log.info("*** couldn't send direct message email") log.error(e) return False
def emailResourceNotification(email, projectId, title, description, resourceName): """ Email resource contacts on resource add. Using template: resource_notification @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') subject = "A project on Changeby.us has added %s as a resource" % resourceName link = "%sproject/%s" % (Config.get('default_host'), str(projectId)) template_values = { 'title': title, 'description': description, 'resource_name': resourceName, 'link': link, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/resource_notification', template_values, suffix = 'txt') # If dev, don't email resources if (Config.get('dev')): log.info("*** body = %s" % body) return True # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send resource notification email") log.error(e) return False
def emailProjectJoin(email, projectId, title, userId, userName): """ Email project admins when new user joins. Using template: project_join @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') defaultUrl = Config.get('default_host') subject = "A new member %s has joined your project %s" % (userName, title) userLink = "%suseraccount/%s" % (defaultUrl, str(userId)) memberLink = "%sproject/%s#show,members" % (defaultUrl, str(projectId)) template_values = { 'title': title, 'user_name': userName, 'user_link': userLink, 'member_link': memberLink, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/project_join', template_values, suffix = 'txt') # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send join email") log.error(e) return False
def emailIdeaConfirmation(email, responseEmail, locationId): """ Email upon idea submission. Using template: idea_confirmation @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') host = Config.get('default_host') subject = "Thanks for submitting an idea to Change by Us!" searchLink = "%ssearch?location_id=%s" % (host, locationId) createLink = "%screate" % host template_values = { 'search_link': searchLink, 'create_link': createLink, 'response_email': emailAccount['from_email'], 'config': Config.get_all() } # Render email body. body = Emailer.render('email/idea_confirmation', template_values, suffix = 'txt') # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send authenticate user email") log.error(e) return False
def emailInvite(email, inviterName, projectId, title, description, message = None): """ Send invitation email. Using template: project_invite @type email: string @param email: Email address to send to ... @rtype: Boolean @returns: Whether emailer was successful or not. """ # Create values for template. emailAccount = Config.get('email') subject = "You've been invited by %s to join a project" % inviterName link = "%sproject/%s" % (Config.get('default_host'), str(projectId)) template_values = { 'inviter': inviterName, 'title':title, 'description':description, 'link': link, 'message': message, 'config': Config.get_all() } # Render email body. body = Emailer.render('email/project_invite', template_values, suffix = 'txt') # Send email. try: return Emailer.send(email, subject, body, from_name = emailAccount['from_name'], from_address = emailAccount['from_email']) except Exception, e: log.info("*** couldn't send invite email") log.error(e) return False
def render(self, template_name, template_values=None, suffix="html", content_type = "text/html", status="200 OK"): """ Custom renderer for Change by Us templates. @type template_name: string @param template_name: Name of template (without extension) @type template_values: dict @param template_values: Values to include in the template. @type suffix: string @param suffix: Extension of template file. @type content_type: string @param content_type: HTTP header content type to output. @rtype: ? @returns: ? """ if template_values is None: template_values = {} # Set the user object in case it's been created since we initialized. self.setUserObject() # Hand all config values to the template. This method is deprecated # but around until all templates have been updated. config = Config.get_all() config['base_url'] = Config.base_url() for key in config: if type(config[key]) is dict: for param in config[key]: template_values["%s_%s" % (key, param)] = config[key][param] else: template_values[key] = config[key] # Give all config values as a dict in a config space. template_values['config'] = config # Send user data to template if self.user: template_values['user'] = self.user template_values['sqla_user'] = self.sqla_user # Add template data object if self.template_data: template_values['template_data'] = self.template_data # Create full URL from web.py values template_values['full_url'] = web.ctx.home + web.ctx.fullpath # Check for "flash"? if hasattr(self.session, 'flash') and self.session.flash is not None: template_values['flash'] = self.session.flash log.info('showing flash message: "' + self.session.flash + '"') self.session.flash = None self.session.invalidate() template_values['session_id'] = self.session.session_id # Put session values into template ?? keys = self.session.keys() for key in keys: template_values[key] = self.session[key] # Set up template and Jinja template_values['template_name'] = template_name renderer = render_jinja(os.path.dirname(__file__) + '/../templates/', extensions=['jinja2.ext.i18n', 'jinja2.ext.with_',]) renderer._lookup.filters.update(custom_filters.filters) # Install the translation translation = self.get_gettext_translation(self.get_language()) renderer._lookup.install_gettext_translations(translation) # Insert HTML for the language chooser curr_lang = self.get_language() all_langs = self.get_supported_languages() template_values['language'] = {"current": curr_lang, "list": all_langs.iteritems()} template_values['language_selector'] = self.choice_list( all_langs, curr_lang) # Set HTTP header web.header("Content-Type", content_type) # Debug data. log.info("%s: %s (%s)" % (status, content_type, template_name)) log.info("*** session = %s" % self.session) # Set status web.ctx.status = status # Return template and data. return (renderer[template_name + "." + suffix](dict(d=template_values))).encode('utf-8')
def render(self, template_name, template_values=None, suffix="html", content_type="text/html", status="200 OK"): """ Custom renderer for Change by Us templates. @type template_name: string @param template_name: Name of template (without extension) @type template_values: dict @param template_values: Values to include in the template. @type suffix: string @param suffix: Extension of template file. @type content_type: string @param content_type: HTTP header content type to output. @rtype: ? @returns: ? """ if template_values is None: template_values = {} # Set the user object in case it's been created since we initialized. self.setUserObject() # Hand all config values to the template. This method is deprecated # but around until all templates have been updated. config = Config.get_all() config['base_url'] = Config.base_url() for key in config: if type(config[key]) is dict: for param in config[key]: template_values["%s_%s" % (key, param)] = config[key][param] else: template_values[key] = config[key] # Give all config values as a dict in a config space. template_values['config'] = config # Send user data to template if self.user: template_values['user'] = self.user template_values['sqla_user'] = self.sqla_user # Add template data object if self.template_data: template_values['template_data'] = self.template_data # Create full URL from web.py values template_values['full_url'] = web.ctx.home + web.ctx.fullpath # Check for "flash"? if hasattr(self.session, 'flash') and self.session.flash is not None: template_values['flash'] = self.session.flash log.info('showing flash message: "' + self.session.flash + '"') self.session.flash = None self.session.invalidate() template_values['session_id'] = self.session.session_id # Put session values into template ?? keys = self.session.keys() for key in keys: template_values[key] = self.session[key] # Set up template and Jinja template_values['template_name'] = template_name renderer = render_jinja(os.path.dirname(__file__) + '/../templates/', extensions=[ 'jinja2.ext.i18n', 'jinja2.ext.with_', ]) renderer._lookup.filters.update(custom_filters.filters) # Install the translation translation = self.get_gettext_translation(self.get_language()) renderer._lookup.install_gettext_translations(translation) # Insert HTML for the language chooser curr_lang = self.get_language() all_langs = self.get_supported_languages() template_values['language'] = { "current": curr_lang, "list": all_langs.iteritems() } template_values['language_selector'] = self.choice_list( all_langs, curr_lang) # Set HTTP header web.header("Content-Type", content_type) # Debug data. log.info("%s: %s (%s)" % (status, content_type, template_name)) log.info("*** session = %s" % self.session) # Set status web.ctx.status = status # Return template and data. return (renderer[template_name + "." + suffix]( dict(d=template_values))).encode('utf-8')