Esempio n. 1
0
    def execute(my):
        database = "sthpw" 

        sql = DbContainer.get(database)
        value_array = sql.do_query("select code, cc from (select code, count(code) as cc from file group by code order by cc desc) as X where cc > 1;")
        #value_array = sql.do_query("select code, cc from (select code, count(code) as cc from file group by code order by cc desc) as X;")

        print "found [%s] pairs" % len(value_array)

        for count, value_list in enumerate(value_array):
            if count >= BATCH:
                break

            # get the file object
            file_code = value_list[0]
            search = Search("sthpw/file")
            search.add_filter("code", file_code)
            files = search.get_sobjects()

            #if len(files) == 1:
            #    continue

            for file in files:
                project_code = file.get_value("project_code")
                if not project_code:
                    print "WARNING: file [%s] has no project_code" % file_code
                    continue

                project = Project.get_by_code(project_code)
                initials = project.get_initials()

                id = file.get_id()
                new_file_code = "%s%s" % (id, initials)
                if file_code == new_file_code:
                    continue

                print "-"*20
                print "switching: ", file_code, "to", new_file_code


                snapshot_code = file.get_value("snapshot_code")
                snapshot = Snapshot.get_by_code(snapshot_code)
                assert snapshot

                snapshot_xml = snapshot.get_xml_value("snapshot")
                print snapshot_xml.to_string()
                node = snapshot_xml.get_node("snapshot/file[@file_code='%s']" % file_code)
                Xml.set_attribute(node, "file_code", new_file_code)
                print snapshot_xml.to_string()

                assert node


                # set the file_code
                file.set_value("code", new_file_code)
                file.commit()

                # set the snapshot
                snapshot.set_value("snapshot", snapshot_xml.to_string() )
                snapshot.commit()
Esempio n. 2
0
    def _test_project(self):

        # create a fake project
        #project = SearchType.create("sthpw/project")
        #project.set_value("code", "db_test")
        #project.set_value("db_resource", "sqlite")
        #project.commit()
        from pyasm.biz import Project
        project = Project.get_by_code("db_test")

        db_resource = project.get_db_resource()

        # so if we have search type, this will use the database resource
        # that is designated by
        search_type = "config/widget_config?project=db_test"
        search = Search(search_type)
        sobjects = search.get_sobject()

        # this is really tricky to do because a *lot* of functions assume
        # a local database by passing just a database string through.
        # things like table_exists() or any of these.  Ultimately, they
        # have to do a DbContainer.get(database) to do any operations.
        # But on the way, db_resource turns into database_name.  To really
        # fix, we have to enforce db_resource at DbContainer

        DbContainer.get(database)
Esempio n. 3
0
    def execute(self):

        self.base_dir = self.kwargs.get("base_dir")
        if not self.base_dir:
            self.base_dir = Environment.get_template_dir()


        self.project_code = self.kwargs.get("project_code")
        if not self.project_code:
            self.project_code = Project.get_project_code()

        assert self.project_code

        # project code can be called anything, and we want to have a _template suffix for the template code
        #self.plugin_code = "%s_template" % self.project_code

        #self.template_project_code = re.sub( '_template$', '', self.plugin_code)
        self.template_project_code = self.project_code
        self.project = Project.get_by_code(self.project_code)
        if not self.project:
            raise TacticException('This project [%s] does not exist'%self.project_code)

        self.project_type = self.project.get_value("type")
        if not self.project_type:
            self.project_type = self.project_code
        Project.set_project(self.project_code)

        self.export_template()
Esempio n. 4
0
    def _test_add_drop_column(self):
        #Project.set_project('unittest')
        from pyasm.command import ColumnAddCmd, ColumnDropCmd, Command
        cmd = ColumnAddCmd('unittest/country', 'special_place', 'varchar(256)')
        Command.execute_cmd(cmd)
        search_type = 'unittest/country'

        # clear cache

        SearchType.clear_column_cache(search_type)

        DatabaseImpl.clear_table_cache()
        exists = SearchType.column_exists(search_type, 'special_place')
        self.assertEquals(exists, True)

        # now drop the column
        cmd = ColumnDropCmd(search_type, 'special_place')
        Command.execute_cmd(cmd)

        # clear cache
        SearchType.clear_column_cache(search_type)
        cache_dict = Container.get("DatabaseImpl:column_info")

        # assume database is the same as sthpw
        database_type = Project.get_by_code("unittest").get_database_type()
        db_resource = DbResource.get_default('unittest')
        table_info = cache_dict.get("%s:%s" % (db_resource, "country"))
        self.assertEquals(table_info == None, True)

        key = "%s:%s" % (db_resource, "country")
        cache_dict[key] = None
        exists = SearchType.column_exists(search_type, 'special_place')
        self.assertEquals(exists, False)
Esempio n. 5
0
    def _test_add_drop_column(my):
        #Project.set_project('unittest')
        from pyasm.command import ColumnAddCmd, ColumnDropCmd, Command
        cmd = ColumnAddCmd('unittest/country','special_place','varchar(256)')
        Command.execute_cmd(cmd)
        search_type = 'unittest/country'

        # clear cache
       
        SearchType.clear_column_cache(search_type)

        DatabaseImpl.clear_table_cache()
        exists = SearchType.column_exists(search_type, 'special_place')
        my.assertEquals(exists, True)

        # now drop the column
        cmd = ColumnDropCmd(search_type,'special_place')
        Command.execute_cmd(cmd)

        # clear cache
        SearchType.clear_column_cache(search_type)
        cache_dict = Container.get("DatabaseImpl:column_info")

       
        # assume database is the same as sthpw
        database_type = Project.get_by_code("unittest").get_database_type()
        db_resource = DbResource.get_default('unittest')
        table_info = cache_dict.get("%s:%s" % (db_resource, "country"))
        my.assertEquals(table_info == None, True)


        key = "%s:%s" % (db_resource, "country")
        cache_dict[key] = None
        exists = SearchType.column_exists(search_type, 'special_place')
        my.assertEquals(exists, False)
Esempio n. 6
0
    def execute(my):

        my.base_dir = my.kwargs.get("base_dir")
        if not my.base_dir:
            my.base_dir = Environment.get_template_dir()


        my.project_code = my.kwargs.get("project_code")
        if not my.project_code:
            my.project_code = Project.get_project_code()

        assert my.project_code

        # project code can be called anything, and we want to have a _template suffix for the template code
        #my.plugin_code = "%s_template" % my.project_code

        #my.template_project_code = re.sub( '_template$', '', my.plugin_code)
        my.template_project_code = my.project_code
        my.project = Project.get_by_code(my.project_code)
        if not my.project:
            raise TacticException('This project [%s] does not exist'%my.project_code)

        my.project_type = my.project.get_value("type")
        if not my.project_type:
            my.project_type = my.project_code
        Project.set_project(my.project_code)

        my.export_template()
Esempio n. 7
0
    def _test_commit(my):

        from pyasm.biz import Project
        database_type = Project.get_by_code("unittest").get_database_type()
        if database_type == "MySQL":
            print
            print "WARNING: !!!!!!!"
            print "_test_commit is disabled"
            print "WARNING: !!!!!!!"
            print
            return

        person_s = Search('unittest/person')
        person_s.add_filter('name_first', 'pete')

        person = person_s.get_sobject()
        from pyasm.biz import Note
        Note.create(person,
                    "3 slashes \\\\\\",
                    context="unittest_commit",
                    process="unittest_commit")
        search = Search('sthpw/note')
        search.add_filter('process', 'unittest_commit')
        search.set_limit(1)
        note = search.get_sobject()
        my.assertEquals(note.get_value('note'), "3 slashes \\\\\\")
Esempio n. 8
0
    def create(my):

        project = Project.get_by_code(my.project_code)
        if project:

            my.delete()

        print "Setting up a basic Sample3d project"

        # create the project
        create_cmd = CreateProjectCmd(
            project_code=my.project_code,
            project_title="Sample 3D")  #, project_type="unittest")
        create_cmd.execute()

        # install the unittest plugin
        installer = PluginInstaller(relative_dir="TACTIC/internal/sample3d",
                                    verbose=False)
        installer.execute()

        # add 30 shots
        for x in xrange(30):
            shot = SearchType.create("prod/shot")
            shot.set_value('name', 'shot%s' % x)
            shot.set_value('sequence_code', 'SEQ_01')
            shot.commit(triggers=False)

        seq = SearchType.create("prod/sequence")
        seq.set_value('code', 'SEQ_01')
        seq.commit(triggers=False)
Esempio n. 9
0
    def get_parent_schema(self):

        parent_schema_code = self.xml.get_value("schema/@parent")
        if parent_schema_code:
            if parent_schema_code == "__NONE__":
                return None

            parent_schema = Schema.get_by_code(parent_schema_code)
            return parent_schema
        else:

            # Note: assume schema code == project_code
            schema_code = self.get_value("code")

            from pyasm.biz import Project
            project = Project.get_by_code(schema_code)
            if not project:
                return None

            project_code = project.get_code()
            project_type = project.get_base_type()
            if not project_type:
                return None

            parent_schema = Schema.get_predefined_schema(project_type) 
            return parent_schema
