def update_response_option(user_type, question_type, question_id, response_option_id): """Update a response options""" try: data = request.json app.logger.debug(f'update_response_option():') app.logger.debug(f'- user_type= {user_type}') app.logger.debug(f'- question_type= {question_type}') app.logger.debug(f'- question_id= {question_id}') app.logger.debug(f'- response_option_id= {response_option_id}') app.logger.debug(f'- data= {data}') mf = MongoFacade(app.logger, DB_NAME) mf.update_in_child_collection(f'{question_type.lower()}Questions', question_id, 'options', data) return Response(json.dumps({}), status=200, mimetype='application/json') except Exception as e: app.logger.error( f'update_response_option: error adding data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def add_question(user_type, question_type): """Add a question""" try: data = request.json app.logger.debug(f'add_question():') app.logger.debug(f'- user_type= {user_type}') app.logger.debug(f'- question_type= {question_type}') app.logger.debug(f'- data= {data}') mf = MongoFacade(app.logger, DB_NAME) mf.insert_to_collection(f'{question_type.lower()}Questions', data) return Response(json.dumps({}), status=200, mimetype='application/json') except Exception as e: app.logger.error(f'add_question: error adding data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def update_question(user_type, question_type, question_id): """Update a question""" try: data = request.json app.logger.debug(f'add_question():') app.logger.debug(f'- user_type= {user_type}') app.logger.debug(f'- question_type= {question_type}') app.logger.debug(f'- data= {data}') question = {} for k, v in dict(data).items(): if k != '_id': question[k] = v mf = MongoFacade(app.logger, DB_NAME) mf.update_in_collection(f'{question_type.lower()}Questions', question_id, question) return Response(json.dumps({}), status=200, mimetype='application/json') except Exception as e: app.logger.error(f'add_question: error adding data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def add_new_host(): """Add a host""" try: data = request.json if 'email' not in data: raise Exception('Email must be included in POST request body') app.logger.debug(f'add_new_host(): data: {data}') mf = MongoFacade(app.logger, DB_NAME) if mf.item_in_collection('hosts', 'email', data['email']): return Response(f'Existing account with email: {data["email"]}', status=400, mimetype='text/plain') mf.insert_to_collection('hosts', data) return Response(status=200, mimetype='application/json') except Exception as e: app.logger.error(f'add_new_host: error adding data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def get_user_type(): """Returns user type""" try: data = request.json if 'email' not in data: raise Exception('Email must be included in POST request body') app.logger.debug(f'get_user_type(): data: {data}') mf = MongoFacade(app.logger, DB_NAME) if mf.item_in_collection('hosts', 'email', data['email']): return Response(json.dumps({'type': 'host'}), status=200, mimetype='application/json') elif mf.item_in_collection('guests', 'email', data['email']): return Response(json.dumps({'type': 'host'}), status=200, mimetype='application/json') else: return Response(json.dumps({'type': 'none'}), status=200, mimetype='application/json') except Exception as e: app.logger.error(f'get_user_type: error : {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def get_host_by_email(): """Returns user type""" try: data = request.json if 'email' not in data: raise Exception('Email must be included in POST request body') app.logger.debug(f'get_user_type(): data: {data}') mf = MongoFacade(app.logger, DB_NAME) if not mf.item_in_collection('hosts', 'email', data['email']): return Response(f'No host with email: {data["email"]}', status=400, mimetype='text/plain') host = mf.get_user_by_email('hosts', data['email']) return Response(json.dumps(host), status=200, mimetype='application/json') except Exception as e: app.logger.error(f'get_user_type: error : {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def get_images(subject): try: email = request.json['email'] app.logger.debug( f'get_images: looking for {subject} photos for {email}') mongo = MongoFacade(app.logger, DB_NAME) records = mongo.get_files_for_user_with_subject(email, subject) result = [] app.logger.debug(f'get_images: records') for r in records: app.logger.debug(f'get_images: - r: {r}') result.append({'id': str(r._id), 'filename': r.filename}) return jsonify(result) # return Response(result, status=200, mimetype='application/json') except Exception as e: app.logger.error(f'get_images: error: {e}') return Response({'error': e}, status=500, mimetype='application/json')
def get_host_info_questions(): """Get all host info questions""" try: mf = MongoFacade(app.logger, DB_NAME) questions = mf.get_collection('infoQuestions', None) return jsonify(questions) except Exception as e: app.logger.error( f'get_host_matching_questions: error getting data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def add_host_info(): """Add the general info for a host""" try: data = request.json app.logger.debug(f'add_host_info(): data: {data}') mf = MongoFacade(app.logger, DB_NAME) mf.add_field_to_record('hosts', 'email', data['email'], 'info', data) return Response(status=200, mimetype='application/json') except Exception as e: app.logger.error(f'add_host_info: error adding data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def image_download(file_id): app.logger.debug(f'image_download: file_id = {file_id}') try: mongo = MongoFacade(app.logger, DB_NAME) img = mongo.load_file(file_id) app.logger.debug(f'image_download: img.filename = {img.filename}') app.logger.debug( f'image_download: img.content_type = {img.content_type}') return send_file(io.BytesIO(img.read()), mimetype=img.content_type, as_attachment=True, attachment_filename=img.filename) except Exception as e: return Response(f'Error: {e}', status=500, mimetype='text/plain')
def add_response_option(user_type, question_id): """Add a response option to a matching question""" try: data = request.json app.logger.debug(f'add_response_option(): data: {data}') mf = MongoFacade(app.logger, DB_NAME) mf.add_to_child_collection('matchingQuestions', question_id, 'options', data) return Response(json.dumps({}), status=200, mimetype='application/json') except Exception as e: app.logger.error(f'add_response_option: error adding data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def add_host_qualifying_response(questionId): """Add the a response to a host qualifying question""" try: data = request.json app.logger.debug(f'add_host_qualifying_response(): data: {data}') mf = MongoFacade(app.logger, DB_NAME) mf.add_field_to_record_child('hosts', 'email', data['email'], 'qualifyingResponses', questionId, data['response']) return Response(json.dumps({}), status=200, mimetype='application/json') except Exception as e: app.logger.error( f'add_host_qualifying_response: error adding data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def delete_question(user_type, question_type, question_id): """Delete a question""" try: app.logger.debug(f'delete_question():') app.logger.debug(f'- user_type= {user_type}') app.logger.debug(f'- question_type= {question_type}') app.logger.debug(f'- question_id= {question_id}') mf = MongoFacade(app.logger, DB_NAME) mf.delete_from_collection(f'{question_type.lower()}Questions', question_id) return Response(json.dumps({}), status=200, mimetype='application/json') except Exception as e: app.logger.error(f'delete_question: error deleting data: {str(e)}') return Response(json.dumps({'error': str(e)}), status=500, mimetype='application/json')
def image_upload(): if 'image' not in request.files: flash('no image file') return Response(status=400, mimetype='application/json') app.logger.debug(f'image_upload(): request.form: {request.form}') email = request.form['email'] subject = request.form['subject'] img = request.files["image"] file_type = img.content_type app.logger.debug(f'image_upload(): img.content_type: {img.content_type}') app.logger.debug(f'image_upload(): img.mimetype: {img.mimetype}') app.logger.debug(f'image_upload(): img.filename: {img.filename}') if img.filename == '': flash('no image was selected') return Response(status=400, mimetype='application/json') if img and allowed_file(img.filename): img_name = secure_filename(img.filename) saveImg = MongoFacade(app.logger, DB_NAME) resp = saveImg.save_file(img, img_name, subject, email, file_type) if resp is not None: return Response(json.dumps({ 'msg': 'Image saved successfully', 'status': 200 }), status=200, mimetype='application/json') else: return Response(status=500, mimetype='application/json') else: return Response(status=500, mimetype='application/json') return Response(status=200, mimetype='application/json')
class Repository: def __init__(self, collection_name, logger, db_name): self.logger = logger self.mongo_facade = MongoFacade(logger, db_name, debug_mode=True) self.collection_name = collection_name def get(self, sort_condition=None): items = self.mongo_facade.get_collection(self.collection_name, sort_condition) return items def get_by_id(self, id): item = self.mongo_facade.get_element_by_id2(self.collection_name, id) return item def add(self, item): result = self.mongo_facade.insert_to_collection( self.collection_name, item) return result def delete(self, id): result = self.mongo_facade.delete_from_collection( self.collection_name, id) return result def update(self, id, item): self.logger.warning('Repository:update: id = {}'.format(id)) self.logger.warning('Repository:update: item = {}'.format(item)) safe_item = {x: item[x] for x in dict(item).keys() if x != '_id'} self.logger.warning('Repository:update: safe_item = {}'.format( json.dumps(safe_item, indent=4))) result = self.mongo_facade.update_in_collection( self.collection_name, id, safe_item) return result def get_using_email(self, email): # pass in the request body here resp = self.mongo_facade.get_user_by_email( self.collection_name, email) # add the request here return resp def _log(self, method_name, message): self.logger.debug('Repository[{}]:{}: {}'.format( self.collection_name, method_name, message))
def __init__(self, collection_name, logger, db_name): self.logger = logger self.mongo_facade = MongoFacade(logger, db_name, debug_mode=True) self.collection_name = collection_name