def tpl_extra(): menu_data = Crud.get_data(Menu, Menu.sort.desc()) rule = str(request.url_rule) #用户权限列表 auth_urls = [] if hasattr(current_user, 'id') and current_user.id != 1: auth_urls = session.get('auth_urls') # 如果有分页,去掉分页标签 has_pagination = rule.find("<") if has_pagination>0: rule = rule[0:has_pagination-1] #获取当前菜单信息,用于页面展示 active_menu = Menu.query.filter(Menu.url == rule).first() #配置项 conf_model_data = Crud.get_data(Conf, Conf.sort.desc()) conf_data,webconfig = [],{} for v in conf_model_data: conf = object_to_dict(v) if conf['optional_values']: conf['optional_values'] = (conf['optional_values']).split(',') conf_data.append(conf) webconfig[conf['ename']] = conf['default_value'] data = dict( online_time= datetime.now().strftime("%Y/%m/%d %H:%M:%S"), menu_tree=build_tree(menu_data, 0, 0), rule = rule, active_menu = active_menu, conf_data = conf_data, webconfig = webconfig, auth_urls = auth_urls ) return data
def article_edit(): if request.method == 'GET': getdata = request.args data = Crud.get_data_by_id(Article, getdata["id"]) article_data = object_to_dict(data) #替换掉产品数据里面的TAGS tags = Crud.search_data(TagRelation,and_(TagRelation.relation_id==getdata["id"] , TagRelation.tag_type == 2),'create_time') if tags: article_data['tags'] = [v.tag_id for v in tags] return {"code": 1, "data": article_data} elif request.method == "PUT": data = request.form form = ArticleForm(data) if form.validate(): #移除已保存的tag tags = Crud.search_data(TagRelation,and_(TagRelation.relation_id==data["id"] , TagRelation.tag_type == 2),'create_time') if tags: del_tags = Crud.clean_all(tags) #保存修改后的信息 result = Crud.update(Article,data) # 如果有标签信息,根据产品的id和标签ID保存关联的标签数据 if data['tags']: tags = data['tags'].split(',') tag_data = [TagRelation( tag_type = 2, relation_id = data["id"], tag_id =v ) for v in tags] Crud.add_all(tag_data) if result: op_log("修改文章 #%s" % data["id"]) return {"code": 1, "msg": '修改成功'} return {"code": 0, "msg": '修改失败,系统错误'} return {"code": 0, "msg": form.get_errors()}
def interface(model_name=None): data = {} if model_name =='category': category_data = Crud.get_data(Category,Category.sort.desc()) data = build_tree(category_data, 0, 0) elif model_name == 'adspace': adspace_data = Crud.get_data(Adspace,Adspace.create_time.desc()) data = [ object_to_dict(v) for v in adspace_data] return jsonify(data)
def menu_edit(): if request.method == 'GET': getdata = request.args data = Crud.get_data_by_id(Menu, getdata['id']) return jsonify({"code": 1, "data": object_to_dict(data)}) elif request.method == "PUT": data = request.form form = MenuForm(data) if form.validate(): result = Crud.update(Menu, data, 'name') if result: op_log("修改菜单#%s" % data["id"]) return {"code": 1, "msg": "修改成功!"} return {"code": 0, "msg": "修改失败,系统错误或菜单已存在"} return {"code": 0, "msg": form.get_errors()}
def auth_edit(): if request.method == 'GET': getdata = request.args data = Crud.get_data_by_id(Auth, getdata["id"]) return {"code": 1, "data": object_to_dict(data)} elif request.method == "PUT": data = request.form form = AuthForm(data) if form.validate(): result = Crud.update(Auth, data, 'name') if result: op_log("修改权限 #%s" % data["id"]) return {"code": 1, "msg": '修改成功'} return {"code": 0, "msg": '修改失败,系统错误或名称重复'} return {"code": 0, "msg": form.get_errors()}
def adspace_edit(): if request.method == 'GET': getdata = request.args data = Crud.get_data_by_id(Adspace, getdata['id']) return {"code": 1, "data": object_to_dict(data)} elif request.method == "PUT": data = request.form form = AdspaceForm(data) if form.validate(): result = Crud.update(Adspace,data,"name") if result: op_log("修改广告位#%s" % data["id"]) return {"code": 1, "msg": '修改成功'} return {"code": 0, "msg": '修改失败,系统错误或名称已存在'} return {"code": 0, "msg": form.get_errors()}
def reptile_edit(): if request.method == 'GET': getdata = request.args data = Crud.get_data_by_id(ReptileRequest, getdata['id']) return jsonify({"code": 1, "data": object_to_dict(data)}) elif request.method == "PUT": data = request.form form = ReptileForm(data) if form.validate(): result = Crud.update(ReptileRequest, data, "name") if result: op_log("修改爬虫任务#%s" % data["id"]) return {"code": 1, "msg": '修改成功'} return {"code": 0, "msg": '修改失败,系统错误或名称已存在'} return {"code": 0, "msg": form.get_errors()}
def conf_edit(): if request.method == 'GET': getdata = request.args data = Crud.get_data_by_id(Conf, getdata['id']) conf_data = object_to_dict(data) return jsonify({"code": 1, "data":conf_data}) elif request.method == "PUT": data = request.form form = ConfForm(data) if form.validate(): result = Crud.update(Conf,data,"ename") if result: op_log("修改配置项#%s" % data["id"]) return {"code":1, "msg": '修改成功'} return {"code":0, "msg": '修改失败,系统错误或调用名已存在'} return {"code": 0, "msg": form.get_errors()}
def product(nav_id=None, id=None): rule = str(request.path) if id == None or nav_id == None: return redirect(url_for('home.index')) #如果在详情页搜索 if ('search' in request.args) and (request.args['search']): search = request.args['search'] return redirect(url_for('home.products', nav_id=nav_id, search=search)) #获取产品的详细信息 product_detail = Crud.get_data_by_id(Product, id) #如果点击量为None,赋值0 if not product_detail.click: product_detail.click = 0 #如果有缓存的点击量,更新点击量 if rule in CLICKS_COUNT: product_detail.click = int(product_detail.click) + int( CLICKS_COUNT[rule]) else: product_detail.click = int(product_detail.click) + 1 Crud.easy_update(product_detail) # 如果进行了非法操作 if product_detail.column_id != nav_id: return redirect(url_for('home.index')) #获取相识产品,用途和分类一致的产品 similar_products = Crud.search_data( Product, and_(Product.column_id == product_detail.column_id, Product.relation_id == product_detail.relation_id), Product.sort.desc(), 9) #获取标签作为关键词 keywords = ','.join([v.name for v in product_detail.tags]) #获取图集 pictures = product_detail.pictures.split(',') product_data = object_to_dict(product_detail) product_data['pictures'] = pictures #SEO信息 seo_data.keywords = keywords seo_data.description = product_detail.description seo_data.title = product_detail.title return render_template("home/product.html", product_data=product_data, seo_data=seo_data, similar_products=similar_products)
def update(model,data,not_repeat = None): """ 更新 :param : :return: 提示信息 """ try: model_data = Crud.get_data_by_id(model, data["id"]) copy_data = deepcopy(model_data) if not_repeat != None: count = model.query.filter(model.is_del == 0,getattr(model, not_repeat)==data[not_repeat]).count() if count == 1 and object_to_dict(copy_data)[not_repeat] != data[not_repeat]: return False model_data.set_attrs(data) db.session.commit() return model_data except Exception as e: db.session.rollback() current_app.logger.info(e) return False
def index(nav_id=None, cate_id=None, content_id=None): # 页码 url = request.url rule = str(request.path) page,artpage,seo_data,templates, category_data,all_templates= 1,1,{},[],getCategory(),getTemplates() nav_data, cate_data, content_data = { 'id': nav_id }, { 'id': cate_id }, { 'id': content_id } if request.args.get('page'): page = int(request.args.get('page')) if nav_id: nav_data = [v for v in category_data if v.id == nav_id][0] seo_data = seoData(nav_data.keywords, nav_data.info, nav_data.name) templates_data = [v for v in all_templates if v.nav_id == nav_id] else: templates_data = [v for v in all_templates if v.nav_id == 0] if cate_id: cate_data = [v for v in category_data if v.id == cate_id][0] seo_data = seoData(cate_data.keywords, cate_data.info, cate_data.name) if content_id: # 详情页 tableName = getTableName(int(cate_data.type)) relation_cate, relation_data = {}, [] if tableName: content_data = getSubData(tableName, content_id) #点击量 click = 0 #如果有缓存的点击量,更新点击量 if rule in CLICKS_COUNT: click = int(content_data.click) + int(CLICKS_COUNT[rule]) # 如果记录了缓存的更新,就把数据重置 CLICKS_COUNT[rule] = 0 else: click = int(content_data.click) + 1 Crud.auto_commit('UPDATE {} SET click = {} WHERE id = {} '.format( tableName, click, content_id)) if not content_data.id: return render_template('admin/404.html') seo_data = seoData(content_data.keywords, content_data.description, content_data.title) if content_data.relation_id: relation_cate = [ v for v in category_data if v.id == content_data.relation_id ][0] # 当前产品tag对应的产品 str_tag_content = '' if content_data.tags: tag_content = Crud.search_data( TagRelation, TagRelation.tag_id.in_((content_data.tags).split(',')), 'tag_id') #关联数据查询语句 if tag_content: str_tag_content = 'OR {0}.id in ({1})'.format( tableName, ','.join([ str(x) for x in [ v.relation_id for v in tag_content if v.tag_type == cate_data.type ] ])) relation_data = selectSubData( tableName, '{tableName}.relation_id={0} {1} AND {tableName}.id<>{2}'. format(noneToZero(content_data.relation_id), str_tag_content, content_data.id, tableName=tableName), 1, 8) temp_data = { "temp": { "template": getContentTemplate(int(cate_data.type)) }, "data": { "cate_data": cate_data, "content_data": content_data, "tags": getTag(), "relation_cate": relation_cate, "relation_data": relation_data } } templates.append(temp_data) else: for v in templates_data: temp_data, data = {}, {} ''' temp_data{ temp:模板设置内容 data:{当前栏目数据 sub_category:子栏目数据 sub_data:子栏目数据 tag:所有标签数据 } } ''' temp_data['temp'] = object_to_dict(v) sub_category, cates = [], [] for val in category_data: if v.data_id: if val.id == v.data_id: # 如果分配了数据当前栏目的数据就是分配的栏目 data = object_to_dict(val) if v.relation: # 如果是关联数据,直接取当前栏目的子栏目 if val.pid == nav_id: sub_category.append(val) else: # 当前栏目的子栏目 if val.pid == v.data_id: sub_category.append(val) data['sub_category'] = sub_category #如果是栏目数据 if v.data_type == 1: if cate_id: # url传了分类id cates.append(cate_id) cates = cates + [ cate.id for cate in category_data if cate.pid == cate_id ] else: # URL没有传分类id,读取设置的数据 cates.append(v.data_id) cates = cates + [cate.id for cate in sub_category] tableName = getTableName(int(data['type'])) # 如果是关联查询,就是通过 relation_id 进行搜索 if v.relation: selectColumn = 'relation_id' # 如果不是关联查询,就是通过 category_id 进行搜索 else: selectColumn = 'category_id' str_cats = ','.join([str(x) for x in cates]) data['sub_data'] = selectSubData( tableName, "%s.%s in (%s)" % (tableName, selectColumn, str_cats), page, v.data_num) elif v.data_type == 2: data['sub_data'] = Crud.search_data(Ad, Ad.space_id == v.data_id, Ad.sort.desc(), v.data_num) data['tags'] = getTag() temp_data['data'] = data # 全部页面数据压入数组 templates.append(temp_data) param = { 'nav_data': nav_data, 'cate_data': cate_data, 'content_data': content_data } return render_template("home/%s/home.html" % getWebTemplate(), seo_data=seo_data, templates=templates, param=param)
def reptile_preview(): getdata = request.args data = Crud.get_data_by_id(ReptileList, getdata['id']) return {"code": 1, "data": object_to_dict(data)}