Esempio n. 10
0
    def execute(self):

        self.base_dir = self.kwargs.get("base_dir")
        if not self.base_dir:
            self.base_dir = Environment.get_template_dir()

        self.project_code = self.kwargs.get("project_code")
        if not self.project_code:
            self.project_code = Project.get_project_code()

        assert self.project_code

        # project code can be called anything, and we want to have a _template suffix for the template code
        #self.plugin_code = "%s_template" % self.project_code

        #self.template_project_code = re.sub( '_template$', '', self.plugin_code)
        self.template_project_code = self.project_code
        self.project = Project.get_by_code(self.project_code)
        if not self.project:
            raise TacticException('This project [%s] does not exist' %
                                  self.project_code)

        self.project_type = self.project.get_value("type")
        if not self.project_type:
            self.project_type = self.project_code
        Project.set_project(self.project_code)

        self.export_template()
Esempio n. 11
0
    def check(my):
        project_code = my.kwargs.get('project_code')
        regexs = '^\d|\W'
        m = re.search(r'%s' % regexs, project_code)
        if m:
            if isinstance(project_code, unicode):
                project_code = project_code.encode('utf-8')
            else:
                project_code = unicode(project_code).encode('utf-8')
            raise TacticException(
                '<project_code> [%s] cannot contain special characters or start with a number.'
                % project_code)

        # check to see if this project already exists
        test_project = Project.get_by_code(project_code)
        if test_project:
            if test_project.get_value('s_status') == 'retired':
                raise TacticException(
                    'Project with code [%s] already exists but is retired.' %
                    project_code)
            else:
                raise TacticException(
                    'Project with code [%s] already exists.' % project_code)

        return True
Esempio n. 12
0
    def execute(my):

        my.base_dir = my.kwargs.get("base_dir")
        if not my.base_dir:
            my.base_dir = Environment.get_template_dir()

        my.project_code = my.kwargs.get("project_code")
        if not my.project_code:
            my.project_code = Project.get_project_code()

        assert my.project_code

        # project code can be called anything, and we want to have a _template suffix for the template code
        #my.plugin_code = "%s_template" % my.project_code

        #my.template_project_code = re.sub( '_template$', '', my.plugin_code)
        my.template_project_code = my.project_code
        my.project = Project.get_by_code(my.project_code)
        if not my.project:
            raise TacticException('This project [%s] does not exist' %
                                  my.project_code)

        my.project_type = my.project.get_value("type")
        if not my.project_type:
            my.project_type = my.project_code
        Project.set_project(my.project_code)

        my.export_template()
Esempio n. 13
0
    def _test_project(self):

        # create a fake project
        #project = SearchType.create("sthpw/project")
        #project.set_value("code", "db_test")
        #project.set_value("db_resource", "sqlite")
        #project.commit()
        from pyasm.biz import Project
        project = Project.get_by_code("db_test")

        db_resource = project.get_db_resource()

        # so if we have search type, this will use the database resource
        # that is designated by
        search_type = "config/widget_config?project=db_test"
        search = Search(search_type)
        sobjects = search.get_sobject()

        # this is really tricky to do because a *lot* of functions assume
        # a local database by passing just a database string through.
        # things like table_exists() or any of these.  Ultimately, they
        # have to do a DbContainer.get(database) to do any operations.
        # But on the way, db_resource turns into database_name.  To really
        # fix, we have to enforce db_resource at DbContainer

        DbContainer.get(database)
Esempio n. 14
0
    def create(self):

        project = Project.get_by_code(self.project_code)
        if project:

            self.delete()

        print "Setting up a basic Sample3d project"

        # create the project
        create_cmd = CreateProjectCmd(project_code=self.project_code, project_title="Sample 3D") #, project_type="unittest")
        create_cmd.execute()

        # install the unittest plugin
        installer = PluginInstaller(relative_dir="TACTIC/internal/sample3d", verbose=False)
        installer.execute()

        # add 30 shots
        for x in xrange(30):
            shot = SearchType.create("prod/shot")
            shot.set_value('name','shot%s'%x)
            shot.set_value('sequence_code','SEQ_01')
            shot.commit(triggers=False)

        if not Search.eval("@SOBJECT(prod/sequence['code','SEQ_01'])"):
            seq = SearchType.create("prod/sequence")
            seq.set_value('code','SEQ_01')
            seq.commit(triggers=False)
Esempio n. 15
0
    def _test_get_connect(self):
        database = 'unittest'
        project = Project.get_by_code(database)
        db_resource = project.get_project_db_resource()
        sql1 = DbContainer.get(db_resource)
        sql2 = DbContainer.get(db_resource)

        self.assertEquals(sql1, sql2)
Esempio n. 16
0
 def _test_get_connect(my):
     database= 'unittest'
     project = Project.get_by_code(database)
     db_resource= project.get_project_db_resource()
     sql1 = DbContainer.get(db_resource)
     sql2 = DbContainer.get(db_resource)
     
     my.assertEquals(sql1, sql2)
Esempio n. 17
0
    def _test_set_value(my): 
        ''' test with different Database Impl'''
        update = Update()
        update.set_database('sthpw')
        update.set_table('task')
        update.set_value('timestamp','2012-12-12')
        my.assertEquals( update.get_statement(), """UPDATE {0}"task" SET "timestamp" = '2012-12-12'""".format(my.sthpw_prefix))

        update = Update()
        update.set_database('sthpw')
        update.set_table('task')


        # Changing database to SQLServer
        sql_impl = DatabaseImpl.get('SQLServer')



        update.impl = sql_impl
        update.set_value('timestamp','2012-12-12')
        my.assertEquals( update.get_statement(), """UPDATE {0}"task" SET "timestamp" = convert(datetime2, \'2012-12-12\', 0)""".format(my.sthpw_prefix))
        update.set_value('timestamp','NOW')
        my.assertEquals( update.get_statement(), """UPDATE {0}"task" SET "timestamp" = getdate()""".format(my.sthpw_prefix))

        from pyasm.biz import Project
        database_type = Project.get_by_code("unittest").get_database_type()
        if database_type == "MySQL":
            print
            print "WARNING: !!!!!!!"
            print "test for NOW is disabled"
            print "WARNING: !!!!!!!"
            print
            return

        
        time_dict = {'SQLServer': "convert(datetime2, '2012-12-25', 0)", 
                    'Sqlite':"'2012-12-25'", 
                    'PostgreSQL':"'2012-12-25'",
                    'MySQL': "'2012-12-25'"}
                    #'Oracle':"TO_DATE('2012-12-25','YYYY-MM-DD'"}
        #TODO: test with cx_Oracle installed
        for db_type in ['Sqlite','SQLServer','MySQL','PostgreSQL']:
            sql_impl = DatabaseImpl.get(db_type)
            update = Update()
            update.set_database('sthpw')
            update.set_table('task')
            update.impl = sql_impl
            update.set_value('timestamp','2012-12-25')
            update.set_value('description','')
            if db_type == 'SQLServer':
                my.assertEquals( update.get_statement(), """UPDATE %s"task" SET "timestamp" = %s, "description" = N\'\'"""% (my.sthpw_prefix, time_dict.get(db_type)))
            else:
                my.assertEquals( update.get_statement(), """UPDATE %s"task" SET "timestamp" = %s, "description" = \'\'"""% (my.sthpw_prefix, time_dict.get(db_type)))

            update.set_value('description',None)
            my.assertEquals( update.get_statement(), """UPDATE %s"task" SET "timestamp" = %s, "description" = NULL"""% (my.sthpw_prefix, time_dict.get(db_type)))
Esempio n. 18
0
    def execute(my):

        my.project_code = my.kwargs.get("project_code")
        my.prefix = my.kwargs.get("prefix")

        my.project = Project.get_by_code(my.project_code)
        my.project_type = my.project.get_value("type")

        my.check_project()
        my.check_search_type()
Esempio n. 19
0
    def execute(self):

        self.project_code = self.kwargs.get("project_code")
        self.prefix = self.kwargs.get("prefix")

        self.project = Project.get_by_code(self.project_code)
        self.project_type = self.project.get_value("type")

        self.check_project()
        self.check_search_type()
Esempio n. 20
0
    def execute(self):

        self.project_code = self.kwargs.get("project_code")
        self.prefix = self.kwargs.get("prefix")


        self.project = Project.get_by_code(self.project_code)
        self.project_type = self.project.get_value("type")

        self.check_project()
        self.check_search_type()
Esempio n. 21
0
    def execute(my):

        my.project_code = my.kwargs.get("project_code")
        my.prefix = my.kwargs.get("prefix")


        my.project = Project.get_by_code(my.project_code)
        my.project_type = my.project.get_value("type")

        my.check_project()
        my.check_search_type()
Esempio n. 22
0
    def _create_table(my):

        from pyasm.search import CreateTable

        project = Project.get_by_code("mongodb")
        #sql = project.get_sql()
        db_resource = project.get_project_db_resource()
        sql = db_resource.get_sql()

        create = CreateTable()
        create.set_table("whatever")

        create.commit(sql)
