def __json__(self, request: Request) -> t.Dict[str, str]:
     """JSON renderer support."""
     return {
         "username": self.username,
         "email": self.email,
         "bio": self.bio,
         "token": request.create_jwt_token(str(self.id)),
         "image": self.image,
     }
Пример #2
0
def _create_user(request: Request) -> Response:
    try:
        user_data = request.json_body
        print(user_data)

        stmt: TextClause = text(
            'select * from bancoco."Cuentahabiente" where "Tarjeta" = :tarjeta and "Fecha_Expiracion" = :fecha'
        )

        stmt = stmt.bindparams(tarjeta=user_data['tarjeta'],
                               fecha=user_data['fechaExpiracion'])
        result = db.execute(stmt)
        data = [dict(r) for r in result]
        if data.__len__() == 0:
            return Response(status=400)

        stmt: TextClause = text(
            'INSERT into cocollector."Usuario"("Nombre_usuario",'
            '"Correo",'
            '"Contrasena",'
            '"Nombre",'
            '"Apellido_paterno",'
            '"Apellido_materno",'
            '"Tarjeta_credito",'
            '"Fecha_Expiracion",'
            '"Tipo") VALUES (:nombre_usuario, :correo, :contrasena, '
            ":nombre, :apellido_paterno, :apellido_materno, :tarjeta, :fecha_expiracion, 'Usuario' )"
        )
        nombre_usuario = user_data['nombreUsuario']
        stmt = stmt.bindparams(nombre_usuario=nombre_usuario,
                               correo=user_data['correo'],
                               contrasena=user_data['contrasena'],
                               nombre=user_data['nombre'],
                               apellido_paterno=user_data['apellidoPaterno'],
                               apellido_materno=user_data['apellidoMaterno'],
                               tarjeta=user_data['tarjeta'],
                               fecha_expiracion=user_data['fechaExpiracion'])

        db.execute(stmt)
        stmt = text(
            'SELECT * FROM cocollector."Usuario" where "Nombre_usuario" = :username'
        )
        stmt = stmt.bindparams(username=nombre_usuario)
        result = db.execute(stmt)
        user_data = [dict(r) for r in result][0]
        token = request.create_jwt_token(user_data['ID'])
        return Response(status=200,
                        charset='utf-8',
                        content_type='application/json',
                        body=json.dumps({
                            'token': token,
                            'userType': user_data['Tipo']
                        }))
    except Exception as e:
        print(e)
        return Response(status=400)
Пример #3
0
def login(request: Request) -> Response:
    """User logs in using GitHub."""
    response = Response()
    result = request.authomatic.login(WebObAdapter(request, response), "github")
    if result is None:
        return response
    if result.error:
        return exception_response(401, json_body={"error": result.error.message})

    result.user.update()
    username = result.user.username
    user = User.by_username(username, db=request.db)
    if user is None:
        return exception_response(401, json_body={"error": "Not authorized."})

    return Response(json_body={"token": request.create_jwt_token(str(user.id))})