def test_common(): '''test general files to improve coverage Simply run to make sure they dont crash the application ''' from common import logger, responses logger.obj('string') app = Flask(__name__) with app.app_context(): responses.respondInternalServerError() responses.respondOk('string') responses.respondUnauthorized('string')
def classificationJob(): '''Handler for the /classification-job POST endpoint \n Creates and starts a classification job. \n Returns the job data immediately and classification continues in a background thread. ''' try: log.start() log.info('/classification-job'.center(20, '-')) if request.is_json: json_data = request.get_json() job = {} job_id = random.randint(100, 200) job['id'] = job_id job['classes'] = json_data['classes'] job['complete'] = False job['percentage'] = 0 jobs[f'{job_id}'] = job classification_thread = threading.Thread( name=f'classification {job_id}', target=startClassificationJob, args=(job,)) classification_thread.start() return responses.respondCreated(job) else: return responses.respondBadRequest('No categories defined') except Exception as error: log.error('Error: ', error) return responses.respondInternalServerError(error)
def updateTableOccupancy(table_number, name): '''Edit table occupancy ''' log.start() log.info(f'PUT: /table/{table_number}'.center(20, '-')) data_is_valid = validateDataForUpdate(request) if data_is_valid: try: occupant = request.get_json() occupant['name'] = name succeded, message = functions.updateTableOccupancy( table_number, occupant) if succeded: return responses.respondOk(message) else: return responses.respondBadRequest(message) except Exception as error: log.info('error while selecting seat') log.info('*'.center(20, '-')) return responses.respondInternalServerError(error) else: log.info('*'.center(20, '-')) return responses.respondBadRequest('Invalid data sent')
def createUploadJob(): '''Handler function for uploading a set of folders. \n Creates and starts upload jobs for the folders specified in the post requests' json payload. \n Returns upload job ids immediately and uploads continue in a background thread ''' try: log.start() log.info(f'/upload-job'.center(20, '-')) classes = validateDataForPostUploadJob(request) if not classes == False: functions.setToken(request.headers.get('Token')) all_folders = functions.getFolderNames('') folders_to_upload = [ name for name in classes if name in all_folders] upload_jobs = [] for folder in folders_to_upload: job_id = random.randint(300, 400) job = {'type': 'upload', 'complete': False, 'percentage': 0, 'id': job_id} jobs[f'{job_id}'] = job upload_jobs.append(job) upload_thread = threading.Thread(name=f'{folder}-upload', target=functions.uploadFiles, args=( f'{folder}', job, f'/{folder}', )) upload_thread.start() return responses.respondCreated(upload_jobs) else: return responses.respondBadRequest('Classes not sent') except Exception as error: return responses.respondInternalServerError(error)
def getTableOccupancy(table_number): '''Get a tables occupants ''' log.start() log.info(f'GET: /table/{table_number}'.center(20, '-')) try: occupancy = functions.getTableOccupancy(table_number) return responses.respondWithData(occupancy) except Exception as error: log.info('error while trying to retreiving occupancy data') log.info('*'.center(20, '-')) return responses.respondInternalServerError(error)
def checkClassificationJobStatus(job_id): '''Handler for checking the status of a classification job \n Responds with the data for the specified job ''' try: log.start() log.info(f'/classification-job/{job_id}') try: job = jobs[job_id] return responses.respondWithData(job) except KeyError as error: log.error('Jobs:', jobs) return responses.respondBadRequest(f'Job {job_id} not found') except Exception as error: log.error('Error:', type(error)) return responses.respondInternalServerError(error)
def checkUploadJobStatus(job_id): '''Handler for checking the status of an upload. \n Returns data on the job with the specified id ''' try: log.start() log.info(f'/upload-job/{job_id}'.center(20, '-')) try: job = jobs[job_id] return responses.respondWithData(job) except KeyError as error: return responses.respondBadRequest(f'Job {job_id} not found') except Exception as error: log.error('Error:', type(error)) log.error('Jobs:', jobs) return responses.respondInternalServerError(error)
def checkDownloadJobStatus(job_id): '''Handler function for checking the status of a download job. \n Responds with the data for the specified job ''' try: log.start() log.info(f'/download-job/{job_id}'.center(20, '-')) try: job = jobs[job_id] return responses.respondWithData(job) except KeyError as error: log.info(jobs) return responses.respondBadRequest(f'Job {job_id} not found') except Exception as error: log.error('Error:', type(error)) log.error('Jobs:', jobs) return responses.respondInternalServerError(error)
def createDownloadJob(): '''Handler function for starting a download job. \n Creates and starts a download job. \n Returns the job data immediately and downloads continue in a background thread. ''' try: log.start() log.info('/download-job'.center(20, '-')) functions.setToken(request.headers.get('Token')) job_id = random.randint(100, 200) job = {'type': 'download', 'complete': False, 'percentage': 0, 'id': job_id} jobs[f'{job_id}'] = job download_thread = threading.Thread( target=functions.downloadFiles, args=(job,)) download_thread.start() return responses.respondCreated(job) except Exception as error: return responses.respondInternalServerError(error)