def populate_au_level1_cbox(cbox, with_star_entry=True, with_restriction=True, select_working_entry=True): cbox.clear() session = SessionHandler().session_instance() # cbox.addItem("UB", 01) if with_star_entry: cbox.addItem("*", -1) if with_restriction: l1_restrictions = DatabaseUtils.l1_restriction_array() for code, name in session.query(AuLevel1.code, AuLevel1.name).filter( AuLevel1.code.in_(l1_restrictions)).\ filter(AuLevel1.code != '013').filter(AuLevel1.code != '012').order_by(AuLevel1.name): cbox.addItem(name, code) else: for code, name in session.query(AuLevel1.code, AuLevel1.name).\ filter(AuLevel1.code != '013').filter(AuLevel1.code != '012').order_by(AuLevel1.name): cbox.addItem(name, code) if select_working_entry: working_l1_code = DatabaseUtils.working_l1_code() idx = cbox.findData(working_l1_code, Qt.UserRole) if idx != -1: cbox.setCurrentIndex(idx)
def populate_au_level2_cbox(cbox, l1_code, with_star_entry=True, with_restriction=True, select_working_entry=True): cbox.clear() session = SessionHandler().session_instance() if l1_code != -1: if with_star_entry: cbox.addItem("*", -1) if with_restriction: l2_restrictions = DatabaseUtils.l2_restriction_array() for code, name in session.query( AuLevel2.code, AuLevel2.name).filter( AuLevel2.code.in_(l2_restrictions)).filter( AuLevel2.code.startswith(l1_code)).order_by( AuLevel2.name): # if code.startswith('1') or code.startswith('01'): # continue cbox.addItem(name, code) else: for code, name in session.query( AuLevel2.code, AuLevel2.name).filter( AuLevel2.code.startswith(l1_code)).order_by( AuLevel2.name): cbox.addItem(name + '_' + code, code) if select_working_entry: working_l2_code = DatabaseUtils.working_l2_code() idx = cbox.findData(working_l2_code, Qt.UserRole) if idx != -1: cbox.setCurrentIndex(idx)
def is_logging_enabled(): session = SessionHandler().session_instance() if session is None: return True result = session.execute( "SELECT log_enabled from settings.set_logging;") for row in result: return row[0]
def populate_au_level3_cbox(cbox, l1_or_l2_code, with_star_entry=True): cbox.clear() session = SessionHandler().session_instance() if with_star_entry: cbox.addItem("*", -1) for code, name in session.query(AuLevel3.code, AuLevel3.name).filter( AuLevel3.code.startswith(l1_or_l2_code)).order_by( AuLevel3.name): cbox.addItem(name, code)
def create_new_contract(): session = SessionHandler().session_instance() contract = CtContract() contract.contract_no = QDateTime.currentDateTime().toString( "yyMMddhhmmss") session.add(contract) return contract
def create_new_m_case(): session = SessionHandler().session_instance() ca_maintenance_case = CaMaintenanceCase() user_name = DatabaseUtils.current_user().user_name user = session.query(SetRole).filter( SetRole.user_name == user_name).filter( SetRole.is_active == True).one() ca_maintenance_case.created_by = user.user_name_real session.add(ca_maintenance_case) return ca_maintenance_case
def create_new_contract(): session = SessionHandler().session_instance() contract = CtContract() contract.contract_no = QDateTime.currentDateTime().toString( "yyMMddhhmmss") contract.au2 = DatabaseUtils.working_l2_code() contract.type = 1 session.add(contract) return contract
def create_new_record(record_no=None): session = SessionHandler().session_instance() record = CtOwnershipRecord() if not record_no: record.record_no = QDateTime.currentDateTime().toString( "yyMMddhhmmss") else: record.record_no = record_no session.add(record) return record
def load_database_layer(schema_name, table_name): uri = QgsDataSourceURI() user = SessionHandler().current_username() db = SessionHandler().current_database() host = SessionHandler().current_host() port = SessionHandler().current_port() pwd = SessionHandler().current_password() uri.setConnection(host, str(port), db, user, pwd) uri.setDataSource(schema_name, table_name, "geometry") vlayer = QgsVectorLayer(uri.uri(), table_name, "postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer) return vlayer
def soum_from_parcel(parcel_id): # try: session = SessionHandler().session_instance() parcel = session.query(CaParcel).filter( CaParcel.parcel_id == parcel_id).one() soum = session.query(AuLevel2.code).filter( func.ST_Covers(AuLevel2.geometry, parcel.geometry)).one() # except SQLAlchemyError, e: # raise LM2Exception(QApplication.translate("LM2", "Database Query Error"), # QApplication.translate("LM2", "Could not execute: {0}").format(e.message)) return soum[0]
def soum_from_parcel(parcel_id): # try: session = SessionHandler().session_instance() parcel_count = session.query(CaParcelTbl).filter( CaParcelTbl.parcel_id == parcel_id).count() if parcel_count == 0: return None else: parcel = session.query(CaParcelTbl).filter( CaParcelTbl.parcel_id == parcel_id).first() soum = session.query(AuLevel2.code).filter( func.ST_Covers(AuLevel2.geometry, parcel.geometry)).one() return soum[0]
def create_new_application(app_no=None): session = SessionHandler().session_instance() current_user = QSettings().value(SettingsConstants.USER) application = CtApplication() if app_no is None: application.app_no = "00" + QDateTime.currentDateTime().toString( "yyMMddhhmmss") else: application.app_no = app_no application_status = CtApplicationStatus() application_status.ct_application = application status = session.query(ClApplicationStatus).filter_by(code='1').one() application_status.status = 1 application_status.status_ref = status application_status.status_date = date.today() date_time_string = QDateTime.currentDateTime().toString( Constants.DATABASE_DATETIME_FORMAT) application.app_timestamp = datetime.strptime( date_time_string, Constants.PYTHON_DATETIME_FORMAT) # application.created_at = datetime.strptime(date_time_string, Constants.PYTHON_DATETIME_FORMAT) # application.updated_at = datetime.strptime(date_time_string, Constants.PYTHON_DATETIME_FORMAT) application.app_type = 1 application.au2 = DatabaseUtils.current_working_soum_schema() application.au1 = DatabaseUtils.working_l1_code() # application.right_type = 1 application.created_by = DatabaseUtils.current_sd_user().user_id application_status.next_officer_in_charge = DatabaseUtils.current_sd_user( ).user_id application_status.next_officer_in_charge_ref = DatabaseUtils.current_sd_user( ) application_status.officer_in_charge_ref = DatabaseUtils.current_sd_user( ) application_status.officer_in_charge = DatabaseUtils.current_sd_user( ).user_id application.statuses.append(application_status) session.add(application) return application
def create_new_application(app_no=None): session = SessionHandler().session_instance() current_user = QSettings().value(SettingsConstants.USER) application = CtApplication() if app_no is None: application.app_no = "00" + QDateTime.currentDateTime().toString( "yyMMddhhmmss") else: application.app_no = app_no application_status = CtApplicationStatus() application_status.ct_application = application status = session.query(ClApplicationStatus).filter_by(code='1').one() application_status.status = 1 application_status.status_ref = status application_status.status_date = date.today() date_time_string = QDateTime.currentDateTime().toString( Constants.DATABASE_DATETIME_FORMAT) application.app_timestamp = datetime.strptime( date_time_string, Constants.PYTHON_DATETIME_FORMAT) application.app_type = 1 officer = session.query(SetRole).filter_by( user_name=current_user).filter(SetRole.is_active == True).one() application_status.next_officer_in_charge = current_user application_status.next_officer_in_charge_ref = officer application_status.officer_in_charge_ref = officer application_status.officer_in_charge = current_user application.statuses.append(application_status) session.add(application) return application
def test_SessionHandler(self): ''' Тестирует класс, отвечающий за сессии ''' # тест на правильный вариант environ = {"QUERY_STRING": "number=12345678", "HTTP_COOKIE": "sid=123"} session = SessionHandler(environ) out = session.result() first = json.loads(json.loads(out)[0][2]) second = [2, 3, 3, 47, 14593] self.assertEqual(first, second, environ) # множественный тест environ_variants = [{"QUERY_STRING": "number=1234567890", "HTTP_COOKIE": ""}, {"QUERY_STRING": "number=-1", "HTTP_COOKIE": "sid=123"}, {"QUERY_STRING": "1", "HTTP_COOKIE": "q=423432543215"}, {"HTTP_COOKIE": ""}, {"SOMETHING": "poo"},] for environ in environ_variants: session = SessionHandler(environ) out = session.result() self.assertIsInstance(out, str, environ)
def load_polygon_layer_base_layer(layer_name, id, schema_name): uri = QgsDataSourceURI() user = QSettings().value(SettingsConstants.USER) db = QSettings().value(SettingsConstants.DATABASE_NAME) host = QSettings().value(SettingsConstants.HOST) port = QSettings().value(SettingsConstants.PORT, "5432") pwd = SessionHandler().current_password() uri.setConnection(host, port, db, user, pwd) uri.setDataSource(schema_name, layer_name, "polygon_geom", "", id) vlayer = QgsVectorLayer(uri.uri(), layer_name, "postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer, False) return vlayer
def load_temp_table(sql, layer_name): uri = QgsDataSourceURI() user = QSettings().value(SettingsConstants.USER) db = QSettings().value(SettingsConstants.DATABASE_NAME) host = QSettings().value(SettingsConstants.HOST) port = QSettings().value(SettingsConstants.PORT, "5432") pwd = SessionHandler().current_password() uri.setConnection(host, port, db, user, pwd) uri.setDataSource("", sql, "geometry", "", "gid") vlayer = QgsVectorLayer(uri.uri(), layer_name, "postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer, False) return vlayer
def refresh_layer1(): session = SessionHandler().session_instance() root = QgsProject.instance().layerTreeRoot() mygroup = root.findGroup(u"Мэдээний хяналт") if mygroup is None: quality_check_group = root.insertGroup(0, u"Мэдээний хяналт") mygroup = root.findGroup(u"Тайлан") if mygroup is None: reports_group = root.insertGroup(2, u"Тайлан") mygroup = root.findGroup(u"ГНСТайлан") if mygroup is None: gt_report_group = root.insertGroup(1, u"ГНСТайлан") mygroup = root.findGroup(u"Кадастр") if mygroup is None: cadastre_group = root.insertGroup(4, u"Кадастр") mygroup = root.findGroup(u"Кадастрын төлөвлөгөө") if mygroup is None: cadastre_plan_group = root.insertGroup(3, u"Кадастрын төлөвлөгөө") mygroup = root.findGroup(u"Тусгай хэрэгцээний газар") if mygroup is None: cadastre_plan_group = root.insertGroup( 6, u"Тусгай хэрэгцээний газар") mygroup = root.findGroup(u"Кадастрын өөрчлөлт") if mygroup is None: cadastre_maintenance_group = root.insertGroup( 2, u"Кадастрын өөрчлөлт") mygroup = root.findGroup(u"Үнэлгээ, төлбөрийн бүс") if mygroup is None: land_fee_and_tax_zones_group = root.insertGroup( 7, u"Үнэлгээ, төлбөрийн бүс") mygroup = root.findGroup(U"Хилийн цэс, бүсчлэл") if mygroup is None: admin_units_group = root.insertGroup(8, u"Хилийн цэс, бүсчлэл") mygroup = root.findGroup(U"CAMA") if mygroup is None: admin_units_group = root.insertGroup(8, u"CAMA") mygroup = root.findGroup(U"Хаяг") if mygroup is None: admin_units_group = root.insertGroup(8, u"Хаяг")
def load_union_layer_by_name(layer_name, id): uri = QgsDataSourceURI() user = QSettings().value(SettingsConstants.USER) db = QSettings().value(SettingsConstants.DATABASE_NAME) host = QSettings().value(SettingsConstants.HOST) port = QSettings().value(SettingsConstants.PORT, "5432") pwd = SessionHandler().current_password() uri.setConnection(host, port, db, user, pwd) uri.setDataSource("data_soums_union", layer_name, "geometry", "", id) vlayer = QgsVectorLayer(uri.uri(), "data_soums_union" + "_" + layer_name, "postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer, False) return vlayer
def load_layer_by_name_set_zones(layer_name, id, restrictions=[]): restrictions = restrictions.split(",") if len(restrictions) > 0: for restriction in restrictions: restriction = restriction.strip() uri = QgsDataSourceURI() user = QSettings().value(SettingsConstants.USER) db = QSettings().value(SettingsConstants.DATABASE_NAME) host = QSettings().value(SettingsConstants.HOST) port = QSettings().value(SettingsConstants.PORT, "5432") pwd = SessionHandler().current_password() uri.setConnection(host, port, db, user, pwd) uri.setDataSource("settings", layer_name, "geometry", "", id) vlayer = QgsVectorLayer(uri.uri(), layer_name, "postgres") QgsMapLayerRegistry.instance().addMapLayer(vlayer, False) return vlayer
def create_new_m_case(): session = SessionHandler().session_instance() ca_maintenance_case = CaMaintenanceCase() user_name = DatabaseUtils.current_user().user_name user = session.query(SetRole).filter( SetRole.user_name == user_name).filter( SetRole.is_active == True).one() sd_user = session.query(SdUser).filter( SdUser.gis_user_real == user.user_name_real).first() ca_maintenance_case.created_by = sd_user.user_id ca_maintenance_case.au2 = DatabaseUtils.working_l2_code() session.add(ca_maintenance_case) return ca_maintenance_case
def main(): global SESSION_HANDLER parser = argparse.ArgumentParser(description="AI for Earth Land Cover") parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose debugging", default=False) # TODO: make sure the storage type is passed onto the Session objects parser.add_argument('--storage_type', action="store", dest="storage_type", type=str, choices=["table", "file"], default=None) parser.add_argument("--storage_path", action="store", dest="storage_path", type=str, help="Path to directory where output will be stored", default=None) parser.add_argument("--host", action="store", dest="host", type=str, help="Host to bind to", default="0.0.0.0") parser.add_argument("--port", action="store", dest="port", type=int, help="Port to listen on", default=8080) subparsers = parser.add_subparsers( dest="subcommand", help='Help for subcommands' ) # TODO: If we use Python3.7 we can use the required keyword here parser_a = subparsers.add_parser( 'local', help='For running models on the local server') parser_b = subparsers.add_parser('remote', help='For running models with RPC calls') parser.add_argument("--remote_host", action="store", dest="remote_host", type=str, help="RabbitMQ host", default="0.0.0.0") parser.add_argument("--remote_port", action="store", dest="remote_port", type=int, help="RabbitMQ port", default=8080) args = parser.parse_args(sys.argv[1:]) # create Session factory to use based on whether we are running locally or remotely run_local = None if args.subcommand == "local": print("Sessions will be spawned on the local machine") run_local = True elif args.subcommand == "remote": print("Sessions will be spawned remotely") run_local = False else: print("Must specify 'local' or 'remote' on command line") return SESSION_HANDLER = SessionHandler(run_local, args) SESSION_HANDLER.start_monitor() # Setup logging log_path = os.getcwd() + "/logs" setup_logging(log_path, "server") # TODO: don't delete logs # Setup the bottle server app = bottle.Bottle() app.add_hook("after_request", enable_cors) app.add_hook("before_request", setup_sessions) app.add_hook( "before_request", manage_sessions ) # before every request we want to check to make sure there are no session issues # API paths app.route( "/predPatch", method="OPTIONS", callback=do_options ) # TODO: all of our web requests from index.html fire an OPTIONS call because of https://stackoverflow.com/questions/1256593/why-am-i-getting-an-options-request-instead-of-a-get-request, we should fix this app.route('/predPatch', method="POST", callback=pred_patch) app.route("/predTile", method="OPTIONS", callback=do_options) app.route('/predTile', method="POST", callback=pred_tile) app.route("/getInput", method="OPTIONS", callback=do_options) app.route('/getInput', method="POST", callback=get_input) app.route("/recordCorrection", method="OPTIONS", callback=do_options) app.route('/recordCorrection', method="POST", callback=record_correction) app.route("/retrainModel", method="OPTIONS", callback=do_options) app.route('/retrainModel', method="POST", callback=retrain_model) app.route("/resetModel", method="OPTIONS", callback=do_options) app.route('/resetModel', method="POST", callback=reset_model) app.route("/doUndo", method="OPTIONS", callback=do_options) app.route("/doUndo", method="POST", callback=do_undo) app.route("/doLoad", method="OPTIONS", callback=do_options) app.route("/doLoad", method="POST", callback=do_load) app.route("/createSession", method="OPTIONS", callback=do_options) app.route("/createSession", method="POST", callback=create_session) app.route("/killSession", method="OPTIONS", callback=do_options) app.route("/killSession", method="POST", callback=kill_session) app.route("/whoami", method="GET", callback=whoami) # Content paths app.route("/", method="GET", callback=get_landing_page) app.route("/favicon.ico", method="GET", callback=get_favicon) app.route("/<filepath:re:.*>", method="GET", callback=get_everything_else) manage_session_folders() session_opts = { 'session.type': 'file', 'session.cookie_expires': 3000, 'session.data_dir': SESSION_FOLDER, 'session.auto': True } app = beaker.middleware.SessionMiddleware(app, session_opts) server = cheroot.wsgi.Server((args.host, args.port), app) server.max_request_header_size = 2**13 server.max_request_body_size = 2**27 try: server.start() finally: server.stop()
def showError(handler): print(handler.getLastFriendlyError()) Analytics.sendAnalytics('AppError', handler.getLastError()) print('Press the Enter key to exit') raw_input() sys.exit(1) if __name__ == '__main__': Analytics.sendAnalytics('AppEvent', 'AppLaunch') user = raw_input('Username: '******'Contact ID: ') handler = SessionHandler() if not handler.authorize(user, password): showError(handler) print('Logged in successfully, requesting export of {}'.format(contactId)) if not handler.requestContactExport(contactId): showError(handler) print('Requested export successfully, waiting for export to be ready.') while not handler.isDownloadReady(): if not handler.checkIfExportIsReady(): showError(handler) print( 'Export is ready, beginning download, this could take a few minutes.') if not handler.downloadExport(): showError(handler) Analytics.sendAnalytics('AppEvent', 'AppCompleted') print('Download completed')
sessionHandlers = list() ########Manage session loop while True: #connect to the database db = DBManager() #get all sessions data from DB sessions = db.getSessions() db.close() #overseerlogger.info("(+) Retrieved Session information from DB") #generate SessionHandler objects add keys to sessionDict #for each row in sessions table for session in sessions: #if this row does not have a sessionHandler, make one for it and start it if not session['phrase'] in sessionsDict: sessionHandler = SessionHandler(session, alertQueue) sessionHandlers.append(sessionHandler) sessionHandler.start() overseerlogger.info("(+) SessionHandler for session:"+sessionHandler.phrase+" started.") sessionsDict[session['phrase']] = True #cleanup SessionHandler objects and remove keys from sessionsDict #Dict to hold all phrases of active sessions (from database) (dict is faster than list) phraseDict = {session['phrase']:1 for session in sessions} #remove sessionHandlers that have ended and sessions that have expired (phrase not in phraseList) for sessionHandler in sessionHandlers[:]: if not sessionHandler.is_alive(): del sessionsDict[sessionHandler.phrase] sessionHandlers.remove(sessionHandler) overseerlogger.info("(+) SessionHandler for session:"+sessionHandler.phrase+" destroyed.")