def get(dereg_id=None): """GET method handler, returns a deregistration request based on request id. """ schema = DeRegDetailsSchema() try: if dereg_id: if dereg_id.isdigit() and DeRegDetails.exists(dereg_id): response = DeRegDetails.get_by_id(dereg_id) response = schema.dump(response).data else: response = app.json_encoder.encode(DEREG_NOT_FOUND_MSG) else: response = DeRegDetails.get_all() response = schema.dump(response, many=True).data return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover app.logger.exception(e) error = { 'message': [_('Failed to retrieve response, please try later')] } return Response(app.json_encoder.encode(error), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()
def post(): """POST method handler, Create/Submit a new De-Registration details. """ tracking_id = uuid.uuid4() try: schema = DeRegDetailsSchema() args = DeRegDetails.curate_args(request) file = request.files.get('file') validation_errors = schema.validate(args) if validation_errors: return Response(app.json_encoder.encode(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) response = Utilities.store_file(file, tracking_id) if response: return Response(json.dumps(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) response = Utilities.process_de_reg_file( file.filename.split("/")[-1], tracking_id, args) errored = 'device_count' in response or 'invalid_imeis' in response or \ 'duplicate_imeis' in response or 'invalid_format' in response if not errored: gsma_response = Utilities.get_device_details_by_tac(response) response = DeRegDetails.create(args, tracking_id) db.session.commit() response = schema.dump(response, many=False).data response = {'request': response, 'devices': gsma_response} return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) else: return Response(app.json_encoder.encode(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover db.session.rollback() Utilities.remove_directory(tracking_id) app.logger.exception(e) data = { 'message': [ _('Registration request failed, check upload path or database connection' ) ] } return Response(app.json_encoder.encode(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()
def get_dashboard_report(cls, user_id, user_type): """ Get dashboard report for de-registration""" if user_type != 'reviewer': total_count = cls.query.filter_by(user_id=user_id).count() new_request = cls.query.filter_by(user_id=user_id).filter_by( status=1).count() awaiting_document = cls.query.filter_by(user_id=user_id).filter_by( status=2).count() pending_review = cls.query.filter_by(user_id=user_id).filter_by( status=3).count() in_review = cls.query.filter_by(user_id=user_id).filter_by( status=4).count() information_requested = cls.query.filter_by( user_id=user_id).filter_by(status=5).count() approved = cls.query.filter_by(user_id=user_id).filter_by( status=6).count() rejected = cls.query.filter_by(user_id=user_id).filter_by( status=7).count() latest_req = cls.query.filter_by(user_id=user_id).filter_by(status=3).filter_by(report_status=10)\ .order_by(cls.created_at.desc()).all()[:10] latest_requests = DeRegDetailsSchema().dump(latest_req, many=True).data return { "total_requests": total_count, "new_requests": new_request, "awaiting_document": awaiting_document, "pending_review": pending_review, "in_review": in_review, "information_requested": information_requested, "approved": approved, "rejected": rejected, "latest_request": latest_requests } else: review_count = cls.query.filter_by(reviewer_id=user_id).filter_by( status=4).count() pending_review_count = cls.query.filter_by(status=3).filter_by( reviewer_id=None).count() pending_review_req = cls.query.filter_by(status=3).filter_by(reviewer_id=None).filter_by(report_status=10)\ .order_by(cls.created_at.desc()).all()[:10] pending_review_requests = DeRegDetailsSchema().dump( pending_review_req, many=True).data return { "in_review_count": review_count, "pending_review_count": pending_review_count, "latest_pending_requests": pending_review_requests }
def get(dereg_id): """GET method handler, to return all section of a request.""" try: if not dereg_id.isdigit() or not DeRegDetails.exists(dereg_id): return Response(app.json_encoder.encode(DEREG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) dereg_details = DeRegDetails.get_by_id(dereg_id) dereg_schema = DeRegDetailsSchema() doc_schema = DeRegDocumentsSchema() device_schema = DeRegDeviceSchema() dereg_devices = DeRegDevice.get_devices_by_dereg_id(dereg_id) dereg_documents = DeRegDocuments.get_by_reg_id(dereg_id) deregistration_data = dereg_schema.dump(dereg_details).data device_data = device_schema.dump(dereg_devices, many=True).data document_data = doc_schema.dump(dereg_documents, many=True).data response = { 'dereg_details': deregistration_data, 'dereg_device': device_data, 'dereg_docs': document_data } return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover app.logger.exception(e) data = { 'message': [ _('De-Registration request failed, check upload path or database connection' ) ] } return Response(app.json_encoder.encode(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()