Esempio n. 23
0
    def _create_table(my):

        from pyasm.search import CreateTable

        project = Project.get_by_code("mongodb")
        #sql = project.get_sql()
        db_resource = project.get_project_db_resource()
        sql = db_resource.get_sql()
    
        create = CreateTable()
        create.set_table("whatever")

        create.commit(sql)
Esempio n. 24
0
    def execute(my):

        my.new_project = my.kwargs.get("new_project")
        if my.new_project in [False, 'false']:
            my.new_project = False
        else:
            my.new_project = True


        my.mode = my.kwargs.get("mode")
        if not my.mode:
            my.mode = 'copy'

        # if a path is specified, then handle this
        my.path = my.kwargs.get("path")
        my.project_code = my.kwargs.get("project_code")
        my.is_template = my.kwargs.get("is_template")

        # check to see if the project already exists
        # FIXME: how to determine which project code? pass it in even with path kwarg for now
        project = Project.get_by_code(my.project_code)
        if my.new_project and project:
            raise TacticException("Project [%s] already exists in this installation. Exiting..." % my.project_code)

        if my.path:
            my.handle_path(my.path)



        assert my.project_code

        # determines which template to use
        my.template_code = my.kwargs.get("template_code")
        if not my.template_code:
            my.template_code = my.project_code

       

        # template code can end with _template or not depending if it's coming from a zip file
        #if my.template_code.endswith("_template"):
        
        #    my.plugin_code = my.template_code
        #else:
        #    my.plugin_code = "%s_template" % my.template_code

        #my.template_project_code = re.sub( '_template$', '', my.template_code)
        my.template_project_code = my.template_code
        my.force_database = my.kwargs.get("force_database")
    
        my.import_template()
Esempio n. 25
0
    def _test_transaction(my):
        # initiate a global transaction

        database = "unittest"
        count_sql = 'select count(*) from "person"'

        transaction = Transaction.get(create=True)
        # adding these 2 lines 
        project = Project.get_by_code(database)
        db_resource= project.get_project_db_resource()

        db = DbContainer.get(db_resource)

        count0 = db.get_value(count_sql)
        person = Person.create("Mr", "Dumpling", "Dumpling Land", "Burnt")

        # check to see that one was added
        count1 = db.get_value(count_sql)
        my.assertEquals( count1, count0+1)

        # FIXME: cant' delete for some reason
        #person.delete()

        # commit the first time
        transaction.commit()

        # check to see that one was added
        count1 = db.get_value(count_sql)
        my.assertEquals( count1, count0+1)

        #transaction.rollback()
        #person.delete()
        last = TransactionLog.get_last()
        last.undo()

        # create another login
        transaction = Transaction.get(create=True)

        count0 = db.get_value(count_sql)
        person = Person.create("Mr", "Dumpling", "Dumpling Land", "Burnt")
        db = DbContainer.get(db_resource)
        count2 = db.get_value(count_sql)
        my.assertEquals( count2, count0+1)

        transaction.rollback()

        # check to see that one was removed/rolled back
        count3 = db.get_value(count_sql)
        my.assertEquals( count3, count0)
Esempio n. 26
0
    def handle_not_logged_in(my, allow_change_admin=True):


        site_obj = Site.get()
        site_obj.set_site("default")

        DbResource.clear_cache()


        from pyasm.widget import WebLoginWdg, BottomWdg
        from tactic.ui.app import TitleTopWdg

        from pyasm.biz import Project
        from tactic.ui.panel import HashPanelWdg


        web = WebContainer.get_web()

        widget = Widget()

        top = TitleTopWdg()
        widget.add(top)
        body = top.get_body()
        body.add_gradient("background", "background", 5, -20)
        body.add_color("color", "color")


        reset_request = web.get_form_value('reset_request') =='true'
        if reset_request:
            from tactic.ui.widget import ResetPasswordWdg
            top.add(ResetPasswordWdg())
        else:
            reset_msg = web.get_form_value('reset_msg')
            if reset_msg:
                web.set_form_value(WebLoginWdg.LOGIN_MSG, reset_msg)


            sudo = Sudo()
            try:
                # get the project from the url because we are still 
                # in the admin project at this stage
                current_project = web.get_context_name()
                try:
                    if current_project != "default":
                        project = Project.get_by_code(current_project)
                        assert project
                except Exception, e:
                    web_wdg = None
                else:
Esempio n. 27
0
    def create(my):

        project = Project.get_by_code(my.project_code)
        if project:

            my.delete()

        print "Setting up clean Unittest project"

        # create the project
        create_cmd = CreateProjectCmd(project_code=my.project_code, project_title="Unittest") #, project_type="unittest")
        create_cmd.execute()

        # install the unittest plugin
        installer = PluginInstaller(relative_dir="TACTIC/internal/unittest", verbose=False)
        installer.execute()
Esempio n. 28
0
    def create(my):

        project = Project.get_by_code(my.project_code)
        if project:

            my.delete()

        print "Setting up a basic Sample3d project"

        # create the project
        create_cmd = CreateProjectCmd(project_code=my.project_code, project_title="Sample 3D") #, project_type="unittest")
        create_cmd.execute()

        # install the unittest plugin
        installer = PluginInstaller(relative_dir="TACTIC/internal/sample3d", verbose=False)
        installer.execute()
Esempio n. 29
0
    def create(self):

        project = Project.get_by_code(self.project_code)
        if project:

            self.delete()

        print "Setting up clean Unittest project"

        # create the project
        create_cmd = CreateProjectCmd(project_code=self.project_code, project_title="Unittest") #, project_type="unittest")
        create_cmd.execute()

        # install the unittest plugin
        installer = PluginInstaller(relative_dir="TACTIC/internal/unittest", verbose=False)
        installer.execute()
Esempio n. 30
0
    def execute(my):

        my.new_project = my.kwargs.get("new_project")
        if my.new_project in [False, 'false']:
            my.new_project = False
        else:
            my.new_project = True

        my.mode = my.kwargs.get("mode")
        if not my.mode:
            my.mode = 'copy'

        # if a path is specified, then handle this
        my.path = my.kwargs.get("path")
        my.project_code = my.kwargs.get("project_code")
        my.is_template = my.kwargs.get("is_template")

        # check to see if the project already exists
        # FIXME: how to determine which project code? pass it in even with path kwarg for now
        project = Project.get_by_code(my.project_code)
        if my.new_project and project:
            raise TacticException(
                "Project [%s] already exists in this installation. Exiting..."
                % my.project_code)

        if my.path:
            my.handle_path(my.path)

        assert my.project_code

        # determines which template to use
        my.template_code = my.kwargs.get("template_code")
        if not my.template_code:
            my.template_code = my.project_code

        # template code can end with _template or not depending if it's coming from a zip file
        #if my.template_code.endswith("_template"):

        #    my.plugin_code = my.template_code
        #else:
        #    my.plugin_code = "%s_template" % my.template_code

        #my.template_project_code = re.sub( '_template$', '', my.template_code)
        my.template_project_code = my.template_code
        my.force_database = my.kwargs.get("force_database")

        my.import_template()
Esempio n. 31
0
    def get_display(my):

        web = WebContainer.get_web()

        widget = Widget()
        html = HtmlElement("html")
        html.add_attr("xmlns:v", 'urn:schemas-microsoft-com:vml')

        is_xhtml = False
        if is_xhtml:
            web.set_content_type("application/xhtml+xml")
            widget.add('''<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            ''')
            html.add_attr("xmlns", "http://www.w3.org/1999/xhtml")
            #html.add_attr("xmlns:svg", "http://www.w3.org/2000/svg")

        # add the copyright
        widget.add(my.get_copyright_wdg())
        widget.add(html)

        # create the header
        head = HtmlElement("head")
        html.add(head)

        head.add(
            '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>\n'
        )
        head.add('<meta http-equiv="X-UA-Compatible" content="IE=edge"/>\n')

        # Add the tactic favicon
        head.add(
            '<link rel="shortcut icon" href="/context/favicon.ico" type="image/x-icon"/>'
        )

        # add the css styling
        head.add(my.get_css_wdg())

        # add the title in the header
        try:
            project = Project.get()
        except Exception, e:
            print "ERROR: ", e
            # if the project doesn't exist, then use the admin project
            project = Project.get_by_code("admin")
Esempio n. 32
0
    def get_display(my):

        web = WebContainer.get_web()

        widget = Widget()
        html = HtmlElement("html")
        html.add_attr("xmlns:v", 'urn:schemas-microsoft-com:vml')

        is_xhtml = False
        if is_xhtml:
            web.set_content_type("application/xhtml+xml")
            widget.add('''<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            ''')
            html.add_attr("xmlns", "http://www.w3.org/1999/xhtml")
            #html.add_attr("xmlns:svg", "http://www.w3.org/2000/svg")


        # add the copyright
        widget.add( my.get_copyright_wdg() )
        widget.add(html)


        # create the header
        head = HtmlElement("head")
        html.add(head)

        head.add('<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>\n')
        head.add('<meta http-equiv="X-UA-Compatible" content="IE=edge"/>\n')

        # Add the tactic favicon
        head.add('<link rel="shortcut icon" href="/context/favicon.ico" type="image/x-icon"/>')

        # add the css styling
        head.add(my.get_css_wdg())

        # add the title in the header
        try:
            project = Project.get()
        except Exception, e:
            print "ERROR: ", e
            # if the project doesn't exist, then use the admin project
            project = Project.get_by_code("admin")
