def delete_scenic_spots(): '''删除景点''' if request.method == 'POST': # 获取id sp_id = request.form.get('id') # 根据id查询,然后删除 try: item = ScenicSpotsModel.query.get(sp_id) # 删除封面图 if os.path.exists(settings.STATIC_DIR + item.image): os.remove(settings.STATIC_DIR + item.image) if item: # 删除对应的图集 item_images = ScenicSpotsImagesModel.query.filter( ScenicSpotsImagesModel.scenic_spots_id == item.id).all() if item_images: for i in item_images: db.session.delete(i) db.session.delete(item) db.session.commit() # 删除完数据库里面的数据后,删除保存在本地的图片 for j in item_images: if os.path.exists(settings.STATIC_DIR + j.image): os.remove(settings.STATIC_DIR + j.image) else: return '失败,资源不存在' except: return '失败' cache.delete('scenic_spots_list') # 删除缓存 return 'true'
def bottom_info(): '''修改网站底部信息''' cache.delete('bottom_info') # 删除缓存 form = BottomInfoForm() is_succeed = 0 # 标志:成功为1,否则为0 try: # 查询看数据库是否有数据 item = BottomInfoModel.query.filter().first() except: return render_template('admin/bottom_info.html', is_succeed=is_succeed, item=None, msg='查询数据失败', form=form) if request.method == 'POST' and form.validate_on_submit(): phone = request.form.get('phone') email = request.form.get('email') phone2 = request.form.get('phone2') # 有数据则更新 if item: item.phone = phone item.email = email item.phone2 = phone2 try: db.session.commit() except: return render_template('admin/bottom_info.html', is_succeed=is_succeed, item=None, msg='失败', form=form) else: item = BottomInfoModel() item.phone = phone item.email = email item.phone2 = phone2 try: db.session.add(item) db.session.commit() except: return render_template('admin/bottom_info.html', is_succeed=is_succeed, item=None, msg='失败', form=form) return render_template('admin/bottom_info.html', is_succeed=1, item=item, form=form) return render_template('admin/bottom_info.html', item=item, is_succeed=is_succeed, form=form)
def add_banner(): '''首页轮播图管理''' cache.delete('image_list') # 删除缓存 form = BannerForm() # 查询所有图片,渲染到页面 image_list = None try: image_list = BannerModel.query.order_by(-BannerModel.id).all() except: return render_template('admin/add_banner.html', form=form, error='数据加载失败') # 上传图片验证 if request.method == 'POST' and form.validate_on_submit(): image = form.image.data # 读取图片,限制大小:15M size = image.read(15 * 1024 * 1024 + 1) if len(size) > 15 * 1024 * 1024: return render_template('admin/add_banner.html', form=form, msg='图片大小限制15M', image_list=image_list) # 将二进制图片保存到本地 image_filename = str(uuid.uuid4()) + '.jpg' # 文件名 image_path = os.path.join(settings.UPLOAD_BANNER_DIR, image_filename) with open(image_path, 'wb') as f: f.write(size) # 图片保存到数据库 item = BannerModel() item.image = '/images/banner/' + image_filename try: db.session.add(item) db.session.commit() return redirect(url_for('admin.add_banner')) except: return render_template('admin/add_banner.html', form=form, msg='上传失败', image_list=image_list) return render_template('admin/add_banner.html', form=form, image_list=image_list)
def delete_banner_img(): '''删除轮播图''' if request.method == 'POST': # 获取id img_id = request.form.get('id') # 根据id查询,然后删除 try: item = BannerModel.query.get(img_id) if item: db.session.delete(item) db.session.commit() # 删除本地的图片 if os.path.exists(settings.STATIC_DIR + item.image): os.remove(settings.STATIC_DIR + item.image) else: return '失败,资源不存在' except: return '失败' cache.delete('image_list') # 删除缓存 return 'true'
def delete_scenic_spots_image(): '''单独删除景点图集对应图片''' if request.method == 'POST': # 获取id sp_image_id = request.form.get('id') # 根据id查询,然后删除 try: item = ScenicSpotsImagesModel.query.get(sp_image_id) if item: db.session.delete(item) db.session.commit() # 删除完数据库里面的数据后,删除保存在本地的图片 if os.path.exists(settings.STATIC_DIR + item.image): os.remove(settings.STATIC_DIR + item.image) else: return '失败,资源不存在' except: return '失败' cache.delete('scenic_spots_list') # 删除缓存 return 'true'
def del_cache_(): """ cache缓存值 :return: """ cache.set('name', 'xiaoming', timeout=30) cache.set('person', {'name': 'aaa', 'age': 20}) x = cache.get('name') print(x) cache.set_many([('name1', 'hhh'), ('name2', 'jjj')]) print(cache.get_many("name1", "name2")) print(cache.delete("name")) print(cache.delete_many("name1", "name2")) print(cache.get('person')) return 'ok'
def change_index_introduction(): '''修改首页简介''' cache.delete('index_brief_introduction') # 删除缓存 form = ChangeIndexIntroductionForm() is_succeed = 0 # 标志:成功为1,否则为0 # get请求,首先查询数据库渲染表单 try: # 查询看数据库是否有数据 item = IndexBriefIntroductionModel.query.filter().first() except: return render_template('admin/change_index_introduction.html', is_succeed=is_succeed, item=None, form=form, msg='查询数据失败') # 表单验证 if request.method == 'POST' and form.validate_on_submit(): # 接收数据 title = form.title.data content = form.content.data opening_hours = form.opening_hours.data rates = form.rates.data # 有则更新,无则添加 if item: item.title = title item.content = content item.create_time = datetime.datetime.now().strftime( '%Y-%m-%d') # 更新时间 item.opening_hours = opening_hours if opening_hours else '24小时开放' item.rates = rates if rates else '免费' # 三元表达式 try: db.session.commit() except: return render_template('admin/change_index_introduction.html', is_succeed=is_succeed, item=item, form=form, msg='修改失败!') else: # 实例化一个新对象 item = IndexBriefIntroductionModel() item.title = title item.content = content item.opening_hours = opening_hours if opening_hours else '24小时开放' item.rates = rates if rates else '免费' # 三元表达式 try: db.session.add(item) db.session.commit() except: return render_template('admin/change_index_introduction.html', form=form, item=item, msg='添加失败!', is_succeed=is_succeed) is_succeed = 1 # 操作成功标志 return render_template('admin/change_index_introduction.html', item=item, form=form, is_succeed=is_succeed) return render_template('admin/change_index_introduction.html', form=form, item=item, is_succeed=is_succeed)
def add_scenic_spot(): '''添加景点''' cache.delete('scenic_spots_list') # 删除缓存 form = AddScenicSpotForm() is_succeed = 0 # 标志:添加成功为1,否则为0 # 有sid则为编辑景点 sid = request.args.get('sid') if sid: # 编辑景点表单验证 form = EditScenicSpotForm() # 查询id对应景点 item = ScenicSpotsModel.query.get(sid) if item and request.method == 'POST' and form.validate_on_submit(): # 接收数据 name = form.name.data content = form.content.data opening_hours = form.opening_hours.data rates = form.rates.data image = form.image.data images = request.files.getlist('images') # 图集 # 判断是否重复提交 if cache.get(name) == 1: return redirect(url_for('admin.admin_index')) # 判断是否上传有封面图 if image: # 判断上传封面图片大小,限制大小:5M size = image.read(5 * 1024 * 1024 + 1) # 限制最大读取字节大小,防止读取全部 if len(size) > 5 * 1024 * 1024: return render_template('admin/add_scenic_spot.html', form=form, image_msg='大小不能超过5M', is_succeed=is_succeed, item=item) # 将二进制图片保存到本地 image_filename = str(uuid.uuid4()) + '.jpg' # 文件名 image_path = os.path.join(settings.SCENIC_SPOT_DIR, image_filename) with open(image_path, 'wb') as f: f.write(size) item.image = '/images/scenic_spot/' + image_filename # 更新数据库 item.name = name item.content = content item.opening_hours = opening_hours if opening_hours else '24小时开放' item.rates = rates if rates else '免费' # 三元表达式 item.create_time = datetime.datetime.now().strftime( '%Y-%m-%d') # 更新时间 db.session.commit() # 判断是否上传有图集 if images[0].filename != '': # 图集处理 for img in images: # 图片后缀名验证 if '.' not in img.filename or img.filename != '' and img.filename.rsplit( '.')[1] not in ('png', 'jpg', 'gif', 'jpeg'): return render_template( 'admin/add_scenic_spot.html', form=form, images_msg='只支持png,jpg,gif,jpeg格式的图片', is_succeed=is_succeed, item=item) # 图片限制大小:15M size = img.read(15 * 1024 * 1024 + 10) # 读取15M加10字节 if len(size) > 15 * 1024 * 1024: return render_template('admin/add_scenic_spot.html', form=form, images_too_big='最大只支持15M的图片', is_succeed=is_succeed, item=item) # 创建文件名,景点id_uuid.jpg img_filename = str(item.id) + '_' + str( uuid.uuid4()) + '.jpg' # 创建文件路径 img_path = os.path.join(settings.SCENIC_SPOT_IMAGES_DIR, img_filename) # 图片保存到本地,二进制写入 with open(img_path, 'wb') as f: f.write(size) # 保存图片到数据库 try: img_item = ScenicSpotsImagesModel() img_item.image = '/images/scenic_spot_images/' + img_filename img_item.scenic_spots_id = item.id db.session.add(img_item) db.session.commit() except: return render_template('admin/add_scenic_spot.html', form=form, is_succeed=is_succeed, msg='添加失败!', item=item) # 防止重复提交,设置唯一标识,放入缓存 cache.set(name, 1, timeout=10) return render_template('admin/add_scenic_spot.html', form=form, item=item, is_succeed=1) # get请求(编辑) return render_template('admin/add_scenic_spot.html', form=form, item=item) # post请求+表单验证 if request.method == 'POST' and form.validate_on_submit(): # 接收数据 name = form.name.data content = form.content.data opening_hours = form.opening_hours.data rates = form.rates.data image = form.image.data images = request.files.getlist('images') # 图集 # 判断是否重复提交 if cache.get(name) == 1: return redirect(url_for('admin.admin_index')) # 判断上传封面图片大小,限制大小:5M size = image.read(5 * 1024 * 1024 + 1) # 限制最大读取字节大小,防止读取全部 if len(size) > 5 * 1024 * 1024: return render_template('admin/add_scenic_spot.html', form=form, image_msg='大小不能超过5M', is_succeed=is_succeed) # 将二进制图片保存到本地 image_filename = str(uuid.uuid4()) + '.jpg' # 文件名 image_path = os.path.join(settings.SCENIC_SPOT_DIR, image_filename) with open(image_path, 'wb') as f: f.write(size) # 查询数据库是否有相同的景点名称,有则更新 try: item = ScenicSpotsModel.query.filter( ScenicSpotsModel.name == name).first() if item: item.name = name item.content = content item.opening_hours = opening_hours if opening_hours else '24小时开放' item.rates = rates if rates else '免费' # 三元表达式 item.image = '/images/scenic_spot/' + image_filename item.create_time = datetime.datetime.now().strftime( '%Y-%m-%d') # 更新时间 db.session.commit() # 判断是否上传有图集 if images[0].filename != '': # 图集处理 for img in images: # 图片后缀名验证 if '.' not in img.filename or img.filename != '' and img.filename.rsplit( '.')[1] not in ('png', 'jpg', 'gif', 'jpeg'): return render_template( 'admin/add_scenic_spot.html', form=form, images_msg='只支持png,jpg,gif,jpeg格式的图片', is_succeed=is_succeed) # 图片限制大小:15M size = img.read(15 * 1024 * 1024 + 1) if len(size) > 15 * 1024 * 1024: return render_template( 'admin/add_scenic_spot.html', form=form, images_too_big='最大只支持15M的图片', is_succeed=is_succeed) # 创建文件名,景点id_uuid.jpg img_filename = str(item.id) + '_' + str( uuid.uuid4()) + '.jpg' # 创建文件路径 img_path = os.path.join( settings.SCENIC_SPOT_IMAGES_DIR, img_filename) # 图片保存到本地,二进制写入 with open(img_path, 'wb') as f: f.write(size) # 保存图片到数据库 try: img_item = ScenicSpotsImagesModel() img_item.image = '/images/scenic_spot_images/' + img_filename img_item.scenic_spots_id = item.id db.session.add(img_item) db.session.commit() except: return render_template( 'admin/add_scenic_spot.html', form=form, is_succeed=is_succeed, msg='添加失败!') # 防止重复提交,设置唯一标识,放入缓存 cache.set(name, 1, timeout=10) return render_template('admin/add_scenic_spot.html', form=form, is_succeed=1) except: return render_template('admin/add_scenic_spot.html', form=form, is_succeed=is_succeed, msg='添加失败!') # 保存到数据库 item = ScenicSpotsModel() item.name = name item.content = content item.opening_hours = opening_hours if opening_hours else '24小时开放' item.rates = rates if rates else '免费' # 三元表达式 item.image = '/images/scenic_spot/' + image_filename try: db.session.add(item) db.session.commit() # 判断是否上传有图集 if images[0].filename != '': # 图集处理 for img in images: # 图片后缀名验证 if '.' not in img.filename or img.filename != '' and img.filename.rsplit( '.')[1] not in ('png', 'jpg', 'gif', 'jpeg'): return render_template( 'admin/add_scenic_spot.html', form=form, images_msg='只支持png,jpg,gif,jpeg格式的图片', is_succeed=is_succeed) # 图片限制大小:15M size = img.read(15 * 1024 * 1024 + 1) if len(size) > 15 * 1024 * 1024: return render_template('admin/add_scenic_spot.html', form=form, images_too_big='最大只支持15M的图片', is_succeed=is_succeed) # 创建文件名,景点id_uuid.jpg img_filename = str(item.id) + '_' + str( uuid.uuid4()) + '.jpg' # 创建文件路径 img_path = os.path.join(settings.SCENIC_SPOT_IMAGES_DIR, img_filename) # 图片保存到本地,二进制写入 with open(img_path, 'wb') as f: f.write(size) # 保存图片到数据库 try: img_item = ScenicSpotsImagesModel() img_item.image = '/images/scenic_spot_images/' + img_filename img_item.scenic_spots_id = item.id db.session.add(img_item) db.session.commit() except: return render_template('admin/add_scenic_spot.html', form=form, is_succeed=is_succeed, msg='添加失败!') # 防止重复提交,设置唯一标识,放入缓存 cache.set(name, 1, timeout=10) return render_template('admin/add_scenic_spot.html', form=form, is_succeed=1) except: return render_template('admin/add_scenic_spot.html', form=form, is_succeed=is_succeed, msg='添加失败!') # get请求 return render_template('admin/add_scenic_spot.html', form=form)
def delete(key): return cache.delete(key)