Beispiel #1
0
def slugify(value, substitutions=()):
    '''
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.

    Took from Django sources.
    '''
    # TODO Maybe steal again from current Django 1.5dev
    value = Markup(value).striptags()
    # value must be unicode per se
    import unicodedata
    from unidecode import unidecode
    # unidecode returns str in Py2 and 3, so in Py2 we have to make
    # it unicode again
    value = unidecode(value)
    if isinstance(value, six.binary_type):
        value = value.decode('ascii')
    # still unicode
    value = unicodedata.normalize('NFKD', value).lower()
    for src, dst in substitutions:
        value = value.replace(src.lower(), dst.lower())
    value = re.sub('[^\w\s-]', '', value).strip()
    value = re.sub('[-\s]+', '-', value)
    # we want only ASCII chars
    value = value.encode('ascii', 'ignore')
    # but Pelican should generally use only unicode
    return value.decode('ascii')
Beispiel #2
0
    def pre_create(self):
        id = is_mongokit_objectid(self.params.get("alert_id"))
        if id:
            alert = dboperation.read("AlertRules", {"_id": id}, True)
            if alert:
                notifications = alert.get("notification", [])
                if notifications:
                    for notification in notifications:
                        if notification.get("type") == "newtestemail":
                            notifications.remove(notification)
                            break
                if self.params.get("notify_newtestemail") == "on":
                    email_template = textual.utf8(
                        self.params.get('email_template'))
                    try:
                        template = Template(email_template)
                    except TemplateSyntaxError:
                        return ((0, 800), {})

                    email_emails = self.params.get('email_emails')

                    if email_emails:
                        email_emails = json.loads(email_emails)
                        email_pattern = re.compile(
                            r"^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)*$"
                        )
                        invalid_emails = []
                        for email in email_emails:
                            if not bool(email_pattern.match(email)):
                                invalid_emails.append(email)

                        if invalid_emails:
                            return ((0, 801), {
                                "errors": {
                                    "invalid_emails": invalid_emails
                                }
                            })
                    else:
                        return ((0, 801), {})

                    email_threshold_option = self.params.get(
                        "email_threshold_option")
                    email_threshold_value = self.params.get(
                        "email_threshold_value")
                    if email_threshold_value:
                        email_threshold_value = int(email_threshold_value)
                    template_file = ""
                    if email_template:
                        disk.prepare_path(ALERT_TEMPLATES_PATH)
                        user_id = dboperation.read(
                            "User", {'username': self.user.get_user_name()},
                            True)
                        template_file = 'alert_%s_%s.tmp' % (str(
                            user_id['_id']), base64.b32encode(alert["name"]))
                        template_file_path = os.path.join(
                            ALERT_TEMPLATES_PATH, template_file)
                        email_template = Markup(
                            email_template.decode('utf-8')).unescape()
                        with open(template_file_path, 'w') as f:
                            email_template = email_template.encode('utf-8')
                            format_template = re.sub(
                                '\|\s*(readable|date|time|datetime)\s*}}',
                                self._regex_replacer, email_template)
                            f.write(format_template)
                    else:
                        email_template = "<br>"
                        disk.prepare_path(ALERT_TEMPLATES_PATH)
                        template_file = 'alert_%s_%s.tmp' % (
                            self.user.get_user_name().encode(
                                'ascii', 'ignore'), base64.b32encode(name))
                        template_file_path = os.path.join(
                            ALERT_TEMPLATES_PATH, template_file)
                        email_template = Markup(
                            email_template.decode('utf-8')).unescape()
                        with open(template_file_path, 'w') as f:
                            email_template = email_template.encode('utf-8')
                            format_template = re.sub(
                                '\|\s*(readable|date|time|datetime)\s*}}',
                                self._regex_replacer, email_template)
                            f.write(format_template)

                    notifications.append({'template_file':template_file,'type':'newtestemail', 'notify_newtestemail':True, 'email_emails':email_emails,\
                                          'email_template':email_template, 'threshold':email_threshold_value, 'threshold_option':email_threshold_option})

                return {"notification": notifications}