def execute(self): from pyasm.search import DbContainer from pyasm.security import Security delete_group = "admin" security = Environment.get_security() if not security.is_in_group(delete_group): raise Exception("Only users in [%s] can delete projects"%delete_group) project_code = self.kwargs.get("project_code") if project_code: project = Project.get_by_code(project_code) else: search_key = self.kwargs.get("search_key") project = Search.get_by_search_key(search_key) project_code = project.get_code() assert project_code assert project # dump the database # remove all dependencies the sthpw database related_types = self.kwargs.get("related_types") if related_types: for related_type in related_types: search = Search(related_type) if related_type == "sthpw/schema": search.add_filter("code", project_code) else: search.add_filter("project_code", project_code) count = search.get_count() sobjects = search.get_sobjects() for sobject in sobjects: if related_type == 'sthpw/snapshot': self.delete_snapshot(sobject) else: sobject.delete() sthpw_project = Project.get_by_code('sthpw') # delete the database sthpw_db_resource = sthpw_project.get_project_db_resource() db_resource = project.get_project_db_resource() impl = sthpw_db_resource.get_database_impl() deleted_impl = db_resource.get_database_impl() if not impl.database_exists(db_resource): # remove the project entry project.delete() return # close this connection to the project to be deleted sql = DbContainer.get(db_resource) sql.close() if sql.get_database_type() == 'Sqlite': DbContainer.release_thread_sql() result = impl.drop_database(db_resource) # this is just extra check if result and "failed" in result: raise TacticException(result) Container.put("Sql:database_exists:%s"%db_resource.get_key(), None) sql = DbContainer.get(db_resource, connect=True) if sql: try: if sql.get_database_type() != 'Sqlite': if sql.get_connection() and sql.connect(): raise TacticException("Database [%s] still exists. There could still be connections to it."%project_code) except SqlException as e: pass # remove the project entry project.delete(triggers=False) schema = Schema.get_by_code(project_code) if schema: schema.delete() # Delete project specific login group and login in group entries expr = "@SOBJECT(sthpw/login_group['project_code','%s'])"%project_code expr2 = "@SOBJECT(sthpw/login_group['project_code','%s'].sthpw/login_in_group)"%project_code sobjs = Search.eval(expr2) for sobj in sobjs: sobj.delete() sobjs = Search.eval(expr) for sobj in sobjs: sobj.delete() return
class DeleteProjectCmd(DeleteCmd): def execute(my): from pyasm.search import DbContainer from pyasm.security import Security security = Environment.get_security() if not security.is_in_group("admin"): raise Exception("Only admin users can delete projects") project_code = my.kwargs.get("project_code") if project_code: project = Project.get_by_code(project_code) else: search_key = my.kwargs.get("search_key") project = Search.get_by_search_key(search_key) project_code = project.get_code() assert project_code assert project # dump the database # remove all dependencies the sthpw database related_types = my.kwargs.get("related_types") if related_types: for related_type in related_types: search = Search(related_type) if related_type == "sthpw/schema": search.add_filter("code", project_code) else: search.add_filter("project_code", project_code) count = search.get_count() sobjects = search.get_sobjects() for sobject in sobjects: if related_type == 'sthpw/snapshot': my.delete_snapshot(sobject) else: sobject.delete() sthpw_project = Project.get_by_code('sthpw') # delete the database sthpw_db_resource = sthpw_project.get_project_db_resource() db_resource = project.get_project_db_resource() impl = sthpw_db_resource.get_database_impl() deleted_impl = db_resource.get_database_impl() if not impl.database_exists(db_resource): # remove the project entry project.delete() return # close this connection to the project to be deleted sql = DbContainer.get(db_resource) sql.close() if sql.get_database_type() == 'Sqlite': DbContainer.release_thread_sql() result = impl.drop_database(db_resource) # this is just extra check if result and "failed" in result: raise TacticException(result) Container.put("Sql:database_exists:%s" % db_resource.get_key(), None) sql = DbContainer.get(db_resource, connect=True) if sql: try: if sql.get_database_type() != 'Sqlite': if sql.get_connection() and sql.connect(): raise TacticException( "Database [%s] still exists. There could still be connections to it." % project_code) except SqlException, e: pass # remove the project entry project.delete(triggers=False) schema = Schema.get_by_code(project_code) if schema: schema.delete() return
def get_display(my): from pyasm.biz import Project security = Environment.get_security() if not security.check_access("builtin", "side_bar_schema", "allow", default="deny"): return DivWdg() section_div = LabeledHidableWdg(label="Schema Views") section_div.set_attr('spt_class_name', Common.get_full_class_name(my) ) palette = Palette.get() color = palette.color("background3") project_div = RoundedCornerDivWdg(hex_color_code=color,corner_size="10") project_div.set_dimensions( width_str='175px', content_height_str='100px' ) project = Project.get() project_code = project.get_code() project_type = project.get_type() div = DivWdg() section_div.add(project_div) project_div.add(div) # get project type schema schema = Schema.get_by_code(project_code) if schema: div.add( my.get_schema_wdg(schema) ) #if not project_type: # raise SetupException("Project type not found for this [%s]" %project_code) if project_type: schema = Schema.get_predefined_schema(project_type) if schema: div.add( my.get_schema_wdg(schema) ) schema = Schema.get_predefined_schema('config') div.add( my.get_schema_wdg(schema) ) schema = Schema.get_admin_schema() div.add( my.get_schema_wdg(schema) ) return section_div # create a fake schema project = Project.get() db_name = project.get_database() sql = DbContainer.get(db_name) tables = sql.get_tables() tables.sort() tables_str = "\n".join( ['<search_type name="%s"/>'%x for x in tables] ) # look at all of the search objects for mapped tables search = Search("sthpw/search_object") #search.add_where('''"namespace" = 'MMS' or namespace = '{project}' ''') search.add_filter("namespace", 'MMS') search.add_filter("namespace", '{project}') search.add_where("or") search_types = search.get_sobjects() #for search_type in search_types: # print "hhhh: ", search_type schema_xml = ''' <schema> %s </schema> ''' % tables_str schema = SearchType.create("sthpw/schema") schema.set_value("code", "table") schema.set_value("schema", schema_xml) #div.add( my.get_schema_wdg(schema) ) return section_div
def get_display(self): from pyasm.biz import Project security = Environment.get_security() if not security.check_access("builtin", "side_bar_schema", "allow", default="deny"): return DivWdg() section_div = LabeledHidableWdg(label="Schema Views") section_div.set_attr('spt_class_name', Common.get_full_class_name(self) ) palette = Palette.get() color = palette.color("background3") project_div = RoundedCornerDivWdg(hex_color_code=color,corner_size="10") project_div.set_dimensions( width_str='175px', content_height_str='100px' ) project = Project.get() project_code = project.get_code() project_type = project.get_type() div = DivWdg() section_div.add(project_div) project_div.add(div) # get project type schema schema = Schema.get_by_code(project_code) if schema: div.add( self.get_schema_wdg(schema) ) #if not project_type: # raise SetupException("Project type not found for this [%s]" %project_code) if project_type: schema = Schema.get_predefined_schema(project_type) if schema: div.add( self.get_schema_wdg(schema) ) schema = Schema.get_predefined_schema('config') div.add( self.get_schema_wdg(schema) ) schema = Schema.get_admin_schema() div.add( self.get_schema_wdg(schema) ) return section_div # create a fake schema project = Project.get() db_name = project.get_database() sql = DbContainer.get(db_name) tables = sql.get_tables() tables.sort() tables_str = "\n".join( ['<search_type name="%s"/>'%x for x in tables] ) # look at all of the search objects for mapped tables search = Search("sthpw/search_object") #search.add_where('''"namespace" = 'MMS' or namespace = '{project}' ''') search.add_filter("namespace", 'MMS') search.add_filter("namespace", '{project}') search.add_where("or") search_types = search.get_sobjects() schema_xml = ''' <schema> %s </schema> ''' % tables_str schema = SearchType.create("sthpw/schema") schema.set_value("code", "table") schema.set_value("schema", schema_xml) #div.add( self.get_schema_wdg(schema) ) return section_div