async def find_many_by_license(slug: str, limit: int, skip: int): collection = get_collection(DOCTYPE_CONTRACT) contracts: List[Contract] = [] cursor = collection.find({"license": slug}, limit=limit, skip=skip) async for row in cursor: contracts.append(row) return contracts
async def update_one(project: str, id: str, data: ProjectModuleUpdate): logging.info(">>> " + __name__ + ":update_one") props = {"updatedAt": datetime.utcnow()} if data.title: props["modules.$.title"] = data.title if data.description: props["modules.$.description"] = data.description collection = get_collection(DOCTYPE_PROJECT) rs = await collection.find_one_and_update( { "_id": ObjectId(project), "modules": { "$elemMatch": { "ref": id } } }, {"$set": props}, { "_id": False, "modules": { "$elemMatch": { "ref": id } } }, return_document=ReturnDocument.AFTER) if rs == None: raise_not_found() if rs and rs["modules"] and len(rs["modules"]) > 0: return rs["modules"][0] raise_server_error()
async def find_many(): collection = get_collection(DOCTYPE_MODULE) modules: List[Module] = [] cursor = collection.find({}) async for row in cursor: modules.append(row) return modules
async def find_one(license: str, id: str): logging.info(">>> " + __name__ + ":find_one") collection = get_collection(DOCUMENT_TYPE) seek = _seek_client(license, id) found = await collection.find_one(seek) return found if found else raise_not_found()
async def find_license_owner(license: str): logging.info(">>> " + __name__ + ":find_one") collection = get_collection(DOCUMENT_TYPE) return await collection.find_one({ "license": license, "licenseOwner": True })
async def find_many(project: str): collection = get_collection(DOCTYPE_PROJECT_MEMBER) members: List[Member] = [] cursor = collection.find({'projectId': project}) async for row in cursor: members.append(row) return members
async def is_license_valid(license: str): logging.info(">>> " + __name__ + ":is_license_valid") collection = get_collection(DOCUMENT_TYPE) seek = _seek(license) found = await collection.find_one(seek, {"_id": True}) logging.info(found) return True if found else False
async def find_one(slug: str): logging.info(">>> " + __name__ + ":find_one") collection = get_collection(DOCUMENT_TYPE) seek = _seek(slug) found = await collection.find_one(seek) return found if found else raise_not_found()
async def create_doc(license: str, projectId: str, personaId: str, fullname: str) -> GPQEvidence: logging.info(">>> " + __name__ + ":create_doc") # Check item numbers from project # Assume it is 30 items = 30 # Per item = 3 minutes maxTime = items * 3 * 60 * 1000 seqs = [i for i in range(1, items + 1)] shuffle(seqs) strSeqs = ' '.join(map(str, seqs)) logging.info(strSeqs) model = GPQEvidenceCreate(license=license, projectId=projectId, personaId=personaId, fullname=fullname, items=items, maxTime=maxTime, sequence=strSeqs) props = fields_in_create(model) try: collection = get_collection(DOCTYPE_EV_GPQ) rs = await collection.insert_one(props) if rs.inserted_id: return await collection.find_one({"_id": rs.inserted_id}) except Exception as e: raise_server_error(str(e.detail))
async def find_one(project: str, search: str): collection = get_collection(DOCTYPE_PROJECT_MEMBER) seek = seek_by_search(project, search) member = await collection.find_one(seek) if member: return member raise_not_found()
async def enable(project: str, id: str, enable: bool): logging.info(">>> " + __name__ + ":enable") collection = get_collection(DOCTYPE_PROJECT) rs = await collection.find_one_and_update( { "_id": ObjectId(project), "modules": { "$elemMatch": { "ref": id } } }, { "$set": { "modules.$.enabled": enable, "updatedAt": datetime.utcnow() } }, { "_id": False, "modules": { "$elemMatch": { "ref": id } } }, return_document=ReturnDocument.AFTER) if rs == None: raise_not_found() if rs and rs["modules"] and len(rs["modules"]) > 0: return rs["modules"][0] raise_server_error()
async def read_many(limit: int = 50, skip: int = 0): collection = get_collection("movies") rs: List[Movie] = [] cursor = collection.find({}, limit=limit, skip=skip) async for row in cursor: rs.append(Movie(**row)) return rs
async def insert( license: str, creator: str, data: ProjectCreate, client: str = None, contract: str = None ): logging.info(">>> " + __name__ + ":insert") logging.info(data) # Project otomatis memiliki seluruh modul yang tersedia aces_modules = await find_project_modules() logging.info(aces_modules) try: project = ProjectInDB( **data.dict(), license=license, clientId=client, contractId=contract, # admin=creator, createdBy=creator, modules=aces_modules, ) logging.info("======") logging.info(project) props = fields_in_create(project) collection = get_collection(DOCTYPE_PROJECT) rs = await collection.insert_one(props) if rs.inserted_id: return await collection.find_one({"_id": rs.inserted_id}) except Exception as e: raise_server_error(str(e))
async def find_many(limit: int, skip: int): collection = get_collection(DOCTYPE_PROJECT) projects: List[Project] = [] cursor = collection.find({}, limit=limit, skip=skip) async for row in cursor: projects.append(row) return projects
async def read_one(movie_id: str): collection = get_collection("movies") if ObjectId.is_valid(movie_id): found = await collection.find_one({"_id": ObjectId(movie_id)}) if found: return found # raise HTTPException(status_code=404, detail="Not found.") # It's OK return JSONResponse({"error": "NOT FOUND"}, status_code=404)
async def delete(slug: str, id: str): logging.info(">>> " + __name__ + ":delete") collection = get_collection(DOCUMENT_TYPE) seek = {"license": slug, "_id": ObjectId(id)} client = await collection.find_one_and_delete(seek, {"_id": True}) if client: return {"message": "Client deleted."} raise_server_error(ERROR_MONGODB_DELETE)
async def find_many(slug: str, limit: int, skip: int): logging.info(">>> " + __name__ + ":find_many") collection = get_collection(DOCUMENT_TYPE) licenses: List[Client] = [] cursor = collection.find({"license": slug}, limit=limit, skip=skip) async for row in cursor: licenses.append(row) return licenses
async def find_many(limit: int, skip: int): logging.info(">>> " + __name__ + ":find_many") collection = get_collection(DOCUMENT_TYPE) rs: List[License] = [] cursor = collection.find({}, limit=limit, skip=skip) async for row in cursor: rs.append(row) return rs
async def find_one(project: str, search: str): collection = get_collection(DOCTYPE_PERSONA) seek = seek_by_search(project, search) # persona = await collection.find_one(seek) # if persona: # return persona # raise_not_found() return await collection.find_one(seek)
async def find_many(project: str): collection = get_collection(DOCTYPE_PERSONA) personas: List[Persona] = [] # cursor = collection.find({"projectId": project, "tests": {"$size": 0}}) cursor = collection.find({"projectId": project}) async for row in cursor: personas.append(row) return personas
async def find_many_by_license(license: str, limit: int, skip: int): collection = get_collection(DOCTYPE_PROJECT) projects: List[Project] = [] cursor = collection.find({"license": license}, {"modules": False}, limit=limit, skip=skip) # cursor = collection.find({"license": license}, limit=limit, skip=skip) async for row in cursor: projects.append(row) return projects
async def get_modules(id: str): collection = get_collection(DOCTYPE_PROJECT) rs = await collection.find_one( {"_id": ObjectId(id)}, {"_id": False, "modules": True} ) logging.info(rs) return rs["modules"]
async def find_one(projectId: str, personaId: str) -> GPQEvidence: logging.info(">>> " + __name__ + ":find_one") collection = get_collection(DOCTYPE_EV_GPQ) return await collection.find_one({ "projectId": projectId, "personaId": personaId })
async def find_by_email_or_username(email: str, username: str): logging.info(">>> " + __name__ + ":find_one") collection = get_collection(DOCUMENT_TYPE) return await collection.find_one( {"$or": [{ "email": email }, { "username": username }]})
async def find_many(license: str, limit: int, skip: int): logging.info(">>> " + __name__ + ":find_many") collection = get_collection(DOCUMENT_TYPE) seek = seek_by_license(license) rs: List[User] = [] cursor = collection.find(seek, limit=limit, skip=skip) async for row in cursor: rs.append(row) return rs
async def delete(id: str): try: collection = get_collection(DOCTYPE_MODULE) module = await collection.find_one_and_delete({"_id": ObjectId(id)}, {"_id": True}) if module: return {"message": "Module has been deleted."} except Exception as e: raise_server_error(str(e))
async def insert(data: ModuleCreate): try: props = fields_in_create(data) collection = get_collection(DOCTYPE_MODULE) rs = await collection.insert_one(props) if rs.inserted_id: return await collection.find_one({"_id": rs.inserted_id}) except Exception as e: raise_server_error(str(e))
async def find_many(license: str, limit: int, skip: int): logging.info(">>> " + __name__ + ":find_many") collection = get_collection(DOCUMENT_TYPE) seek = {"license": license.strip().lower()} rs: List[collection] = [] cursor = collection.find(seek, limit=limit, skip=skip) async for row in cursor: rs.append(row) return rs
async def delete_one(project: str, search: str): try: collection = get_collection(DOCTYPE_PROJECT_MEMBER) seek = seek_by_search(project, search) member = await collection.find_one_and_delete(seek, {"_id": True}) if member: return {"message": "Member has been deleted."} except Exception as e: raise_server_error(str(e))
async def delete(term: str): logging.info(">>> " + __name__ + ":delete") collection = get_collection(DOCUMENT_TYPE) seek = seek_by_term(term) found = await collection.find_one_and_delete(seek, {"_id": True}) logging.info(found) message = {"message": "User deleted"} return message if found else raise_server_error(ERROR_MONGODB_DELETE)