コード例 #1
0
class TestVersion(db.Model):
    __tablename__ = 'ogre_versions'
    id = db.Column(db.Integer, primary_key=True)
    authortitle = db.Column(db.String(500))
    version = db.Column(db.Integer)
    format = db.Column(db.String(20))
    user = db.Column(db.String(100))
    size = db.Column(db.Integer)
    filehash = db.Column(db.String(256))
    uploaded = db.Column(db.Boolean)
    dedrm = db.Column(db.Boolean)
    sdbkey = db.Column(db.String(256))

    def __init__(self, sdbkey):
        self.sdbkey = sdbkey

    def add_value(self, item, value):
        setattr(self, item, value)

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
コード例 #2
0
class Log(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer)
    api_session_key = db.Column(db.String(256))
    timestamp = db.Column(db.DateTime)  # TODO Log timestamp losing seconds
    type = db.Column(db.String(30))
    data = db.Column(db.String(200))

    def __init__(self, user_id, api_session_key, type, data):
        self.user_id = user_id
        self.api_session_key = api_session_key
        self.timestamp = datetime.utcnow()
        self.type = type
        self.data = str(data)

    @staticmethod
    def create(user_id, type, data, api_session_key=None):
        print "%s %s" % (type, data)
        db.session.add(Log(user_id, api_session_key, type, data))
        db.session.commit()
コード例 #3
0
class TestFormat(db.Model):
    __tablename__ = 'ogre_formats'
    id = db.Column(db.Integer, primary_key=True)
    authortitle = db.Column(db.String(500))
    format = db.Column(db.String(20))
    version_count = db.Column(db.Integer)
    sdbkey = db.Column(db.String(256))

    def __init__(self, sdbkey):
        self.sdbkey = sdbkey

    def add_value(self, item, value):
        setattr(self, item, value)

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
コード例 #4
0
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80))
    password = db.Column(db.String(256))
    email = db.Column(db.String(120), unique=True)
    display_name = db.Column(db.String(50), unique=True)
    api_key_expires = db.Column(db.DateTime)
    points = db.Column(db.Integer)
    badges = db.relationship(UserBadge, backref='user', lazy='dynamic')

    def __init__(self, username, password, email):
        self.username = username
        self.password = pwd_context.encrypt(password)
        self.email = email

    @staticmethod
    def authenticate(username, password):
        user = User.query.filter_by(username=username).first()
        if not user:
            return None
        elif security.pwd_context.verify(password, user.password) == False:
            return None
        return user

    @staticmethod
    def _compile_pre_key(username, password, timestamp):
        # construct a unique identifier to be hashed
        return "%s:%s:%s:%s" % (app.config['SECRET_KEY'], username, password,
                                timestamp)

    @staticmethod
    def create_auth_key(username, password, timestamp):
        # hash the value from _compile_pre_key()
        return security.pwd_context.encrypt(
            User._compile_pre_key(username, password, timestamp))

    @staticmethod
    def validate_auth_key(username, api_key):
        # load the user by name
        user = User.query.filter_by(username=username).first()
        if not user:
            return None

        # TODO check API key hasn't expired

        # reconstruct the key and verify it
        prekey = User._compile_pre_key(user.username, user.password,
                                       user.api_key_expires)
        if security.pwd_context.verify(prekey, api_key) == True:
            return user
        else:
            return None

    def assign_auth_key(self):
        # generate a new API key and save against the user
        self.api_key_expires = datetime.utcnow()
        self.api_key_expires = self.api_key_expires.replace(
            microsecond=0)  # remove microseconds for mysql
        api_key = User.create_auth_key(self.username, self.password,
                                       self.api_key_expires)
        db.session.add(self)
        db.session.commit()
        return api_key

    # Flask-Login method
    def is_authenticated(self):
        if self.email is not None:
            return True
        else:
            return False

    def has_badge(self, badge):
        for b in self.badges:
            if b.badge == badge:
                return True
        return False
コード例 #5
0
class TestBook(db.Model, DictMixin):
    __tablename__ = 'ogre_books'
    id = db.Column(db.Integer, primary_key=True)
    authortitle = db.Column(db.String(500))
    users = db.Column(db.String(200))
    formats = db.Column(db.String(200))
    sdbkey = db.Column(db.String(256), unique=True)

    def __init__(self, sdbkey):
        self.sdbkey = sdbkey
        self._users = []
        self._formats = []

    def add_value(self, item, value):
        self.__setitem__(item, value)

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()

    def __getitem__(self, item):
        if item == "authortitle":
            return self.authortitle

        if item == "users":
            if self.users is not None:
                self._users = self.users.split(',')
                return self._users
            else:
                return []

        if item == "formats":
            if self.formats is not None:
                self._formats = self.formats.split(',')
                return self._formats
            else:
                return []

    def __setitem__(self, item, value):
        if item == "authortitle":
            self.authortitle = value

        if item == "users":
            if type(value) is list:
                self._users = value
            else:
                self._users.append(value)

            self.users = ','.join(self._users)

        if item == "formats":
            if type(value) is list:
                self._formats = value
            else:
                self._formats.append(value)

            self.formats = ','.join(self._formats)

        self.save()

    def __delitem__(self, item):
        pass

    def keys(self):
        pass