Esempio n. 33
0
    def handle_not_logged_in(my, allow_change_admin=True):

        site_obj = Site.get()
        site_obj.set_site("default")

        DbResource.clear_cache()

        from pyasm.widget import WebLoginWdg, BottomWdg
        from tactic.ui.app import TitleTopWdg

        from pyasm.biz import Project
        from tactic.ui.panel import HashPanelWdg

        web = WebContainer.get_web()

        widget = Widget()

        top = TitleTopWdg()
        widget.add(top)
        body = top.get_body()
        body.add_gradient("background", "background", 5, -20)
        body.add_color("color", "color")

        reset_request = web.get_form_value('reset_request') == 'true'
        if reset_request:
            from tactic.ui.widget import ResetPasswordWdg
            top.add(ResetPasswordWdg())
        else:
            reset_msg = web.get_form_value('reset_msg')
            if reset_msg:
                web.set_form_value(WebLoginWdg.LOGIN_MSG, reset_msg)

            web_wdg = None
            #sudo = Sudo()
            try:
                # get the project from the url because we are still
                # in the admin project at this stage
                current_project = web.get_context_name()
                try:
                    if current_project != "default":
                        project = Project.get_by_code(current_project)
                        assert project
                except Exception, e:
                    pass
                else:
Esempio n. 34
0
    def get_display(self):

        widget = DivWdg()
        widget.add_style("text-align: center")

        search_type = self.get_current_sobject()

        project_code = search_type.get_value("database")
        if project_code == "{project}":
            # HACK: assumes database == project_code.  Can't seem to get
            # around the {project} variable ... need to look at this
            # sometime
            project = Project.get()
        else:
            project = Project.get_by_code(project_code)


        if not project:
            widget.add( IconWdg("Exists", IconWdg.ERROR) )
            widget.add( "Project does not exist")
            return widget

        table = search_type.get_table()

        if not table:
            return ""


        try:
            db_resource = project.get_project_db_resource()
            sql = DbContainer.get(db_resource)
            tables = sql.get_tables()
            has_table = table in tables
        except DatabaseException:
            has_table = False


        if has_table:
            widget.add( IconWdg("Exists", IconWdg.DOT_GREEN) )
        else:
            widget.add( IconWdg("Does not Exist", IconWdg.DOT_RED) )

        return widget
Esempio n. 35
0
    def set_sobjects(my, sobjects):

        from pyasm.search import Search, SObject, Insert, SearchType

        my.sobjects = sobjects
        first = my.sobjects[0]

        # get the database
        project_code = first.get_project_code()
        from pyasm.biz import Project
        project = Project.get_by_code(project_code)
        if not project:
            raise Exception("SObject [%s] has a project_code [%s] that does not exist" % (first.get_search_key(), project_code) )

        my.db_resource = project.get_project_db_resource()

        # get the search_type
        my.search_type = first.get_base_search_type()
        my.search_type_obj = SearchType.get(my.search_type)
        my.table = my.search_type_obj.get_table()
    def check(my):
        project_code = my.kwargs.get('project_code')
        regexs = '^\d|\W'
        m = re.search(r'%s' % regexs, project_code) 
        if m:
            if isinstance(project_code, unicode):
                project_code = project_code.encode('utf-8')
            else:
                project_code = unicode(project_code).encode('utf-8')
            raise TacticException('<project_code> [%s] cannot contain special characters or start with a number.' %project_code)

        # check to see if this project already exists
        test_project = Project.get_by_code(project_code)
        if test_project:
            if test_project.get_value('s_status') == 'retired':
                raise TacticException('Project with code [%s] already exists but is retired.' %project_code)
            else:
                raise TacticException('Project with code [%s] already exists.' %project_code)

        return True
Esempio n. 37
0
    def run_sql(my, sql):
        ''' run an sql statement. my is an instance of the dynamically created 
        <project_type>Upgrade class. If SqlException arise, it will record the
        error, and the user is advised to check if the error is a result of syntax 
        error or the upgrade function is doing redundant work'''
        project = Project.get_by_code(my.project_code)
        db_resource = project.get_project_db_resource()
        db = DbContainer.get(db_resource)
        #if not my.quiet:
        #    print sql
        try:
            db.do_update(sql, quiet=my.quiet)
        except SqlException, e:
            print "Error: ", e
            # TEST for Sqlite
            if str(e).startswith("duplicate column name:"):
                pass
            elif str(e).startswith("table") and str(e).endswith(
                    "already exists"):
                pass

            elif not my.quiet:
                print
                print "WARNING: Skipping due to SqlException..."
                print "Message: ", e
                print
            members = inspect.getmembers(my, predicate=inspect.ismethod)
            key = '%s|%s' % (my.project_code, my.upgrade_class)

            Container.append_seq(key, (my.upgrade_method, str(e)))
            """  
            for name, member in members:
                # there should only be 1 upgrade method
                if name.startswith('upgrade_v'):       
                    Container.append_seq(key, (my.upgrade_method, str(e)))
                    break
            """
            # to prevent sql error affecting query that follows the Upgrade
            #DbContainer.abort_thread_sql()
            DbContainer.release_thread_sql()
Esempio n. 38
0
    def get_display(self):

        widget = DivWdg()
        widget.add_style("text-align: center")

        search_type = self.get_current_sobject()

        project_code = search_type.get_value("database")
        if project_code == "{project}":
            # HACK: assumes database == project_code.  Can't seem to get
            # around the {project} variable ... need to look at this
            # sometime
            project = Project.get()
        else:
            project = Project.get_by_code(project_code)

        if not project:
            widget.add(IconWdg("Exists", IconWdg.ERROR))
            widget.add("Project does not exist")
            return widget

        table = search_type.get_table()

        if not table:
            return ""

        try:
            db_resource = project.get_project_db_resource()
            sql = DbContainer.get(db_resource)
            tables = sql.get_tables()
            has_table = table in tables
        except DatabaseException:
            has_table = False

        if has_table:
            widget.add(IconWdg("Exists", IconWdg.DOT_GREEN))
        else:
            widget.add(IconWdg("Does not Exist", IconWdg.DOT_RED))

        return widget
Esempio n. 39
0
    def check(my):
        project_code = my.kwargs.get("project_code")
        regexs = "^\d|\W"
        m = re.search(r"%s" % regexs, project_code)
        if m:
            if isinstance(project_code, unicode):
                project_code = project_code.encode("utf-8")
            else:
                project_code = unicode(project_code).encode("utf-8")
            raise TacticException(
                "<project_code> [%s] cannot contain special characters or start with a number." % project_code
            )

        # check to see if this project already exists
        test_project = Project.get_by_code(project_code)
        if test_project:
            if test_project.get_value("s_status") == "retired":
                raise TacticException("Project with code [%s] already exists but is retired." % project_code)
            else:
                raise TacticException("Project with code [%s] already exists." % project_code)

        return True
Esempio n. 40
0
    def run_sql(my, sql):
        ''' run an sql statement. my is an instance of the dynamically created 
        <project_type>Upgrade class. If SqlException arise, it will record the
        error, and the user is advised to check if the error is a result of syntax 
        error or the upgrade function is doing redundant work'''
        project = Project.get_by_code(my.project_code)
        db_resource = project.get_project_db_resource()
        db = DbContainer.get(db_resource)
        #if not my.quiet:
        #    print sql
        try:
            db.do_update(sql, quiet=my.quiet)
        except SqlException, e:
            print "Error: ", e
            # TEST for Sqlite
            if str(e).startswith("duplicate column name:"):
                pass
            elif str(e).startswith("table") and str(e).endswith("already exists"):
                pass

            elif not my.quiet:
                print
                print "WARNING: Skipping due to SqlException..."
                print "Message: ", e
                print
            members = inspect.getmembers(my, predicate=inspect.ismethod)
            key = '%s|%s' %(my.project_code, my.upgrade_class)
            
            Container.append_seq(key, (my.upgrade_method, str(e)))
            """  
            for name, member in members:
                # there should only be 1 upgrade method
                if name.startswith('upgrade_v'):       
                    Container.append_seq(key, (my.upgrade_method, str(e)))
                    break
            """          
            # to prevent sql error affecting query that follows the Upgrade
            #DbContainer.abort_thread_sql()
            DbContainer.release_thread_sql()
Esempio n. 41
0
    def _test_transaction(my):
        """test a transaction"""

        database_type = Project.get_by_code("unittest").get_database_type()
        if database_type == "MySQL":
            print
            print "WARNING: !!!!!!!"
            print "_test_tranaction is disabled"
            print "WARNING: !!!!!!!"
            print
            return


        db_res = DbResource.get_default('unittest')
        sql = DbContainer.get(db_res)

        count_sql = 'select count(*) from "person"'

        num_records = sql.get_int(count_sql)

        # start the transaction, update and roll back
        sql.start()


        insert = Insert()
        insert.set_table("person")
        insert.set_value("name_first", "cow")
        insert.set_value("name_last", "sql")
        query = insert.get_statement()

        sql.do_update(query)
        new_num_records = sql.get_value(count_sql)
        my.assertEquals( new_num_records, num_records+1 )
        sql.rollback()

        # dump after the rollback
        new_num_records = sql.get_int(count_sql)
        my.assertEqual( new_num_records, num_records )
