Python pyramid.request.Request() Examples

The following are 30 code examples of pyramid.request.Request(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyramid.request , or try the search function .
Example #1
Source File: cookie_auth.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 6 votes vote down vote up
def get_user_id_via_auth_cookie(request: Request) -> Optional[int]:
    if auth_cookie_name not in request.cookies:
        return None

    val = request.cookies[auth_cookie_name]
    parts = val.split(':')
    if len(parts) != 2:
        return None

    user_id = parts[0]
    hash_val = parts[1]
    hash_val_check = __hash_text(user_id)
    if hash_val != hash_val_check:
        print("Warning: Hash mismatch, invalid cookie value")
        return None

    return try_int(user_id) 
Example #2
Source File: package_details_viewmodel.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 6 votes vote down vote up
def __init__(self, request: Request):
        super().__init__(request)

        self.package_name = request.matchdict.get('package_name')
        self.package = package_service.find_package_by_name(self.package_name)

        self.latest_version = '0.0.0'
        self.latest_release = None

        if self.package.releases:
            self.latest_release = self.package.releases[0]
            self.latest_version = '{}.{}.{}'.format(
                self.latest_release.major_ver,
                self.latest_release.minor_ver,
                self.latest_release.build_ver
            )

        self.release_version = self.latest_version
        self.maintainers = []
        self.is_latest = True 
Example #3
Source File: __init__.py    From pyramid_openapi3 with MIT License 6 votes vote down vote up
def openapi_validation_error(
    context: t.Union[RequestValidationError, ResponseValidationError], request: Request
) -> Response:
    """Render any validation errors as JSON."""
    if isinstance(context, RequestValidationError):
        logger.warning(context)
    if isinstance(context, ResponseValidationError):
        logger.error(context)

    extract_errors = request.registry.settings["pyramid_openapi3_extract_errors"]
    errors = list(extract_errors(request, context.errors))

    # If validation failed for request, it is user's fault (-> 400), but if
    # validation failed for response, it is our fault (-> 500)
    if isinstance(context, RequestValidationError):
        status_code = 400
        for error in context.errors:
            if isinstance(error, InvalidSecurity):
                status_code = 401

    if isinstance(context, ResponseValidationError):
        status_code = 500

    return exception_response(status_code, json_body=errors) 
Example #4
Source File: package_details_viewmodel.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 6 votes vote down vote up
def __init__(self, request: Request):
        super().__init__(request)

        self.package_name = request.matchdict.get('package_name')
        self.package = package_service.find_package_by_name(self.package_name)

        self.latest_version = '0.0.0'
        self.latest_release = None

        if self.package and self.package.releases:
            self.latest_release = self.package.releases[0]
            self.latest_version = '{}.{}.{}'.format(
                self.latest_release.major_ver,
                self.latest_release.minor_ver,
                self.latest_release.build_ver
            )

        self.release_version = self.latest_version
        self.maintainers = []
        self.is_latest = True 
Example #5
Source File: cookie_auth.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 6 votes vote down vote up
def get_user_id_via_auth_cookie(request: Request) -> Optional[int]:
    if auth_cookie_name not in request.cookies:
        return None

    val = request.cookies[auth_cookie_name]
    parts = val.split(':')
    if len(parts) != 2:
        return None

    user_id = parts[0]
    hash_val = parts[1]
    hash_val_check = __hash_text(user_id)
    if hash_val != hash_val_check:
        print("Warning: Hash mismatch, invalid cookie value")
        return None

    return try_int(user_id) 
Example #6
Source File: cookie_auth.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 6 votes vote down vote up
def get_user_id_via_auth_cookie(request: Request) -> Optional[int]:
    if auth_cookie_name not in request.cookies:
        return None

    val = request.cookies[auth_cookie_name]
    parts = val.split(':')
    if len(parts) != 2:
        return None

    user_id = parts[0]
    hash_val = parts[1]
    hash_val_check = __hash_text(user_id)
    if hash_val != hash_val_check:
        print("Warning: Hash mismatch, invalid cookie value")
        return None

    return try_int(user_id) 
Example #7
Source File: account_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 6 votes vote down vote up
def register_post(request: Request):
    email = request.POST.get('email')
    name = request.POST.get('name')
    password = request.POST.get('password')

    if not email or not name or not password:
        return {
            'email': email,
            'name': name,
            'password': password,
            'error': 'Some required fields are missing.',
            'user_id': cookie_auth.get_user_id_via_auth_cookie(request)
        }

    # create user
    user = user_service.create_user(email, name, password)
    cookie_auth.set_auth(request, user.id)

    return x.HTTPFound('/account')


# ################### LOGIN ################################# 
Example #8
Source File: account_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 6 votes vote down vote up
def login_post(request: Request):
    data = request_dict.create(request)
    email = data.email
    password = data.password

    user = user_service.login_user(email, password)

    if not user:
        return {
            'email': email,
            'password': password,
            'error': 'The user could not found or the password is incorrect.',
            'user_id': cookie_auth.get_user_id_via_auth_cookie(request)
        }

    cookie_auth.set_auth(request, user.id)
    return x.HTTPFound('/account')


# ################### LOGOUT ################################# 
Example #9
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def details(request: Request):
    package_name = request.matchdict.get('package_name')
    if not package_name:
        raise x.HTTPNotFound()

    return {
        'package_name': package_name
    }


# /project/{package_name}/releases 
Example #10
Source File: cms_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def cms_page(request: Request):
    subpath = request.matchdict.get('subpath')
    suburl = '/'.join(subpath)

    page = fake_db.get(suburl)
    if not page:
        raise HTTPNotFound()

    page['user_id'] = str(cookie_auth.get_user_id_via_auth_cookie(request))
    return page 
Example #11
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def releases(request: Request):
    package_name = request.matchdict.get('package_name')
    if not package_name:
        raise x.HTTPNotFound()

    return {
        'package_name': package_name,
        'releases': []
    }


# /project/{package_name}/releases/{release_version} 
Example #12
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def release_version(request: Request):
    package_name = request.matchdict.get('package_name')
    release_ver = request.matchdict.get('release_version')
    if not package_name:
        raise x.HTTPNotFound()

    return {
        'package_name': package_name,
        'release_version': release_ver,
        'releases': []
    }


# /{num} 
Example #13
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def release_version(request: Request):
    package_name = request.matchdict.get('package_name')
    release_ver = request.matchdict.get('release_version')
    if not package_name:
        raise x.HTTPNotFound()

    return {
        'package_name': package_name,
        'release_version': release_ver,
        'releases': []
    }


# /{num} 
Example #14
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def release_version(request: Request):
    package_name = request.matchdict.get('package_name')
    release_ver = request.matchdict.get('release_version')
    if not package_name:
        raise x.HTTPNotFound()

    return {
        'package_name': package_name,
        'release_version': release_ver,
        'releases': []
    }


# /{num} 
Example #15
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def popular(request: Request):
    num = int(request.matchdict.get('num', -1))
    if not (1 <= num or num <= 10):
        raise x.HTTPNotFound()

    return {
        'package_name': "The {}th popular package".format(num)
    } 
Example #16
Source File: cms_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def cms_page(request: Request):
    subpath = request.matchdict.get('subpath')
    suburl = '/'.join(subpath)

    page = fake_db.get(suburl)
    if not page:
        raise HTTPNotFound()

    return page 
Example #17
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def details(request: Request):
    package_name = request.matchdict.get('package_name')
    if not package_name:
        raise x.HTTPNotFound()

    return {
        'package_name': package_name
    }


# /project/{package_name}/releases 
Example #18
Source File: cms_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def cms_page(request: Request):
    subpath = request.matchdict.get('subpath')
    suburl = '/'.join(subpath)

    page = fake_db.get(suburl)
    if not page:
        raise HTTPNotFound()

    return page 
Example #19
Source File: cookie_auth.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def set_auth(request: Request, user_id: int):
    hash_val = __hash_text(str(user_id))
    val = "{}:{}".format(user_id, hash_val)

    request.add_response_callback(lambda req, resp: __add_cookie_callback(
        req, resp, auth_cookie_name, val
    )) 
Example #20
Source File: cms_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def cms_page(request: Request):
    subpath = request.matchdict.get('subpath')
    suburl = '/'.join(subpath)

    page = fake_db.get(suburl)
    if not page:
        raise HTTPNotFound()

    return page 
Example #21
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def popular(request: Request):
    num = int(request.matchdict.get('num', -1))
    if not (1 <= num or num <= 10):
        raise x.HTTPNotFound()

    return {
        'package_name': "The {}th popular package".format(num)
    } 
Example #22
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def details(request: Request):
    package_name = request.matchdict.get('package_name')
    if not package_name:
        raise x.HTTPNotFound()

    return {
        'package_name': package_name
    }


# /project/{package_name}/releases 
Example #23
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def popular(request: Request):
    num = int(request.matchdict.get('num', -1))
    if not (1 <= num or num <= 10):
        raise x.HTTPNotFound()

    return {
        'package_name': "The {}th popular package".format(num),
        'user_id': cookie_auth.get_user_id_via_auth_cookie(request)
    } 
Example #24
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def details(request: Request):
    package_name = request.matchdict.get('package_name')

    package = package_service.find_package_by_name(package_name)
    if not package:
        raise x.HTTPNotFound()

    latest_version = '0.0.0'
    latest_release = None
    if package.releases:
        latest_release = package.releases[0]
        latest_version = '{}.{}.{}'.format(
            latest_release.major_ver,
            latest_release.minor_ver,
            latest_release.build_ver
        )

    return {
        'package': package,
        'latest_version': latest_version,
        'latest_release': latest_release,
        'release_version': latest_version,
        'maintainers': [],
        'is_latest': True,
        'user_id': cookie_auth.get_user_id_via_auth_cookie(request)
    }


# /{num} 
Example #25
Source File: cookie_auth.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def set_auth(request: Request, user_id: int):
    hash_val = __hash_text(str(user_id))
    val = "{}:{}".format(user_id, hash_val)

    request.add_response_callback(lambda req, resp: __add_cookie_callback(
        req, resp, auth_cookie_name, val
    )) 
Example #26
Source File: request_dict.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def create(request: Request) -> RequestDictionary:
    data = {
        **request.GET,
        **request.headers,
        **request.POST,
        **request.matchdict,
    }

    return RequestDictionary(data) 
Example #27
Source File: cms_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def cms_page(request: Request):
    subpath = request.matchdict.get('subpath')
    suburl = '/'.join(subpath)

    page = fake_db.get(suburl)
    if not page:
        raise HTTPNotFound()

    return page 
Example #28
Source File: packages_controller.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def popular(request: Request):
    num = int(request.matchdict.get('num', -1))
    if not (1 <= num or num <= 10):
        raise x.HTTPNotFound()

    return {
        'package_name': "The {}th popular package".format(num)
    } 
Example #29
Source File: page_viewmodel.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def __init__(self, request: Request):
        super().__init__(request)

        self.subpath = request.matchdict.get('subpath')
        self.suburl = None
        if self.subpath:
            self.suburl = '/'.join(self.subpath)

        self.page = fake_db.get(self.suburl) 
Example #30
Source File: account_home_viewmodel.py    From data-driven-web-apps-with-pyramid-and-sqlalchemy with MIT License 5 votes vote down vote up
def __init__(self, request: Request):
        super().__init__(request)

        self.user = user_service.find_user_by_id(self.user_id)