def document_store(): form = DocumentCreateForm(request.form) if form.validate(): document = Document() document.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def guestbook_store(): form = GuestbookCreateForm(request.form) if form.validate(): guestbook = Guestbook() guestbook.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def save(self, new_filename=None, quality=95): """ 保存 :param new_filename: 新文件地址 :param quality: 图片质量 :return: """ self._new_file_image = self._old_file_name if new_filename: self._new_file_image = new_filename if self._img.mode == 'RGBA': self._ext = 'png' elif self._img.mode == 'RGB': self._ext = 'jpeg' elif self._img.mode == 'CMYK': self._ext = 'jpeg' else: self._ext = 'png' try: self._img.save(os.path.join(self._storage_path, self._new_file_image), self._ext, quality=quality) except Exception as e: os.remove(os.path.join(self._storage_path, self._new_file_image)) raise Fail(message="图片处理失败") return self._new_file_image
def menu_store(): form = MenuCreateForm(request.form) if form.validate(): menu = Menu() menu.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def link_store(): form = LinkCreateForm(request.form) if form.validate(): link = Link() link.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def admin_store(): form = AdminCreateForm(request.form) if form.validate(): admin = Admin() admin.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def role_store(): form = RoleCreateForm(request.form) if form.validate(): role = Role() role.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def permission_store(): form = PermissionCreateForm(request.form) if form.validate(): permission = Permission() permission.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def note_store(): form = NoteCreateForm(request.form) if form.validate(): note = Note() note.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def focus_store(): form = FocusCreateForm(request.form) if form.validate(): focus = Focus() focus.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def role_delete(ids): ids = ids.split('-') if '1' in ids: return Fail(message="不能删除超级管理员角色") roles = Role.get_all_in_ids(ids=ids) for role in roles: role.destroy() return Success(message="成功删除")
def admin_delete(ids): ids = ids.split('-') if '1' in ids: return Fail(message="网站创始人不能删除") admins = Admin.get_all_in_ids(ids=ids) for admin in admins: admin.destroy() return Success(message="成功删除")
def upload_attach(): upload_type = request.form.get('file_type', 'attach') upload = Upload(upload_type=upload_type) upload.upload() info = upload.info() if info['state'] == "SUCCESS": return Success(data=info) else: return Fail(message=info['state'])
def __init__(self, old_file_name=None): if old_file_name: self._ext = old_file_name.rsplit('.', 1)[1].lower() self._old_file_name = old_file_name self._storage_path = os.path.join(current_app.root_path, 'storage') self._system = read_ini_file('system') try: self._img = Image.open( os.path.join(self._storage_path, old_file_name)) except IOError: raise Fail(message="图片不能打开")
def admin_edit_password(): """ 修改密码 :return: """ form = AdminPasswordForm(request.form) if request.method == "POST": if form.validate(): user = current_user if not user.check_password(form.oldpassword.data): return Fail(message="原始密码错误!") if form.oldpassword.data == form.password.data: return Fail(message="新旧密码必须不一样!") with db.auto_commit(): user.set_password(form.password.data) return Success(message="操作成功!") return Fail(message=form.first_error) else: return render_template('admin/admin/password.html')
def upload_avatar(): upload_type = request.form.get('file_type', 'images') upload = Upload(upload_type=upload_type) upload.upload() info = upload.info() if info['state'] == "SUCCESS": # if upload_type == 'images': # Images(old_file_name=info['filename']).clip(dst_w=100, dst_h=100).thumb(width=250, height=250).save() return Success(data=info) else: return Fail(message=info['state'])
def admin_profile(): form = AdminProfileForm(request.form) if request.method == "POST": if form.validate(): user = current_user with db.auto_commit(): user.set_attrs(form.data) return Success(message="操作成功!") return Fail(message=form.first_error) else: roles = Role.get_all() return render_template('admin/admin/profile.html', roles=roles)
def permission_update(id): edit_one_field = request.form.get('edit_one_field', None) if not edit_one_field: form = PermissionEditForm(formdata=request.form, id=id) else: form = PermissionEditOneKeyForm(formdata=request.form, id=id) if not form.validate(): return Fail(message=form.first_error) permission = Permission.get_by_id(id=id) permission.update(form.data, edit_one_field) return Success(message="操作成功!")
def link_update(id): edit_one_field = request.form.get('edit_one_field', None) if not edit_one_field: form = LinkEditForm(formdata=request.form, id=id) else: form = LinkEditOneKeyForm(formdata=request.form, id=id) if not form.validate(): return Fail(message=form.first_error) link = Link.get_by_id(id=id) link.update(form.data, edit_one_field) return Success(message="操作成功!")
def admin_system(): form = SystemForm(request.form) if request.method == "POST": if form.validate(): system = System.query.first() system.update(form.data) write_ini_file('system', form.data) return Success(message="操作成功!") return Fail(message=form.first_error) else: system = System.query.first() return render_template('admin/system/index.html', system=system)
def upload_thumb(): upload_type = request.form.get('file_type', 'images') upload = Upload(upload_type=upload_type) upload.upload() info = upload.info() if info['state'] == "SUCCESS": if upload_type == 'images': Images(old_file_name=info['filename']).thumb(width=640, height=480).save() return Success(data=info) else: return Fail(message=info['state'])
def admin_mail(): form = MailForm(request.form) if request.method == "POST": if form.validate(): mail = Mail.query.first() mail.update(form.data) write_ini_file('mail', form.data) return Success(message="操作成功!") return Fail(message=form.first_error) else: mail = Mail.query.first() return render_template('admin/mail/index.html', mail=mail)
def admin_edit_avatar(): if request.method == "POST": form = AdminAvatarForm(request.form) if form.validate(): user = current_user with db.auto_commit(): user.set_attrs(form.data) return Success(message="操作成功!") return Fail(message=form.first_error) else: user = current_user avatar = user.avatar return render_template('admin/admin/avatar.html', avatar=avatar)
def upload_file(): action = request.form.get('action', 'chunk_upload') upload_type = request.form.get('file_type', None) promise = { 'chunk_upload': __chunk_upload, 'md5check': __md5check, 'chunk': __chunk, 'merge': __merge, } info = promise[action]() if info['state'] == "SUCCESS": if action == 'merge' and upload_type == "images": image = Images(old_file_name=info['filename']) image.clip(dst_w=100, dst_h=100).thumb(width=250, height=250).save() return Success(data=info) else: return Fail(message=info['state'])
def page_not_found(e): print('user') return Fail(message="page not found")
def csrf_error(e): return Fail(message="CsrfToken错误,请刷新页面重试")
def api_first_or_404(self): rv = self.first() if rv is None: raise Fail(message="找不到该记录!") return rv
def api_get_or_404(self, ident): rv = self.get(ident) if rv is None: raise Fail(message="找不到该记录!") return rv
def water(self, logo_path=None, local=5, offset_x=10, offset_y=10, water_font=None): """ 打水印 :param logo_path: 图片水印地址 :param local: 水印位置 :param offset_x: x偏移 :param offset_y: y偏移 :param water_font: True 字体水印 ,值为文字,就是水印文字 :return: """ bw, bh = self._img.size if self._system['water_type'] == 'image': if logo_path is None: logo_path = os.path.join(current_app.root_path, 'static', self._system['images_water']) else: logo_path = os.path.join(current_app.root_path, logo_path) try: mark = Image.open(logo_path) except IOError as e: raise Fail(message="水印文件找不到") layer = Image.new('RGBA', self._img.size, (0, 0, 0, 0)) lw, lh = mark.size else: water_txt = self._system['txt_water'] font_file = os.path.join(current_app.root_path, 'static', self._system['txt_water_font']) if not os.path.exists(font_file): raise Fail(message="水印字体不存在") n_font = ImageFont.truetype(font_file, size=int( self._system['txt_water_size'])) lw, lh = n_font.getsize(water_txt) x = bw - lw - offset_x y = bh - lh - offset_y if x and y: if local == 1: x = offset_x y = offset_y elif local == 2: x = math.ceil((bw - lw) / 2) y = offset_y elif local == 3: x = bw - lw - offset_x y = offset_y elif local == 4: x = offset_x y = math.ceil((bh - lh) / 2) elif local == 5: x = math.ceil((bw - lw) / 2) y = math.ceil((bh - lh) / 2) elif local == 6: x = bw - lw - offset_x y = math.ceil((bh - lh) / 2) elif local == 7: x = offset_x y = bh - lh - offset_y elif local == 8: x = math.ceil((bw - lw) / 2) y = bh - lh - offset_y if self._system['water_type'] == 'image': layer.paste(mark, (x, y)) self._img = Image.composite(layer, self._img, layer) else: draw = ImageDraw.Draw(self._img) draw.text((x, y), water_txt, font=n_font, fill=self._system['txt_water_color']) return self