Esempio n. 42
0
    def _test_commit(self):
       
        from pyasm.biz import Project
        database_type = Project.get_by_code("unittest").get_database_type()
        if database_type == "MySQL":
            print
            print "WARNING: !!!!!!!"
            print "_test_commit is disabled"
            print "WARNING: !!!!!!!"
            print
            return

        person_s = Search('unittest/person')
        person_s.add_filter('name_first','pete')

        person = person_s.get_sobject()
        from pyasm.biz import Note
        Note.create(person, "3 slashes \\\\\\", context="unittest_commit", process="unittest_commit")
        search = Search('sthpw/note')
        search.add_filter('process','unittest_commit')
        search.set_limit(1)
        note = search.get_sobject()
        self.assertEquals(note.get_value('note'), "3 slashes \\\\\\")
Esempio n. 43
0
    def _test_transaction(self):
        """test a transaction"""

        database_type = Project.get_by_code("unittest").get_database_type()
        if database_type == "MySQL":
            print
            print "WARNING: !!!!!!!"
            print "_test_tranaction is disabled"
            print "WARNING: !!!!!!!"
            print
            return

        db_res = DbResource.get_default('unittest')
        sql = DbContainer.get(db_res)

        count_sql = 'select count(*) from "person"'

        num_records = sql.get_int(count_sql)

        # start the transaction, update and roll back
        sql.start()

        insert = Insert()
        insert.set_table("person")
        insert.set_value("name_first", "cow")
        insert.set_value("name_last", "sql")
        query = insert.get_statement()

        sql.do_update(query)
        new_num_records = sql.get_value(count_sql)
        self.assertEquals(new_num_records, num_records + 1)
        sql.rollback()

        # dump after the rollback
        new_num_records = sql.get_int(count_sql)
        self.assertEqual(new_num_records, num_records)
Esempio n. 44
0
    def get_display(my):

        top = my.top
        top.add_color("background", "background")
        top.add_color("color", "color")
        top.add_style("width", "400px")
        top.add_border()
        top.add_class("spt_delete_project_tool_top")

        login = Environment.get_user_name()
        if login != 'admin':
            top.add(IconWdg(icon=IconWdg.WARNING))
            top.add("Only Admin can delete projects")
            top.add_style("padding: 50px")
            top.add_style("text-align: center")
            return top

        site = my.kwargs.get("site")
        if site:
            Site.set_site(site)

        project_code = my.kwargs.get("project_code")

        # check if delete permissions exist for this site and project
        security = Environment.get_security()
        if False and not security.check_access("project", project_code,
                                               "delete"):
            top.add(IconWdg(icon=IconWdg.WARNING))
            top.add("Not permitted to delete this project")
            top.add_style("padding: 30px")
            top.add_style("text-align: center")
            top.add_style("margin: 50px 30px")
            top.add_border()
            top.add_color("background", "background3")
            return top

        if project_code:
            project = Project.get_by_code(project_code)
            if not project:
                top.add(IconWdg(icon=IconWdg.WARNING))
                top.add("No project [%s] exists in this database" %
                        project_code)
                top.add_style("padding: 30px")
                top.add_style("text-align: center")
                top.add_style("margin: 50px 30px")
                top.add_border()
                top.add_color("background", "background3")
                return top
            search_key = project.get_search_key()
        else:
            search_key = my.kwargs.get("search_key")
            project = Search.get_by_search_key(search_key)
            if project:
                project_code = project.get_code()

        title_wdg = DivWdg()

        if project:
            top.add(title_wdg)
            title_wdg.add(IconWdg(icon=IconWdg.WARNING))
            title_wdg.add("Deleting Project: %s" % project.get_value("title"))
            title_wdg.add_color("background", "background", -5)
            title_wdg.add_style("padding: 5px")
            title_wdg.add_style("font-weight: bold")
            title_wdg.add_style("font-size: 14px")

        content = DivWdg()
        top.add(content)
        content.add_style("padding: 10px")

        if not search_key:
            warning_msg = "Projects must be deleted individually"
            content.add(DivWdg(warning_msg, css='warning'))
            content.add("<br/>")
            return top

        warning_msg = "Deleting a project will delete the database associated with this project.  All data and files will be lost.  Please consider carefully before proceeding."
        if warning_msg:
            warning_wdg = DivWdg(warning_msg, css='warning')
            content.add(warning_wdg)
            warning_wdg.add_style("margin: 20 10px")
            content.add("<br/>")

        if not project_code:
            content.add("This project [%s] has been deleted." % search_key)
            return top
        elif not project:
            content.add("This project [%s] has been deleted." % project_code)
            return top

        assert project_code
        assert project

        content.add("<br/>")

        content.add(
            "<b>NOTE: These items will be deleted, but the sTypes entries in search_objects table will be retained.</b> "
        )

        content.add("<br/>")
        content.add("<br/>")

        total_items_wdg = DivWdg()
        total_items = 0
        content.add(total_items_wdg)

        # find all of the sTypes
        details_wdg = DivWdg()
        content.add(details_wdg)
        details_wdg.add_style("max-height: 300px")
        details_wdg.add_style("overflow-y: auto")
        details_wdg.add_style("padding-left: 15px")
        details_wdg.add_border()

        search_types = project.get_search_types()

        related_types = []

        for search_type_obj in search_types:
            search_type_wdg = DivWdg()
            title = search_type_obj.get_title()
            search_type = search_type_obj.get_value("search_type")

            search_type_wdg.add_style("margin-top: 5px")
            search_type_wdg.add_style("margin-bottom: 5px")

            details_wdg.add(search_type_wdg)
            search_type_wdg.add(title)
            search_type_wdg.add(" (%s)" % search_type)

            search = Search(search_type, project_code=project_code)
            count = search.get_count()
            total_items += count
            search_type_wdg.add("&nbsp; - &nbsp; %s item(s)" % count)

            # TODO: this is similar to SearchType.get_related_types(). streamline at some point.
            related_types = my.get_related_types(search_type)
            for related_type in related_types:

                try:
                    search = Search(related_type)
                except Exception, e:
                    print "WARNING: ", e
                    continue
                full_search_type = "%s?project=%s" % (search_type,
                                                      project_code)
                if related_type.startswith("sthpw/"):
                    search.add_filter("search_type", full_search_type)
                count = search.get_count()
                if count == 0:
                    continue
                total_items += count

                related_wdg = DivWdg()
                related_wdg.add_style('padding-left: 25px')
                search_type_wdg.add(related_wdg)
                related_wdg.add(related_type)
                related_wdg.add("&nbsp; - &nbsp; %s item(s)" % count)
Esempio n. 45
0
def query_search_types_extended(project_code, namespace):
    """
    This crazy stuff made to execute queries on server
    All needed info is getting almost half time faster
    :return:
    """
    # TODO remove query, and dig deeper to get more info about pipelines, processes, stypes
    # from pyasm.search import Search
    # from pyasm.biz import Pipeline
    #
    # search_type = 'cgshort/scenes'
    # search = Search("sthpw/pipeline")
    # search.add_filter("search_type", search_type)
    # pipelines = search.get_sobjects()
    #
    # return str(pipelines)
    import json
    from pyasm.biz import Project
    api = server.server
    get_sobject_dict = api.get_sobject_dict

    prj = Project.get_by_code(project_code)

    stypes = prj.get_search_types()

    all_stypes = []
    for stype in stypes:
        stype_dict = get_sobject_dict(stype)
        stype_dict['column_info'] = stype.get_column_info(stype.get_code())
        all_stypes.append(stype_dict)

    # getting pipeline process
    # stypes_codes = []
    # for stype in all_stypes:
    #     stypes_codes.append(stype['code'])
    # stypes_codes.append('sthpw/task')
    search_type = 'sthpw/pipeline'
    # filters = [('search_type', stypes_codes)] - THIS IS VALID CODE
    # filters = [('project_code', 'is', 'NULL'), ('project_code', 'like', project_code)]
    filters = []  # temporary
    stypes_pipelines = server.query(search_type, filters, return_sobjects=True)

    # getting processes info
    pipelines = []
    for stype in stypes_pipelines:
        stypes_dict = stype.get_sobject_dict()

        processes = []
        for process in stype.get_process_names():
            process_sobject = stype.get_process_sobject(process)
            if process_sobject:
                processes.append(api.get_sobject_dict(process_sobject))

        task_processes = []
        for process in stype.get_processes():
            process_obj = process.get_task_pipeline()
            if process_obj != 'task':
                task_processes.append(process_obj)

        stypes_dict['tasks_processes'] = task_processes
        stypes_dict['stypes_processes'] = processes
        pipelines.append(stypes_dict)

    # getting project schema
    schema = server.query('sthpw/schema', [('code', project_code)])

    result = {'schema': schema, 'pipelines': pipelines, 'stypes': all_stypes}

    return json.dumps(result, separators=(',', ':'))
