Esempio n. 1
0
def create():
    schema = schemas.UserSchema()
    try:
        user_data = schema.load(request.get_json())
    except Exception as e:
        print(e)
        response_dict = request.get_json()
        response_dict.update(e.messages)
        response = render_json(response_dict)
        response.status_code = 400
        abort(response)

    user = models.User.objects(
        me.Q(username=user_data.data['username'])
        | me.Q(email=user_data.data['email'])).first()
    if user is None:
        user = models.User(**user_data.data)
        user.set_password(user_data.data['password'],
                          salt=current_app.secret_key)
        user.status = 'active'
        user.save()
        return render_json(schema.dump(user).data)

    errors = [{
        'status': '400',
        'title': 'Found User in System',
        'detail': 'User name or email found in system'
    }]

    response_dict = schema.dump(user_data.data).data
    response_dict['errors'] = errors

    response = render_json(response_dict)
    response.status_code = 400
    abort(response)
Esempio n. 2
0
def auth():
    auth_dict = request.get_json()['auth']
    token = None

    try:
        provider = auth_dict['identity']['token']['provider']
        token = auth_dict['identity']['token']['token']
    except Exception as e:
        response_dict = request.get_json()
        response_dict.update(e.messages)
        response = render_json(response_dict)
        response.status_code = 400
        abort(response)

    user = None
    if provider == 'principal':
        user = accounts.get_principal_user(token)

    if user:
        app = current_app
        now = datetime.datetime.utcnow()
        expires_at = now + app.config.get('JWT_ACCESS_TOKEN_EXPIRES')

        access_token = create_access_token(user)
        refresh_token = create_refresh_token(user)

        token = dict(
            methods=['jwt_token'],
            user=dict(
                id=user.id,
                name=user.username
                ),
            access_token=access_token,
            refresh_token=refresh_token,
            issued_date=datetime.datetime.utcnow(),
            expires_at=expires_at
            )
        return render_json(token)

    errors = [
        {
          'status': '401',
          'title':  'User or Password mismatch',
          'detail': 'User or Password mismatch'
        }
    ]

    response_dict = request.get_json()
    response_dict['errors'] = errors

    response = render_json(response_dict)
    response.status_code = 401
    abort(response)
Esempio n. 3
0
def get(group_id):
    group = None

    schema = schemas.GroupSchema()
    try:
        group = models.Group.objects.get(id=group_id)
    except Exception as e:
        response_dict = request.get_json()
        response_dict.update(e.messages)
        response = render_json(response_dict)
        response.status_code = 404
        abort(response)

    return render_json(schema.dump(group).data)
Esempio n. 4
0
def get(room_id):
    room = None

    schema = schemas.RoomSchema()
    try:
        room = models.Room.objects.get(id=room_id)
    except Exception as e:
        response_dict = request.get_json()
        response_dict.update(e.messages)
        response = render_json(response_dict)
        response.status_code = 404
        abort(response)

    return render_json(schema.dump(room).data)
Esempio n. 5
0
def create():
    schema = schemas.RoomSchema()

    try:
        room_data = schema.load(request.get_json()).data
    except Exception as e:
        response_dict = request.get_json()
        response_dict.update(e.messages)
        response = render_json(response_dict)
        response.status_code = 400
        abort(response)

    user = current_user._get_current_object()
    room = models.Room(user=user, **room_data)
    room.save()
    return render_json(schema.dump(room).data)
Esempio n. 6
0
def create():
    schema = schemas.GroupSchema()

    try:
        group_data = schema.load(request.get_json()).data
    except Exception as e:
        response_dict = request.get_json()
        response_dict.update(e.messages)
        response = render_json(response_dict)
        response.status_code = 400
        abort(response)

    user = current_user._get_current_object()
    group = models.Group(user=user,
                         **group_data)
    group.save()
    return render_json(schema.dump(group).data)
Esempio n. 7
0
def update(group_id):
    schema = schemas.GroupSchema()

    try:
        group = models.Group.objects.get(id=group_id)
        group_data = schema.load(request.get_json()).data

        group_data.pop('id')
        group.update(**group_data)
    except Exception as e:
        response_dict = request.get_json()
        response_dict.update(e.messages)
        response = render_json(response_dict)
        response.status_code = 400
        abort(response)

    group.save()
    return render_json(schema.dump(group).data)
Esempio n. 8
0
def update(room_id):
    schema = schemas.RoomSchema()

    try:
        room = models.Room.objects.get(id=room_id)
        room_data = schema.load(request.get_json()).data

        room_data.pop('id')
        room.update(**room_data)
    except Exception as e:
        response_dict = request.get_json()
        response_dict.update(e.messages)
        response = render_json(response_dict)
        response.status_code = 400
        abort(response)

    room.save()
    return render_json(schema.dump(room).data)
Esempio n. 9
0
def all():
    schema_list = schemas.__all__

    spec = APISpec(
        title='Pichayon API',
        version='1.0.0',
        plugins=[
            'apispec.ext.flask',
            'apispec.ext.marshmallow',
        ],
    )

    for schema in schema_list:
        spec.definition(schema.Meta.type_, schema=schema)
    return render_json(spec.to_dict())
Esempio n. 10
0
def refresh_token():
    user = current_user

    access_token = create_access_token(user)
    refresh_token = create_refresh_token(user)

    jwt_data = decode_token(access_token)

    token = dict(
        access_token=access_token,
        refresh_token=refresh_token,
        issued_date=datetime.datetime.utcnow(),
        expiry_date=datetime.datetime.utcfromtimestamp(
            jwt_data.get('exp'))
        )

    return render_json(token)
Esempio n. 11
0
def list():
    schema = schemas.GroupSchema()
    groups = models.Group.objects()
    return render_json(schema.dump(groups, many=True).data)
Esempio n. 12
0
def list():
    schema = schemas.RoomSchema()
    rooms = models.Room.objects()

    return render_json(schema.dump(rooms, many=True).data)
Esempio n. 13
0
def get(user_id):
    schema = schemas.UserSchema()
    return render_json(schema.dump(current_user).data)
Esempio n. 14
0
def list():
    schema = schemas.UserSchema()
    users = models.User.objects()

    return render_json(schema.dump(users, many=True).data)