Esempio n. 1
0
def admin(f):
    """
    @admin
    A decorator that turns a class into ADMIN
    """
    import auth.decorators as a_deco

    if not inspect.isclass(f):
        raise TypeError("@ADMIN expects a Mocha class")

    if config("ADMIN_ENABLED", True):

        # Index route
        index_route = config("ADMIN_INDEX_ROUTE", "/")

        # ROLES
        min_role = config("ADMIN_MIN_ACL", "ADMIN")
        role_name = "accepts_%s_roles" % min_role.lower()

        if not hasattr(a_deco, role_name):
            raise ValueError("Invalid ADMIN_MIN_ACL: %s" % min_role)

        getattr(a_deco, role_name)(f)
        a_deco.login_required(f)

        set_view_attr(f, "nav_tags", [ADMIN_TAG])
        layout = config("ADMIN_LAYOUT") or ADMIN_LAYOUT
        return render.template(layout=layout)(f)

    else:
        set_view_attr(f, "nav_visible", False)
        f.before_request = disable_admin
        return f
Esempio n. 2
0
def get_jwt_secret():
    """
    Get the JWT secret
    :return: str
    """
    secret_key = __options__.get("jwt_secret") or config(
        "JWT_SECRET") or config("SECRET_KEY")
    if not secret_key:
        raise exceptions.AuthError("Missing config JWT/SECRET_KEY")
    return secret_key
Esempio n. 3
0
def _url_for_email(endpoint, base_url=None, **kw):
    """
    Create an external url_for by using a custom base_url different from the domain we
    are on
    :param endpoint:
    :param base_url:
    :param kw:
    :return:
    """
    base_url = base_url or config("MAIL_EXTERNAL_BASE_URL")
    _external = True if not base_url else False
    url = url_for(endpoint, _external=_external, **kw)
    if base_url and not _external:
        url = "%s/%s" % (base_url.strip("/"), url.lstrip("/"))
    return url
Esempio n. 4
0
def init_oauth(app):

    oauth.init_app(app)

    oauth_creds = config("OAUTH_CREDENTIALS", {})

    for name, kwargs in oauth_creds.items():
        if "consumer_key" in kwargs and kwargs.get("consumer_key"):
            # swap kwargs, t
            if name in _CONFIG:
                _kwargs = _CONFIG[name]
                _kwargs.update(kwargs)
                kwargs = _kwargs
            params = kwargs.pop("__params__", {})
            provider = oauth.remote_app(name, **kwargs)
            setattr(provider, "__params__", params)
            setattr(this, name, provider)
Esempio n. 5
0
    def page(self):

        recipients = app_data.get(APP_DATA_KEY, "recipients") \
                     or __options__.get("recipients") \
                     or config("CONTACT_EMAIL")

        if not recipients:
            abort(500, "ContactPage missing email recipient")

        success_message = app_data.get(APP_DATA_KEY,
                                       "success_message",
                                       __options__.get("success_message"))

        return_to = __options__.get("return_to", None)
        if return_to:
            if "/" not in return_to:
                return_to = url_for(return_to)
        else:
            return_to = url_for(self)

        if request.method == "POST":
            email = request.form.get("email")
            subject = request.form.get("subject")
            message = request.form.get("message")
            name = request.form.get("name")

            try:
                if recaptcha.verify():
                    if not email or not subject or not message:
                        raise exceptions.AppError("All fields are required")
                    elif not utils.is_email_valid(email):
                        raise exceptions.AppError("Invalid email address")
                    else:
                        try:
                            send_mail(to=recipients,
                                      reply_to=email,
                                      mail_from=email,
                                      mail_subject=subject,
                                      mail_message=message,
                                      mail_name=name,
                                      template=__options__.get("template",
                                                               "contact-us.txt")
                                      )
                            flash_data("ContactPage:EmailSent")
                        except Exception as ex:
                            logging.exception(ex)
                            raise exceptions.AppError("Unable to send email")
                else:
                    raise exceptions.AppError("Security code is invalid")
            except exceptions.AppError as e:
                flash_error(e.message)
            return redirect(self)

        title = __options__.get("title", _("Contact Us"))
        page_attr(title)

        fd = get_flash_data()
        return {
            "title": title,
            "email_sent": True if fd and "ContactPage:EmailSent" in fd else False,
            "success_message": success_message,
            "return_to": return_to
        }