def statistic(): """多条广告展示量/点击量汇总列表展示""" req_arg = get_arg() code = req_arg.get("code") start_date = req_arg.get("start_date") end_date = req_arg.get("end_date") page_index = req_arg.get('page_index') page_size = req_arg.get('page_size') sort = req_arg.get("sort") # 图与表排序不一样 if not page_index or not page_size: page_index = 1 page_size = 20 try: ad_obj = db.session.query(AdCtr.code, AdCtr.show_count, AdCtr.click_count, AdCtr.crt, AdCtr.show_day, AdCtr.click_day, AdCtr.create_date, AdCtr.create_time).filter() if code: ad_obj = ad_obj.filter(AdCtr.code.in_(code.split(','))) if sort: ad_obj = ad_obj.order_by(AdCtr.create_date.desc()) else: ad_obj = ad_obj.order_by(AdCtr.create_date) if not start_date: start_date = time.strftime('%Y-%m-%d %H:%M:%S') if not end_date: end_date = time.strftime('%Y-%m-%d %H:%M:%S') ad_obj = ad_obj.filter(AdCtr.create_date <= end_date, AdCtr.create_date >= start_date).paginate( int(page_index), int(page_size), False) count = ad_obj.total data = all2dict(ad_obj.items) return js(cs.OK, None, {"count": count, "data": data}) except Exception as e: print(traceback.format_exc()) return js(cs.DB_ERR)
def set_ad_style(): """CRUD各端广告""" req_arg = get_arg() status = int(req_arg.get("status")) group_id = req_arg.get("group_id") image_id = req_arg.get("image_id") system = req_arg.get("system") if not req_arg: return js(cs.VALUE_ERROR) need = ("group_id", "image_id", "oper_uid", "oper_uname", "status", "close", "mode", "frequency", "position", "system", "note", "up_time", "down_time") if request.method == "POST": try: group_check = AdGroup.query.filter_by(id=group_id).first() if not group_check: return js(cs.VALUE_ERROR, "组ID不存在") img_obj = AdImage.query.filter_by(id=image_id).first() if not img_obj: return js(cs.VALUE_ERROR, "图片ID不存在") img_obj = values2db(req_arg, AdStyle2(), need) code = str(uuid.uuid1()) img_obj.code = code db.session.add(img_obj) except Exception as e: print(traceback.format_exc()) return js(cs.DB_ERR, "内部错误") elif request.method == "PUT": try: ad_id = int(req_arg.get("id")) if not ad_id: return js(cs.VALUE_ERROR) group_check = AdGroup.query.filter_by(id=group_id).first() if not group_check: return js(cs.VALUE_ERROR, "组ID不存在") img_obj = AdImage.query.filter_by(id=image_id).first() if not img_obj: return js(cs.VALUE_ERROR, "图片ID不存在") style_obj = AdStyle2.query.filter_by(id=ad_id).first() code = style_obj.code img_obj = values2db(req_arg, style_obj, need) db.session.add(img_obj) except Exception as e: print(traceback.format_exc()) return js(cs.DB_ERR, "内部错误") elif request.method == "DELETE": try: ad_id = int(req_arg.get("id")) if not ad_id: return js(cs.VALUE_ERROR) style_obj = AdStyle2.query.filter_by(id=ad_id).first() code = style_obj.code db.session.delete(style_obj) except Exception as e: print(traceback.format_exc()) return js(cs.DB_ERR, "内部错误") else: return js(cs.VALUE_ERROR) sync2redis(code, status) db.session.commit() return js(cs.OK)
def crud_image(): """图片的CRUD""" req_arg = get_arg() pid = req_arg.get('id') # 图片id group_id = req_arg.get('group_id') # 分组id image_name = req_arg.get('image_name') # 图片自定义名称 image_url = req_arg.get('image_url') # 图片存储url ad_url = req_arg.get('ad_url') # 广告url note = req_arg.get('note') if request.method == "GET": if pid: img_obj = AdImage.query.filter_by(id=pid).first() else: img_obj = AdImage.query.filter().all() return js(cs.OK, None, all2dict(img_obj)) elif request.method == "PUT": if pid: group_check = AdGroup2.query.filter_by(id=group_id).first() if not group_check: return js(cs.VALUE_ERROR, "组ID不存在") if ',' not in pid: img_obj = AdImage2.query.filter_by(id=pid).first() if not img_obj: return js(cs.VALUE_ERROR, "图片ID不存在") img_obj = values2db(req_arg, AdImage2(), ("image_name", "image_url", "ad_url", "note", "group_id")) db.session.add(img_obj) else: pids = pid.split(',') # 批量分组 for item in pids: img_obj = AdImage2.query.filter_by(id=item).first() if not img_obj: return js(cs.VALUE_ERROR, "图片ID:%s不存在" % item) img_obj.group_id = group_id db.session.add(img_obj) else: return js(cs.VALUE_ERROR) elif request.method == "POST": group_check = AdGroup.query.filter_by(id=group_id).first() if not group_check: return js(cs.VALUE_ERROR, "组ID不存在") img_obj = values2db(req_arg, AdImage2(), ("image_name", "image_url", "ad_url", "note", "group_id")) db.session.add(img_obj) elif request.method == "DELETE": if pid: if ',' not in pid: img_obj = AdImage2.query.filter_by(id=pid).first() if img_obj: db.session.delete(img_obj) else: pids = pid.split(',') # 批量删除 for item in pids: img_obj = AdImage2.query.filter_by(id=item).first() if img_obj: db.session.delete(img_obj) else: return js(cs.VALUE_ERROR) db.session.commit() return js(cs.OK)
def crud_group(): """分组的CRUD""" req_arg = get_arg() gid = req_arg.get('id') gname = req_arg.get('group') note = req_arg.get('note') if request.method == "GET": if gid: # group_obj = AdGroup.query.filter_by(id=id).first() group_obj = AdGroup.query.filter(id=gid).first() else: group_obj = AdGroup.query.filter().all() return js(cs.OK, None, all2dict(group_obj)) if request.method == "PUT": if gid: group_obj = AdGroup2.query.filter_by(id=gid).first() if not group_obj: return js(cs.VALUE_ERROR, "分组不存在") group_check = AdGroup2.query.filter_by(group=gname).first() if group_check: return js(cs.VALUE_ERROR, "组名已存在") group_obj.group = gname group_obj.note = note db.session.add(group_obj) else: return js(cs.VALUE_ERROR) if request.method == "POST": if gname: group_obj = AdGroup2.query.filter_by(group=gname).first() if group_obj: return js(cs.VALUE_ERROR, "组名已存在") group_obj = values2db(req_arg, AdGroup2(), ("group", "note")) db.session.add(group_obj) else: return js(cs.VALUE_ERROR) if request.method == "DELETE": if gid: group_obj = AdGroup2.query.filter_by(id=gid).first() db.session.delete(group_obj) else: return js(cs.VALUE_ERROR) db.session.commit() return js(cs.OK)
def ad_list(): """广告汇总列表展示""" req_arg = get_arg() status = req_arg.get("status") group_id = req_arg.get("group_id") image_id = req_arg.get("image_id") system = req_arg.get("system") position = req_arg.get("position") page_index = req_arg.get('page_index') page_size = req_arg.get('page_size') if not page_index or not page_size: page_index = 1 page_size = 20 try: ad_obj = db.session.query(AdStyle.id, AdStyle.code, AdStyle.mode, AdStyle.frequency, AdStyle.position, AdStyle.system, AdImage.image_name, AdImage.image_url, AdStyle.ad_url, AdImage.group_id, AdStyle.up_time, AdStyle.down_time, AdStyle.status, AdStyle.note) \ .join(AdImage, AdImage.id == AdStyle.image_id).filter() if status: ad_obj = ad_obj.filter(AdStyle.status == status) if system: ad_obj = ad_obj.filter(AdStyle.system == system) if position: ad_obj = ad_obj.filter(AdStyle.position == position) if image_id: ad_obj = ad_obj.filter(AdStyle.image_id == image_id) if group_id: ad_obj = ad_obj.filter(AdStyle.image_id == group_id) ad_obj = ad_obj.paginate(int(page_index), int(page_size), False) count = ad_obj.total data = all2dict(ad_obj.items) return js(cs.OK, None, {"count": count, "data": data}) except Exception as e: print(traceback.format_exc()) return js(cs.DB_ERR)
def get_ad_style(): # 各端调用广告接口,查询可以展示的广告 req_arg = get_arg() group_key = req_arg.get('system', "1") rds_key = group_dict.get(group_key) now_time = time.strftime('%Y-%m-%d %H:%M:%S') if not group_key: return js(cs.VALUE_ERROR, 'ad_key error', None) try: ad_dict = rds.get(rds_key) # 尝试获取缓存 print("ad_dict:", ad_dict) if ad_dict: ad_dict = json.loads(ad_dict) for item in ad_dict: if item.get("down_time") < now_time: # 删除失效广告缓存 raise Exception("%s Advert Expired" % rds_key) else: ad_dict = refresh(group_key) rds.set(rds_key, json.dumps(ad_dict)) # 加上缓存 except Exception as e: print(traceback.format_exc()) ad_dict = refresh(group_key) rds.set(rds_key, json.dumps(ad_dict)) # 加上缓存 return js(cs.OK, None, ad_dict)