def banners(): template = env.get_template('banners.html') if request.method == 'GET': all_banners = Banner.select() return template.render({'banners': all_banners}) elif request.method == 'POST': banner_img = request.files.get('banner_img') banners_folder = static_path('img/banners/') file_path = os.path.join(banners_folder, banner_img.filename) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(banner_img.file.read()) link = post_get('link') parsed_link = urlparse(link) if parsed_link.scheme == '': link = 'http://{0}'.format(link) if not parsed_link.path or parsed_link.path == '#': link = '#' banner = Banner.create(desc=post_get('desc'), link=link, img=banner_img.filename) app.flash('+1 banner') redirect('/banners')
def bannerAdd(): form = BannerrForm() if request.method == "GET": form.active.data = 1 form.type.data = 1 # 提交 print(form.validate_on_submit()) print(form.errors) if form.validate_on_submit(): data = form.data print(data) # 图片处理 move_file(data["picurl"]) banner = Banner(path=data["path"], picurl=data["picurl"].replace('/temp/', '/images/'), title=data["title"], tag=data["tag"], type=int(data["type"]), sort=int(data["sort"]), active=int(data["active"])) db.session.add(banner) db.session.commit() flash("success!", "ok") return redirect(url_for('admin.banners')) return render_template('admin/banner/create.html', form=form)
def banner_add(): """添加banner @@@ #### args | args | nullable | type | remark | |--------|--------|--------|--------| | name | False | String | 昵称 | | desc | True | String | 描述 | | path | True | Int | 图片地址 | #### return - ##### json > {"msg": "success", "code": 200} @@@ """ name = request.form.get("name") if request.form.get("name") else None desc = request.form.get("desc") if request.form.get("desc") else None path = request.form.get("path") if path is None: return jsonify({"status": 201, "msg": "图片不能为空"}) else: res = Banner(name=name, desc=desc, path=path) db.session.add(res) db.session.commit() return jsonify({"name": res.name, "status": 200, "msg": "添加成功"})
def banner_delete(banner_id): try: banner = Banner.get(Banner.banner_id == banner_id) banner.delete_instance() redirect('/banners') except DoesNotExist: abort(404)
def banner_add(): form = BannerForm() if form.validate_on_submit(): data = form.data file_img = secure_filename(form.img.data.filename) if not os.path.exists(app.config["UP_DIR"]): os.makedirs(app.config["UP_DIR"]) os.chmod(app.config["UP_DIR"], "rw") img = change_filename(file_img) form.img.data.save(app.config["UP_DIR"] + img) banner = Banner( title=data["title"], info=data["info"], img=img, ) db.session.add(banner) db.session.commit() flash("图片添加成功", "ok") return redirect(url_for('admin.banner_add')) return render_template("admin/banner_add.html", form=form)
def post_index(): all_posts = Post.get_posts(index_only=True)\ .order_by(Post.date_posted.desc())\ .limit(config.POSTS_PER_PAGE) for item in all_posts: item.post_text = shorten_text(item.post_text) random_banner = Banner.select()\ .where(Banner.disabled == False)\ .order_by(fn.Rand())\ .first() quote = Quote.select().order_by(fn.Rand()).first() messages = StreamMessage.select() template = env.get_template('post/index.html') return template.render(posts=all_posts, banner=random_banner, stream_messages=messages, quote=quote, link_what='pstlink')
def create_banner(): try: res = request.get_json() status = res.get('status') sort = res.get('sort') image = res.get('image') link = res.get('link') article_id = res.get('article_id') group_id = res.get('group_id') admin_id = g.user_id if not all([status, sort, image]): return jsonify(errno="-2", errmsg='参数不完整') if link and article_id: return jsonify(errno="-2", errmsg='不能同时设置文章和外链') try: status = int(status) sort = int(sort) article_id = int(article_id) except Exception as e: print(e) return jsonify(errno="-2", errmsg='参数类型错误') if status not in [0, 1]: return jsonify(errno="-2", errmsg='参数status错误') if sort <= 0: return jsonify(errno="-2", errmsg='请输入大于0的序号') article = Article.query.get(article_id) if not article: return jsonify(errno="-2", errmsg='文章不存在') # group = Group.query.get('group_id') # if not group: # return jsonify(errno="-2", errmsg='文章分类不存在') results = Banner.query.all() count = len(results) if sort > count: return jsonify(errno="-2", errmsg='输入的序号超出范围') for item in results: if item.sort >= sort: item.sort += 1 data = Banner( image=image, link=link, sort=sort, status=status, article_id=article_id, group_id=group_id, admin_id=admin_id ) sort_obj = Banner.query.order_by(Banner.sort.asc()).first() first_sort = sort_obj.sort print(first_sort) if sort <= first_sort and status == 1: # 记录小程序banner第一张图有更新,发送通知 records = UserBTN.query.filter(UserBTN.btn_num == 4).all() for record in records: record.is_new = 1 record.is_send = 0 db.session.add(record) # 记录更新的时间 today = datetime.datetime.today().date() up_obj = UpdateTime.query.filter(UpdateTime.type == 5).filter( UpdateTime.create_time.like(str(today) + "%")).first() if not up_obj: time_obj = UpdateTime() time_obj.type = 5 db.session.add(time_obj) try: db.session.add(data) db.session.commit() data = {'errno': '0', 'msg': 'success'} except Exception as e: db.session.rollback() print(e) data = {'errno': '-2', 'msg': 'Fail'} except Exception as e: print(e) data = {'errno': '-2', 'msg': '网络异常'} return jsonify(data)
def banner_disable(banner_id): banner = Banner.get_or_404(Banner.banner_id == banner_id) banner.disabled = not banner.disabled banner.save() return 'Ok'
def index(): form1 = BannerForm() form2 = PromotionForm() form3 = FeatureForm() productform = ProductForm() productform.catid.choices = [ (c.id, c.subcategories) for c in db.session.query(SubCategories).all() ] productform.pdbid.choices = [(p.id, p.productbrand) for p in db.session.query(ProductBrand).all()] if productform.validate_on_submit(): product = Product(product=productform.product.data, volumn=productform.volumn.data, price=productform.price.data, details=productform.details.data, origin=productform.origin.data, productimage=productform.url.data, categories_id=productform.catid.data, productbrand_id=productform.pdbid.data, pricedown=productform.pricedown.data) db.session.add(product) db.session.commit() flash(_('New Product added')) return redirect(url_for('main.index')) elif form3.validate_on_submit(): feature = Feature(title=form3.title.data, description=form3.description.data, url=form3.url.data) db.session.add(feature) db.session.commit() flash(_('New Promotion added')) return redirect(url_for('main.index')) elif form2.validate_on_submit(): promotion = Promotion(name=form2.name.data, url=form2.url.data) db.session.add(promotion) db.session.commit() flash(_('New Promotion added')) return redirect(url_for('main.index')) elif form1.validate_on_submit(): banneritem = Banner(banner=form1.banner.data) db.session.add(banneritem) db.session.commit() flash(_('New Banner added')) return redirect(url_for('main.index')) banners = Banner.query.order_by(Banner.id) categoriess = Categories.query.order_by(Categories.categories) subcats = SubCategories.query.order_by(SubCategories.subcategories) promotions = Promotion.query.order_by(Promotion.id) features = Feature.query.all() page = request.args.get('page', 1, type=int) pdbrands = ProductBrand.query.order_by(func.random()).paginate( page, current_app.config['PRODUCTBRAND_PER_PAGE'], False) newproducts = Product.query.order_by(Product.id.desc()).paginate( page, current_app.config['NEWPRODUCT_PER_PAGE'], False) reviews = Review.query.all() return render_template('index.html', title=_('Home'), form1=form1, form2=form2, form3=form3, banners=banners, categoriess=categoriess, subcats=subcats, newproducts=newproducts.items, promotions=promotions, features=features, pdbrands=pdbrands.items, page=page, productform=productform, reviews=reviews)