def getEvalById(evalID: str) -> EVALUATORS_ITEM_TEMPLATE: collection = db.getCollection(EVALUATORS_DOCUMENT) try: return collection.find_one({'_id': ObjectId(evalID)}) except PyMongoError: raise ConnectDatabaseError( 'Error while retrieving evaluator with the _id : ' + str(evalID))
def getUserById(userId: str) -> USERS_ITEM_TEMPLATE: collection = db.getCollection(USERS_DOCUMENT) try: result = collection.find_one({'_id': ObjectId(userId)}) return result except PyMongoError: raise ConnectDatabaseError('Error while getting the user bu it\'s _id')
def getAllCandidateSub(candidateID: str) -> list: print(candidateID) collection = db.getCollection(ASSIGNMENT_SUBMISSIONS_DOCUMENT) try: return collection.find({ASSIGNMENT_SUB_CAND_ID: ObjectId(candidateID)}) except PyMongoError: raise ConnectDatabaseError( 'Error while retrieving candidate submissions')
def updateAssignFilename(assignID: str, filename: str) -> None: collection = db.getCollection(ASSIGNMENTS_DOCUMENT) try: db.connect() collection.find_one_and_update({'_id': ObjectId(assignID)}, {'$set': {ASSIGNMENT_FILENAME: filename}}) db.close() except PyMongoError: raise ConnectDatabaseError('Error while updating filename for the assignment with the id ' + assignID)
def deleteUser(userID: str) -> None: collection = db.getCollection(USERS_DOCUMENT) try: collection.delete_one({'_id': ObjectId(userID)}) except PyMongoError: raise ConnectDatabaseError( 'There was an error while deleting user with the _id : ' + str(userID))
def deleteCandidate(candID: str) -> None: collection = db.getCollection(CANDIDATES_DOCUMENT) try: collection.delete_one({'_id': ObjectId(candID)}) except PyMongoError: raise ConnectDatabaseError( 'There was an error while deleting user with the _id : ' + str(candID))
def getGroupFromId(groupID: str) -> GROUP_TEMPLATE: collection = db.getCollection(GROUPS_DOCUMENT) try: result = collection.find_one({'_id': ObjectId(groupID)}) return result except PyMongoError: raise ConnectDatabaseError('Error while retriving the group _id :' + str(groupID))
def saveIOS(assignID: str, ios: list): try: db.connect() collection = db.getCollection(ASSIGNMENTS_DOCUMENT) collection.find_one_and_update({'_id': ObjectId(assignID)}, {'$set': {ASSIGNMENT_INPUT_OUTPUTS: ios}}) db.close() except PyMongoError: raise ConnectDatabaseError('Error while saving Inputs/Outpus for the assignment : ' + assignID)
def getCandidateById(candID: str) -> CANDIDATES_ITEM_TEMPLATE: collection = db.getCollection(CANDIDATES_DOCUMENT) try: return collection.find_one({'_id': ObjectId(candID)}) except PyMongoError: raise ConnectDatabaseError( 'Error while retrieving the candidate with the _id : ', str(candID))
def getAllGroupNameFromEvalId(evalId: str) -> list: try: groups = [{ 'id': str(g['_id']), GROUPS_NAME_FIELD: g[GROUPS_NAME_FIELD] } for g in getAllEvalGroups(evalId)] return groups except PyMongoError: raise ConnectDatabaseError('Error while groups name for an evaluator')
def getGroupFromId(idGroup: str) -> GROUP_TEMPLATE: collection = db.getCollection(GROUPS_DOCUMENT) try: result = collection.find_one({'_id': ObjectId(idGroup)}) return result except PyMongoError: raise ConnectDatabaseError( 'Error while getting the group form the _id')
def removeAssignmentsSubmissions(submissionsID: list) -> None: collection = db.getCollection(ASSIGNMENT_SUBMISSIONS_DOCUMENT) try: db.connect() for s in submissionsID: collection.delete_one({'_id': ObjectId(s['_id'])}) db.close() except PyMongoError: raise ConnectDatabaseError('Error while removing submission')
def getAllAssignmentsForEval(eval: EVALUATORS_ITEM_TEMPLATE) -> list: try: db.connect() collection = db.getCollection(ASSIGNMENTS_DOCUMENT) assigns = collection.find({ASSIGNMENT_AUTHOR_ID: ObjectId(eval['_id'])}) db.close() return assigns except PyMongoError: raise ConnectDatabaseError('Error while connecting to the database')
def getSubmissionFromID(subID: str) -> dict: try: db.connect() collection = db.getCollection(ASSIGNMENT_SUBMISSIONS_DOCUMENT) result = collection.find_one({'_id': ObjectId(subID)}) db.close() return result except PyMongoError: raise ConnectDatabaseError('Error while retrieving submission with _id : ' + str(subID))
def updateGroupName(groupID: str, newName: str) -> bool: collection = db.getCollection(GROUPS_DOCUMENT) try: result = collection.find_one_and_update( {'_id': ObjectId(groupID)}, {'$set': { GROUPS_NAME_FIELD: newName }}) return result is not None except PyMongoError: raise ConnectDatabaseError('Error while updating the ')
def getAllGroupsFromUserId(userId: str) -> list: collection = db.getCollection(GROUPS_DOCUMENT) try: return [ x for x in collection.find({GROUPS_ID_EVAL_FIELD: ObjectId(userId)}) ] except PyMongoError: raise ConnectDatabaseError( 'Error while getting group for the user with the id ' + userId)
def getGroupNameFromId(groupID: str) -> str: collection = db.getCollection(GROUPS_DOCUMENT) try: group = collection.find_one({'_id': ObjectId(groupID)}) if group is not None: return group[GROUPS_NAME_FIELD] except PyMongoError: raise ConnectDatabaseError( 'Error while retrieving group name for _id :' + str(groupID))
def getAssignmentFromId(assignID: str) -> ASSIGNMENT_ITEM_TEMPLATE: db.connect() collection = db.getCollection(ASSIGNMENTS_DOCUMENT) try: assign = collection.find_one({'_id': ObjectId(assignID)}) db.close() return assign except PyMongoError: raise ConnectDatabaseError('Error while getting assignment with the id ' + assignID)
def getCandidateFromMail(mail: str) -> CANDIDATES_ITEM_TEMPLATE: collection = db.getCollection(CANDIDATES_DOCUMENT) try: user = getOneUserByMail(mail) if user is None: return None if user[TYPE_FIELD] != CANDIDATE_TYPE: raise WrongUserTypeException('This user is not a candidate') return getCandidateByUserId(user['_id']) except PyMongoError: raise ConnectDatabaseError('Error while retrieving candidate')
def updateUserFields(userID: str, fieldsToUpdate: object) -> bool: print(fieldsToUpdate) collection = db.getCollection(USERS_DOCUMENT) try: result = collection.find_one_and_update({'_id': ObjectId(userID)}, {'$set': fieldsToUpdate}) return result except PyMongoError: raise ConnectDatabaseError('Error while updating user with the id : ' + set(userID))
def addGroupToEval(evalId: str, groupId: str) -> None: collection = db.getCollection(EVALUATORS_DOCUMENT) try: collection.find_one_and_update( {'_id': ObjectId(evalId)}, {'$push': { EVALUATOR_GROUPS_FIELD: ObjectId(groupId) }}) except PyMongoError: raise ConnectDatabaseError( 'Error while adding group to the evaluator.')
def removeCandidateFromGroups(candID: str, groupsID: list) -> None: collection = db.getCollection(GROUPS_DOCUMENT) try: for g in groupsID: collection.update({'_id': ObjectId(g)}, { '$pull': { GROUPS_CANDIDATES_IDS_FIELD: ObjectId(candID) }, }) except PyMongoError: raise ConnectDatabaseError('Error while removing user with _id : ' + str(candID) + ' from groups.')
def getAllEvalGroups(evalID: str) -> list: collection = db.getCollection(EVALUATORS_DOCUMENT) try: evaluator = collection.find_one({'_id': ObjectId(evalID)}) groups = [ getGroupFromId(gID) for gID in evaluator[EVALUATOR_GROUPS_FIELD] ] return groups except PyMongoError: raise ConnectDatabaseError( 'Error while retrieving groups for evaluator with the _id : ' + str(evalID))
def setEvalPremium(evaluatorID: str) -> None: collection = db.getCollection(EVALUATORS_DOCUMENT) try: return collection.find_one_and_update( {'_id': ObjectId(evaluatorID)}, {'$set': { EVALUATOR_IS_PREMIUM: True }}) except PyMongoError: raise ConnectDatabaseError('rror while setting account to premium ' + set(evaluatorID))
def getOneUserByMail(mail: str) -> USERS_ITEM_TEMPLATE: """ Retrieve a user from the databse with a mail. :param mail: Mail of the user as string. :return: Dictionary representing the user form the database. """ try: collection = db.getCollection(USERS_DOCUMENT) db.close() return collection.find_one({MAIL_FIELD: mail}) except PyMongoError: raise ConnectDatabaseError( 'Error while getting the user with his email')
def decrEvalCorrectionsAllowedFromID(evaluatorID: str) -> None: collection = db.getCollection(EVALUATORS_DOCUMENT) try: r = collection.find_one_and_update( {'_id': ObjectId(evaluatorID)}, {'$inc': { EVALUATOR_CORRECTED_PROGRAM_LEFT_NAME: -1 }}) return r except PyMongoError: raise ConnectDatabaseError( 'Error while decrementing counter of correction allowed for evaluator : ' + str(evaluatorID))
def getEvalByUserId(userId: str) -> EVALUATORS_ITEM_TEMPLATE: """ Retrieve Evaluator objects from the database from an id. :param userId: Id user as string. Converted as ObjectId inside the function. :return: Dictionary representing the evaluator from the database. """ collection = db.getCollection(EVALUATORS_DOCUMENT) try: eval = collection.find_one({'user_id': ObjectId(userId)}) return eval except PyMongoError: raise ConnectDatabaseError( 'Error while getting the user with it\'s _id')
def getEvalFromMail(mail: str) -> dict: """ Retrieve an evaluator object from the database with his mail address. :param mail: Mail as string. :return: Dictionary representing the user from the databse. """ try: userEntity = getOneUserByMail(mail.lower()) if userEntity is None: return None if userEntity[TYPE_FIELD] != EVALUATOR_TYPE: raise WrongUserTypeException("This user is not an evaluator.") return getEvalByUserId(userEntity['_id']) except PyMongoError: raise ConnectDatabaseError('')
def addAssignment(evalualor: EVALUATORS_ITEM_TEMPLATE, assignName: str, assignDesc: str, markingScheme: ASSIGNMENT_MARKING_SCHEME, originalFilename: str) -> str: assignment = ASSIGNMENT_ITEM_TEMPLATE assignment[ASSIGNMENT_AUTHOR_ID] = ObjectId(evalualor['_id']) assignment[ASSIGNMENT_NAME] = assignName assignment[ASSIGNMENT_DESCRIPTION] = str(assignDesc) assignment[ASSIGNMENT_MARKING_SCHEME_NAME] = markingScheme assignment[ASSIGNMENT_ORIGINAL_FILENAME] = originalFilename assignment[CREATED_TIMESTAMP] = datetime.now().timestamp() try: db.connect() assignInserted = db.insert(ASSIGNMENTS_DOCUMENT, assignment.copy()) return str(assignInserted.inserted_id) except PyMongoError: raise ConnectDatabaseError('Error while adding an assignment')
def saveSubmission(assignID: str, groupID: str, candID: str, savedFilename: str, dateSub: float, originalFilename:str) -> str: submission = CANDIDATE_ASSIGNMENT_SUBMISSION_TEMPLATE submission[ASSIGNMENT_SUB_CAND_ID] = ObjectId(candID) submission[ASSIGNMENT_SUB_ASSIGN_ID] = ObjectId(assignID) submission[ASSIGNMENT_SUB_GROUP_ID] = ObjectId(groupID) submission[ASSIGNMENT_FILENAME] = savedFilename submission[ASSIGNMENT_SUB_DATE_TIME_STAMP] = dateSub submission[CREATED_TIMESTAMP] = datetime.now().timestamp() submission[ASSIGNMENT_ORIGINAL_FILENAME] = originalFilename try: db.connect() insertedSub = db.insert(ASSIGNMENT_SUBMISSIONS_DOCUMENT, submission.copy()) db.close() return insertedSub.inserted_id except PyMongoError as e: raise ConnectDatabaseError('Errow hile saing submission : ' + str(e))