Esempio n. 46
0

        #if project_code in ['default']:
        #    startup = cherrypy.startup
        #    config = startup.config
        #    startup.register_project(project_code, config, site=site)
        #    return



        # if the url does not exist, but the project does, then check to
        # to see if cherrypy knows about it
        project = None
        if has_site:
            try:
                project = Project.get_by_code(project_code)
            except Exception, e:
                print "WARNING: ", e
                raise

        if not has_project and project and project.get_value("type") != 'resource':

            startup = cherrypy.startup
            config = startup.config
            startup.register_project(project_code, config, site=site)
            #cherrypy.config.update( config )

            # give some time to refresh
            #import time
            #time.sleep(1)
Esempio n. 47
0
 def get_database_type(my):
     project = Project.get_by_code(my.project_code)
     db_resource = project.get_project_db_resource()
     db = DbContainer.get(db_resource)
     return db.get_database_type()
Esempio n. 48
0
    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
Esempio n. 49
0
    def get_display(self):
       
        top = self.top
        top.add_color("background", "background")
        top.add_color("color", "color")
        top.add_style("width", "400px")
        top.add_border()
        top.add_class("spt_delete_project_tool_top")
        site = self.kwargs.get("site")
        set_site = self.kwargs.get("set_site")

        if set_site != False and site:
            Site.set_site(site)

        login = Environment.get_user_name()
        
        security = Environment.get_security()

        if not security.is_admin() and not security.is_in_group(self.delete_group):

            top.add(IconWdg(icon=IconWdg.WARNING))
            top.add("Only Admin can delete projects")
            top.add_style("padding: 50px")
            top.add_style("text-align: center")
            if set_site and site:
                Site.pop_site()
            return top






        project_code = self.kwargs.get("project_code")

        # check if delete permissions exist for this site and project
        security = Environment.get_security()
        if False and not security.check_access("project", project_code, "delete"):
            top.add(IconWdg(icon=IconWdg.WARNING))
            top.add("Not permitted to delete this project")
            top.add_style("padding: 30px")
            top.add_style("text-align: center")
            top.add_style("margin: 50px 30px")
            top.add_border()
            top.add_color("background", "background3")
            return top


        


        if project_code:
            project = Project.get_by_code(project_code)
            if not project:
                top.add(IconWdg(icon=IconWdg.WARNING))
                top.add("No project [%s] exists in this database" % project_code)
                top.add_style("padding: 30px")
                top.add_style("text-align: center")
                top.add_style("margin: 50px 30px")
                top.add_border()
                top.add_color("background", "background3")
                return top
            search_key = project.get_search_key()
        else:
            search_key = self.kwargs.get("search_key")
            project = Search.get_by_search_key(search_key)
            if project:
                project_code = project.get_code()

       
           
        title_wdg = DivWdg()

        if project:
            top.add(title_wdg)
            title_wdg.add(IconWdg(icon=IconWdg.WARNING))
            title_wdg.add("Deleting Project: %s" % project.get_value("title") ) 
            title_wdg.add_color("background", "background", -5)
            title_wdg.add_style("padding: 5px")
            title_wdg.add_style("font-weight: bold")
            title_wdg.add_style("font-size: 14px")


        content = DivWdg()
        top.add(content)
        content.add_style("padding: 10px")

        if not search_key:
            warning_msg = "Projects must be deleted individually"
            content.add(DivWdg(warning_msg, css='warning'))
            content.add("<br/>")
            return top

        warning_msg = "Deleting a project will delete the database associated with this project.  All data and files will be lost.  Please consider carefully before proceeding."
        if warning_msg:
            warning_wdg = DivWdg(warning_msg, css='warning')
            content.add(warning_wdg)
            warning_wdg.add_style("margin: 20 10px")
            content.add("<br/>")

        
        if not project_code:
            content.add("This project [%s] has been deleted."%search_key)
            return top
        elif not project:
            content.add("This project [%s] has been deleted."%project_code)
            return top


        assert project_code
        assert project

        content.add("<br/>")

        content.add("<b>NOTE: These items will be deleted, but the sTypes entries in search_objects table will be retained.</b> ")


      
        content.add("<br/>")
        content.add("<br/>")

        total_items_wdg = DivWdg()
        total_items = 0
        content.add(total_items_wdg)


        # find all of the sTypes
        details_wdg = DivWdg()
        content.add(details_wdg)
        details_wdg.add_style("max-height: 300px")
        details_wdg.add_style("overflow-y: auto")
        details_wdg.add_style("padding-left: 15px")
        details_wdg.add_border()

        search_types = project.get_search_types()

        related_types = []

        for search_type_obj in search_types:
            search_type_wdg = DivWdg()
            title = search_type_obj.get_title()
            search_type = search_type_obj.get_value("search_type")

            search_type_wdg.add_style("margin-top: 5px")
            search_type_wdg.add_style("margin-bottom: 5px")


            details_wdg.add(search_type_wdg)
            search_type_wdg.add(title)
            search_type_wdg.add(" (%s)" % search_type )

            search = Search( search_type, project_code=project_code )
            count = search.get_count()
            total_items += count
            search_type_wdg.add("&nbsp; - &nbsp; %s item(s)" % count)

            # TODO: this is similar to SearchType.get_related_types(). streamline at some point. 
            related_types = self.get_related_types(search_type)
            for related_type in related_types:

                try:
                    search = Search(related_type)
                except Exception as e:
                    print("WARNING: ", e)
                    continue
                full_search_type = "%s?project=%s" % (search_type, project_code)
                if related_type.startswith("sthpw/"):
                    search.add_filter("search_type", full_search_type)
                count = search.get_count()
                if count == 0:
                    continue
                total_items += count


                related_wdg = DivWdg()
                related_wdg.add_style('padding-left: 25px')
                search_type_wdg.add(related_wdg)
                related_wdg.add(related_type)
                related_wdg.add("&nbsp; - &nbsp; %s item(s)" % count)


        if total_items:
            total_items_wdg.add("Total # of items to be deleted: ")
            total_items_wdg.add(total_items)
            total_items_wdg.add_style("font-size: 14px")
            total_items_wdg.add_style("font-weight: bold")
            total_items_wdg.add("<br/>")



        content.add("<br/>"*2)
        content.add("<b>Do you wish to continue deleting? </b>")
        radio = RadioWdg("mode")
        radio.add_class("spt_mode_radio")
        radio.set_value("delete")
        radio.add_style("margin-left: 15")
        radio.add_style("margin-top: 5")
        content.add(radio)
        content.add("<br/>"*3)

        #content.add("<b>Or do you just wish to retire the project? </b>")
        #radio = RadioWdg("mode")
        #radio.add_class("spt_mode_radio")
        #radio.set_value("retire")
        #content.add(radio)
        #content.add(radio)

        #content.add("<br/>"*2)


        #button = ActionButtonWdg(title="Retire")
        #content.add(button)
        #button.add_style("float: left")

        buttons = Table()
        content.add(buttons)
        buttons.add_row()
        buttons.add_style("margin-left: auto")
        buttons.add_style("margin-right: auto")
        buttons.add_style("width: 250px")


        button = ActionButtonWdg(title="Delete", color="danger")
        buttons.add_cell(button)

        command_class = self.kwargs.get("command_class")
        if not command_class:
            command_class = 'tactic.ui.tools.DeleteProjectCmd'

        on_complete = self.kwargs.get("on_complete")

        button.add_behavior( {
        'type': 'click_up',
        #'search_type': search_type,
        'project_code': project_code,
        'site': site,
        'related_types': related_types,
        'command_class': command_class,
        'on_complete': on_complete,
        'cbjs_action': '''
            spt.app_busy.show("Deleting");
            var class_name = bvr.command_class;
            var kwargs = {
                'site': bvr.site,
                'project_code': bvr.project_code,
                'related_types': bvr.related_types
            };

            var top = bvr.src_el.getParent(".spt_delete_project_tool_top");
            var radios = top.getElements(".spt_mode_radio");

            //if (!radios[0].checked && !radios[1].checked) {
            if (!radios[0].checked) {
                spt.alert("Please confirm the delete by checking the radio button.");
                spt.app_busy.hide();
                return;
            }

            var mode = 'retire';
            if (radios[0].checked == true) {
                mode = 'delete';
            }


            if (mode == 'retire') {
                return;
            }



            var success = false;
            var server = TacticServerStub.get();
            setTimeout(function() {
                spt.app_busy.show("Deleting Project ["+bvr.project_code+"]")
                var error_message = "Error deleting project ["+bvr.project_code+"]";
                try {
                    server.start({'title': 'Deleted Project ', 'description': 'Deleted Project [' + bvr.project_code + ']'});
                    server.execute_cmd(class_name, kwargs);
                    success = true;
                }
                catch(e) {
                    error_message = spt.exception.handler(e);
                }

                
                spt.app_busy.hide();

                if (success) {


                    if (bvr.on_complete) {
                       on_complete = function() {
                           eval(bvr.on_complete);
                       }
                       on_complete();
                    }

                    spt.notify.show_message("Successfully deleted project ["+bvr.project_code+"]");

                    spt.tab.set_main_body_tab();
                    spt.tab.reload_selected();
                }
                else {
                    if (error_message.test(/does not exist/))
                        error_message += '. You are advised to sign out and log in again.';
                    spt.error(error_message);
                }
                
                
                var top = bvr.src_el.getParent(".spt_popup");
                spt.popup.destroy(top);
                server.finish();
                    
                    
            }, 100);
       
        '''
        } )



        button = ActionButtonWdg(title="Cancel")
        buttons.add_cell(button)
        button.add_behavior( {
        'type': 'click_up',
        'cbjs_action': '''
        var top = bvr.src_el.getParent(".spt_popup");
        spt.popup.destroy(top);
        '''
        } )


        if site:
            Site.pop_site()


        return top
