def get_display(self): #project = Project.get() schema = Schema.get() # no hierarchy to prevent all sthpw and parent sTypes search_type_names = schema.get_search_types(hierarchy=False) search = Search('sthpw/search_object') search.add_filters('search_type', search_type_names) search_types = search.get_sobjects() task_search_type = SearchType.get("sthpw/task") search_types.append(task_search_type) values = [ x.get_value("search_type") for x in search_types] labels = [] for x in search_types: label = "%s (%s)" % (x.get_value("title"), x.get_value("search_type")) labels.append(label) sobject = self.get_current_sobject() if not sobject: value = "" else: value = sobject.get_value(self.get_name() ) self.set_option("values", values) self.set_option("labels", labels) self.add_empty_option("-- Select --") if value: self.set_value(value) return super(SearchTypeInputWdg, self).get_display()
def _get_result(my, sobject): # get the parent or relation column = my.get_column() parent_id = sobject.get_value(column) parent = my.parents_dict.get(parent_id) if not parent: return super(ForeignKeyElementWdg,my).get_display() template = my.get_option('template') # if not set, then look at the schema if not template: schema = Schema.get_by_project_code( Project.get_project_code() ) search_type = parent.get_base_search_type() template = schema.get_attr_by_search_type(search_type,'display_template') if template: value = NamingUtil.eval_template(template, sobject, parent=parent) else: # NOTE: put something ... anything as a default columns = parent.get_search_type_obj().get_columns() if not len(columns): value = parent.get_value(columns[0]) else: value = parent.get_value(columns[1]) return value
def _get_result(self, sobject): # get the parent or relation column = self.get_column() parent_id = sobject.get_value(column) parent = self.parents_dict.get(parent_id) if not parent: return super(ForeignKeyElementWdg,self).get_display() template = self.get_option('template') # if not set, then look at the schema if not template: schema = Schema.get_by_project_code( Project.get_project_code() ) search_type = parent.get_base_search_type() template = schema.get_attr_by_search_type(search_type,'display_template') if template: value = NamingUtil.eval_template(template, sobject, parent=parent) else: # NOTE: put something ... anything as a default columns = parent.get_search_type_obj().get_columns() if not len(columns): value = parent.get_value(columns[0]) else: value = parent.get_value(columns[1]) return value
def get_display(my): #project = Project.get() schema = Schema.get() # no hierarchy to prevent all sthpw and parent sTypes search_type_names = schema.get_search_types(hierarchy=False) search = Search('sthpw/search_object') search.add_filters('search_type', search_type_names) search_types = search.get_sobjects() task_search_type = SearchType.get("sthpw/task") search_types.append(task_search_type) values = [x.get_value("search_type") for x in search_types] labels = [] for x in search_types: label = "%s (%s)" % (x.get_value("title"), x.get_value("search_type")) labels.append(label) sobject = my.get_current_sobject() if not sobject: value = "" else: value = sobject.get_value(my.get_name()) my.set_option("values", values) my.set_option("labels", labels) my.add_empty_option("-- Select --") if value: my.set_value(value) return super(SearchTypeInputWdg, my).get_display()
def get_related_types(my, search_type): # find all the relationships schema = Schema.get() related_types = schema.get_related_search_types(search_type) parent_type = schema.get_parent_type(search_type) child_types = schema.get_child_types(search_type) # some special considerations # FIXME: this needs to be more automatic. Should only be # deletable children (however, that will be defined) if search_type in ['sthpw/task', 'sthpw/note', 'sthpw/snapshot']: if "sthpw/project" in related_types: related_types.remove("sthpw/project") if "sthpw/login" in related_types: related_types.remove("sthpw/login") if "config/process" in related_types: related_types.remove("config/process") if parent_type in related_types: related_types.remove(parent_type) related_types.append('sthpw/note') related_types.append('sthpw/task') related_types.append('sthpw/snapshot') related_types.append('sthpw/work_hour') related_types.append('sthpw/pipeline') related_types.append('sthpw/sobject_list') return related_types
def get_related_types(self, search_type): # find all the relationships schema = Schema.get() related_types = schema.get_related_search_types(search_type) parent_type = schema.get_parent_type(search_type) child_types = schema.get_child_types(search_type) # some special considerations # FIXME: this needs to be more automatic. Should only be # deletable children (however, that will be defined) if search_type in ['sthpw/task','sthpw/note', 'sthpw/snapshot']: if "sthpw/project" in related_types: related_types.remove("sthpw/project") if "sthpw/login" in related_types: related_types.remove("sthpw/login") if "config/process" in related_types: related_types.remove("config/process") if parent_type in related_types: related_types.remove(parent_type) related_types.append('sthpw/note') related_types.append('sthpw/task') related_types.append('sthpw/snapshot') related_types.append('sthpw/work_hour') related_types.append('sthpw/pipeline') related_types.append('sthpw/sobject_list') return related_types
def get_hier_sel(my, search_type): sel = SelectWdg(my.RELATED_SEARCH_TYPE, label='Related Search Type: ') sel.add_empty_option() schema = Schema.get() search_type_list = [search_type] if schema: parent_search_type = schema.get_parent_type(search_type) if parent_search_type: search_type_list.append(parent_search_type) child_types = schema.get_child_types(search_type) search_type_list.extend(child_types) sel.set_option('values', search_type_list) sel.set_value(my.related_search_type) return sel
def get_hier_sel(self, search_type): sel = SelectWdg(self.RELATED_SEARCH_TYPE, label='Related Search Type: ') sel.add_empty_option() schema = Schema.get() search_type_list = [search_type] if schema: parent_search_type = schema.get_parent_type(search_type) if parent_search_type: search_type_list.append(parent_search_type) child_types = schema.get_child_types(search_type) search_type_list.extend(child_types) sel.set_option('values', search_type_list) sel.set_value(self.related_search_type) return sel
def init(self): self.schema = Schema.get() if not self.schema: self.parent_type = None self.select = None return web = WebContainer.get_web() self.search_type = web.get_form_value("filter|search_type") if not self.search_type: search_type = self.options.get("search_type") self.parent_type = self.schema.get_parent_type(self.search_type) if not self.parent_type: self.select = None else: self.select = FilterSelectWdg("filter|%s" % self.parent_type)
def init(my): my.schema = Schema.get() if not my.schema: my.parent_type = None my.select = None return web = WebContainer.get_web() my.search_type = web.get_form_value("filter|search_type") if not my.search_type: search_type = my.options.get("search_type") my.parent_type = my.schema.get_parent_type(my.search_type) if not my.parent_type: my.select = None else: my.select = FilterSelectWdg("filter|%s" % my.parent_type)
def init(my): my.schema = Schema.get() if not my.schema: my.parent_type = None my.select = None return web = WebContainer.get_web() my.search_type = web.get_form_value("filter|search_type") if not my.search_type: search_type = my.options.get("search_type") my.parent_type = my.schema.get_parent_type(my.search_type) if not my.parent_type: my.select = None else: my.select = FilterSelectWdg("filter|%s" % my.parent_type)
def _test_relationship(my): search_type = "table/posts?project=mongodb" search_type2 = "table/posts2?project=mongodb" from pyasm.biz import Schema schema = Schema.get() relationship = schema.get_relationship(search_type, search_type2) my.assertEquals(None, relationship) search_type2 = "sthpw/task" relationship = schema.get_relationship(search_type, search_type2) my.assertEquals("search_id", relationship) attrs = schema.get_relationship_attrs(search_type, search_type2) my.assertEquals("*", attrs.get("to") ) my.assertEquals("search_type", attrs.get("relationship") )
def _test_relationship(my): search_type = "table/posts?project=mongodb" search_type2 = "table/posts2?project=mongodb" from pyasm.biz import Schema schema = Schema.get() relationship = schema.get_relationship(search_type, search_type2) my.assertEquals(None, relationship) search_type2 = "sthpw/task" relationship = schema.get_relationship(search_type, search_type2) my.assertEquals("search_id", relationship) attrs = schema.get_relationship_attrs(search_type, search_type2) my.assertEquals("*", attrs.get("to")) my.assertEquals("search_type", attrs.get("relationship"))
def get_display(my): #project = Project.get() schema = Schema.get() # no hierarchy to prevent all sthpw and parent sTypes search_type_names = schema.get_search_types(hierarchy=False) search = Search('sthpw/search_object') search.add_filters('search_type', search_type_names) search_types = search.get_sobjects() task_search_type = SearchType.get("sthpw/task") search_types.append(task_search_type) values = [ x.get_value("search_type") for x in search_types] filtered = [] labels = [] for x in search_types: base_type = x.get_base_key() exists = SearchType.column_exists(base_type, "pipeline_code") if not exists: continue label = "%s (%s)" % (x.get_value("title"), x.get_value("search_type")) labels.append(label) filtered.append(base_type) values = filtered sobject = my.get_current_sobject() if not sobject: value = "" else: value = sobject.get_value(my.get_name() ) my.set_option("values", values) my.set_option("labels", labels) my.add_empty_option("-- Select --") if value: my.set_value(value) return super(SearchTypeWithPipelineInputWdg, my).get_display()
def get_display(self): #project = Project.get() schema = Schema.get() # no hierarchy to prevent all sthpw and parent sTypes search_type_names = schema.get_search_types(hierarchy=False) search = Search('sthpw/search_object') search.add_filters('search_type', search_type_names) search_types = search.get_sobjects() task_search_type = SearchType.get("sthpw/task") search_types.append(task_search_type) values = [x.get_value("search_type") for x in search_types] filtered = [] labels = [] for x in search_types: base_type = x.get_base_key() exists = SearchType.column_exists(base_type, "pipeline_code") if not exists: continue label = "%s (%s)" % (x.get_value("title"), x.get_value("search_type")) labels.append(label) filtered.append(base_type) values = filtered sobject = self.get_current_sobject() if not sobject: value = "" else: value = sobject.get_value(self.get_name()) self.set_option("values", values) self.set_option("labels", labels) self.add_empty_option("-- Select --") if value: self.set_value(value) return super(SearchTypeWithPipelineInputWdg, self).get_display()
def execute(my): # if a single sobject is passed in sobject = my.kwargs.get("sobject") if not sobject: search_key = my.kwargs.get("search_key") sobject = Search.get_by_search_key(search_key) if sobject: sobjects = [sobject] else: search_keys = my.kwargs.get("search_keys") sobjects = Search.get_by_search_keys(search_keys) if not sobjects: return # find all the relationships my.schema = Schema.get() for sobject in sobjects: my.delete_sobject(sobject)
def execute(self): # if a single sobject is passed in sobject = self.kwargs.get("sobject") if not sobject: search_key = self.kwargs.get("search_key") sobject = Search.get_by_search_key(search_key) if sobject: sobjects = [sobject] else: search_keys = self.kwargs.get("search_keys") sobjects = Search.get_by_search_keys(search_keys) if not sobjects: return # find all the relationships self.schema = Schema.get() for sobject in sobjects: self.delete_sobject(sobject)
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): div = DivWdg() search_type = "prod/asset" search_type_obj = SearchType.get(search_type) title = search_type_obj.get_title() title_wdg = DivWdg() title_wdg.add_style("font-size: 1.8em") title_wdg.add("%s" % (title)) div.add(title_wdg) div.add(HtmlElement.hr()) table = Table() div.add(table) table.set_max_width() table.add_row() from pyasm.widget import ThumbWdg, DiscussionWdg, SObjectTaskTableElement td = table.add_cell() td.add_style("width: 250px") td.add_style("vertical-align: top") td.add_style("border-right: solid 1px") title = DivWdg() title.add_class("maq_search_bar") x = DivWdg("[?] [x]") x.add_style("float: right") title.add(x) title.add("Info") td.add(title) thumb = ThumbWdg() thumb.set_sobject(sobject) td.add(thumb) from pyasm.prod.web import AssetInfoWdg info = AssetInfoWdg() info.thumb = thumb info.set_sobject(sobject) td.add(info) td = table.add_cell() td.add_style("vertical-align: top") td.add_style("padding-left: 5px") td.add_style("border-right: solid 1px") # notes title = DivWdg() title.add_class("maq_search_bar") x = DivWdg("[x]") x.add_style("float: right") title.add(x) title.add("Notes") td.add(title) discussion_wdg = DiscussionWdg() discussion_wdg.preprocess() discussion_wdg.set_sobject(sobject) td.add(discussion_wdg) td = table.add_cell() td.add_style("vertical-align: top") td.add_style("padding-left: 5px") td.add_style("border-right: solid 1px") # tasks title = DivWdg() title.add_class("maq_search_bar") x = DivWdg("[x]") x.add_style("float: right") title.add(x) title.add("Custom Properties") td.add(title) search = Search("prod/custom_property") search.add_filter("search_type", "sample3d/book") sobjects = search.get_sobjects() table = TableLayoutWdg(search_type="prod/custom_property", view='table') table.set_sobjects(sobjects) td.add(table) div.add(HtmlElement.hr()) div.add(HtmlElement.br(clear="all")) title_wdg = DivWdg() title_wdg.add_class("maq_search_bar") x = DivWdg("[x]") x.add_style("float: right") title_wdg.add(x) #title_wdg.add_style("font-size: 1.5em") title_wdg.add("Detail") div.add(title_wdg) div.add(HtmlElement.br()) # TEST getting schema search_type = sobject.get_base_search_type() schema = Schema.get_by_sobject(sobject) # add a second table table = Table() table.set_max_width() # show the snapshots for this sobject search_type = "sthpw/snapshot" search = Search(search_type) search.add_sobject_filter(sobject) search.set_limit(50) sobjects = search.get_sobjects() table.add_row() nav_td = table.add_cell() nav_td.add_style("width: 100px") nav_td.add_style("vertical-align: top") #section_wdg = self.get_section_wdg(sobject) #nav_td.add( section_wdg ) #from tactic.ui.panel import ManageViewPanelWdg #defined = ManageViewPanelWdg() from tactic.ui.panel import ManageViewWdg defined = ManageViewWdg() nav_td.add(defined) #content_wdg = TableLayoutWdg(search_type=search_type, view='table') #content_wdg.set_sobjects(sobjects) #content_td = table.add_cell() #content_td.set_id("sobject_relation") #content_td.add_style("display: table-cell") #content_td.add_style("vertical-align: top") #content_td.add(content_wdg) div.add(table) return div
def get_display(my): sobject = my.get_current_sobject() sobj_name = sobject.get_name() search_type = sobject.get_search_type_obj() stype_title = search_type.get_title() from pyasm.biz import Schema schema = Schema.get() related_types = schema.get_related_search_types( search_type.get_base_key()) related_types = ",".join(related_types) name = my.get_name() top = DivWdg() top.add_attr("spt_related_types", related_types) top.add_style("min-width: 250px") if sobject.is_insert(): top.add_style("opacity: 0.3") else: # this gives the swap it's behaviors, so will be disabled # on insert top.add_class("spt_hidden_row_%s" % name) my.set_option("label", "{}%s" % (sobj_name)) label = my.get_option("label") if label: label = Search.eval(label, sobject) else: label = None if not label: label = sobject.get_code() icon = my.get_option("icon") icon = "DETAILS" #from pyasm.widget import ThumbWdg #thumb_div = DivWdg() #thumb = ThumbWdg() #thumb_div.add(thumb) #thumb.set_sobject(search_type) #thumb.set_option("icon_size", "16") #thumb_div.add_style("float: left") #thumb_div.add_style("width: 22px") title = DivWdg() #title.add(thumb_div) title.add(label) stype_div = SpanWdg(" (%s)" % stype_title) title.add(stype_div) stype_div.add_style("opacity: 0.3") stype_div.add_style("font-syle: italic") stype_div.add_style("font-size: 9px") #swap = SwapDisplayWdg(title=label, icon=icon) swap = SwapDisplayWdg(title=title, icon=icon) swap.set_behavior_top(my.layout) swap.add_style("float: left") swap.add_class("spt_level_listen") top.add(swap) return top
def get_display(my): top = my.top my.set_as_panel(top) top.add_class("spt_delete_top") top.add_color("background", "background") top.add_color("color", "color") top.add_border() top.add_style("width: 300px") top.add_border() search_key = my.kwargs.get("search_key") search_keys = my.kwargs.get("search_keys") if search_key: sobject = Search.get_by_search_key(search_key) sobjects = [sobject] search_keys = [search_key] elif search_keys: sobjects = Search.get_by_search_keys(search_keys) sobject = sobjects[0] if not sobjects: msg = "%s not found" %search_key return msg search_type = sobject.get_base_search_type() if search_type in ['sthpw/project', 'sthpw/search_object']: msg = 'You cannot delete these items with this tool' return msg my.search_keys = search_keys title = DivWdg() top.add(title) title.add_color("background", "background", -10) if my.search_keys: title.add("Delete %s Items" % len(my.search_keys)) else: title.add("Delete Item [%s]" % (sobject.get_code())) title.add_style("font-size: 14px") title.add_style("font-weight: bold") title.add_style("padding: 10px") content = DivWdg() top.add(content) content.add_style("padding: 10px") content.add("The item to be deleted has a number of dependencies as described below:<br/>", 'heading') # find all the relationships schema = Schema.get() related_types = schema.get_related_search_types(search_type, direction="children") parent_type = schema.get_parent_type(search_type) child_types = schema.get_child_types(search_type) # some special considerations # FIXME: this needs to be more automatic. Should only be # deletable children (however, that will be defined) if search_type in ['sthpw/task','sthpw/note', 'sthpw/snapshot']: if "sthpw/project" in related_types: related_types.remove("sthpw/project") if "sthpw/login" in related_types: related_types.remove("sthpw/login") if "config/process" in related_types: related_types.remove("config/process") if parent_type in related_types: related_types.remove(parent_type) related_types.append('sthpw/note') related_types.append('sthpw/task') related_types.append('sthpw/snapshot') if 'sthpw/work_hour' not in related_types: related_types.append('sthpw/work_hour') items_div = DivWdg() content.add( items_div ) items_div.add_style("padding: 10px") valid_related_ctr = 0 for related_type in related_types: if related_type == "*": print "WARNING: related_type is *" continue if related_type == search_type: continue if related_type in ['sthpw/search_object','sthpw/search_type']: continue item_div = my.get_item_div(sobjects, related_type) if item_div: items_div.add(item_div) valid_related_ctr += 1 if valid_related_ctr > 0: icon = IconWdg("WARNING", IconWdg.WARNING) icon.add_style("float: left") content.add( icon ) content.add("<div><b>WARNING: By selecting the related items above, you can delete them as well when deleting this sObject.</b></div>") content.add("<br/>"*2) else: # changed the heading to say no dependencies content.add("The item to be deleted has no dependencies.<br/>", 'heading') content.add("There are %s items to be deleted" % len(my.search_keys)) content.add("<br/>"*2) content.add("Do you wish to continue deleting?") content.add("<br/>"*2) button_div = DivWdg() button_div.add_styles('width: 300px; height: 50px') button = ActionButtonWdg(title="Delete") button_div.add(button) content.add(button_div) button.add_style("float: left") button.add_behavior( { 'type': 'click_up', 'search_keys': my.search_keys, 'cbjs_action': ''' spt.app_busy.show("Deleting"); var top = bvr.src_el.getParent(".spt_delete_top"); var values = spt.api.Utility.get_input_values(top); var class_name = "tactic.ui.tools.DeleteCmd"; var kwargs = { 'search_keys': bvr.search_keys, 'values': values }; var server = TacticServerStub.get(); try { server.start({'title': 'Delete sObject', 'description': 'Delete sObject [' + bvr.search_keys + ']'}); server.execute_cmd(class_name, kwargs); server.finish(); // run the post delete and destroy the popup var popup = bvr.src_el.getParent(".spt_popup"); if (popup.spt_on_post_delete) { popup.spt_on_post_delete(); } spt.popup.destroy(popup); } catch(e) { spt.alert(spt.exception.handler(e)); } spt.app_busy.hide(); ''' } ) button = ActionButtonWdg(title="Cancel") button.add_style("float: left") button_div.add(button) button.add_behavior( { 'type': 'click_up', 'cbjs_action': ''' var top = bvr.src_el.getParent(".spt_popup"); top.destroy(); ''' } ) content.add("<br clear='all'/>") return top
def get_by_sobjects(sobjects, process=None, order=True): if not sobjects: return [] # quickly go through the sobjects to determine if their search types # are the same multi_stypes = False for sobject in sobjects: if sobject.get_search_type() != sobjects[0].get_search_type(): multi_stypes = True break search = Search( Task.SEARCH_TYPE ) if multi_stypes: # sort this into a dictionary and make multiple calls to # search.add_relationship_filters # use the first sobject as a sample sobjects_dict = {} for sobject in sobjects: st = sobject.get_search_type() sobj_list = sobjects_dict.get(st) if sobj_list == None: sobjects_dict[st] = [sobject] else: sobj_list.append(sobject) search.add_op('begin') for key, sobj_list in sobjects_dict.items(): search.add_op('begin') search.add_relationship_filters(sobj_list) search.add_op('and') search.add_op('or') else: from pyasm.biz import Schema schema = Schema.get() # FIXME: why doesn't the ops work here? filters = [] search.add_relationship_filters(sobjects) """ for sobject in sobjects: search_type = sobject.get_search_type() attrs = schema.get_relationship_attrs("sthpw/task", search_type) attrs = schema.resolve_relationship_attrs(attrs, "sthpw/task", search_type) search_code = sobject.get_value(attrs.get("to_col")) #search_code = sobject.get_value("code") #search.add_filter('search_type', search_type) #search.add_filter('search_id', search_id, quoted=False) #search.add_op("and") if attrs.get("from_col") == "search_code": filters.append("search_type = '%s' and search_code = '%s'" % (search_type, search_code)) else: filters.append("search_type = '%s' and search_id = %s" % (search_type, search_code)) search.add_where(" or ".join(filters)) """ search.add_order_by("search_type") search.add_order_by("search_code") search.add_order_by("search_id") # get the pipeline of the sobject pipeline = Pipeline.get_by_sobject(sobject) if order: if pipeline: process_names = pipeline.get_process_names(True) search.add_enum_order_by("process", process_names) else: search.add_order_by("process") search.add_order_by("id") if process: if isinstance(process, basestring): search.add_filter("process", process) else: search.add_filters("process", process) tasks = search.get_sobjects() return tasks
def _get_predefined_url(cls, key, hash): # only allow people with site admin security = Environment.get_security() is_admin = security.is_admin() if not is_admin and key == "admin": return None # make some predefined fake urls if key in ["link", "tab", "admin"]: # this is called by PageNav if key == "admin": expression = "/admin/link/{link}" else: expression = "/%s/{link}" % key options = Common.extract_dict(hash, expression) link = options.get("link") if not link: return None from tactic.ui.panel import SideBarBookmarkMenuWdg personal = False if '.' in link: # put in a check to ensure this is a user parts = link.split(".") user = Environment.get_user_name() def is_personal(user, parts): '''See if parts contains period seperated form of username.''' acc = "" for part in parts: if acc == "": acc = part else: acc = "%s.%s" % (acc, part) if user == acc: return True return False personal = is_personal(user, parts) # test link security project_code = Project.get_project_code() security = Environment.get_security() keys = [ { "element": link }, { "element": "*" }, { "element": link, "project": project_code }, { "element": "*", "project": project_code } ] if not personal and not security.check_access("link", keys, "allow", default="deny"): print "Not allowed" return None # This is used to find a sub menu (?) #view = link view = "definition" config = SideBarBookmarkMenuWdg.get_config("SideBarWdg", view, personal=personal) view = config.get_element_attribute(link, 'view') if view: options['widget_key'] = 'custom_layout' options['view'] = view class_name = None else: options = config.get_display_options(link) class_name = config.get_display_handler(link) if not options: from pyasm.biz import Schema config_xml = [] config_xml.append( ''' <config> ''') config_schema = Schema.get_predefined_schema('config') SideBarBookmarkMenuWdg.get_schema_snippet("_config_schema", config_schema, config_xml) schema = Schema.get_admin_schema() SideBarBookmarkMenuWdg.get_schema_snippet("_admin_schema", schema, config_xml) config_xml.append( ''' </config> ''') xml = "".join(config_xml) from pyasm.widget import WidgetConfig schema_config = WidgetConfig.get(view="_admin_schema", xml=xml) options = schema_config.get_display_options(link) if not options: schema_config.set_view("_config_schema") options = schema_config.get_display_options(link) if not options: return None if not class_name or class_name == "LinkWdg": class_name = options.get("class_name") widget_key = options.get("widget_key") if widget_key: class_name = WidgetClassHandler().get_display_handler(widget_key) elif not class_name: class_name = 'tactic.ui.panel.ViewPanelWdg' if key in ["admin", "tab"]: use_index = "false" else: use_index = "true" if key in ['admin']: use_admin = "true" else: use_admin = "false" xml = [] xml.append('''<element admin="%s" index="%s">''' % (use_admin, use_index)) xml.append(''' <display class="%s">''' % class_name) for name, value in options.items(): xml.append("<%s>%s</%s>" % (name, value, name) ) xml.append(''' </display>''') xml.append('''</element>''') xml = "\n".join(xml) sobject = SearchType.create("config/url") sobject.set_value("url", "/%s/{value}" % key) sobject.set_value("widget", xml ) return sobject elif key == "rest": xml = '''<element widget='true'> <display class='tactic.protocol.APIRestHandler'> </display> </element>''' sobject = SearchType.create("config/url") sobject.set_value("url", "/rest") sobject.set_value("widget", xml ) return sobject else: return None
def import_manifest(my, nodes): paths_read = [] for node in nodes: node_name = my.xml.get_node_name(node) if node_name == 'search_type': search_type = my.xml.get_attribute(node, 'code') # implicitly add the entry to the schema table. # Reset the cache every time to ensure that any updates to # the scehma are reflected here. schema = Schema.get(reset_cache=True) xml = schema.get_xml() schema_node = xml.get_node("schema/search_type[@name='%s']" % search_type) parent = xml.get_node("schema") if schema_node == None: schema_node = xml.create_element("search_type") xml.set_attribute(schema_node, "name", search_type) #parent = xml.get_parent(node) xml.append_child(parent, schema_node) schema.set_value('schema', xml.to_string() ) schema.commit() # TODO: connections? path = my.xml.get_attribute(node, "path") if not path: path = "%s.spt" % search_type.replace("/", "_") path = "%s/%s" % (my.plugin_dir, path) if path in paths_read: continue if my.verbose: print "Reading search_type: ", path # NOTE: priviledged knowledge of the order or return values jobs = my.import_data(path, commit=True) paths_read.append(path) if not jobs: continue search_type_obj = jobs[0] if len(jobs) == 1: # only the search type was defined table = None else: table = jobs[1] try: # check to see if the search type already exists search_type_chk = SearchType.get(search_type) if search_type_chk: if my.verbose: print 'WARNING: Search Type [%s] is already registered' % search_type_chk.get_value("search_type") else: search_type_obj.commit() except SearchException, e: if e.__str__().find('not registered') != -1: search_type_obj.commit() # check if table exists has_table = False if has_table: if my.verbose: print 'WARNING: Table [%s] already exists' elif table: #print table.get_statement() if table: database = table.get_database() table_name = table.get_table() TableUndo.log(search_type, database, table_name) elif node_name == 'sobject': path = my.xml.get_attribute(node, "path") search_type = my.xml.get_attribute(node, "search_type") seq_max = my.xml.get_attribute(node, "seq_max") try: if seq_max: seq_max = int(seq_max) except ValueError: seq_max = 0 if not path: if search_type: path = "%s.spt" % search_type.replace("/","_") if not path: raise TacticException("No path specified") path = "%s/%s" % (my.plugin_dir, path) if path in paths_read: continue unique = my.xml.get_attribute(node, "unique") if unique == 'true': unique = True else: unique = False if my.verbose: print "Reading: ", path # jobs doesn't matter for sobject node jobs = my.import_data(path, unique=unique) # compare sequence st_obj = SearchType.get(search_type) SearchType.sequence_nextval(search_type) cur_seq_id = SearchType.sequence_currval(search_type) sql = DbContainer.get("sthpw") if seq_max > 0 and seq_max > cur_seq_id: # TODO: SQL Server - Reseed the sequences instead of passing. if sql.get_database_type() == 'SQLServer': pass else: SearchType.sequence_setval(search_type, seq_max) else: cur_seq_id -= 1 # TODO: SQL Server - Reseed the sequences instead of passing. if sql.get_database_type() == 'SQLServer': pass else: # this is a db requirement if cur_seq_id > 0: SearchType.sequence_setval(search_type, cur_seq_id) paths_read.append(path)
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
def get_display(my): sobject = my.get_current_sobject() sobj_name = sobject.get_name() search_type = sobject.get_search_type_obj() stype_title = search_type.get_title() from pyasm.biz import Schema schema = Schema.get() related_types = schema.get_related_search_types(search_type.get_base_key()) related_types = ",".join(related_types) name = my.get_name() top = DivWdg() top.add_attr("spt_related_types", related_types) top.add_style("min-width: 250px") if sobject.is_insert(): top.add_style("opacity: 0.3") else: # this gives the swap it's behaviors, so will be disabled # on insert top.add_class("spt_hidden_row_%s" % name) my.set_option("label", "{}%s" % (sobj_name)) label = my.get_option("label") if label: label = Search.eval(label, sobject) else: label = None if not label: label = sobject.get_code() icon = my.get_option("icon") icon = "DETAILS" #from pyasm.widget import ThumbWdg #thumb_div = DivWdg() #thumb = ThumbWdg() #thumb_div.add(thumb) #thumb.set_sobject(search_type) #thumb.set_option("icon_size", "16") #thumb_div.add_style("float: left") #thumb_div.add_style("width: 22px") title = DivWdg() #title.add(thumb_div) title.add(label) stype_div = SpanWdg(" (%s)" % stype_title) title.add(stype_div) stype_div.add_style("opacity: 0.3") stype_div.add_style("font-syle: italic") stype_div.add_style("font-size: 9px") #swap = SwapDisplayWdg(title=label, icon=icon) swap = SwapDisplayWdg(title=title, icon=icon) swap.set_behavior_top(my.layout) swap.add_style("float: left") swap.add_class("spt_level_listen") top.add(swap) return top
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 init(my): my.is_refresh = my.kwargs.get("refresh") my.search_key = my.kwargs.get("search_key") my.ticket_key = my.kwargs.get("ticket") my.parent_key = my.kwargs.get("parent_key") my.expression = my.kwargs.get("expression") # This assumed parent can cause errors as it tries to find a # relationship between to stypes that don't exist ... or worse, # try to bind them when one stype does not have the sufficent columns # ie: pipeline_code #if not my.parent_key: # project = Project.get() # my.parent_key = project.get_search_key() my.code = my.kwargs.get("code") sobject = None if my.search_key: sobject = Search.get_by_search_key(my.search_key) my.search_id = sobject.get_id() my.search_type = sobject.get_base_search_type() if sobject.is_insert(): my.mode = 'insert' else: my.mode = 'edit' elif my.expression: sobject = Search.eval(my.expression, single=True) my.search_id = sobject.get_id() my.search_type = sobject.get_base_search_type() my.mode = 'edit' elif my.ticket_key: from pyasm.security import Ticket, Login ticket = Ticket.get_by_valid_key(my.ticket_key) if not ticket: raise TacticException("No valid ticket") login_code = ticket.get_value("login") login = Login.get_by_code(login_code) my.search_type = "sthpw/login" my.search_id = login.get_id() my.mode = 'edit' elif my.code: my.search_type = my.kwargs.get("search_type") search = Search(my.search_type) search.add_filter("code", my.code) sobject = search.get_sobject() my.search_id = sobject.get_id() my.search_type = sobject.get_base_search_type() my.mode = 'edit' else: my.search_type = my.kwargs.get("search_type") my.search_id = my.kwargs.get("search_id") if not my.search_id: my.search_id = -1 my.search_id = int(my.search_id) if my.search_id != -1: my.mode = "edit" else: my.mode = "insert" # explicit override if my.kwargs.get("mode"): my.mode = my.kwargs.get("mode") my.view = my.kwargs.get("view") if not my.view: my.view = my.kwargs.get("config_base") if not my.view: my.view = "edit" default_data = my.kwargs.get('default') if not default_data: default_data = {} elif isinstance(default_data, basestring): try: default_data = jsonloads(default_data) except: #may be it's regular dictionary try: default_data = eval(default_data) except: print "Warning: Cannot evaluate [%s]" % default_data default_data = {} if sobject: my.set_sobjects([sobject], None) else: my.do_search() # TODO: get_config() is going the right direction (less features) but the more complicated method is biased # towards edit and insert view.. and so it needs improvement as well if my.view not in ["insert", "edit"]: # try a new smaller way to get config only when an explicit view # is set my.config = my.get_config() else: my.config = WidgetConfigView.get_by_search_type(my.search_type, my.view, use_cache=False) # for inline config definitions config_xml = my.kwargs.get("config_xml") if config_xml: #from pyasm.common import Xml #xml = Xml() #xml.read_string(config_xml) #node = xml.get_node("config/%s" % my.view) #xml.set_attribute(node, "class", "tactic.ui.panel.EditWdg") #config = WidgetConfig.get(view=my.view, xml=xml) config_xml = config_xml.replace("&", "&") config = WidgetConfig.get(view="tab", xml=config_xml) my.config.insert_config(0, config) my.skipped_element_names = [] # if there is a layout view, then find the element names using that layout_view = my.kwargs.get("layout_view") if layout_view: layout_view = layout_view.replace("/", ".") search = Search("config/widget_config") search.add_filter("view", layout_view) layout_config = search.get_sobject() xml = layout_config.get_xml_value("config") my.element_names = xml.get_values("config//html//element/@name") else: my.element_names = my.config.get_element_names() override_element_names = my.kwargs.get("element_names") if override_element_names: element_names = override_element_names ignore = my.kwargs.get("ignore") if isinstance(ignore, basestring): ignore = ignore.split("|") if not ignore: ignore = [] my.element_titles = [] my.element_descriptions = [] for element_name in my.element_names: my.element_titles.append(my.config.get_element_title(element_name)) my.element_descriptions.append( my.config.get_element_description(element_name)) #my.element_titles = my.config.get_element_titles() #my.element_descriptions = my.config.get_element_descriptions() # MongoDb # Default columns if not my.element_names: impl = SearchType.get_database_impl_by_search_type(my.search_type) if impl.get_database_type() == "MongoDb": my.element_names = impl.get_default_columns() my.element_titles = ['Code', 'Name', 'Description'] my.element_descriptions = ['Code', 'Name', 'Description'] my.input_prefix = my.kwargs.get('input_prefix') if not my.input_prefix: my.input_prefix = 'edit' security = Environment.get_security() default_access = "edit" project_code = Project.get_project_code() if my.parent_key: from pyasm.biz import Schema schema = Schema.get() parent_stype = SearchKey.extract_base_search_type(my.parent_key) relationship = schema.get_relationship_attrs(parent_stype, my.search_type, type="hierarchy") for element_name in my.element_names: # If parent_key is available, data associated with the parent table does not need # to be specified by the user, and their widgets can be excluded from the edit widget if element_name == relationship.get("from_col"): ignore.append(element_name) for i, element_name in enumerate(my.element_names): if element_name in ignore: my.skipped_element_names.append(element_name) continue # check security access access_key2 = { 'search_type': my.search_type, 'project': project_code } access_key1 = { 'search_type': my.search_type, 'key': element_name, 'project': project_code } access_keys = [access_key1, access_key2] is_editable = security.check_access('element', access_keys, "edit", default=default_access) if not is_editable: my.skipped_element_names.append(element_name) continue widget = my.config.get_display_widget(element_name, kbd_handler=False) # some element could be disabled due to its data_type e.g. sql_timestamp if not widget: my.skipped_element_names.append(element_name) continue widget.set_sobject(my.sobjects[0]) default_value = default_data.get(element_name) if default_value: widget.set_value(default_value) attrs = my.config.get_element_attributes(element_name) editable = widget.is_editable() if editable: editable = attrs.get("edit") editable = editable != "false" if not editable: my.skipped_element_names.append(element_name) continue # set parent widget.set_parent_wdg(my) # set parent_key in insert mode for now if my.mode == 'insert' and my.parent_key: widget.set_option('parent_key', my.parent_key) title = my.element_titles[i] if title: widget.set_title(title) my.widgets.append(widget) description = my.element_descriptions[i] widget.add_attr("title", description)
def import_manifest(my, nodes): paths_read = [] for node in nodes: node_name = my.xml.get_node_name(node) if node_name == 'search_type': search_type = my.xml.get_attribute(node, 'code') # implicitly add the entry to the schema table. # Reset the cache every time to ensure that any updates to # the scehma are reflected here. schema = Schema.get(reset_cache=True) xml = schema.get_xml() schema_node = xml.get_node("schema/search_type[@name='%s']" % search_type) parent = xml.get_node("schema") if schema_node == None: schema_node = xml.create_element("search_type") xml.set_attribute(schema_node, "name", search_type) #parent = xml.get_parent(node) xml.append_child(parent, schema_node) schema.set_value('schema', xml.to_string() ) schema.commit() # TODO: connections? path = my.xml.get_attribute(node, "path") if not path: path = "%s.spt" % search_type.replace("/", "_") path = "%s/%s" % (my.plugin_dir, path) if path in paths_read: continue if my.verbose: print "Reading search_type: ", path # NOTE: priviledged knowledge of the order or return values jobs = my.import_data(path, commit=True) paths_read.append(path) if not jobs: continue search_type_obj = jobs[0] if len(jobs) == 1: # only the search type was defined table = None else: table = jobs[1] try: # check to see if the search type already exists search_type_chk = SearchType.get(search_type) if search_type_chk: if my.verbose: print 'WARNING: Search Type [%s] is already registered' % search_type_chk.get_value("search_type") else: search_type_obj.commit() except SearchException, e: if e.__str__().find('not registered') != -1: search_type_obj.commit() # check if table exists has_table = False if has_table: if my.verbose: print 'WARNING: Table [%s] already exists' elif table: #print table.get_statement() if table: database = table.get_database() table_name = table.get_table() TableUndo.log(search_type, database, table_name) elif node_name == 'sobject': path = my.xml.get_attribute(node, "path") search_type = my.xml.get_attribute(node, "search_type") seq_max = my.xml.get_attribute(node, "seq_max") try: if seq_max: seq_max = int(seq_max) except ValueError: seq_max = 0 if not path: if search_type: path = "%s.spt" % search_type.replace("/","_") if not path: raise TacticException("No path specified") path = "%s/%s" % (my.plugin_dir, path) if path in paths_read: continue unique = my.xml.get_attribute(node, "unique") if unique == 'true': unique = True else: unique = False if my.verbose: print "Reading: ", path # jobs doesn't matter for sobject node jobs = my.import_data(path, unique=unique) # reset it in case it needs to execute a PYTHON tag right after Schema.get(reset_cache=True) # compare sequence st_obj = SearchType.get(search_type) SearchType.sequence_nextval(search_type) cur_seq_id = SearchType.sequence_currval(search_type) sql = DbContainer.get("sthpw") if seq_max > 0 and seq_max > cur_seq_id: # TODO: SQL Server - Reseed the sequences instead of passing. if sql.get_database_type() == 'SQLServer': pass else: SearchType.sequence_setval(search_type, seq_max) else: cur_seq_id -= 1 # TODO: SQL Server - Reseed the sequences instead of passing. if sql.get_database_type() == 'SQLServer': pass else: # this is a db requirement if cur_seq_id > 0: SearchType.sequence_setval(search_type, cur_seq_id) paths_read.append(path)
def _get_predefined_url(cls, key, hash): # make some predefined fake urls if key in ["link", "tab", "admin"]: # this is called by PageNav if key == "admin": expression = "/admin/link/{link}" else: expression = "/%s/{link}" % key options = Common.extract_dict(hash, expression) link = options.get("link") if not link: return None # test link security project_code = Project.get_project_code() security = Environment.get_security() keys = [ { "element": link }, { "element": "*" }, { "element": link, "project": project_code }, { "element": "*", "project": project_code } ] if not security.check_access("link", keys, "allow", default="deny"): return None from tactic.ui.panel import SideBarBookmarkMenuWdg personal = False if '.' in link: personal = True config = SideBarBookmarkMenuWdg.get_config("SideBarWdg", link, personal=personal) options = config.get_display_options(link) if not options: from pyasm.biz import Schema config_xml = [] config_xml.append( ''' <config> ''') config_schema = Schema.get_predefined_schema('config') SideBarBookmarkMenuWdg.get_schema_snippet("_config_schema", config_schema, config_xml) schema = Schema.get_admin_schema() SideBarBookmarkMenuWdg.get_schema_snippet("_admin_schema", schema, config_xml) config_xml.append( ''' </config> ''') xml = "".join(config_xml) from pyasm.widget import WidgetConfig schema_config = WidgetConfig.get(view="_admin_schema", xml=xml) options = schema_config.get_display_options(link) if not options: schema_config.set_view("_config_schema") options = schema_config.get_display_options(link) if not options: return None class_name = options.get("class_name") widget_key = options.get("widget_key") if widget_key: class_name = WidgetClassHandler().get_display_handler(widget_key) elif not class_name: class_name = 'tactic.ui.panel.ViewPanelWdg' if key in ["admin", "tab"]: use_index = "false" else: use_index = "true" if key in ['admin']: use_admin = "true" else: use_admin = "false" xml = [] xml.append('''<element admin="%s" index="%s">''' % (use_admin, use_index)) xml.append(''' <display class="%s">''' % class_name) for name, value in options.items(): xml.append("<%s>%s</%s>" % (name, value, name) ) xml.append(''' </display>''') xml.append('''</element>''') xml = "\n".join(xml) sobject = SearchType.create("config/url") sobject.set_value("url", "/%s/{value}" % key) sobject.set_value("widget", xml ) return sobject else: return None
class PluginUninstaller(PluginBase): # NOTE: this is still a work in progress. It will remove entries added # by the plugin, but it is not clear that this is what we want to do. def execute(my): # uninstall the plugin nodes = my.xml.get_nodes("manifest/*") nodes.reverse() my.handle_nodes(nodes) my.add_description('Remove plugin [%s]' %my.code) def handle_nodes(my, nodes): for node in nodes: node_name = my.xml.get_node_name(node) if node_name == 'search_type': my.remove_search_type(node) elif node_name == 'sobject': my.remove_sobjects(node) elif node_name == 'include': my.handle_include(node) elif node_name == 'python': my.handle_python(node) # remove plugin contents search = Search("config/plugin_content") search.add_filter("plugin_code", my.code) plugin_contents = search.get_sobjects() for plugin_content in plugin_contents: plugin_content.delete() # deregister the plugin plugin = Search.eval("@SOBJECT(config/plugin['code','%s'])" % my.code, single=True) if plugin: plugin.delete() def remove_search_type(my, node): search_type = my.xml.get_attribute(node, 'code') # get sobject entry search = Search("sthpw/search_object") search.add_filter("search_type", search_type) search_type_sobj = search.get_sobject() if not search_type_sobj: print "WARNING: Search type [%s] does not exist" % search_type else: # dump the table first ??? # get the table and remove it ??? from pyasm.search import DropTable try: table_drop = DropTable(search_type) table_drop.commit() # NOTE: do we need to log the undo for this? except Exception, e: print "Error: ", e.message # NOTE: it is not clear that unloading a plugin should delete # the search type ... this search type (at present) can be # shared amongst multiple projects and this will break it return # remove entry from schema schema = Schema.get() xml = schema.get_xml() node = xml.get_node("schema/search_type[@name='%s']" % search_type) if node != None: parent = xml.get_parent(node) xml.remove_child(parent, node) schema.set_value('schema', xml.to_string() ) schema.commit() # remove search type entry if search_type.startswith("config/") or search_type.startswith("sthpw/"): print "WARNING: A plugin cannot deregister a search type from the 'sthpw' or 'config' namespace'" else: search = Search("sthpw/search_object") search.add_filter("search_type", search_type) search_type_sobj = search.get_sobject() if search_type_sobj: search_type_sobj.delete()
def _get_predefined_url(cls, key, hash): # make some predefined fake urls if key in ["link", "tab", "admin"]: # this is called by PageNav if key == "admin": expression = "/admin/link/{link}" else: expression = "/%s/{link}" % key options = Common.extract_dict(hash, expression) link = options.get("link") if not link: return None from tactic.ui.panel import SideBarBookmarkMenuWdg personal = False if "." in link: # put in a check to ensure this is a user parts = link.split(".") user = Environment.get_user_name() def is_personal(user, parts): """See if parts contains period seperated form of username.""" acc = "" for part in parts: if acc == "": acc = part else: acc = "%s.%s" % (acc, part) if user == acc: return True return False personal = is_personal(user, parts) # test link security project_code = Project.get_project_code() security = Environment.get_security() keys = [ {"element": link}, {"element": "*"}, {"element": link, "project": project_code}, {"element": "*", "project": project_code}, ] if not personal and not security.check_access("link", keys, "allow", default="deny"): print "Not allowed" return None config = SideBarBookmarkMenuWdg.get_config("SideBarWdg", link, personal=personal) options = config.get_display_options(link) if not options: from pyasm.biz import Schema config_xml = [] config_xml.append( """ <config> """ ) config_schema = Schema.get_predefined_schema("config") SideBarBookmarkMenuWdg.get_schema_snippet("_config_schema", config_schema, config_xml) schema = Schema.get_admin_schema() SideBarBookmarkMenuWdg.get_schema_snippet("_admin_schema", schema, config_xml) config_xml.append( """ </config> """ ) xml = "".join(config_xml) from pyasm.widget import WidgetConfig schema_config = WidgetConfig.get(view="_admin_schema", xml=xml) options = schema_config.get_display_options(link) if not options: schema_config.set_view("_config_schema") options = schema_config.get_display_options(link) if not options: return None class_name = options.get("class_name") widget_key = options.get("widget_key") if widget_key: class_name = WidgetClassHandler().get_display_handler(widget_key) elif not class_name: class_name = "tactic.ui.panel.ViewPanelWdg" if key in ["admin", "tab"]: use_index = "false" else: use_index = "true" if key in ["admin"]: use_admin = "true" else: use_admin = "false" xml = [] xml.append("""<element admin="%s" index="%s">""" % (use_admin, use_index)) xml.append(""" <display class="%s">""" % class_name) for name, value in options.items(): xml.append("<%s>%s</%s>" % (name, value, name)) xml.append(""" </display>""") xml.append("""</element>""") xml = "\n".join(xml) sobject = SearchType.create("config/url") sobject.set_value("url", "/%s/{value}" % key) sobject.set_value("widget", xml) return sobject else: return None
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
def get_display(my): div = DivWdg() search_type = "prod/asset" search_type_obj = SearchType.get(search_type) title = search_type_obj.get_title() title_wdg = DivWdg() title_wdg.add_style("font-size: 1.8em") title_wdg.add("%s" % (title) ) div.add(title_wdg) div.add(HtmlElement.hr()) table = Table() div.add(table) table.set_max_width() table.add_row() from pyasm.widget import ThumbWdg, DiscussionWdg, SObjectTaskTableElement td = table.add_cell() td.add_style("width: 250px") td.add_style("vertical-align: top") td.add_style("border-right: solid 1px") title = DivWdg() title.add_class("maq_search_bar") x = DivWdg("[?] [x]") x.add_style("float: right") title.add(x) title.add("Info") td.add(title) thumb = ThumbWdg() thumb.set_sobject(sobject) td.add(thumb) from pyasm.prod.web import AssetInfoWdg info = AssetInfoWdg() info.thumb = thumb info.set_sobject(sobject) td.add(info) td = table.add_cell() td.add_style("vertical-align: top") td.add_style("padding-left: 5px") td.add_style("border-right: solid 1px") # notes title = DivWdg() title.add_class("maq_search_bar") x = DivWdg("[x]") x.add_style("float: right") title.add(x) title.add("Notes") td.add(title) discussion_wdg = DiscussionWdg() discussion_wdg.preprocess() discussion_wdg.set_sobject(sobject) td.add(discussion_wdg) td = table.add_cell() td.add_style("vertical-align: top") td.add_style("padding-left: 5px") td.add_style("border-right: solid 1px") # tasks title = DivWdg() title.add_class("maq_search_bar") x = DivWdg("[x]") x.add_style("float: right") title.add(x) title.add("Custom Properties") td.add(title) search = Search("prod/custom_property") search.add_filter("search_type", "sample3d/book") sobjects = search.get_sobjects() table = TableLayoutWdg(search_type="prod/custom_property", view='table') table.set_sobjects(sobjects) td.add(table) div.add(HtmlElement.hr()) div.add(HtmlElement.br(clear="all")) title_wdg = DivWdg() title_wdg.add_class("maq_search_bar") x = DivWdg("[x]") x.add_style("float: right") title_wdg.add(x) #title_wdg.add_style("font-size: 1.5em") title_wdg.add("Detail" ) div.add(title_wdg) div.add(HtmlElement.br()) # TEST getting schema search_type = sobject.get_base_search_type() schema = Schema.get_by_sobject(sobject) # add a second table table = Table() table.set_max_width() # show the snapshots for this sobject search_type = "sthpw/snapshot" search = Search(search_type) search.add_sobject_filter(sobject) search.set_limit(50) sobjects = search.get_sobjects() table.add_row() nav_td = table.add_cell() nav_td.add_style("width: 100px") nav_td.add_style("vertical-align: top") #section_wdg = my.get_section_wdg(sobject) #nav_td.add( section_wdg ) #from tactic.ui.panel import ManageViewPanelWdg #defined = ManageViewPanelWdg() from tactic.ui.panel import ManageViewWdg defined = ManageViewWdg() nav_td.add(defined) #content_wdg = TableLayoutWdg(search_type=search_type, view='table') #content_wdg.set_sobjects(sobjects) #content_td = table.add_cell() #content_td.set_id("sobject_relation") #content_td.add_style("display: table-cell") #content_td.add_style("vertical-align: top") #content_td.add(content_wdg) div.add(table) return div
def get_display(my): div = DivWdg() sobject = my.get_sobject_from_kwargs() if not sobject: div.add("SObject not found") return div search_type_obj = sobject.get_search_type_obj() title = search_type_obj.get_title() title_wdg = DivWdg() title_wdg.add_style("font-size: 1.8em") title_wdg.add("%s: %s" % (title, sobject.get_code() ) ) div.add(title_wdg) div.add(HtmlElement.hr()) table = Table() table.set_max_width() col1 = table.add_col() col1.add_style('width: 200px') col2 = table.add_col() col2.add_style('width: 320px') col3 = table.add_col() col3.add_style('width: 400px') table.add_row() from pyasm.widget import ThumbWdg, DiscussionWdg, SObjectTaskTableElement td = table.add_cell() td.add_style("vertical-align: top") td.add_style("border-right: solid 1px") title = DivWdg() title.add_class("maq_search_bar") #x = DivWdg("[?] [x]") #x.add_style("float: right") #title.add(x) title.add("Info") td.add(title) thumb = ThumbWdg() thumb.set_sobject(sobject) thumb.set_option("detail", "false") td.add(thumb) from pyasm.prod.web import AssetInfoWdg info = AssetInfoWdg() info.thumb = thumb info.set_sobject(sobject) td.add(info) # tasks td = table.add_cell() td.add_style("vertical-align: top") td.add_style("padding-left: 5px") td.add_style("border-right: solid 1px") title = DivWdg() title.add_class("maq_search_bar") #x = DivWdg("[x]") #x.add_style("float: right") #title.add(x) title.add("Tasks") td.add(title) task_wdg = SObjectTaskTableElement() task_wdg.set_sobject(sobject) td.add(task_wdg) td.add_style('cell-padding','10') # discussion td = table.add_cell() #td.add_style("min-width: 300px") #td.add_style("width: 600px") td.add_style("vertical-align: top") td.add_style("padding-left: 5px") td.add_style("border-right: solid 1px") title = DivWdg() title.add_class("maq_search_bar") #x = DivWdg("[x]") #x.add_style("float: right") #title.add(x) title.add("Notes") td.add(title) discussion_wdg = DiscussionWdg() discussion_wdg.preprocess() discussion_wdg.set_sobject(sobject) td.add(discussion_wdg) note_panel = discussion_wdg.get_note_menu() td.add(note_panel) div.add(table) div.add(HtmlElement.hr()) div.add(HtmlElement.br(clear="all")) title_wdg = DivWdg() title_wdg.add_class("maq_search_bar") #x = DivWdg("[x]") #x.add_style("float: right") #title_wdg.add(x) #title_wdg.add_style("font-size: 1.5em") title_wdg.add("Detail" ) div.add(title_wdg) div.add(HtmlElement.br()) # TEST getting schema search_type = sobject.get_base_search_type() schema = Schema.get_by_sobject(sobject) child_types = schema.get_child_types(search_type) # add a second table table = Table() table.set_max_width() # show the snapshots for this sobject search_type = "sthpw/snapshot" search = Search(search_type) search.add_sobject_filter(sobject) search.set_limit(25) sobjects = search.get_sobjects() table.add_row() nav_td = table.add_cell() nav_td.add_style("width: 100px") nav_td.add_style("vertical-align: top") section_wdg = my.get_section_wdg(sobject) nav_td.add( section_wdg ) parent_key = SearchKey.get_by_sobject(sobject) content_wdg = ViewPanelWdg(search_type=search_type, element_name='Snapshots', \ title='Snapshots', view='table', parent_key=parent_key, do_search=True) #content_wdg.set_sobjects(sobjects) content_td = table.add_cell() content_td.set_id("sobject_relation") content_td.add_style("display: table-cell") content_td.add_style("vertical-align: top") content_td.add_style("padding-left: 10px") content_td.add(content_wdg) div.add(table) return div
def get_by_sobjects(sobjects, process=None, order=True): if not sobjects: return [] # quickly go through the sobjects to determine if their search types # are the same multi_stypes = False for sobject in sobjects: if sobject.get_search_type() != sobjects[0].get_search_type(): multi_stypes = True break search = Search( Task.SEARCH_TYPE ) if multi_stypes: # sort this into a dictionary and make multiple calls to # search.add_relationship_filters # use the first sobject as a sample sobjects_dict = {} for sobject in sobjects: st = sobject.get_search_type() sobj_list = sobjects_dict.get(st) if sobj_list == None: sobjects_dict[st] = [sobject] else: sobj_list.append(sobject) search.add_op('begin') for key, sobj_list in sobjects_dict.items(): search.add_op('begin') search.add_relationship_filters(sobj_list) search.add_op('and') search.add_op('or') else: from pyasm.biz import Schema schema = Schema.get() # FIXME: why doesn't the ops work here? filters = [] search.add_relationship_filters(sobjects) """ for sobject in sobjects: search_type = sobject.get_search_type() attrs = schema.get_relationship_attrs("sthpw/task", search_type) attrs = schema.resolve_relationship_attrs(attrs, "sthpw/task", search_type) search_code = sobject.get_value(attrs.get("to_col")) #search_code = sobject.get_value("code") #search.add_filter('search_type', search_type) #search.add_filter('search_id', search_id, quoted=False) #search.add_op("and") if attrs.get("from_col") == "search_code": filters.append("search_type = '%s' and search_code = '%s'" % (search_type, search_code)) else: filters.append("search_type = '%s' and search_id = %s" % (search_type, search_code)) search.add_where(" or ".join(filters)) """ search.add_order_by("search_type") search.add_order_by("search_code") search.add_order_by("search_id") # get the pipeline of the sobject pipeline = Pipeline.get_by_sobject(sobject) if order: if pipeline: process_names = pipeline.get_process_names(True) search.add_enum_order_by("process", process_names) else: search.add_order_by("process") search.add_order_by("id") if process: if isinstance(process, basestring): search.add_filter("process", process) else: search.add_filters("process", process) tasks = search.get_sobjects() return tasks
def _get_predefined_url(cls, key, hash): # make some predefined fake urls if key in ["link", "tab", "admin"]: # this is called by PageNav if key == "admin": expression = "/admin/link/{link}" else: expression = "/%s/{link}" % key options = Common.extract_dict(hash, expression) link = options.get("link") if not link: return None # test link security project_code = Project.get_project_code() security = Environment.get_security() keys = [ { "element": link }, { "element": "*" }, { "element": link, "project": project_code }, { "element": "*", "project": project_code } ] if not security.check_access("link", keys, "allow", default="deny"): return None from tactic.ui.panel import SideBarBookmarkMenuWdg personal = False if '.' in link: personal = True config = SideBarBookmarkMenuWdg.get_config("SideBarWdg", link, personal=personal) options = config.get_display_options(link) if not options: from pyasm.biz import Schema config_xml = [] config_xml.append( ''' <config> ''') config_schema = Schema.get_predefined_schema('config') SideBarBookmarkMenuWdg.get_schema_snippet("_config_schema", config_schema, config_xml) schema = Schema.get_admin_schema() SideBarBookmarkMenuWdg.get_schema_snippet("_admin_schema", schema, config_xml) config_xml.append( ''' </config> ''') xml = "".join(config_xml) from pyasm.widget import WidgetConfig schema_config = WidgetConfig.get(view="_admin_schema", xml=xml) options = schema_config.get_display_options(link) if not options: schema_config.set_view("_config_schema") options = schema_config.get_display_options(link) if not options: return None class_name = options.get("class_name") widget_key = options.get("widget_key") if widget_key: class_name = WidgetClassHandler().get_display_handler(widget_key) elif not class_name: class_name = 'tactic.ui.panel.ViewPanelWdg' if key in ["admin", "tab"]: use_index = "false" else: use_index = "true" if key in ['admin']: use_admin = "true" else: use_admin = "false" xml = [] xml.append('''<element admin="%s" index="%s">''' % (use_admin, use_index)) xml.append(''' <display class="%s">''' % class_name) for name, value in options.items(): xml.append("<%s>%s</%s>" % (name, value, name) ) xml.append(''' </display>''') xml.append('''</element>''') xml = "\n".join(xml) sobject = SearchType.create("config/url") sobject.set_value("url", "/%s/{value}" % key) sobject.set_value("widget", xml ) return sobject else: return None
def init(my): my.is_refresh = my.kwargs.get("refresh") my.search_key = my.kwargs.get("search_key") my.ticket_key = my.kwargs.get("ticket") my.parent_key = my.kwargs.get("parent_key") my.expression = my.kwargs.get("expression") # This assumed parent can cause errors as it tries to find a # relationship between to stypes that don't exist ... or worse, # try to bind them when one stype does not have the sufficent columns # ie: pipeline_code #if not my.parent_key: # project = Project.get() # my.parent_key = project.get_search_key() my.code = my.kwargs.get("code") sobject = None if my.search_key: sobject = Search.get_by_search_key(my.search_key) my.search_id = sobject.get_id() my.search_type = sobject.get_base_search_type() if sobject.is_insert(): my.mode = 'insert' else: my.mode = 'edit' elif my.expression: sobject = Search.eval(my.expression, single=True) my.search_id = sobject.get_id() my.search_type = sobject.get_base_search_type() my.mode = 'edit' elif my.ticket_key: from pyasm.security import Ticket, Login ticket = Ticket.get_by_valid_key(my.ticket_key) if not ticket: raise TacticException("No valid ticket") login_code = ticket.get_value("login") login = Login.get_by_code(login_code) my.search_type = "sthpw/login" my.search_id = login.get_id() my.mode = 'edit' elif my.code: my.search_type = my.kwargs.get("search_type") search = Search(my.search_type) search.add_filter("code", my.code) sobject = search.get_sobject() my.search_id = sobject.get_id() my.search_type = sobject.get_base_search_type() my.mode = 'edit' else: my.search_type = my.kwargs.get("search_type") my.search_id = my.kwargs.get("search_id") if not my.search_id: my.search_id = -1 my.search_id = int(my.search_id) if my.search_id != -1: my.mode = "edit" else: my.mode = "insert" # explicit override if my.kwargs.get("mode"): my.mode = my.kwargs.get("mode") my.view = my.kwargs.get("view") if not my.view: my.view = my.kwargs.get("config_base") if not my.view: my.view = "edit" default_data = my.kwargs.get('default') if not default_data: default_data = {} elif isinstance(default_data, basestring): try: default_data = jsonloads(default_data) except: #may be it's regular dictionary try: default_data = eval(default_data) except: print "Warning: Cannot evaluate [%s]" %default_data default_data = {} if sobject: my.set_sobjects([sobject], None) else: my.do_search() # TODO: get_config() is going the right direction (less features) but the more complicated method is biased # towards edit and insert view.. and so it needs improvement as well if my.view not in ["insert", "edit"]: # try a new smaller way to get config only when an explicit view # is set my.config = my.get_config() else: my.config = WidgetConfigView.get_by_search_type(my.search_type, my.view, use_cache=False) # for inline config definitions config_xml = my.kwargs.get("config_xml") if config_xml: #from pyasm.common import Xml #xml = Xml() #xml.read_string(config_xml) #node = xml.get_node("config/%s" % my.view) #xml.set_attribute(node, "class", "tactic.ui.panel.EditWdg") #config = WidgetConfig.get(view=my.view, xml=xml) config_xml = config_xml.replace("&", "&") config = WidgetConfig.get(view="tab", xml=config_xml) my.config.insert_config(0, config) my.skipped_element_names = [] # if there is a layout view, then find the element names using that layout_view = my.kwargs.get("layout_view") if layout_view: layout_view = layout_view.replace("/", ".") search = Search("config/widget_config") search.add_filter("view", layout_view) layout_config = search.get_sobject() xml = layout_config.get_xml_value("config") my.element_names = xml.get_values("config//html//element/@name") else: my.element_names = my.config.get_element_names() override_element_names = my.kwargs.get("element_names") if override_element_names: element_names = override_element_names ignore = my.kwargs.get("ignore") if isinstance(ignore, basestring): ignore = ignore.split("|") if not ignore: ignore = [] my.element_titles = [] my.element_descriptions = [] for element_name in my.element_names: my.element_titles.append( my.config.get_element_title(element_name) ) my.element_descriptions.append( my.config.get_element_description(element_name) ) #my.element_titles = my.config.get_element_titles() #my.element_descriptions = my.config.get_element_descriptions() # MongoDb # Default columns if not my.element_names: impl = SearchType.get_database_impl_by_search_type(my.search_type) if impl.get_database_type() == "MongoDb": my.element_names = impl.get_default_columns() my.element_titles = ['Code', 'Name', 'Description'] my.element_descriptions = ['Code', 'Name', 'Description'] my.input_prefix = my.kwargs.get('input_prefix') if not my.input_prefix: my.input_prefix = 'edit' security = Environment.get_security() default_access = "edit" project_code = Project.get_project_code() if my.parent_key: from pyasm.biz import Schema schema = Schema.get() parent_stype = SearchKey.extract_base_search_type(my.parent_key) relationship = schema.get_relationship_attrs(parent_stype, my.search_type, type="hierarchy") for element_name in my.element_names: # If parent_key is available, data associated with the parent table does not need # to be specified by the user, and their widgets can be excluded from the edit widget if element_name == relationship.get("from_col"): ignore.append(element_name) for i, element_name in enumerate(my.element_names): if element_name in ignore: my.skipped_element_names.append(element_name) continue # check security access access_key2 = { 'search_type': my.search_type, 'project': project_code } access_key1 = { 'search_type': my.search_type, 'key': element_name, 'project': project_code } access_keys = [access_key1, access_key2] is_editable = security.check_access('element', access_keys, "edit", default=default_access) if not is_editable: my.skipped_element_names.append(element_name) continue widget = my.config.get_display_widget(element_name, kbd_handler=False) # some element could be disabled due to its data_type e.g. sql_timestamp if not widget: my.skipped_element_names.append(element_name) continue widget.set_sobject(my.sobjects[0]) default_value = default_data.get(element_name) if default_value: widget.set_value(default_value) attrs = my.config.get_element_attributes(element_name) editable = widget.is_editable() if editable: editable = attrs.get("edit") editable = editable != "false" if not editable: my.skipped_element_names.append(element_name) continue # set parent widget.set_parent_wdg(my) # set parent_key in insert mode for now if my.mode =='insert' and my.parent_key: widget.set_option('parent_key', my.parent_key) title = my.element_titles[i] if title: widget.set_title(title) my.widgets.append(widget) description = my.element_descriptions[i] widget.add_attr("title", description)