Exemplo n.º 1
0
def get(building_id):
    schema = schemas.BuildingSchema()
    owner = current_user._get_current_object()
    building = models.Building.objects(id=building_id,
                                       owner=owner).first()
    if not building:
        return abort(get_building_error_not_found())

    return render_json(schema.dump(building).data)
Exemplo n.º 2
0
def create():
    schema = schemas.BuildingSchema()
    try:
        building_data = schema.load(request.get_json())
    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)

    owner = current_user._get_current_object()
    building = models.Building(owner=owner,
                               status='active',
                               **building_data.data)
    building.save()

    return render_json(schema.dump(building).data)
Exemplo n.º 3
0
def delete(building_id):
    owner = current_user._get_current_object()
    building = models.Building.objects(id=building_id,
                                       owner=owner).first()
    if not building:
        return abort(get_building_error_not_found())

    building.delete()

    return render_json()
Exemplo n.º 4
0
def auth():
    auth_dict = request.get_json()['auth']
    try:
        name = auth_dict['identity']['password']['user']['name']
        password = auth_dict['identity']['password']['user']['password']
    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 = models.User.objects(me.Q(username=name) | me.Q(email=name)).first()
    if user:
        if user.verify_password(password):
            access_token = create_access_token(user)
            refresh_token = create_refresh_token(user)

            jwt_data = decode_token(access_token)

            token = dict(methods=['password'],
                         user=dict(id=user.id, name=name),
                         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)

    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)
Exemplo n.º 5
0
def get_application_error_not_found():
    response_dict = request.get_json()
    errors = [{
        'status': '404',
        'title': 'Application not found',
        'detail': 'Application not found'
    }]
    response_dict.update(errors=errors)
    response = render_json(response_dict)
    response.status_code = 404

    return response
Exemplo n.º 6
0
def deactivate_application(building_id, application_id):
    owner = current_user._get_current_object()
    building = models.Building.objects(id=building_id,
                                       owner=owner).first()
    if not building:
        return abort(get_building_error_not_found())

    building.activated_applications = [
        aa for aa in building.activated_applications
        if str(aa.id) != application_id]
    building.save()

    return render_json()
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
0
def get(application_id):
    schema = schemas.ApplicationSchema()
    app = None

    try:
        app = models.Application.objects.with_id(application_id)
    except Exception as e:
        pass

    if not app:
        app = models.Application.objects(name__iexact=application_id).first()

    if not app:
        return get_application_error_not_found()

    return render_json(schema.dump(app).data)
Exemplo n.º 10
0
def activate_application(building_id):
    owner = current_user._get_current_object()
    building = models.Building.objects(id=building_id,
                                       owner=owner).first()
    if not building:
        return abort(get_building_error_not_found())

    try:
        schema = schemas.ApplicationSchema()
        app_dict = schema.load(request.get_json(), partial=True).data
        application = models.Application.objects.with_id(app_dict['id'])
    except Exception as e:
        from . import applications
        return abort(applications.get_application_error_not_found())

    activated_application = models.ActivatedApplication(
            id=application.id,
            owner=owner,
            application=application)
    building.activated_applications.append(activated_application)
    building.save()

    schema = schemas.BuildingSchema()
    return render_json(schema.dump(building).data)
Exemplo n.º 11
0
def list_applications():
    schema = schemas.ApplicationSchema(many=True)

    apps = models.Application.objects(status='active')
    return render_json(schema.dump(apps).data)
Exemplo n.º 12
0
def get(user_id):
    schema = schemas.UserSchema()
    return render_json(schema.dump(current_user).data)
Exemplo n.º 13
0
def list():
    schema = schemas.BuildingSchema(many=True)
    owner = current_user._get_current_object()
    buildings = models.Building.objects(owner=owner)
    return render_json(schema.dump(buildings).data)