Beispiel #1
0
 def user_id(cls):
     return db.Column(db.ForeignKey('user.id'), index=True)
Beispiel #2
0
 def id(cls):
     return db.Column(db.Integer, primary_key=True, index=True)
Beispiel #3
0
 def user_book_id(self):
     return db.Column(db.ForeignKey('books_of_user.id'), index=True)
Beispiel #4
0
class FileMixin(TimeMixin):
    __repr_attrs__ = [
        'id', 'mime_type', 'uid_filename', 'file_md5', 'name', 'url'
    ]

    mime_type = db.Column(db.String(128))
    uid_filename = db.Column(db.String(256), unique=True)
    name = db.Column(db.String(256))
    file_md5 = db.Column(db.String(256), unique=True)

    @declared_attr
    def id(cls):
        return db.Column(db.Integer, primary_key=True, index=True)

    def __init__(self,
                 filename,
                 mime_type,
                 *args,
                 file_md5=None,
                 uid=None,
                 **kws):
        super(FileMixin, self).__init__(*args, **kws)
        self.name = filename
        self.mime_type = mime_type
        self.file_md5 = file_md5
        self.uid_filename = uid or self.get_uid_filename(filename)

    @classmethod
    def get_uid_filename(cls, filename):
        rs = filename.rsplit('.')
        ext = rs[-1] if len(rs) > 1 else ''
        assert ext
        ufilename = uuid.uuid4().hex + '.' + ext
        return ufilename

    def copy_from_this(self, other_cls):
        return other_cls(filename=self.name,
                         mime_type=self.mime_type,
                         file_md5=self.file_md5)

    @property
    def filepath(self):
        return get_file_path(self.uid_filename)

    @property
    def url(self):
        return file_url_fmt(self.uid_filename)

    @classmethod
    def create_from_file(cls, file):
        filename = secure_filename(file.filename)
        obj = cls(filename, file.mimetype)
        file.save(obj.filepath)

        with open(obj.filepath, 'rb') as f:
            obj.file_md5 = get_file_md5(f)
            record = cls.query.filter_by(file_md5=obj.file_md5).first()
        if record:
            os.remove(obj.filepath)
            return record
        obj.save()
        return obj
Beispiel #5
0
 def book_id(cls):
     return db.Column(db.ForeignKey('book.id'), index=True)