def _send_alert_email(self, description): if not self.email_contact: logging.warning("No email contact for service %s" % self.name) try: email_msg = loader.get_template("alert_email.txt").render( Context({ "description": description, "service_name": self.name, "url": "%s%s" % (settings.DOMAIN, reverse("main:service", kwargs={'service_id': self.id})) if settings.hasattr("DOMAIN") and settings.DOMAIN else "" })) send_mail("MOMONITOR EVENT TRIGGERED", email_msg, settings.SERVER_EMAIL, [ self.email_contact, ], fail_silently=False) except SMTPException: logging.error("Failed to send email to %s for error %s" % (self.email_contact, description))
def __call__(self, details, user=None, *args, **kwargs): """Get a new username. We check for existing usernames in a case-insensitive way. """ if user: return {'username': user.username} warn_setting('SOCIAL_AUTH_FORCE_RANDOM_USERNAME', 'get_username') warn_setting('SOCIAL_AUTH_DEFAULT_USERNAME', 'get_username') warn_setting('SOCIAL_AUTH_UUID_LENGTH', 'get_username') warn_setting('SOCIAL_AUTH_USERNAME_FIXER', 'get_username') if getattr(settings, 'SOCIAL_AUTH_FORCE_RANDOM_USERNAME', False): username = uuid4().get_hex() elif details.get(USERNAME): username = details[USERNAME] elif settings.hasattr('SOCIAL_AUTH_DEFAULT_USERNAME'): username = settings.SOCIAL_AUTH_DEFAULT_USERNAME if callable(username): username = username() else: username = uuid4().get_hex() uuid_lenght = getattr(settings, 'SOCIAL_AUTH_UUID_LENGTH', 16) username_fixer = getattr(settings, 'SOCIAL_AUTH_USERNAME_FIXER', lambda u: u) short_username = username[:USERNAME_MAX_LENGTH - uuid_lenght] final_username = None while True: final_username = username_fixer(username)[:USERNAME_MAX_LENGTH] try: User.objects.get(username__iexact=final_username) except User.DoesNotExist: break else: # User with same username already exists, generate a unique # username for current user using username as base but adding # a unique hash at the end. Original username is cut to avoid # the field max_length. username = short_username + uuid4().get_hex()[:uuid_lenght] return {'username': final_username}
def _send_alert_email(self, description): if not self.email_contact: logging.warning("No email contact for service %s" % self.name) try: email_msg = loader.get_template("alert_email.txt").render( Context( { "description": description, "service_name": self.name, "url": "%s%s" % (settings.DOMAIN, reverse("main:service", kwargs={"service_id": self.id})) if settings.hasattr("DOMAIN") and settings.DOMAIN else "", } ) ) send_mail( "MOMONITOR EVENT TRIGGERED", email_msg, settings.SERVER_EMAIL, [self.email_contact], fail_silently=False ) except SMTPException: logging.error("Failed to send email to %s for error %s" % (self.email_contact, description))