Esempio n. 1
0
 def user_id(cls):
     return db.Column(db.Integer, db.ForeignKey('users.id'))
Esempio n. 2
0
class TorrentBase(DeclarativeHelperBase):
    __tablename_base__ = 'torrents'

    id = db.Column(db.Integer, primary_key=True)
    info_hash = db.Column(BinaryType(length=20),
                          unique=True,
                          nullable=False,
                          index=True)
    display_name = db.Column(db.String(length=255,
                                       collation=COL_UTF8_GENERAL_CI),
                             nullable=False,
                             index=True)
    torrent_name = db.Column(db.String(length=255), nullable=False)
    information = db.Column(db.String(length=255), nullable=False)
    description = db.Column(TextType(collation=COL_UTF8MB4_BIN),
                            nullable=False)

    filesize = db.Column(db.BIGINT, default=0, nullable=False, index=True)
    encoding = db.Column(db.String(length=32), nullable=False)
    flags = db.Column(db.Integer, default=0, nullable=False, index=True)

    @declarative.declared_attr
    def uploader_id(cls):
        # Even though this is same for both tables, declarative requires this
        return db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)

    uploader_ip = db.Column(db.Binary(length=16), default=None, nullable=True)
    has_torrent = db.Column(db.Boolean, nullable=False, default=False)

    comment_count = db.Column(db.Integer,
                              default=0,
                              nullable=False,
                              index=True)

    created_time = db.Column(db.DateTime(timezone=False),
                             default=datetime.utcnow,
                             nullable=False)
    updated_time = db.Column(db.DateTime(timezone=False),
                             default=datetime.utcnow,
                             onupdate=datetime.utcnow,
                             nullable=False)

    @declarative.declared_attr
    def main_category_id(cls):
        fk = db.ForeignKey(cls._table_prefix('main_categories.id'))
        return db.Column(db.Integer, fk, nullable=False)

    sub_category_id = db.Column(db.Integer, nullable=False)

    @declarative.declared_attr
    def redirect(cls):
        fk = db.ForeignKey(cls._table_prefix('torrents.id'))
        return db.Column(db.Integer, fk, nullable=True)

    @declarative.declared_attr
    def __table_args__(cls):
        return (Index(cls._table_prefix('uploader_flag_idx'), 'uploader_id',
                      'flags'),
                ForeignKeyConstraint(['main_category_id', 'sub_category_id'], [
                    cls._table_prefix('sub_categories.main_category_id'),
                    cls._table_prefix('sub_categories.id')
                ]), {})

    @declarative.declared_attr
    def user(cls):
        return db.relationship('User',
                               uselist=False,
                               back_populates=cls._table_prefix('torrents'))

    @declarative.declared_attr
    def main_category(cls):
        return db.relationship(cls._flavor_prefix('MainCategory'),
                               uselist=False,
                               back_populates='torrents',
                               lazy="joined")

    @declarative.declared_attr
    def sub_category(cls):
        join_sql = (
            "and_({0}SubCategory.id == foreign({0}Torrent.sub_category_id), "
            "{0}SubCategory.main_category_id == {0}Torrent.main_category_id)")
        return db.relationship(cls._flavor_prefix('SubCategory'),
                               uselist=False,
                               backref='torrents',
                               lazy="joined",
                               primaryjoin=join_sql.format(cls.__flavor__))

    @declarative.declared_attr
    def filelist(cls):
        return db.relationship(cls._flavor_prefix('TorrentFilelist'),
                               uselist=False,
                               cascade="all, delete-orphan",
                               back_populates='torrent')

    @declarative.declared_attr
    def stats(cls):
        return db.relationship(cls._flavor_prefix('Statistic'),
                               uselist=False,
                               cascade="all, delete-orphan",
                               back_populates='torrent',
                               lazy='joined')

    @declarative.declared_attr
    def trackers(cls):
        return db.relationship(
            cls._flavor_prefix('TorrentTrackers'),
            uselist=True,
            cascade="all, delete-orphan",
            lazy='select',
            order_by=cls._flavor_prefix('TorrentTrackers.order'))

    @declarative.declared_attr
    def comments(cls):
        return db.relationship(cls._flavor_prefix('Comment'),
                               uselist=True,
                               cascade="all, delete-orphan")

    def __repr__(self):
        return '<{0} #{1.id} \'{1.display_name}\' {1.filesize}b>'.format(
            type(self).__name__, self)

    def update_comment_count(self):
        self.comment_count = Comment.query.filter_by(
            torrent_id=self.id).count()
        return self.comment_count

    @property
    def created_utc_timestamp(self):
        ''' Returns a UTC POSIX timestamp, as seconds '''
        return (self.created_time - UTC_EPOCH).total_seconds()

    @property
    def information_as_link(self):
        ''' Formats the .information into an IRC or HTTP(S) <a> if possible,
            otherwise escapes it. '''
        irc_match = re.match(r'^#([a-zA-Z0-9-_]+)@([a-zA-Z0-9-_.:]+)$',
                             self.information)
        if irc_match:
            # Return a formatted IRC uri
            return '<a href="irc://{1}/{0}">#{0}@{1}</a>'.format(
                *irc_match.groups())

        url_match = re.match(r'^(https?:\/\/.+?)$', self.information)
        if url_match:
            url = url_match.group(1)

            invalid_url_characters = '<>"'
            # Check if url contains invalid characters
            if not any(c in url for c in invalid_url_characters):
                return ('<a rel="noopener noreferrer nofollow" '
                        'href="{0}">{1}</a>'.format(
                            url, escape_markup(unquote_url(url))))
        # Escaped
        return escape_markup(self.information)

    @property
    def info_dict_path(self):
        ''' Returns a path to the info_dict file in form of 'info_dicts/aa/bb/aabbccddee...' '''
        info_hash = self.info_hash_as_hex
        return os.path.join(app.config['BASE_DIR'], 'info_dicts',
                            info_hash[0:2], info_hash[2:4], info_hash)

    @property
    def info_hash_as_b32(self):
        return base64.b32encode(self.info_hash).decode('utf-8')

    @property
    def info_hash_as_hex(self):
        return self.info_hash.hex()

    @property
    def magnet_uri(self):
        return create_magnet(self)

    @property
    def uploader_ip_string(self):
        if self.uploader_ip:
            return str(ip_address(self.uploader_ip))

    # Flag properties below

    anonymous = FlagProperty(TorrentFlags.ANONYMOUS)
    hidden = FlagProperty(TorrentFlags.HIDDEN)
    deleted = FlagProperty(TorrentFlags.DELETED)
    banned = FlagProperty(TorrentFlags.BANNED)
    trusted = FlagProperty(TorrentFlags.TRUSTED)
    remake = FlagProperty(TorrentFlags.REMAKE)
    complete = FlagProperty(TorrentFlags.COMPLETE)
    comment_locked = FlagProperty(TorrentFlags.COMMENT_LOCKED)

    # Class methods

    @classmethod
    def by_id(cls, id):
        return cls.query.get(id)

    @classmethod
    def by_info_hash(cls, info_hash):
        return cls.query.filter_by(info_hash=info_hash).first()

    @classmethod
    def by_info_hash_hex(cls, info_hash_hex):
        info_hash_bytes = bytearray.fromhex(info_hash_hex)
        return cls.by_info_hash(info_hash_bytes)
