def get_preset_labels(project_name): id_token = request.args.get('id_token') if id_token is None or id_token == "": response = { 'message': "ID Token is not included with the request uri in args" } response = make_response(response) return response, 400 requestor_email = get_email(id_token) if requestor_email is None: response = {'message': "ID Token has expired or is invalid"} response = make_response(response) return response, 400 user_col = get_db_collection(project_name, "users") requestor = user_col.find_one({'email': requestor_email}) if requestor is None: response = {'message': "You are not authorised to perform this action"} response = make_response(response) return response, 403 labels_col = get_col(project_name, "labels") labels = labels_col.find({}) labels_list = list(labels) labels_dict = {'labels': labels_list} labels_out = JSONEncoder().encode(labels_dict) return labels_out, 200
def get_document(project_name, document_id): id_token = request.args.get('id_token') if id_token is None or id_token == "": response = { 'message': "ID Token is not included with the request uri in args" } response = make_response(response) return response, 400 requestor_email = get_email(id_token) if requestor_email is None: response = {'message': "ID Token has expired or is invalid"} response = make_response(response) return response, 400 users_col = get_col(project_name, "users") requestor = users_col.find_one({'email': requestor_email}) if requestor is None: response = {'message': "You are not authorised to perform this action"} response = make_response(response) return response, 403 col = get_db_collection(project_name, "documents") doc = col.find_one({'_id': ObjectId(document_id)}, {'_id': 0}) doc = {'document': doc} doc = JSONEncoder().encode(doc) return doc, 200
def export_documents(project_name): # get all documents doc_col = get_db_collection(project_name, "documents") documents = doc_col.find(projection={'comments': 0}) docs_to_write = [["ID", "BODY", "LABEL"]] # Generate data in correct format for export for d in documents: id_as_string = str(d['_id']) # Find final label id user_and_labels = d['user_and_labels'] final_label_id = None if len(user_and_labels) > 1: final_label_id = user_and_labels[0]['label'] for item in user_and_labels: # check that label is the same if item['label'] != final_label_id: final_label_id = None break # Get label name if final_label_id is not None: label_col = get_db_collection(project_name, 'labels') final_label = label_col.find_one({"_id": ObjectId(final_label_id)}) final_label = final_label['name'] else: final_label = '' docs_to_write.append([id_as_string, d['data'], final_label]) # TODO: Generated CSV file is buggy and not formatted correctly. Needs to be fixed. # Generator lets system create csv file without storing it locally def generate_csv(): for doc in docs_to_write: yield ','.join(doc) + '\r\n' return Response( generate_csv(), mimetype='text/csv', headers={"Content-Disposition": "attachment;filename=export.csv"})
def get_document(): if 'project' in request.json: project = request.json['project'] else: response = {'message': "Missing project"} response = make_response(response) return response, 400 if 'document_id' in request.json: identifier = request.json['document_id'] else: response = {'message': "Missing content"} response = make_response(response) return response, 400 col = get_db_collection(project, "documents") doc = col.find_one({'_id': ObjectId(identifier)}, {'_id': 0}) response = {'document': doc} response = make_response(response) return response, 200
def add_preset_labels(project_name): id_token = request.args.get('id_token') if id_token is None or id_token == "": response = { 'message': "ID Token is not included with the request uri in args" } response = make_response(response) return response, 400 requestor_email = get_email(id_token) if requestor_email is None: response = {'message': "ID Token has expired or is invalid"} response = make_response(response) return response, 400 user_col = get_db_collection(project_name, "users") requestor = user_col.find_one({'email': requestor_email, 'isAdmin': True}) if requestor is None: response = {'message': "You are not authorised to perform this action"} response = make_response(response) return response, 403 if 'label_name' in request.json: label_name = request.json['label_name'] else: response = {'message': "Missing label to add"} response = make_response(response) return response, 400 labels_col = get_col(project_name, "labels") label_in_database = labels_col.find_one({"name": label_name}) if label_in_database is not None: response = {'message': "That label already exists"} response = make_response(response) return response, 400 labels_col.insert_one({"name": label_name}) return "", 204
def delete_preset_labels(project_name, label_id): id_token = request.args.get('id_token') if id_token is None or id_token == "": response = { 'message': "ID Token is not included with the request uri in args" } response = make_response(response) return response, 400 requestor_email = get_email(id_token) if requestor_email is None: response = {'message': "ID Token has expired or is invalid"} response = make_response(response) return response, 400 user_col = get_db_collection(project_name, "users") requestor = user_col.find_one({'email': requestor_email, 'isAdmin': True}) if requestor is None: response = {'message': "You are not authorised to perform this action"} response = make_response(response) return response, 403 labels_col = get_col(project_name, "labels") labels_col.delete_one({"_id": ObjectId(label_id)}) # Go into each document, and delete all mentions of that label from each document document_col = get_col(project_name, "documents") document_col.update( {"user_and_labels": { "$elemMatch": { "label": ObjectId(label_id) } }}, {"$pull": { "user_and_labels": { 'label': ObjectId(label_id) } }}) return "", 204
def get_document_ids(project_name): id_token = request.args.get('id_token') try: page = int(request.args.get('page')) page_size = int(request.args.get('page_size')) except (ValueError, TypeError): response = {'message': "page and page_size must be integers"} response = make_response(response) return response, 400 if id_token is None or id_token == "": response = { 'message': "ID Token is not included with the request uri in args" } response = make_response(response) return response, 400 requestor_email = get_email(id_token) if requestor_email is None: response = {'message': "ID Token has expired or is invalid"} response = make_response(response) return response, 400 users_col = get_col(project_name, "users") requestor = users_col.find_one({'email': requestor_email}) if requestor is None: response = {'message': "You are not authorised to perform this action"} response = make_response(response) return response, 403 col = get_db_collection(project_name, "documents") docs = col.find({}, {'_id': 1}).skip(page * page_size).limit(page_size) docs_dict = {'docs': list(docs)} docs = JSONEncoder().encode(docs_dict) return docs, 200
def update_preset_labels(project_name, label_id): id_token = request.args.get('id_token') if id_token is None or id_token == "": response = { 'message': "ID Token is not included with the request uri in args" } response = make_response(response) return response, 400 requestor_email = get_email(id_token) if requestor_email is None: response = {'message': "ID Token has expired or is invalid"} response = make_response(response) return response, 400 user_col = get_db_collection(project_name, "users") requestor = user_col.find_one({'email': requestor_email, 'isAdmin': True}) if requestor is None: response = {'message': "You are not authorised to perform this action"} response = make_response(response) return response, 403 if 'label_name' in request.json: label_name = request.json['label_name'] else: response = {'message': "Missing label to add"} response = make_response(response) return response, 400 labels_col = get_col(project_name, "labels") labels_col.update_one({"_id": ObjectId(label_id)}, {'$set': { 'name': label_name }}) return "", 204
def set_label_for_user(): if 'project' in request.json: project = request.json['project'] else: response = {'message': "Missing projectID"} response = make_response(response) return response, 400 if 'document_id' in request.json: identifier = request.json['document_id'] else: response = {'message': "Missing identifier"} response = make_response(response) return response, 400 if 'email' in request.json: email = request.json['email'] else: response = {'message': "Missing email"} response = make_response(response) return response, 400 if 'label_id' in request.json: label_id = request.json['label_id'] else: response = {'message': "Missing label"} response = make_response(response) return response, 400 # get user obj user_col = get_db_collection(project, "users") user = user_col.find_one({'email': email}) if user is None: response = {'message': "Invalid User Email"} response = make_response(response) return response, 400 # get label obj label_col = get_db_collection(project, "labels") label = label_col.find_one({'_id': ObjectId(label_id)}) if label is None: response = {'message': "Invalid Label"} response = make_response(response) return response, 400 col = get_db_collection(project, "documents") # if the label already exists for the user if col.find_one({ '_id': ObjectId(identifier), "user_and_labels": { '$elemMatch': { "email": email } } }) is not None: col.update_one( { '_id': ObjectId(identifier), "user_and_labels": { '$elemMatch': { "email": email } } }, {'$set': { "user_and_labels.$.label": ObjectId(label_id) }}) else: # if the label assignment does not exist for the user col.update_one({'_id': ObjectId(identifier)}, { '$push': { "user_and_labels": { "email": email, "label": ObjectId(label_id) } } }) return '', 204
else: # if the label assignment does not exist for the user col.update_one({'_id': ObjectId(identifier)}, { '$push': { "user_and_labels": { "email": email, "label": ObjectId(label_id) } } }) return '', 204 if __name__ == '__main__': col = get_db_collection("New_Project", "documents") identifier = "5f6578baa50829d1e7115498" email = "*****@*****.**" label_ = "5f4f0b3d3aac88555576f0aa" col.update_one( { '_id': ObjectId(identifier), "user_and_labels": { '$elemMatch': { "email": email } } }, {'$set': { "user_and_labels.$.label": "abdbsgwasd" }}) '''col.update_one({'_id': ObjectId(identifier)},
def set_label_for_user(project_name, document_id): id_token = request.args.get('id_token') if id_token is None or id_token == "": response = { 'message': "ID Token is not included with the request uri in args" } response = make_response(response) return response, 400 requestor_email = get_email(id_token) if requestor_email is None: response = {'message': "ID Token has expired or is invalid"} response = make_response(response) return response, 400 if 'label_id' in request.json: label_id = request.json['label_id'] else: response = {'message': "Missing label"} response = make_response(response) return response, 400 # get user obj user_col = get_db_collection(project_name, "users") requestor = user_col.find_one({ 'email': requestor_email, 'isContributor': True }) if requestor is None: response = {'message': "You are not authorised to perform this action"} response = make_response(response) return response, 403 # get label obj label_col = get_db_collection(project_name, "labels") label = label_col.find_one({'_id': ObjectId(label_id)}) if label is None: response = {'message': "Invalid Label"} response = make_response(response) return response, 400 col = get_db_collection(project_name, "documents") # if the label already exists for the user if col.find_one({ '_id': ObjectId(document_id), "user_and_labels": { '$elemMatch': { "email": requestor_email } } }) is not None: col.update_one( { '_id': ObjectId(document_id), "user_and_labels": { '$elemMatch': { "email": requestor_email } } }, {'$set': { "user_and_labels.$.label": ObjectId(label_id) }}) else: # if the label assignment does not exist for the user col.update_one({'_id': ObjectId(document_id)}, { '$push': { "user_and_labels": { "email": requestor_email, "label": ObjectId(label_id) } } }) return '', 204