def exec(self, *args, **kwargs):
        try:
            keyword = kwargs['keyword']
            board = kwargs.get('board', '')
            author = kwargs.get('author', '')
        except (IndexError, KeyError):
            raise BadArgsException

        if board == '' and author == '':
            raise BadArgsException
        _type = 'board' if board != '' else 'author'
        name = board if board != '' else author

        cur = db.cursor()
        cur.execute(
            'select id from subscribe where owner_id=? and type=? and name=? and keyword=?',
            (self.user.id, _type, name, keyword))
        if len(cur.fetchall()) > 0:
            self.write('Already subscribed')
            return

        SubscribeModel.create(self.user,
                              _type='board' if board != '' else 'author',
                              name=board if board != '' else author,
                              keyword=keyword)
        self.write('Subscribe successfully')
 def getmany(cls, key, value, like=False):
     cur = db.cursor()
     cur.execute(
         'select * from {} where {} {} ?'.format(cls.__name__.lower(), key,
                                                 'like' if like else '='),
         (value, ))
     res = cur.fetchall()
     objs = [cls(*row) for row in res]
     return objs
Exemplo n.º 3
0
    def create(self, name, user):
        cur = db.cursor()
        res = cur.execute('select name from board where name = ?', [
            name,
        ])
        if res.fetchone() != None:
            raise BoardAlreadyExistException

        cur.execute('INSERT INTO board (name, moderator_id) VALUES (?, ?)',
                    [name, user.id])
        db.commit()
 def get(cls, key, value, like=False):
     cur = db.cursor()
     cur.execute(
         'select * from {} where {} {} ?'.format(cls.__name__.lower(), key,
                                                 'like' if like else '='),
         (value, ))
     res = cur.fetchall()
     if len(res) == 0:
         raise ObjectNotExist
     if len(res) > 1:
         raise objectExistMoreThenOne
     return cls(*res[0])
Exemplo n.º 5
0
    def login(cls, *args):
        if len(args) != 2:
            raise BadArgsException
        cur = db.cursor()
        res = cur.execute(
            'select id, username, email, password from user where username = ?',
            [args[0]])
        res = res.fetchone()
        if res is None:
            raise UsernameNotExistException
        if cls.sha_pass(args[1]) != res[3]:
            raise WrongPasswordException

        return User(*res)
Exemplo n.º 6
0
    def register(cls, *args):
        if len(args) != 3:
            raise BadArgsException
        cur = db.cursor()
        res = cur.execute('select username from user where username = ?',
                          [args[0]])
        if res.fetchone() != None:
            raise UsernameAlreadyExistException

        hashed_pass = cls.sha_pass(args[2])
        cur.execute(
            'INSERT INTO user (username, email, password) VALUES (?, ?, ?)',
            [*args[0:2], hashed_pass])
        db.commit()
    def exec(self, *args, **kwargs):
        board = kwargs.get('board', '')
        author = kwargs.get('author', '')
        if board == '' and author == '':
            raise BadArgsException

        _type = 'board' if board != '' else 'author'
        name = board if board != '' else author

        cur = db.cursor()
        cur.execute(
            'select id from subscribe where owner_id=? and type=? and name=?',
            (self.user.id, _type, name))
        if len(cur.fetchall()) == 0:
            self.write('You haven\'t subscribed {}'.format(name))
            return

        cur.execute(
            'delete from subscribe where owner_id=? and type=? and name=?',
            (self.user.id, _type, name))
        db.commit()
        self.write('Unsubscribe successfully')
 def getall(cls):
     cur = db.cursor()
     cur.execute('select * from {}'.format(cls.__name__.lower()))
     res = cur.fetchall()
     objs = [cls(*row) for row in res]
     return objs