def get(self): """GET method handler for server-configs.""" technologies = Technologies.get_technologies() device_types = DeviceType.get_device_types() status_types = Status.get_status_types() system_configs = { 'label': 'automate_imei_request', 'flag': app.config['AUTOMATE_IMEI_CHECK'] },\ { 'label': 'overwrite_device_info', 'flag': app.config['USE_GSMA_DEVICE_INFO'] } documents = { 'registration': Documents.get_documents('registration'), 'de_registration': Documents.get_documents('deregistration') } response = ServerConfigSchema().dump(dict(technologies=technologies, documents=documents, status_types=status_types, device_types=device_types, system_config=system_configs)).data return Response(json.dumps(response), status=200, mimetype='application/json')
def create_dummy_documents(files, request_type, request, app=None): """Helper method to create dummy documents for a request. """ if request_type == 'Registration': current_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S') for file in files: document = Documents.get_document_by_name(file.get('label'), 1) reg_doc = RegDocuments(filename='{0}_{1}'.format(current_time, file.get('file_name'))) reg_doc.reg_details_id = request.id reg_doc.document_id = document.id reg_doc.save() file_path = '{0}/{1}/{2}'.format(app.config['DRS_UPLOADS'], request.tracking_id, file.get('file_name')) if not os.path.exists(os.path.dirname(file_path)): os.makedirs(os.path.dirname(file_path)) with open(file_path, 'wb') as f: f.seek(1073741824-1) f.write(b"\0") else: current_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S') for file in files: document = Documents.get_document_by_name(file.get('label'), 2) dereg_doc = DeRegDocuments(filename='{0}_{1}'.format(current_time, file.get('file_name'))) dereg_doc.dereg_id = request.id dereg_doc.document_id = document.id dereg_doc.save() file_path = '{0}/{1}/{2}'.format(app.config['DRS_UPLOADS'], request.tracking_id, file.get('file_name')) if not os.path.exists(os.path.dirname(file_path)): os.makedirs(os.path.dirname(file_path)) with open(file_path, 'wb') as f: f.seek(1073741824-1) f.write(b"\0") return request
def test_get_document_by_name(session): """Verify that the get_document_by_name() returns a document provided the name.""" docs = [ Documents(id=11101, label='shp doc', type=1, required=True), Documents(id=21102, label='ath doc', type=2, required=True) ] session.bulk_save_objects(docs) session.commit() assert Documents.get_document_by_name('shp doc', 1) assert Documents.get_document_by_name('ath doc', 2) assert Documents.get_document_by_name('88668', 1) is None
def test_get_document_by_id(session): """Verify that the get_document_by_id returns a document provided an id.""" docs = [ Documents(id=1110, label='shp doc', type=1, required=True), Documents(id=2110, label='ath doc', type=1, required=True) ] session.bulk_save_objects(docs) session.commit() assert Documents.get_document_by_id(1110) assert Documents.get_document_by_id(2110) assert Documents.get_document_by_id(79897777879) is None
def test_get_label_by_id(session): """Verify that the get_label_by_id() returns correct document label when id is provided.""" docs = [ Documents(id=111, label='shp doc', type=1, required=True), Documents(id=211, label='ath doc', type=1, required=True) ] session.bulk_save_objects(docs) session.commit() assert Documents.get_label_by_id(111) == 'shp doc' assert Documents.get_label_by_id(211) == 'ath doc' assert Documents.get_label_by_id(2334242323322) is None
def get(self): """GET method handler for server-configs.""" technologies = Technologies.get_technologies() device_types = DeviceType.get_device_types() status_types = Status.get_status_types() documents = { 'registration': Documents.get_documents('registration'), 'de_registration': Documents.get_documents('deregistration') } response = ServerConfigSchema().dump(dict(technologies=technologies, documents=documents, status_types=status_types, device_types=device_types)).data return Response(json.dumps(response), status=200, mimetype='application/json')
def validate_required_docs(self, data): """Validates required documents.""" required = Documents.get_required_docs('deregistration') labels = list(map(lambda document: document.label, required)) missing_required = list(filter(lambda x: x not in data['files'], labels)) if len(missing_required) > 0: raise ValidationError('This is a required Document', field_names=missing_required)
def get_document_label(self, data): """Returns a document label.""" dereg_details = DeRegDetails.get_by_id(data.dereg_id) upload_dir_path = GLOBAL_CONF['upload_directory'] document = Documents.get_document_by_id(data.document_id) data.label = document.label data.required = Documents.required data.link = '{server_dir}/{local_dir}/{file_name}'.format( server_dir=upload_dir_path, local_dir=dereg_details.tracking_id, file_name=data.filename)
def bulk_create(cls, documents, dereg_details, time): """Create/save documents in bulk.""" created_documents = [] for name in documents: document = Documents.get_document_by_name(name, 2) dereg_document = DeRegDocuments( filename='{0}_{1}'.format(time, documents[name].filename)) dereg_document.dereg_id = dereg_details.id dereg_document.document_id = document.id dereg_document.save() created_documents.append(dereg_document) return created_documents
def test_server_config_api(flask_app, db): # pylint: disable=unused-argument """To verify that the server config apis response is correct as per data in database. """ # get data from database status_types = Status.get_status_types() device_types = DeviceType.get_device_types() technologies = Technologies.get_technologies() documents = { 'registration': Documents.get_documents('registration'), 'de_registration': Documents.get_documents('deregistration') } expected_response = ServerConfigsSchema().dump(dict(technologies=technologies, documents=documents, status_types=status_types, device_types=device_types)).data rv = flask_app.get(SERVER_CONFIGS) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data == expected_response
def bulk_create(cls, documents, reg_details, time): """Create documents for a request in bulk.""" try: created_documents = [] for name in documents: document = Documents.get_document_by_name(name, 1) reg_document = cls( filename='{0}_{1}'.format(time, documents[name].filename)) reg_document.reg_details_id = reg_details.id reg_document.document_id = document.id reg_document.save() created_documents.append(reg_document) return created_documents except Exception: raise Exception
def bulk_update(cls, documents, dereg_details, time): """Update the documents in bulk.""" try: updated_documents = [] for name in documents: document = Documents.get_document_by_name(name, 2) cls.trash_document(document.id, dereg_details) dereg_document = cls( filename='{0}_{1}'.format(time, documents[name].filename)) dereg_document.dereg_id = dereg_details.id dereg_document.document_id = document.id dereg_document.save() updated_documents.append(dereg_document) return updated_documents except Exception: raise Exception
def bulk_update(cls, documents, reg_details, time): """Update documents of a request in bulk.""" try: updated_documents = [] for name in documents: document = Documents.get_document_by_name(name, 1) cls.trash_document(document.id, reg_details) reg_document = cls( filename='{0}_{1}'.format(time, documents[name].filename)) reg_document.reg_details_id = reg_details.id reg_document.document_id = document.id reg_document.save() updated_documents.append(reg_document) return updated_documents except Exception: raise Exception
def test_required_docs(session): """Verify that the required_docs() only returns docs for which the required flag is true.""" docs = [ Documents(id=111010034, label='shp doc', type=1, required=True), Documents(id=211020045, label='ath doc', type=1, required=False), Documents(id=111010056, label='shp doc', type=2, required=True), Documents(id=211020067, label='ath doc', type=2, required=True) ] session.bulk_save_objects(docs) session.commit() docs = Documents.get_required_docs('registration') for doc in docs: assert doc.required docs = Documents.get_required_docs('avdd') for doc in docs: assert doc.required
def test_get_documents(session): """Verify that the get_documents() returns all the documents of same type.""" docs = [ Documents(id=11101003, label='shp doc', type=1, required=True), Documents(id=21102004, label='ath doc', type=1, required=True), Documents(id=11101005, label='shp doc', type=2, required=True), Documents(id=21102006, label='ath doc', type=2, required=True) ] session.bulk_save_objects(docs) session.commit() docs_1 = Documents.get_documents('registration') assert docs_1 for doc in docs_1: assert doc.type == 1 docs_2 = Documents.get_documents('avddd') assert docs_1 for doc in docs_2: assert doc.type == 2