Beispiel #1
0
    def get(self, one=False):
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        cur.execute(
            "SELECT * FROM " + self.TABLE + " WHERE " +
            self.generateWhereCondition() + self.ORDERBY + self.LIMIT,
            self.generateWhereValues())
        if one:
            row = cur.fetchone()
            if row is not None:
                for column, value in row.items():
                    if column in self.ATTRIBUTES:
                        self.ATTRIBUTES[column] = value
                    elif column in self.HIDDEN:
                        self.HIDDEN[column] = value
                self.RESPONSE = self.ATTRIBUTES
            return self

        rows = cur.fetchall()
        if rows is None:
            return self
        self.RESPONSE = []
        for row in rows:
            data = {}
            for column, value in row.items():
                if column in self.ATTRIBUTES:
                    data[column] = value
            self.RESPONSE.append(data)
        return self
Beispiel #2
0
    def delete(self):
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        cur.execute(
            "DELETE FROM " + self.TABLE + " WHERE " +
            self.generateWhereCondition(), self.generateWhereValues())
        conn.commit()

        self.RESPONSE = {}
Beispiel #3
0
    def update(self, *args):
        for key, value in args[0].items():
            self.UPDATES[key] = value

        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        cur.execute(
            "UPDATE " + self.TABLE + " SET " + self.generateUpdateColumns() +
            " WHERE " + self.generateWhereCondition() + " returning *",
            self.generateUpdateValues())
        returnedValue = cur.fetchone()
        self.setData(returnedValue)
        conn.commit()
Beispiel #4
0
    def save(self):
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        cur.execute(
            "INSERT INTO " + self.TABLE + self.generateInsertIntoColumns() +
            "VALUES" + self.generateInsertIntoValueStrings() + " returning *",
            self.generateInsertIntoValues())

        returnedValue = cur.fetchone()
        self.setData(returnedValue)

        conn.commit()
        return self
Beispiel #5
0
 def generateSlug(self, name):
     cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
     slug = slugify(name)
     slug_is_not_unique = True
     i = 2
     tslug = slug
     while slug_is_not_unique:
         cur.execute(
             "SELECT * FROM " + self.TABLE + " WHERE slug=%s LIMIT 1",
             (slug, ))
         found = cur.fetchone()
         if found is not None:
             slug = tslug + str(i)
             i += 1
         else:
             slug_is_not_unique = False
     return slug
Beispiel #6
0
 def hasRole(self, roles):
     cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
     if isinstance(roles, list):
         cur.execute("SELECT id FROM roles WHERE name IN roles")
         role_ids = cur.fetchall()
         for role_id in role_ids:
             cur.execute(
                 "SELECT * FROM user_roles WHERE user_id=%s AND role_id=%s",
                 (self.ATTRIBUTES['id'], role_id))
             exist = cur.fetchone()
             if exist:
                 return True
     else:
         cur.execute("SELECT id FROM roles WHERE name=%s", (roles, ))
         role_id = cur.fetchone()['id']
         cur.execute(
             "SELECT * FROM user_roles WHERE user_id=%s AND role_id=%s",
             (self.ATTRIBUTES['id'], role_id))
         exist = cur.fetchone()
         if exist:
             return True
     return False