Esempio n. 50
0
    def _get_display(my):

        # set up the security object
        from pyasm.security import Security, Sudo
        from pyasm.biz import Project
        from pyasm.web import WebContainer
        web = WebContainer.get_web()

        security = Security()
        security = my.handle_security(security)
        is_logged_in = security.is_logged_in()


        # guest mode
        #
        allow_guest = Config.get_value("security", "allow_guest")
        if allow_guest == 'true':
            allow_guest = True
        else:
            allow_guest = False

        guest_mode = Config.get_value("security", "guest_mode")
        if not guest_mode:
            guest_mode = 'restricted'

        #allow_guest = True
        #guest_mode = "full"



        # if not logged in, then log in as guest
        if not is_logged_in:
            if not allow_guest:
                return my.handle_not_logged_in()
            else:
                # login as guest
                security = Security()
                my.handle_guest_security(security)


        # for here on, the user is logged in
        login_name = Environment.get_user_name()



        # check if the user has permission to see this project
        project = web.get_context_name()
        if project == 'default':
            override_default = Config.get_value("install", "default_project")
            if override_default:
                project = override_default
        if project != 'default':
            security_version = get_security_version()
            if security_version == 1:
                default = "view"
                access = security.check_access("project", project, "view", default="view")
            else:
                default = "deny"
                key = { "code": project }
                key2 = { "code": "*" }
                #keys = [key]
                keys = [key, key2]
                access = security.check_access("project", keys, "allow", default=default)
        else:
            # you always have access to the default project
            access = True


        access = True
        if not access:
            if login_name == "guest":
                from pyasm.widget import WebLoginWdg

                msg = web.get_form_value(WebLoginWdg.LOGIN_MSG)
                if not msg:
                    msg = "User [%s] is not allowed to see this project [%s]" % (login_name, project)
                    web.set_form_value(WebLoginWdg.LOGIN_MSG, msg)
                return my.handle_not_logged_in(allow_change_admin=False)

            else:
                from pyasm.widget import WebLicenseWdg, BottomWdg, Error403Wdg
                widget = Widget()
                top = my.get_top_wdg()
                widget.add( top )
                widget.add( Error403Wdg() )
                widget.add( BottomWdg() )
                widget.get_display()
     
                return


        if login_name == 'guest' and guest_mode == "full":
            # some extra security for guest users
            guest_url_allow = Config.get_value("security", "guest_url_allow")
            if guest_url_allow:
                items = guest_url_allow.split("|")
                allowed = False
                if my.hash:
                    url = my.hash[0]
                else:
                    url = "index"
                for item in items:
                    item = item.strip("/")
                    if item == url:
                        allowed = True
                        break
                if not allowed:
                    return my.handle_not_logged_in()



        # some extra precautions in guest mode
        if login_name == 'guest' and guest_mode != "full":
            # show a restricted guest mode
            from pyasm.widget import WebLoginWdg, BottomWdg
            from tactic.ui.app import TitleTopWdg

            from pyasm.biz import Project
            from tactic.ui.panel import HashPanelWdg
            web = WebContainer.get_web()

            widget = Widget()
            top = TitleTopWdg()
            widget.add(top)
            body = top.get_body()
            body.add_gradient("background", "background", 5, -20)
            body.add_color("color", "color")

            # get the project from the url because we are still 
            # in the admin project at this stage
            current_project = web.get_context_name()
            try:
                if current_project != "default":
                    project = Project.get_by_code(current_project)
                    assert project
            except Exception, e:
                web_wdg = None
            else:
                if not current_project or current_project == "default":
                    current_project = Config.get_value("install", "default_project")
                if current_project and current_project != "default":
                    Project.set_project(current_project)

                    web_wdg = HashPanelWdg.get_widget_from_hash("/guest", return_none=True)
                    if web_wdg:
                        web_wdg = web_wdg.get_buffer_display()
                        top.add(web_wdg)
                else:
                    web_wdg = None

            if not web_wdg:
                msg = "No widget for Guest defined"
                web.set_form_value(WebLoginWdg.LOGIN_MSG, msg)
                top.add(WebLoginWdg() )


            # create a web app and run it through the pipeline
            web_app = WebApp()
            web_app.get_display(widget)
            return
Esempio n. 51
0
    def execute(my):
        from pyasm.search import DbContainer

        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
Esempio n. 52
0
    def get_display(my):
        top = my.top
        top.add_color("background", "background")
        top.add_color("color", "color")
        top.add_style("width", "400px")
        top.add_border()
        top.add_class("spt_delete_project_tool_top")

        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)
            if project:
                project_code = project.get_code()

        title_wdg = DivWdg()

        if project:
            top.add(title_wdg)
            title_wdg.add(IconWdg(icon=IconWdg.WARNING))
            title_wdg.add("Deleting Project: %s" % project.get_value("title"))
            title_wdg.add_gradient("background", "background", -10, -10)
            title_wdg.add_style("padding: 5px")
            title_wdg.add_style("font-weight: bold")
            title_wdg.add_style("font-size: 14px")

        content = DivWdg()
        top.add(content)
        content.add_style("padding: 10px")

        if not search_key:
            warning_msg = "Projects must be deleted individually"
            content.add(DivWdg(warning_msg, css='warning'))
            content.add("<br/>")
            return top

        warning_msg = "Deleting a project will delete the database associated with this project.  All data will be lost.  Please consider carefully before proceeding."
        if warning_msg:
            content.add(DivWdg(warning_msg, css='warning'))
            content.add("<br/>")

        if not project_code:
            content.add("This project [%s] has been deleted." % search_key)
            return top
        elif not project:
            content.add("This project [%s] has been deleted." % project_code)
            return top

        assert project_code
        assert project

        content.add("<br/>")

        content.add(
            "<b>WARNING: These items will be deleted, but the sTypes entries in search_objects table will be retained.</b> "
        )

        content.add("<br/>")
        content.add("<br/>")

        total_items_wdg = DivWdg()
        total_items = 0
        content.add(total_items_wdg)

        # find all of the sTypes
        details_wdg = DivWdg()
        content.add(details_wdg)
        details_wdg.add_style("max-height: 300px")
        details_wdg.add_style("overflow-y: auto")
        details_wdg.add_style("padding-left: 15px")
        details_wdg.add_border()

        search_types = project.get_search_types()

        related_types = []

        for search_type_obj in search_types:
            search_type_wdg = DivWdg()
            title = search_type_obj.get_title()
            search_type = search_type_obj.get_value("search_type")

            search_type_wdg.add_style("margin-top: 5px")
            search_type_wdg.add_style("margin-bottom: 5px")

            details_wdg.add(search_type_wdg)
            search_type_wdg.add(title)
            search_type_wdg.add(" (%s)" % search_type)

            search = Search(search_type, project_code=project_code)
            count = search.get_count()
            total_items += count
            search_type_wdg.add("&nbsp; - &nbsp; %s item(s)" % count)

            # TODO: this is similar to SearchType.get_related_types(). streamline at some point.
            related_types = my.get_related_types(search_type)
            for related_type in related_types:

                search = Search(related_type)
                full_search_type = "%s?project=%s" % (search_type,
                                                      project_code)
                if related_type.startswith("sthpw/"):
                    search.add_filter("search_type", full_search_type)
                count = search.get_count()
                if count == 0:
                    continue
                total_items += count

                related_wdg = DivWdg()
                related_wdg.add_style('padding-left: 25px')
                search_type_wdg.add(related_wdg)
                related_wdg.add(related_type)
                related_wdg.add("&nbsp; - &nbsp; %s item(s)" % count)

        if total_items:
            total_items_wdg.add("Total # of items to be deleted: ")
            total_items_wdg.add(total_items)
            total_items_wdg.add_style("font-size: 14px")
            total_items_wdg.add_style("font-weight: bold")
            total_items_wdg.add("<br/>")

        content.add("<br/>" * 2)
        content.add("<b>Do you wish to continue deleting? </b>")
        radio = RadioWdg("mode")
        radio.add_class("spt_mode_radio")
        radio.set_value("delete")
        content.add(radio)
        content.add("<br/>" * 2)

        #content.add("<b>Or do you just wish to retire the project? </b>")
        #radio = RadioWdg("mode")
        #radio.add_class("spt_mode_radio")
        #radio.set_value("retire")
        #content.add(radio)
        #content.add(radio)

        #content.add("<br/>"*2)

        #button = ActionButtonWdg(title="Retire")
        #content.add(button)
        #button.add_style("float: left")

        buttons = Table()
        content.add(buttons)
        buttons.add_row()
        buttons.add_style("margin-left: auto")
        buttons.add_style("margin-right: auto")
        buttons.add_style("width: 250px")

        button = ActionButtonWdg(title="Delete")
        buttons.add_cell(button)

        button.add_behavior({
            'type':
            'click_up',
            #'search_type': search_type,
            'project_code':
            project_code,
            'related_types':
            related_types,
            'cbjs_action':
            '''
            spt.app_busy.show("Deleting");
            var class_name = "tactic.ui.tools.DeleteProjectCmd";
            var kwargs = {
                'project_code': bvr.project_code,
                'related_types': bvr.related_types
            };

            var top = bvr.src_el.getParent(".spt_delete_project_tool_top");
            var radios = top.getElements(".spt_mode_radio");

            //if (!radios[0].checked && !radios[1].checked) {
            if (!radios[0].checked) {
                spt.alert("Please confirm the delete by checking the radio button.");
                spt.app_busy.hide();
                return;
            }

            var mode = 'retire';
            if (radios[0].checked == true) {
                mode = 'delete';
            }


            if (mode == 'retire') {
                return;
            }



            var success = false;
            var server = TacticServerStub.get();
            setTimeout(function() {
                spt.app_busy.show("Deleting Project ["+bvr.project_code+"]")
                var error_message = "Error deleting project ["+bvr.project_code+"]";
                try {
                    server.start({'title': 'Deleted Project ', 'description': 'Deleted Project [' + bvr.project_code + ']'});
                    server.execute_cmd(class_name, kwargs);
                    success = true;

                    var top = bvr.src_el.getParent(".spt_popup");
                    spt.popup.destroy(top);
                    server.finish();
                }
                catch(e) {
                    error_message = spt.exception.handler(e);
                }

                
                spt.app_busy.hide();

                if (success) {
                    spt.notify.show_message("Successfully deleted project ["+bvr.project_code+"]");

                    spt.tab.set_main_body_tab();
                    spt.tab.reload_selected();
                }
                else {
                    if (error_message.test(/does not exist/))
                        error_message += '. You are advised to sign out and log in again.';
                    spt.error(error_message);
                }
            }, 100);
       
        '''
        })

        button = ActionButtonWdg(title="Cancel")
        buttons.add_cell(button)
        button.add_behavior({
            'type':
            'click_up',
            'cbjs_action':
            '''
        var top = bvr.src_el.getParent(".spt_popup");
        spt.popup.destroy(top);
        '''
        })

        return top
