def upload_videos(): files = request.files.getlist('file') if not files: raise ApiError('请选择要上传的文件') data = upload_to_videos(files) if not data: raise ApiError('上传失败') return api_success(data)
def admin_login(): phone = request.json.get('phone') password = request.json.get('password') admin = Admin.query.filter_by(phone=phone).first() if not admin: raise ApiError('用户不存在') if not admin.verify_password(password): raise ApiError('密码错误') return api_success(admin.to_dict())
def upload_attachment(): file_ = request.files.get('file') if not file_: raise ApiError('请选择要上传的文件') data = upload_to_images(file_) if data.get('status') is True: res = { 'url': data.get('url', ''), } return api_success(res) raise ApiError('上传失败')
def change_password(admin_id): old_password = request.json.get('old_password') new_password = request.json.get('new_password') check_new_password = request.json.get('check_new_password') admin = Admin.query.get_or_404(admin_id) if not admin.verify_password(old_password): raise ApiError('原密码有误') if new_password != check_new_password: raise ApiError('两次密码不一致') admin.password = Admin.generate_password(new_password) admin.save() return api_success(msg='密码修改成功')
def login(phone, password): phone = phone.strip() admin = Admin.query.\ filter_by(phone=phone).\ filter_by(is_delete=False).\ first() if not admin: raise ApiError('该手机未注册') if admin.verify_password(password): login_admin(admin) return admin raise ApiError('密码错误')
def create(cls, data): admin = cls.assemble(data) admin.password = cls.generate_password(admin.password) if Admin.query.filter_by(phone=admin.phone).first(): raise ApiError('手机号已存在') admin.save() return admin
def upload_video(): file_ = request.files.get('file') if not file_: raise ApiError('请选择要上传的文件') data = upload_to_video(file_) url = data.get('url', '') file_size = round(video_service.get_size(url) / 1024 / 1024, 2) if data.get('status') is True: res = { 'url': url, 'thumbnail': data.get('thumbnail', ''), 'file_size': file_size, 'file_name': file_.filename } return api_success(res) raise ApiError('上传失败')
def parse_epub_info(file_): try: ns = { 'n': 'urn:oasis:names:tc:opendocument:xmlns:container', 'pkg': 'http://www.idpf.org/2007/opf', 'dc': 'http://purl.org/dc/elements/1.1/' } _zip = zipfile.ZipFile(file_) txt = _zip.read('META-INF/container.xml') tree = etree.fromstring(txt) cfname = tree.xpath('n:rootfiles/n:rootfile/@full-path', namespaces=ns)[0] cf = _zip.read(cfname) tree = etree.fromstring(cf) p = tree.xpath('/pkg:package/pkg:metadata', namespaces=ns)[0] metadata = {} for s in ['title', 'creator', 'date', 'publisher']: res = p.xpath('dc:%s/text()' % s, namespaces=ns) if res: metadata[s] = res[0] else: metadata[s] = None except: raise ApiError('图书格式错误') return metadata
def upload_ebook(): file_ = request.files.get('file') if not file_: raise ApiError('请选择要上传的文件') data = upload_book(file_) if data.get('status') is True: if not os.path.splitext(data['url'])[1] == '.pdf': metadata = parse_epub_info(file_) else: metadata = '' res = { 'file': data.get('url', ''), 'metadata': metadata, 'pdf_image': data.get('pdf_image') if data.get('pdf_image') else '' } return api_success(res) raise ApiError('上传失败')
def update_picture(picture_id): picture = Picture.query.get(picture_id) if not picture: raise ApiError('图片不存在') data = request.json picture = picture.update(data) res = picture.to_dict() project_ids = data.get('project_ids', []) picture_service.generate_project(project_ids, picture_id) return api_success(res)
def picture_set_order(project_id): # 专题排序轮播图 project = Project.query.get_or_404(project_id) sorted_ids = request.json.get('sorted_ids', []) if len(sorted_ids) == 0: raise ApiError('轮播图的id列表不能为空') project.picture_sort = sorted_ids res = project.picture_sort project.save() return api_success(res)
def update_book(id_): book = Book.query.get(id_) if not book: raise ApiError('书本不存在') data = request.json book = book.update(data) book = book.to_dict() project_ids = data.get('project_ids', []) Project.generate_project(id_, project_ids) return api_success(book)
def update_video(video_id): video = Video.query.get(video_id) if not video: raise ApiError('视频不存在') data = request.json data['file_size'] = video.file_size video = video.update(data) res = video.to_dict() project_ids = data.get('project_ids',[]) video_service.generate_project(video_id, project_ids) return api_success(res)
def add_video(): data = request.json data['file_type'] = os.path.splitext(data['file_path'])[1].strip('.') if data['file_type'] not in video_allow_extensions: raise ApiError('视频格式不正确') data['file_size'] = video_service.get_size(data['file_path']) video = Video.create(data) res = video.to_dict() project_ids = data.get('project_ids', []) video_service.generate_project(res['id'], project_ids) return api_success(res)
def create_project(): # 创建专题 title = request.json.get('title') project = Project.query.filter_by(title=title).first() if project: raise ApiError('专题名称不可重复') data = { 'title': title, 'summary': request.json.get('summary', ''), 'creater_id': g.admin.id } res = Project.create(data) return api_success(res.to_dict())
def read_book(book_id): query = Book.query.with_entities(Book.id).\ filter_by(id=book_id) book = query.first() if not book: raise ApiError('书本不存在') query.update({'read_count': Book.read_count + 1}) db.session.commit() books = Book.query.with_entities(Book.id, ProjectBook.project_id).\ join(ProjectBook, Book.id==ProjectBook.book_id).\ filter(Book.id==book_id).all() StatisticLog.add_read_book_record(books) return api_success(books)
def play_video(video_id): query = Video.query.with_entities(Video.id).\ filter_by(id=video_id) video = query.first() if not video: raise ApiError('视频不存在') query.update({'play_count': Video.play_count + 1}) db.session.commit() query = Video.query.with_entities(Video.id, ProjectVideo.project_id).\ join(ProjectVideo, Video.id==ProjectVideo.video_id).\ filter(Video.id==video_id) videos = query.all() StatisticLog.add_play_count_record(videos) return api_success(videos)
def assemble(cls, data, obj=None): v = cls.generate_validator() update = True if obj else False if not v.validate(data, update=update): raise ApiError(v.errors) obj = obj or cls() attrs = dir(obj) keys = data.keys() for key in keys: if key in cls.global_ignore_assemble_fields: continue if isinstance(getattr(cls, key, ''), InstrumentedAttribute)\ or isinstance(getattr(cls, key, ''), property): setattr(obj, key, data[key]) return obj
def update(self, data): data_keys = data.keys() for field in self.ignore_update_fields: if field in data_keys: del(data[field]) if data.get('phone'): admin = Admin.query.\ filter_by(phone=data.get('phone')).\ filter(Admin.id != self.id).\ first() if admin: raise ApiError('手机号已存在') self = Admin.assemble(data, self) self.save() return self
def update_project(project_id): # 编辑专题 title = request.json.get('title') list_project_id = [project_id] project_title = Project.query.filter(~Project.id.in_(list_project_id)).\ filter_by(title=title).\ first() if project_title: raise ApiError('专题名称不可重复') project = Project.query.get_or_404(project_id) data = { 'title': title, 'summary': request.json.get('summary', '') } res = project.update(data) return api_success(res.to_dict())
def generate_opf_from_epub(file_, url): try: ns = { 'n': 'urn:oasis:names:tc:opendocument:xmlns:container', 'pkg': 'http://www.idpf.org/2007/opf', 'dc': 'http://purl.org/dc/elements/1.1/' } _zip = zipfile.ZipFile(file_) txt = _zip.read('META-INF/container.xml') tree = etree.fromstring(txt) cfname = tree.xpath('n:rootfiles/n:rootfile/@full-path', namespaces=ns)[0] path = current_app.config['APP_PATH'] + url if not os.path.isdir(path + "_files"): os.mkdir(path + "_files") for names in _zip.namelist(): _zip.extract(names, path + "_files/") _zip.close() except Exception as e: current_app.logger.error(e) raise ApiError('opf_path解析失败') return cfname
def delete_book(id_): book = Book.query.get(id_) if not book: raise ApiError('书本不存在') book.soft_delete() return api_success({})
def admin_soft_delete(admin_id): if admin_id == g.admin.id: raise ApiError('当前登录用户不可删除') return Admin.rest_soft_delete(admin_id)
def decorated_view(*args, **kwargs): for arg in header_args: if not arg in request.headers: raise ApiError('请输入heder:{}'.format(arg)) return func(*args, **kwargs)
def decorated_view(*args, **kwargs): for arg in req_args: if not arg in request.args.keys(): raise ApiError('请输入参数:{}'.format(arg)) return func(*args, **kwargs)
def remove_book(project_id, book_id): project_book = ProjectBook.get_project_book(project_id, book_id) if not project_book: raise ApiError('专题不存在或该专题无此书') project_book.delete()
def delete_picture(picture_id): picture = Picture.query.get(picture_id) if not picture: raise ApiError('图片不存在') picture.soft_delete() return api_success({})
def delete_video(video_id): video = Video.query.get(video_id) if not video: raise ApiError('视频不存在') video.soft_delete() return api_success({})