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 article(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.articles', nav_id=nav_id, search=search)) #获取产品的详细信息 article_detail = Crud.get_data_by_id(Article, id) #如果点击量为None,赋值0 if not article_detail.click: article_detail.click = 0 #如果有缓存的点击量,更新点击量 if rule in CLICKS_COUNT: article_detail.click = int(article_detail.click) + int( CLICKS_COUNT[rule]) else: article_detail.click = int(article_detail.click) + 1 Crud.easy_update(article_detail) # 如果进行了非法操作 if article_detail.column_id != nav_id: return redirect(url_for('home.index')) #获取标签作为关键词 keywords = ','.join([v.name for v in article_detail.tags]) #SEO信息 seo_data.keywords = keywords seo_data.description = article_detail.description seo_data.title = article_detail.title return render_template( "home/article.html", article_data=article_detail, seo_data=seo_data, )
def reptile_get(): getdata = request.args reptile_request = Crud.get_data_by_id(ReptileRequest, getdata['id']) urls = [] #处理为数组,接受多页查询 if reptile_request.begin_page and reptile_request.end_page: #有起止页面 urls = [ reptile_request.url.replace("{}", str(v)) for v in range(int(reptile_request.begin_page), int(reptile_request.end_page) + 1) ] else: urls.append(reptile_request.url) content_obj = { 'content_name': reptile_request.content_name, 'content_info': reptile_request.content_info, 'content_main': reptile_request.content_main, 'content_img': reptile_request.content_img, } page_list, number = [], 0 for url in urls: page_list.extend(getReptileList(url, reptile_request.dom)) for content_url in page_list: number += 1 print('-' * 40 + '开始爬第%d/%d个数据(%s)' % (number, len(page_list), content_url) + '-' * 40) #如果url已经被爬过了,跳过 count = ReptileList.query.filter( ReptileList.is_del == 0, getattr(ReptileList, 'url') == content_url).count() if count > 0: print('-' * 40 + '该数据已经被拿下' + '-' * 40) continue content = getReptileContent(content_url, content_obj) #如果没有取到数据 if 'content_name' not in content.keys( ) or 'content_main' not in content.keys(): return #如果有图片,下载图片,替换url if 'content_img' in content.keys(): if content['content_img']: content['content_img'] = downImage( full_url(url, content['content_img'])) content['request_id'] = getdata['id'] content['url'] = content_url #匹配新增代码,因批量爬取可能会中断,采用采集一个存一个的方式 # reptile_obj = ReptileList( # request_id=getdata['id'], # url=content_url, # content_name=content['content_name'], # content_info=content.setdefault('content_info', ''), # content_main=content['content_main'], # content_img=content.setdefault('content_img', ''), # ) Crud.add(ReptileList, content, 'url') print('-' * 40 + '第%d个数据已爬取成功' % (number) + '-' * 40) if number > 0: return {"code": 1, "msg": "已成功爬取" + str(number) + "个数据!"} return {"code": 0, "msg": "数据不存在或已经被爬过啦!"}
def admin_reset(): getdata = request.args data = Crud.get_data_by_id(Admin, getdata['id']) data.password = "******" result = Crud.easy_update(data) if result: op_log("重置密码") return {"code": 1, "msg": "重置密码成功!"} return {"code": 0, "msg": '重置失败,系统错误'}
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 admin_edit(): if request.method == 'GET': getdata = request.args data = Crud.get_data_by_id(Admin, getdata['id']) return { "code": 1, "id": data.id, "role_id": data.role_id, "username": data.username } elif request.method == "PUT": data = request.form result = Crud.update(Admin, data) if result: op_log("修改管理员角色#%s" % data["id"]) return {"code": 1, "msg": '修改成功'} return {"code": 0, "msg": '修改失败,系统错误或管理员已存在'}
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 role_edit(): if request.method == 'GET': getdata = request.args if int(getdata["id"]) == 1: return {"code": 0, "msg": "超级管理员不能修改!"} data = Crud.get_data_by_id(Role, getdata["id"]) # auth_list = list(map(lambda v:int(v),(data.auths).split(","))) auth_list = (data.auths).split(",") role_data = {"name": data.name,"id":data.id, "auths": auth_list} return {"code": 1, "data": role_data} elif request.method == "PUT": data = request.form form = RoleForm(data) if form.validate(): result = Crud.update(Role,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_preview(): getdata = request.args data = Crud.get_data_by_id(ReptileList, getdata['id']) return {"code": 1, "data": object_to_dict(data)}