Esempio n. 53
0
    def _test_search_filter(my):

        # NOTE: this unittest is flawed because it relies on project
        # that may not exist

        my.security.set_admin(False)

        # exclude sample3d tasks and include unittest tasks only
        rules = """
        <rules>
        <rule value='sample3d' search_type='sthpw/task' column='project_code' op='!=' group='search_filter'/>
        <rule value='unittest' search_type='sthpw/task' column='project_code' group='search_filter'/>
        </rules>
        """

        xml = Xml()
        xml.read_string(rules)
        access_manager = Environment.get_security().get_access_manager()
        access_manager.add_xml_rules(xml)

        search = Search('sthpw/task')
        tasks = search.get_sobjects()
        project_codes = SObject.get_values(tasks,'project_code', unique=True)
        my.assertEquals(False, 'sample3d' in project_codes)
        my.assertEquals(True, 'unittest' in project_codes)

        # test list-based expression
        rules = """
        <rules>
        <rule value='$PROJECT' search_type='sthpw/task' column='project_code' group='search_filter'/>
        <rule value="@GET(sthpw/login['login','EQ','unittest'].login)" search_type='sthpw/task' op='in' column='assigned' group='search_filter' project='*'/>
        </rules>
        """
        xml = Xml()
        xml.read_string(rules)
        # reset it
        Environment.get_security().reset_access_manager()

        access_manager = Environment.get_security().get_access_manager()
        access_manager.add_xml_rules(xml)

        search = Search('sthpw/task')
        tasks = search.get_sobjects()
        # 3 tasks were created above for a person
        my.assertEquals(3, len(tasks))
        assigned_codes = SObject.get_values(tasks,'assigned', unique=True)
        project_codes = SObject.get_values(tasks,'project_code', unique=True)
        my.assertEquals({'unittest_guy': 1,'unittest_gal': 1}, my.count(assigned_codes))
        my.assertEquals(True, ['unittest'] == project_codes)

        rules = """
        <rules>
        <rule group="project" code='sample3d' access='allow'/>
        <rule group="project" code='unittest' access='allow'/>
        <rule group="project" code='art' access='allow'/>
        <rule value='$PROJECT' search_type='sthpw/task' column='project_code' group='search_filter'/>
        <rule value='@GET(login.login)' search_type='sthpw/task' column='assigned' group='search_filter' project='*'/>
        </rules>
        """
        xml = Xml()
        xml.read_string(rules)
        # reset it
        security = Environment.get_security()
        security.reset_access_manager()

        access_manager = security.get_access_manager()
        access_manager.add_xml_rules(xml)

        search = Search('sthpw/task')
        tasks = search.get_sobjects()

        # 2 tasks were created above for unittest_guy
        my.assertEquals(2, len(tasks))
        assigned_codes = SObject.get_values(tasks,'assigned', unique=True)
        project_codes = SObject.get_values(tasks,'project_code', unique=True)
        my.assertEquals(True, ['unittest_guy'] == assigned_codes)
        my.assertEquals(True, ['unittest'] == project_codes)

        Project.set_project('sample3d')
        try:
            search = Search('sthpw/task')
            tasks = search.get_sobjects()

            my.assertEquals(1, len(tasks))
            assigned_codes = SObject.get_values(tasks,'assigned', unique=True)
            project_codes = SObject.get_values(tasks,'project_code', unique=True)
            my.assertEquals(True, ['unittest_guy'] == assigned_codes)
            my.assertEquals(True, ['sample3d'] == project_codes)
        finally:
            Project.set_project('unittest')


      

        # project specific rule
        proj_rules = """
        <rules>
        <rule group="project" code='sample3d' access='allow'/>
        <rule group="project" code='unittest' access='allow'/>
        <rule value='$PROJECT' search_type='sthpw/task' column='project_code' group='search_filter'/>
        <rule value='@GET(login.login)' search_type='sthpw/task' column='assigned' group='search_filter' project='unittest'/>
        <rule group="process" process="anim" access="allow"/>
        <rule group="process" process="comp" access="allow"/>
        </rules>
        """
        xml = Xml()
        xml.read_string(proj_rules)
        # reset it
        Environment.get_security().reset_access_manager()

        access_manager = Environment.get_security().get_access_manager()
        access_manager.add_xml_rules(xml)

        project = Project.get_by_code('sample3d')
        if project:
            Project.set_project('sample3d')
            search = Search('sthpw/task')
            tasks = search.get_sobjects()

            assigned_codes = SObject.get_values(tasks,'assigned', unique=True)
            project_codes = SObject.get_values(tasks,'project_code', unique=True)
            # should fail since project is switched to sample3d.. and it should have more than just unittest
            my.assertEquals(False, ['unittest'] == assigned_codes)
            my.assertEquals(True, ['sample3d'] == project_codes)




            # unittest specific rule that uses negation !=, this takes care of NULL value automatically
            rules = """
            <rules>
                <rule group="project" code='sample3d' access='allow'/>
                <rule value='5' search_type='sthpw/task' column='priority' op='!=' group='search_filter' project='sample3d'/>
                 <rule group="process" process="anim" access="allow"/>
                <rule group="process" process="comp" access="allow"/>
            </rules>
            """
            xml = Xml()
            xml.read_string(rules)
            # reset it
            Environment.get_security().reset_access_manager()

            access_manager = Environment.get_security().get_access_manager()
            access_manager.add_xml_rules(xml)

            Project.set_project('sample3d')
            search = Search('sthpw/task')
            tasks = search.get_sobjects()

            priorities = SObject.get_values(tasks,'priority', unique=True)
            #project_codes = SObject.get_values(tasks,'project_code', unique=True)
            
            for p in priorities:
                my.assertEquals(True, p != 5)
      
        try: 
            Project.set_project('unittest')
        except SecurityException, e:
            # should get an SecurityException
            my.assertEquals('User [unittest_guy] is not permitted to view project [unittest]', e.__str__())
            xml = Xml()
            xml.read_string(proj_rules)
            # reset it
            Environment.get_security().reset_access_manager()


            access_manager = Environment.get_security().get_access_manager()
            access_manager.add_xml_rules(xml)
Esempio n. 54
0
    def execute(my):
        from pyasm.search import DbContainer

        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()

       
        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_connection() and sql.connect():
                    raise TacticException("Database [%s] still exists. There could still be connections to it."%project_code)
            except SqlException, e:
                pass