def get_student_profile_sub_unsub_link(handler, student, unused_course): email = student.email is_unsubscribed = unsubscribe.has_unsubscribed(email) # I18N: Control allowing user to subscribe/unsubscribe from email invitation sub_unsub_title = handler.gettext('Subscribe/Unsubscribe') sub_unsub_message = safe_dom.NodeList() if is_unsubscribed: resubscribe_url = unsubscribe.get_resubscribe_url(handler, email) sub_unsub_message.append( safe_dom.Text( # I18N: Message - user has unsubscribed from email invitations. handler.gettext( 'You are currently unsubscribed from course-related emails.' ))) sub_unsub_message.append( safe_dom.A(resubscribe_url).add_text( # I18N: Control allowing user to re-subscribe to email invitations. handler.gettext('Click here to re-subscribe.'))) else: unsubscribe_url = unsubscribe.get_unsubscribe_url(handler, email) sub_unsub_message.append( safe_dom.Text( # I18N: Text indicating user has opted in to email invitations. handler.gettext( 'You are currently receiving course-related emails. '))) sub_unsub_message.append( safe_dom.A(unsubscribe_url).add_text( # I18N: Control allowing user to unsubscribe from email invitations. handler.gettext('Click here to unsubscribe.'))) return (sub_unsub_title, sub_unsub_message)
def get_student_profile_sub_unsub_link(handler, student, unused_course): email = student.email is_unsubscribed = unsubscribe.has_unsubscribed(email) # I18N: Control allowing user to subscribe/unsubscribe from email invitation sub_unsub_title = handler.gettext('Subscribe/Unsubscribe') sub_unsub_message = safe_dom.NodeList() if is_unsubscribed: resubscribe_url = unsubscribe.get_resubscribe_url(handler, email) sub_unsub_message.append(safe_dom.Text( # I18N: Message - user has unsubscribed from email invitations. handler.gettext( 'You are currently unsubscribed from course-related emails.'))) sub_unsub_message.append(safe_dom.A(resubscribe_url).add_text( # I18N: Control allowing user to re-subscribe to email invitations. handler.gettext('Click here to re-subscribe.'))) else: unsubscribe_url = unsubscribe.get_unsubscribe_url(handler, email) sub_unsub_message.append(safe_dom.Text( # I18N: Text indicating user has opted in to email invitations. handler.gettext( 'You are currently receiving course-related emails. '))) sub_unsub_message.append(safe_dom.A(unsubscribe_url).add_text( # I18N: Control allowing user to unsubscribe from email invitations. handler.gettext('Click here to unsubscribe.'))) return ( sub_unsub_title, sub_unsub_message)
def post(self): """Handle POST requests.""" request = transforms.loads(self.request.get('request')) if not self.assert_xsrf_token_or_fail(request, self.XSRF_SCOPE, {}): return user = self.get_user() if not user: transforms.send_json_response(self, 401, 'Access denied.', {}) return student = models.Student.get_enrolled_student_by_user(user) if not student: transforms.send_json_response(self, 401, 'Access denied.', {}) return if not InvitationEmail.is_available(self): transforms.send_json_response(self, 500, 'Unavailable.', {}) return payload_json = request.get('payload') payload_dict = transforms.json_to_dict(payload_json, self.SCHEMA) email_set = { email.strip() for email in payload_dict.get('emailList').split(',') if email.strip() } if not email_set: transforms.send_json_response( # I18N: Error indicating no email addresses were submitted. self, 400, self.gettext('Error: Empty email list')) return invitation_data = InvitationStudentProperty.load_or_default(student) # Limit the number of emails a user can send, to prevent spamming if invitation_data.invited_list_size() + len(email_set) > MAX_EMAILS: missing_count = MAX_EMAILS - invitation_data.invited_list_size() # I18N: Error indicating that the user cannot add the desired # list of additional email addresses to the list of invitations; # the total size of the list with the additions would be more # than any single user is allowed to send. No email addresses # were added to the list to send, and no further email messages # were sent. transforms.send_json_response( self, 200, self.gettext( 'This exceeds your email cap. Number of remaining ' 'invitations: %s. No messages sent.' % missing_count)) return email_messages = [] for email in email_set: if not is_email_valid(email): # I18N: Error indicating an email addresses is not well-formed. email_messages.append( self.gettext('Error: Invalid email "%s"' % email)) elif invitation_data.is_in_invited_list(email): # I18N: Error indicating an email addresses is already known. email_messages.append( self.gettext( 'Error: You have already sent an invitation email to "%s"' % email)) elif unsubscribe.has_unsubscribed(email): # No message to the user, for privacy reasons logging.info('Declined to send email to unsubscribed user') elif models.Student.is_email_in_use(email): # No message to the user, for privacy reasons logging.info('Declined to send email to registered user') else: InvitationEmail(self, email, student.name).send() invitation_data.append_to_invited_list(email_set) invitation_data.put() if email_messages: # I18N: Error indicating not all email messages were sent. email_messages.insert( 0, self.gettext('Not all messages were sent (%s / %s):') % (len(email_set) - len(email_messages), len(email_set))) transforms.send_json_response(self, 400, '\n'.join(email_messages)) else: transforms.send_json_response( self, 200, # I18N: Success message indicating number of emails sent. self.gettext('OK, %s messages sent' % len(email_set)))
def post(self): """Handle POST requests.""" request = transforms.loads(self.request.get('request')) if not self.assert_xsrf_token_or_fail(request, self.XSRF_SCOPE, {}): return user = self.get_user() if not user: transforms.send_json_response(self, 401, 'Access denied.', {}) return student = models.Student.get_enrolled_student_by_user(user) if not student: transforms.send_json_response(self, 401, 'Access denied.', {}) return if not InvitationEmail.is_available(self): transforms.send_json_response(self, 500, 'Unavailable.', {}) return payload_json = request.get('payload') payload_dict = transforms.json_to_dict(payload_json, self.SCHEMA) email_set = { email.strip() for email in payload_dict.get('emailList').split(',') if email.strip()} if not email_set: transforms.send_json_response( # I18N: Error indicating no email addresses were submitted. self, 400, self.gettext('Error: Empty email list')) return invitation_data = InvitationStudentProperty.load_or_default(student) # Limit the number of emails a user can send, to prevent spamming if invitation_data.invited_list_size() + len(email_set) > MAX_EMAILS: missing_count = MAX_EMAILS - invitation_data.invited_list_size() # I18N: Error indicating that the user cannot add the desired # list of additional email addresses to the list of invitations; # the total size of the list with the additions would be more # than any single user is allowed to send. No email addresses # were added to the list to send, and no further email messages # were sent. transforms.send_json_response(self, 200, self.gettext( 'This exceeds your email cap. Number of remaining ' 'invitations: %s. No messages sent.' % missing_count)) return email_messages = [] for email in email_set: if not is_email_valid(email): # I18N: Error indicating an email addresses is not well-formed. email_messages.append(self.gettext( 'Error: Invalid email "%s"' % email)) elif invitation_data.is_in_invited_list(email): # I18N: Error indicating an email addresses is already known. email_messages.append(self.gettext( 'Error: You have already sent an invitation email to "%s"' % email)) elif unsubscribe.has_unsubscribed(email): # No message to the user, for privacy reasons logging.info('Declined to send email to unsubscribed user') elif models.Student.is_email_in_use(email): # No message to the user, for privacy reasons logging.info('Declined to send email to registered user') else: InvitationEmail(self, email, student.name).send() invitation_data.append_to_invited_list(email_set) invitation_data.put() if email_messages: # I18N: Error indicating not all email messages were sent. email_messages.insert(0, self.gettext( 'Not all messages were sent (%s / %s):') % ( len(email_set) - len(email_messages), len(email_set))) transforms.send_json_response(self, 400, '\n'.join(email_messages)) else: transforms.send_json_response( self, 200, # I18N: Success message indicating number of emails sent. self.gettext('OK, %s messages sent' % len(email_set)))
def assertSubscribed(self, email, namespace): with utils.Namespace(namespace): self.assertFalse(unsubscribe.has_unsubscribed(email))