Esempio n. 3
0
 def admin_id(cls):
     return db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
Esempio n. 4
0
 def torrent_id(cls):
     return db.Column(db.Integer,
                      db.ForeignKey(cls._table_prefix('torrents.id'),
                                    ondelete='CASCADE'),
                      nullable=False)
Esempio n. 5
0
 def user_id(cls):
     return db.Column(db.Integer,
                      db.ForeignKey('users.id', ondelete='CASCADE'))
Esempio n. 6
0
class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(length=32, collation=COL_ASCII_GENERAL_CI),
                         unique=True,
                         nullable=False)
    email = db.Column(EmailType(length=255, collation=COL_ASCII_GENERAL_CI),
                      unique=True,
                      nullable=True)
    password_hash = db.Column(PasswordType(max_length=255, schemes=['argon2']),
                              nullable=False)
    status = db.Column(ChoiceType(UserStatusType, impl=db.Integer()),
                       nullable=False)
    level = db.Column(ChoiceType(UserLevelType, impl=db.Integer()),
                      nullable=False)

    created_time = db.Column(db.DateTime(timezone=False),
                             default=datetime.utcnow)
    last_login_date = db.Column(db.DateTime(timezone=False),
                                default=None,
                                nullable=True)
    last_login_ip = db.Column(db.Binary(length=16),
                              default=None,
                              nullable=True)
    registration_ip = db.Column(db.Binary(length=16),
                                default=None,
                                nullable=True)

    nyaa_torrents = db.relationship('NyaaTorrent',
                                    back_populates='user',
                                    lazy='dynamic')
    nyaa_comments = db.relationship('NyaaComment',
                                    back_populates='user',
                                    lazy='dynamic')

    sukebei_torrents = db.relationship('SukebeiTorrent',
                                       back_populates='user',
                                       lazy='dynamic')
    sukebei_comments = db.relationship('SukebeiComment',
                                       back_populates='user',
                                       lazy='dynamic')

    bans = db.relationship('Ban', uselist=True, foreign_keys='Ban.user_id')

    preferences = db.relationship('UserPreferences',
                                  back_populates='user',
                                  uselist=False)

    def __init__(self, username, email, password):
        self.username = username
        self.email = email
        self.password_hash = password
        self.status = UserStatusType.INACTIVE
        self.level = UserLevelType.REGULAR

    def __repr__(self):
        return '<User %r>' % self.username

    def validate_authorization(self, password):
        ''' Returns a boolean for whether the user can be logged in '''
        checks = [
            # Password must match
            password == self.password_hash,
            # Reject inactive and banned users
            self.status == UserStatusType.ACTIVE
        ]
        return all(checks)

    def gravatar_url(self):
        if 'DEFAULT_GRAVATAR_URL' in app.config:
            default_url = app.config['DEFAULT_GRAVATAR_URL']
        else:
            default_url = flask.url_for('static',
                                        filename='img/avatar/default.png',
                                        _external=True)
        if app.config['ENABLE_GRAVATAR']:
            # from http://en.gravatar.com/site/implement/images/python/
            params = {
                # Image size (https://en.gravatar.com/site/implement/images/#size)
                's': 120,
                # Default image (https://en.gravatar.com/site/implement/images/#default-image)
                'd': default_url,
                # Image rating (https://en.gravatar.com/site/implement/images/#rating)
                # Nyaa: PG-rated, Sukebei: X-rated
                'r': 'pg' if app.config['SITE_FLAVOR'] == 'nyaa' else 'x',
            }
            # construct the url
            return 'https://www.gravatar.com/avatar/{}?{}'.format(
                md5(self.email.encode('utf-8').lower()).hexdigest(),
                urlencode(params))
        else:
            return default_url

    @property
    def userlevel_str(self):
        level = ''
        if self.level == UserLevelType.REGULAR:
            level = 'User'
        elif self.level == UserLevelType.TRUSTED:
            level = 'Trusted'
        elif self.level == UserLevelType.MODERATOR:
            level = 'Moderator'
        elif self.level >= UserLevelType.SUPERADMIN:
            level = 'Administrator'
        if self.is_banned:
            level = 'BANNED ' + level
        return level

    @property
    def userstatus_str(self):
        if self.status == UserStatusType.INACTIVE:
            return 'Inactive'
        elif self.status == UserStatusType.ACTIVE:
            return 'Active'
        elif self.status == UserStatusType.BANNED:
            return 'Banned'

    @property
    def userlevel_color(self):
        color = ''
        if self.level == UserLevelType.REGULAR:
            color = 'default'
        elif self.level == UserLevelType.TRUSTED:
            color = 'success'
        elif self.level >= UserLevelType.MODERATOR:
            color = 'purple'
        if self.is_banned:
            color += ' strike'
        return color

    @property
    def ip_string(self):
        if self.last_login_ip:
            return str(ip_address(self.last_login_ip))

    @property
    def reg_ip_string(self):
        if self.registration_ip:
            return str(ip_address(self.registration_ip))

    @classmethod
    def by_id(cls, id):
        return cls.query.get(id)

    @classmethod
    def by_username(cls, username):
        def isascii(s):
            return len(s) == len(s.encode())

        if not isascii(username):
            return None

        user = cls.query.filter_by(username=username).first()
        return user

    @classmethod
    def by_email(cls, email):
        user = cls.query.filter_by(email=email).first()
        return user

    @classmethod
    def by_username_or_email(cls, username_or_email):
        return cls.by_username(username_or_email) or cls.by_email(
            username_or_email)

    @property
    def is_moderator(self):
        return self.level >= UserLevelType.MODERATOR

    @property
    def is_superadmin(self):
        return self.level == UserLevelType.SUPERADMIN

    @property
    def is_trusted(self):
        return self.level >= UserLevelType.TRUSTED

    @property
    def is_banned(self):
        return self.status == UserStatusType.BANNED

    @property
    def is_active(self):
        return self.status != UserStatusType.INACTIVE

    @property
    def age(self):
        '''Account age in seconds'''
        return (datetime.utcnow() - self.created_time).total_seconds()

    @property
    def created_utc_timestamp(self):
        ''' Returns a UTC POSIX timestamp, as seconds '''
        return (self.created_time - UTC_EPOCH).total_seconds()
