示例#1
0
def read_one(id_: str, dao: GenericSQLDAO = None):
    result = dao.get(id_=id_)

    if not result:
        return success_response(status_code=404,
                                message="EntityForTest not found in database",
                                data={"id_": id_})

    return success_response(message="EntityForTest retrieved",
                            data={"EntityForTest": dict(result)})
示例#2
0
def read_one(id_: str, phone_id: str, dao: GenericSQLDAO = None):
    result = dao.get(id_=phone_id)

    if result.contact.id_ != id_:
        result = None

    if not result:
        return success_response(status_code=404,
                                message="Phone not found in database",
                                data={"id_": id_})

    return success_response(message="Phone retrieved",
                            data={"Phone": dict(result)})
示例#3
0
def create(entity: dict, dao: GenericSQLDAO = None):
    entity_to_create = EntityForTest(**entity)

    dao.create(entity=entity_to_create)

    return success_response(message="EntityForTest created",
                            data={"EntityForTest": dict(entity_to_create)})
示例#4
0
def read(length: int = 20,
         offset: int = 0,
         dao: GenericSQLDAO = None,
         **kwargs):
    filters = dict()

    entity_attributes = [field.name for field in fields(EntityForTest)]

    for key, value in kwargs.items():
        if key not in entity_attributes:
            continue

        filters[key] = value.split(',') \
                       if len(str(value).split(',')) > 1 \
                       else value

    total, results = dao.get_all(length=length,
                                 offset=offset,
                                 filters=filters if filters else None)
    return success_response(message="List of entityfortest",
                            data={
                                "total": total,
                                "results":
                                [dict(result) for result in results]
                            })
示例#5
0
def update(id_: str, phone_id: str, entity: dict, dao: GenericSQLDAO = None):
    entity_to_update = dao.get(phone_id)

    if entity_to_update.contact.id_ != id_:
        entity_to_update = None

    if not entity_to_update:
        return error_response(status_code=404,
                              message="Phone not found",
                              data={"id_": id_})

    entity_fields = dao.fields.keys()

    for key, value in entity.items():
        if key not in entity_fields:
            raise KeyError("{key} not in {entity}"
                           .format(key=key,
                                   entity=dao.return_class))

        entity_to_update.__dict__[key] = value

    dao.update(entity_to_update)

    return success_response(message="Phone updated",
                            data={"Phone": dict(entity_to_update)})
示例#6
0
def create(id_: str, entity: dict, dao: GenericSQLDAO = None):
    entity_to_create = Phone(**entity)
    entity_to_create.contact = Contact(id_=id_)

    dao.create(entity=entity_to_create)

    return success_response(message="Phone created",
                            data={"Phone": dict(entity_to_create)})
示例#7
0
def delete(id_: str, dao: GenericSQLDAO):
    entity = dao.get(id_=id_)

    if not entity:
        return error_response(status_code=404,
                              message="EntityForTest not found",
                              data={"id_": id_})

    dao.remove(entity)

    return success_response(message="EntityForTest deleted",
                            data={"EntityForTest": dict(entity)})
示例#8
0
 def test_success_response(self, mocker, data):
     default_response_mock = mocker.patch("nova_api.default_response",
                                          return_value="OK")
     default_response_mock.return_value = 1
     ret_val = nova_api.success_response(data=data)
     assert (default_response_mock.mock_calls == [
         call(success=True,
              status_code=200,
              message="OK",
              data={} if data is None else data)
     ])
     assert ret_val == 1
示例#9
0
def read(length: int = 20, offset: int = 0,
         dao: GenericSQLDAO = None, **kwargs):
    for key, value in kwargs.items():
        kwargs[key] = value.split(',') \
            if len(value.split(',')) > 1 \
            else value
    total, results = dao.get_all(length=length, offset=offset,
                                 filters=kwargs if len(kwargs) > 0 else None)
    return success_response(message="List of contact",
                            data={"total": total, "results": [dict(result)
                                                              for result
                                                              in results]})
示例#10
0
def delete(id_: str, phone_id: str, dao: GenericSQLDAO):
    entity = dao.get(id_=phone_id)

    if entity.contact.id_ != id_:
        entity = None

    if not entity:
        return error_response(status_code=404,
                              message="Phone not found",
                              data={"id_": id_})

    dao.remove(entity)

    return success_response(message="Phone deleted",
                            data={"Phone": dict(entity)})
示例#11
0
def probe(dao: GenericSQLDAO = None):
    total, _ = dao.get_all(length=1, offset=0, filters=None)
    return success_response(message="API Ready", data={"available": total})
示例#12
0
def probe(id_: str = None, dao: GenericSQLDAO = None):
    total, results = dao.get_all(length=1, offset=0, filters={"contact": id_})
    return success_response(message="API Ready",
                            data={"available": total})