Esempio n. 1
0
class Post:
    def __init__(self, id_, title, text, owner, files):
        self._id = id_
        self.title = title
        self.text = text
        self.owner = owner
        self.files = files
        self.__db = DbController(os.path.join(os.getcwd(), '..', 'db.sqlite3'),
                                 'post')

    @staticmethod
    def create(title, text, owner, files):
        try:
            db = DbController(os.path.join(os.getcwd(), '..', 'db.sqlite3'),
                              'post')
            post_id = db.create(title, text, owner)
            db = DbController(os.path.join(os.getcwd(), '..', 'db.sqlite3'),
                              'p_files')
            post_files = []
            if files:
                for i in files:
                    try:
                        id_ = db.create(i[0], post_id, i[1], i[2])
                        post_files.append(id_)
                    except:
                        continue
            return Post(post_id, title, text, owner, post_files)
        except sqlite3.Error as e:
            print(e)
        return None

    @staticmethod
    def all():
        try:
            db = DbController(os.path.join(os.getcwd(), '..', 'db'), 'post')
            posts = db.all()
            user_list = []
            db = DbController(os.path.join(os.getcwd(), '..', 'db'), 'p_files')
            for data in posts:
                file_list = [
                    i['Id'] for i in db.all(param_name='post_id',
                                            param_value=data['Id'])
                ]
                user_list.append(
                    Post(data['Id'], data['title'], data['text'],
                         data['owner'], file_list))
            return user_list
        except sqlite3.Error as e:
            print(e)
        return None

    def update(self):
        try:
            self.__db.update(self._id,
                             title=self.title,
                             text=self.text,
                             owner=self.owner)
            return True
        except sqlite3.Error as e:
            print(e)
            return False

    def delete(self):
        try:
            self.__db.delete(self._id)
            return True
        except sqlite3.Error as e:
            print(e)
            return False
Esempio n. 2
0
class User:
    def __init__(self, id_, username, password):
        self._id = id_
        self.password = password
        self.username = username
        self.__db = DbController(os.path.join(os.getcwd(), '..', 'db.sqlite3'),
                                 'user')

    @staticmethod
    def registration(username, password):
        try:
            db = DbController(os.path.join(os.getcwd(), '..', 'db.sqlite3'),
                              'user')
            id_ = db.create(username=username, password=password)
            return User(id_, username, password)
        except sqlite3.Error as e:
            print(e)
        return None

    def verify(self):
        try:
            db = DbController(os.path.join(os.getcwd(), '..', 'db.sqlite3'),
                              'user')
            id_ = db.query(
                f'SELECT id from user WHERE user_name=\'{self.username}\' and password=\'{self.password}\';'
            )
            if id_:
                return True
        except sqlite3.Error as e:
            print(e)
        return False

    @staticmethod
    def get_user(username):
        try:
            db = DbController(os.path.join(os.getcwd(), '..', 'db'), 'user')
            data = db.get('username', username)
            if not data:
                return None
            return User(data['Id'], data['username'], data['password'])
        except sqlite3.Error as e:
            print(e)
        return None

    @staticmethod
    def all_user():
        try:
            db = DbController(os.path.join(os.getcwd(), '..', 'db'), 'user')
            users = db.all()
            user_list = []
            for data in users:
                user_list.append(
                    User(data['Id'], data['username'], data['password']))
            return user_list
        except sqlite3.Error as e:
            print(e)
        return None

    def update(self):
        try:
            self.__db.update(self._id,
                             username=self.username,
                             password=self.password)
            return True
        except sqlite3.Error as e:
            print(e)
            return False

    def delete_user(self):
        try:
            self.__db.delete(self._id)
            return True
        except sqlite3.Error as e:
            print(e)
            return False