Esempio n. 7
0
 def tracker_id(cls):
     fk = db.ForeignKey('trackers.id', ondelete="CASCADE")
     return db.Column(db.Integer, fk, primary_key=True)
Esempio n. 8
0
 def main_category_id(cls):
     fk = db.ForeignKey(cls._table_prefix('main_categories.id'))
     return db.Column(db.Integer, fk, primary_key=True)
Esempio n. 9
0
 def torrent_id(cls):
     fk = db.ForeignKey(cls._table_prefix('torrents.id'),
                        ondelete="CASCADE")
     return db.Column(db.Integer, fk, primary_key=True)
Esempio n. 10
0
 def redirect(cls):
     fk = db.ForeignKey(cls._table_prefix('torrents.id'))
     return db.Column(db.Integer, fk, nullable=True)
Esempio n. 11
0
 def main_category_id(cls):
     fk = db.ForeignKey(cls._table_prefix('main_categories.id'))
     return db.Column(db.Integer, fk, nullable=False)
Esempio n. 12
0
 def uploader_id(cls):
     # Even though this is same for both tables, declarative requires this
     return db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
Esempio n. 13
0
class CommentBase(DeclarativeHelperBase):
    __tablename_base__ = 'comments'

    id = db.Column(db.Integer, primary_key=True)

    @declarative.declared_attr
    def torrent_id(cls):
        return db.Column(db.Integer,
                         db.ForeignKey(cls._table_prefix('torrents.id'),
                                       ondelete='CASCADE'),
                         nullable=False)

    @declarative.declared_attr
    def user_id(cls):
        return db.Column(db.Integer,
                         db.ForeignKey('users.id', ondelete='CASCADE'))

    created_time = db.Column(db.DateTime(timezone=False),
                             default=datetime.utcnow)
    edited_time = db.Column(db.DateTime(timezone=False),
                            onupdate=datetime.utcnow)
    text = db.Column(TextType(collation=COL_UTF8MB4_BIN), nullable=False)

    @declarative.declared_attr
    def user(cls):
        return db.relationship('User',
                               uselist=False,
                               back_populates=cls._table_prefix('comments'),
                               lazy="joined")

    @declarative.declared_attr
    def torrent(cls):
        return db.relationship(cls._flavor_prefix('Torrent'),
                               uselist=False,
                               back_populates='comments',
                               lazy="joined")

    def __repr__(self):
        return '<Comment %r>' % self.id

    @property
    def created_utc_timestamp(self):
        ''' Returns a UTC POSIX timestamp, as seconds '''
        return (self.created_time - UTC_EPOCH).total_seconds()

    @property
    def edited_utc_timestamp(self):
        ''' Returns a UTC POSIX timestamp, as seconds '''
        return (self.edited_time -
                UTC_EPOCH).total_seconds() if self.edited_time else 0

    @property
    def editable_until(self):
        return self.created_utc_timestamp + config['EDITING_TIME_LIMIT']

    @property
    def editing_limit_exceeded(self):
        limit = config['EDITING_TIME_LIMIT']
        return bool(
            limit and
            (datetime.utcnow() - self.created_time).total_seconds() >= limit)