示例#1
0
    def put(self, pid):
        comment = PostModel.find_one_by(id=pid, user_id=current_user.id)
        form = PostForm(meta=dict(csrf=False))
        if comment:
            if form.validate():
                _form = form.form
                # 图片字段不存在时候,NoneType类型无法更新,所以先删除
                del _form['image']

                image = request.files.get('image')
                if image:
                    filename = hash_filename(image.filename)
                    if not uploader.file_allowed(image, filename):
                        raise UploadNotAllowed('file type not allow')
                    path = uploader.save(storage=image,
                                         folder=str(comment.id),
                                         name=filename)
                    print('path ->', path)
                    # 获得对应到文件夹路径
                    url = uploader.url(path)
                    _form['image'] = url

                comment.update(commit=False, **_form)
                db.session.add(comment)
                db.session.commit()
                return dict(code=200, msg='success', data=comment.asdict())
            else:
                return dict(code=400, msg='form validate failure', data=None)
        else:
            return dict(code=404, msg='not found', data=None)
示例#2
0
 def to_json(self):
     """资源和JSON的序列化转换"""
     json_asset = {
         'id': self.id,
         'view_url': uploader.url(self.filepath),
         'filepath': self.filepath,
         'filename': self.filename,
         'created_at': self.created_at
     }
     return json_asset
示例#3
0
 def to_json(self):
     """资源和JSON的序列化转换"""
     json_asset = {
         'id': self.id,
         'serial_no': self.serial_no,
         's_model': self.s_model,
         'cover': uploader.url(self.cover.filepath),
         'sale_price': str(self.sale_price),
         's_weight': str(self.s_weight)
     }
     return json_asset
示例#4
0
    def post(self):
        form = PostForm(meta=dict(csrf=False))
        if form.validate():
            path = ''
            try:
                _form = form.form
                del _form['image']  # 文件对象不能直接放数据库,类型不一致
                m = PostModel.new(**_form,
                                  user_id=current_user.id,
                                  commit=False)

                # 先flush到数据库,获得ID,下面好根据ID存储文件,但是不提交事务,方便回退
                db.session.add(m)
                db.session.flush()

                image = request.files.get('image')
                if image:
                    filename = hash_filename(image.filename)
                    if not uploader.file_allowed(image, filename):
                        raise UploadNotAllowed('file type not allow')
                    path = uploader.save(storage=image,
                                         folder=str(m.id),
                                         name=filename)
                    # 获得对应到文件夹路径
                    url = uploader.url(path)
                    m.image = 'http://198.13.50.56' + url

                # 提交
                db.session.commit()

                return dict(code=200, msg='success', data=m.asdict())
            except Exception as e:
                _logger.exception(e)
                db.session.rollback()
                # 删除无用附件
                if path:
                    os.remove(pathlib.Path(uploader.config.destination) / path)
                return dict(code=500, msg=str(e), data=None)
        else:
            return form.errors
示例#5
0
 def view_url(self):
     return uploader.url(self.filepath)