Example #1
1
    def addUser(self, id, email=None):
        if not findUser(id):
            raise ScalakError("User with given login doesn't exists.")

        db = openDB()
        c = db.cursor()
        res = c.execute(
            "select user from user_project where user=%s and \
                project=%s limit 1",
            (id, self.id),
        )
        if res:
            raise ScalakError("User is already project member.")

        c.execute("insert into user_project values (%s, %s)", (id, self.id))

        if not email:
            res = c.execute(
                "select * from users where \
                    login=%s limit 1",
                (id,),
            )
            if res:
                id, name, last_name, email, passwd, sha_passwd, note = c.fetchone()

        db.commit()
        c.close()

        for srv in self.getService():
            srv.addUser(id, name, last_name, email, passwd, sha_passwd, note)
Example #2
1
    def setRequest(self, user, accept):
        db = openDB()
        c = db.cursor()
        c.execute("delete from project_requests where project = %s and user = %s", (self.id, user))
        db.commit()
        c.close()

        if accept:
            self.addUser(user)
Example #3
1
    def removeUser(self, id):
        if id == self.admin:
            raise ScalakError("You can't remove project administrator.")

        db = openDB()
        c = db.cursor()
        c.execute("delete from user_project where user=%s and project=%s", (id, self.id))
        db.commit()
        c.close()

        for srv in self.getService():
            srv.removeUser(id)
Example #4
1
    def isMemeber(self, id):
        """Checking whether user with given id is project member"""

        db = openDB()
        c = db.cursor()
        c.execute(
            "select login from user_project where user=%s and \
                project=%s limit 1",
            (id, self.id),
        )
        db.commit()
        c.close()

        if c.fetchone():
            return True
        return False
Example #5
1
 def add_user(db):
     c = db.cursor()
     c.execute("delete from session_attribute where sid=%s", (login,))
     c.execute("delete from permission where username=%s", (login,))
     c.execute(
         "insert into session_attribute (sid, authenticated, \
             name, value) values (%s, 1, 'email', %s)",
         (login, email),
     )
     c.execute(
         "insert into session_attribute (sid, authenticated, \
             name, value) values (%s, 1, 'name', %s)",
         (login, "{0} {1}".format(name, last_name)),
     )
     c.execute('insert into permission values (%s, "BROWSER_VIEW")', (login,))
     db.commit()
     c.close()
Example #6
1
    def prepareTables(self):
        # First connect raw
        if isinstance(self.db_connect, dict):
            db = self.db.connect(**self.db_connect)
        else:
            db = self.db.connect(*self.db_connect)
        if self.db_mod == "postgresql":
            db.autocommit(1)
        try:
            c = db.cursor()
            if "sqlite" in str(db).lower():
                # Ignore sync-checks for faster import
                c.execute("pragma synchronous = off")
            # Find our root by inspecting our own module
            import testforgetsql2

            root = os.path.dirname(testforgetsql2.__file__)
            file = os.path.join(root, "test-data.sql")
            sql = codecs.open(file, encoding="utf8").read()

            # DROP TABLE
            if self.db_mod == "mysql":
                for table in ("county", "municipal", "postal", "insertion", "shop", "changed"):
                    c.execute("DROP TABLE IF EXISTS %s" % table)
            elif self.db_mod == "postgresql":
                c.execute(
                    """SELECT tablename FROM pg_catalog.pg_tables 
                                 WHERE schemaname=pg_catalog.current_schema()"""
                )
                existing = c.fetchall()
                for table in ("county", "municipal", "postal", "insertion", "shop", "changed"):
                    if (table,) in existing:
                        c.execute("DROP TABLE %s" % table)
            elif self.db_mod == "sqlite":
                # No need to drop tables in sqlite, we blank out the db each
                # time
                pass
            else:
                raise "Unknown db", self.db_mod

            # CREATE TABLE // EXECUTE
            if self.db_mod == "sqlite":
                # We have to fake since sqlite does not support the
                # fancy "bool" type.
                sql = sql.replace("FALSE", "0")
                sql = sql.replace("TRUE", "1")
                c.executescript(sql)
            elif self.db_mod in ("mysql", "postgresql"):
                for statement in sql.split(";"):
                    if not statement.strip():
                        continue  # Skip empty lines
                    c.execute(statement.encode("utf8"))

            # Create database specific table "insertion"
            if self.db_mod == "sqlite":
                # This one is seperate because of "AUTOINCREMENT" vs "AUTO_INCREMENT"
                c.execute(
                    """
                    CREATE TABLE insertion (
                      insertion_id INTEGER PRIMARY KEY AUTOINCREMENT,
                      value VARCHAR(15)
                    )"""
                )
            elif self.db_mod == "mysql":
                c.execute(
                    """
                    CREATE TABLE insertion (
                      insertion_id INTEGER PRIMARY KEY AUTO_INCREMENT,
                      value VARCHAR(15)
                    )"""
                )
            elif self.db_mod == "postgresql":
                c.execute(
                    """
                    CREATE TABLE insertion (
                      insertion_id SERIAL PRIMARY KEY,
                      value VARCHAR(15)
                    )"""
                )
            else:
                raise "Unknown db", self.db_mod
            db.commit()
        finally:
            db.rollback()