def do_my_work(self): log.info('worker start, item_id=%s, key=%s' % (self.prj_dict['item_id'], self.prj_dict['data_key'])) kw_list = CacheAdpter.get(self.prj_dict['from_key'], self.prj_dict['from_db'], []) if not kw_list: log.error('can not get group from memcache and the group is = %s' % self.prj_dict['from_key']) kw_list = CacheAdpter.get(self.prj_dict['from_key'], self.prj_dict['from_db'], []) group_dict = {} if kw_list: if ('click > 0' not in self.filter_conf) and ( 'click>0' not in self.filter_conf) or kw_list[0][2] > 0: cat_id = self.prj_dict['from_key'].split('_')[0] try: group_dict = group_kwlist(kw_list, self.item_scorer, int(cat_id), self.cat_cpc, self.cats, self.remove_word_list, self.filter_conf, self.filter_list, self.price_list) except Exception, e: log.error('group_kwlist error: cat_id=%s, e=%s' % (cat_id, e))
def calc_match(prj_dict): prj_dict = prj_dict item_scorer = ItemScorer(prj_dict['label_conf_list']) cats = prj_dict['cats'] filter_conf = prj_dict['filter_conf'] filter_list = prj_dict['filter_list'] price_list = prj_dict['price_list'] remove_word_list = prj_dict['remove_words'] and prj_dict[ 'remove_words'].split(',') or [] cat_cpc = prj_dict['cat_cpc'] log.info('worker start, item_id=%s, key=%s' % (prj_dict['item_id'], prj_dict['data_key'])) kw_list = CacheAdpter.get(prj_dict['from_key'], prj_dict['from_db'], []) if not kw_list: log.error('can not get group from memcache and the group is = %s' % prj_dict['from_key']) kw_list = CacheAdpter.get(prj_dict['from_key'], prj_dict['from_db'], []) group_dict = {} if kw_list: if ('click > 0' not in filter_conf) and ( 'click>0' not in filter_conf) or kw_list[0][2] > 0: cat_id = prj_dict['from_key'].split('_')[0] try: group_dict = group_kwlist(kw_list, item_scorer, int(cat_id), cat_cpc, cats, remove_word_list, filter_conf, filter_list, price_list) except Exception, e: log.error('group_kwlist error: cat_id=%s, e=%s' % (cat_id, e))
def is_accessible(): is_mutual = CacheAdpter.get(CacheKey.MNT_MUTUAL_LOCK % task_type, 'web') loop_count = 0 while (loop_count < 10): loop_count += 1 if not is_mutual: return True else: time.sleep(random.randint(1, 5)) is_mutual = CacheAdpter.get( CacheKey.MNT_MUTUAL_LOCK % task_type, 'web') return False
def get_rob_rank_task(cls): new_task_list = [] is_mutual = CacheAdpter.get(CacheKey.ENGINE_ROBRANK_TASK_MUTUAL_LOCK, 'web') if not is_mutual: # 否则,马上设置缓存,将门关了 kw_lockers = [] try: CacheAdpter.set(CacheKey.ENGINE_ROBRANK_TASK_MUTUAL_LOCK, True, 'web', 60 * 2) now = datetime.datetime.now() time_str = now.strftime('%H:%M') # 关键词自动抢排名的开始时间、结束时间,只在取任务时做检验。每个关键词抢排名时,不再校验,因为可能抢排名时,时间刚好超过该执行的时间 kw_lockers = KeywordLocker.objects.filter( Q(is_stop__in = [0, None], start_time__lte = time_str, end_time__gte = time_str, next_run_time__lte = now) & (Q(is_running__in = [0, None]) | Q(is_running = 1, last_run_time__lte = now - datetime.timedelta(hours = 1)))) \ .order_by('-is_running', 'next_run_time', 'adgroup_id').limit(100) kw_id_list = [kl.keyword_id for kl in kw_lockers] kw_locker_coll.update({'_id': { '$in': kw_id_list }}, {'$set': { 'is_running': 1, 'last_run_time': now }}, multi=True) except Exception, e: log.error("robrank_task get_valid_task error, e=%s" % e) finally:
def load_in_cache_if_not(): flag_dict = CacheAdpter.get(CacheKey.KWLIB_SYNOWORD % -1, 'web', {'init': ''}) if not flag_dict.has_key('init'): return temp_cat_id = -1 word_dict = {} syno_word_list = SynonymWord.objects.all().order_by('cat_id') for sw in syno_word_list: if not sw: continue word_list = sw.word_list.split(',') temp_word_dict = { word.replace('\r', ''): word_list for word in word_list } if sw.cat_id == temp_cat_id: word_dict.update(temp_word_dict) else: CacheAdpter.set(CacheKey.KWLIB_SYNOWORD % temp_cat_id, word_dict, 'web', 60 * 60 * 24 * 7) temp_cat_id = sw.cat_id word_dict = temp_word_dict CacheAdpter.set(CacheKey.KWLIB_SYNOWORD % temp_cat_id, word_dict, 'web', 60 * 60 * 24 * 7) log.info('init all synoword into memcache')
def get_mnt_info(shop_id): """获取左侧导航全自动引擎菜单""" try: mnt_list = CacheAdpter.get(CacheKey.WEB_MNT_MENU % shop_id, 'web', []) if not mnt_list: from apps.subway.models_campaign import Campaign from apps.mnt.models import MntCampaign, MNT_TYPE_CHOICES campid_mnttype_dict = dict( MntCampaign.objects.filter(shop_id=shop_id).values_list( 'campaign_id', 'mnt_type').order_by('start_time')) campid_title_dict = dict( Campaign.objects.filter(shop_id=shop_id).values_list( 'campaign_id', 'title')) mnt_desc_dict = dict(MNT_TYPE_CHOICES) for campaign_id, mnt_type in campid_mnttype_dict.items(): name = campid_title_dict[campaign_id] mnt_type_name = mnt_desc_dict[mnt_type].replace('托管', '') mnt_list.append({ 'name': name, 'campaign_id': campaign_id, 'mnt_type_name': mnt_type_name }) CacheAdpter.set(CacheKey.WEB_MNT_MENU % shop_id, mnt_list, 'web', 60 * 60 * 6) return mnt_list except Exception, e: log.error('get_mnt_info error,e=%s, shop_id=%s' % (e, shop_id)) return []
def load_redis_newcat_word_2memcache(cls): ''' .实时导入新词数据到memcache ''' from apps.kwslt.select_words import MemcacheAdpter r = WordCat.r_wckeyword for cat_id in r.smembers('new_cat_word_set'): count = MemcacheAdpter.get_list_count(str(cat_id), 'kwlib') word_list = [word.decode('utf8') for word in r.lrange('%s_new_word' % cat_id, 0, -1)] if word_list: word_list = WordCat.get_wordcat_data_2memcache(word_list, cat_id) if count: cache_word_list = CacheAdpter.get('%s_%s' % (cat_id, count - 1), 'kwlib') if len(cache_word_list) < 4500: word_list = cache_word_list + word_list count = count - 1 else: count = 0 for wl in genr_sublist(word_list, 4500): CacheAdpter.set('%s_%s' % (cat_id, count), wl, 'kwlib') count += 1 CacheAdpter.set(str(cat_id), count, 'kwlib') r.delete('%s_new_word' % cat_id) r.delete('new_cat_word_set')
def get_attr_by_cat(cls, cat_id, key): """ /通过属性key来获取该类目下的属性,属性key如下所示 cat_path_name, cat_path_id, cat_name, danager_info, product_dict, brand_list, sale_dict, meta_list, forbid_list, cat_child_list, selectconf_dict prop_product_dict, hot_prop_list """ attr_key = CacheKey.KWLIB_CAT_ATTR % (cat_id, key) value = CacheAdpter.get(attr_key, 'web', '') if not value: cat = cls.get_catinfo(cat_id) if cat: value = getattr(cat, key) or '' if value: CacheAdpter.set(attr_key, value, 'web', Const.KWLIB_ATTRIBUT_CACHE_TIME) return value
def get_catinfo(cls, cat_id): cat = None if cat_id != 0 and not cat_id: return cat try: str_cat_id = str(cat_id) cat = CacheAdpter.get(CacheKey.KWLIB_CAT_INFO % str_cat_id, 'web', None) if not cat: cat = cls.get_cat_from_db(cat_id) if not cat: tmp = get_catinfo_new(1, category_id_list=[str_cat_id]) if tmp: tmp = tmp[int(cat_id)] tmp['_id'] = tmp.pop('cat_id') temp_dict = { 'cat_child_list': [], 'danager_info': {}, 'product_dict': {}, 'sale_dict': {}, 'selectconf_dict': {}, 'brand_list': [], 'meta_list': [], 'forbid_list': [], 'exclude_list': [] } cat_coll.insert(dict(tmp), **temp_dict) cat = cls.get_cat_from_db(cat_id) CacheAdpter.set(CacheKey.KWLIB_CAT_INFO % str_cat_id, cat, 'web', Const.KWLIB_CAT_CACHE_TIME) except Exception, e: log.error( "get cat error , can not get the cat by cat_id = %s and the error = %s" % (cat_id, e))
def download_data(shop_id, is_force, **kwargs): cache_key = CacheKey.WEB_SYNC_DATA_MUTUAL % shop_id mutual_timeout = is_force and (60 * 40) or (60 * 10) is_mutual = CacheAdpter.get(cache_key, 'web') if not is_mutual: CacheAdpter.set(cache_key, True, 'web', mutual_timeout) try: dler = Downloader.objects.get(shop_id=shop_id) if not dler.sync_all_struct(is_force=is_force): raise Exception('dl_struct_failed', is_force) args = is_force and { 'is_force': True, 'rpt_days': kwargs['rpt_days'] } or {} if not dler.sync_all_rpt(**args): raise Exception('dl_rpt_failed', is_force) return True, '' except Exception, e: log.error('download data error,shop_id=%s, e=%s' % (shop_id, e.args)) return False, e[0] finally:
def flow_control(request): '''对外部JAPI执行流控控制。因为api函数要调用api_router中的dispatch,貌似无法通过装饰器实现该逻辑''' if not request: return '' req_dict = request.REQUEST isv_key = req_dict.get('appkey', '') if isv_key: limit_count = Config.get_value('common.ISV_APICONTROL_%s' % isv_key, default={}).get(req_dict['method'], None) if limit_count: call_count = CacheAdpter.get( CacheKey.WEB_ISV_APICOUNT % (isv_key, req_dict['method']), 'web', ()) if not call_count: call_count = (1, datetime.datetime.now() ) # TODO 缓存中没有,从数据库取调用数,每间隔1小时,写入数据库 if call_count[0] >= limit_count and time_is_someday( dt=call_count[1], days=0): return { "error_response": { "code": 7, "msg": "App Call Limited", "sub_code": "accesscontrol.limited-by-api-access-count", "sub_msg": "This ban will last for 1 more seconds" } } else: CacheAdpter.set( CacheKey.WEB_ISV_APICOUNT % (isv_key, req_dict['method']), (call_count[0] + 1, datetime.datetime.now()), 'web', 60 * 60 * 24) return ''
def set_session_to_cache(key_type, key, platform, session): '''将session根据不同平台存到缓存中,格式{"web":(web_session, cached_date1), "qn":(qn_session, cached_date2)}''' sess_dict = CacheAdpter.get(CacheKey.WEB_USER_TAPI % (key_type, key), 'web', {}) sess_dict.update({platform: (session, date.today())}) CacheAdpter.set(CacheKey.WEB_USER_TAPI % (key_type, key), sess_dict, 'web', 60 * 60 * 24)
def get_winner_list(cls): cachekey = 'WEB_WINNERS_LIST' show_count = 60 winner_list = CacheAdpter.get(cachekey, 'default', []) if not winner_list: winner_list = [] objs = list(UserLottery.objects.filter(lottery_num = cls.lottery_num, awards_type__in = [1, 2, 3, 4, 5, 6]).order_by('-create_time')[:show_count]) if show_count - len(objs) > 0: # 抽奖人数少时,伪造数据 objs = list(Customer.objects.filter(is_b2c = 1).order_by('-create_time')[:show_count]) # 按权重生成 奖项 list awards_list = [v[0] for v in cls.awards_config_dict.values()] awards_list.extend(['免费宝贝拍摄', '100元软件抵价券', '圣诞礼包']) random_cfg_list = [10 for i in range(len(cls.awards_config_dict))] random_cfg_list.extend([5, 7, 1]) new_awards_list = [] for i, awards_desc in enumerate(awards_list): temp_list = [awards_desc for j in range(random_cfg_list[i])] new_awards_list.extend(temp_list) for obj in objs: size = len(obj.nick) if size > 2: new_user_name = '%s%s%s' % (obj.nick[0], '*' * (min(size - 2, 4)), obj.nick[-1]) awards_desc = random.choice(new_awards_list) winner_list.append({'nick': new_user_name, 'awards_desc': awards_desc}) CacheAdpter.set(cachekey, winner_list, 'default', 60 * 5) return winner_list
def test_api(cls, tapi): if not tapi: return False, 'tapi为空' session = tapi.auth.session_key if session: balance = CacheAdpter.get(CacheKey.NCRM_ACCOUNT_BALANCE % session, 'web', None) if balance is None: try: tobj_balance = tapi.simba_account_balance_get() CacheAdpter.set(CacheKey.NCRM_ACCOUNT_BALANCE % session, tobj_balance.balance, 'web', 60 * 5) return True, '' except Exception, e: error_msg = str(e) if 'Call only from jst' in error_msg: msg = "聚石塔调用限制" elif 'session-expired' in error_msg: msg = "session过期" elif 'Missing session' in error_msg: msg = "缺少session" elif 'session-not-exist' in error_msg: msg = "session不存在" elif 'Invalid session' in error_msg: msg = "session过期" elif 'need to wait' in error_msg: msg = "今日淘宝API接口已调用完" return True, '' # 流控错误放过 else: msg = "其他错误" return False, msg else: return True, ''
def get_seller_cids(request): result_list = CacheAdpter.get(CacheKey.WEB_SHOP_SELLER_CIDS % request.user.shop_id, 'web', []) if not result_list: tapi = get_tapi(request.user) try: tobj = tapi.sellercats_list_get(nick = request.user.nick) except TopError, e: log.error("get_sellercats error, e=%s, shop_id=%s" % (e, request.user.shop_id)) return {'errMsg': '', 'cat_list': result_list} cat_list = [] # 有这些属性:cid, parent_cid, name, pic_url, sort_order, created, modified, type if hasattr(tobj, 'seller_cats') and hasattr(tobj.seller_cats, 'seller_cat'): for top_cat in tobj.seller_cats.seller_cat: cat_list.append({'cat_id': top_cat.cid, 'cat_name': top_cat.name, 'parent_cat_id': top_cat.parent_cid, 'sort_order': top_cat.sort_order}) cat_dict = {} child_cat_dict = collections.defaultdict(list) for cat in cat_list: if cat['parent_cat_id'] != 0: child_cat_dict[cat['parent_cat_id']].append(cat) else: cat_dict.update({cat['sort_order']: cat}) for order, cat in cat_dict.items(): if cat['cat_id'] in child_cat_dict: cat['child_cat_list'] = child_cat_dict[cat['cat_id']] else: cat['child_cat_list'] = [] result_list.append(cat) CacheAdpter.set(CacheKey.WEB_SHOP_SELLER_CIDS % request.user.shop_id, result_list, 'web', 60 * 10)
def load_danger_cats_info(): cache_key = 'danger_cats' danger_cats = CacheAdpter.get(cache_key, 'web') if not danger_cats: jobj = Cat.get_danger_cat_list() danger_cats = json.loads(jobj) CacheAdpter.set(cache_key, danger_cats, 'web', 60 * 60 * 24) return danger_cats
def get_all_configs(cls): sc_dict = CacheAdpter.get(cls.CACHEKEY, 'web', None) if not sc_dict: objs = cls.objects.all() sc_dict = {obj.name: obj for obj in objs} if sc_dict: CacheAdpter.set(cls.CACHEKEY, sc_dict, 'web', 60 * 60 * 24) return sc_dict
def token(self): """获取淘宝的临时验证码""" if not hasattr(self, '_token'): cachekey = CacheKey.SUBWAY_TOKEN % self.shop_id token = CacheAdpter.get(cachekey, 'web') if not token: token = self._get_subway_token() CacheAdpter.set(cachekey, token, 'web', timeout=60 * 60 * 2) self._token = token return self._token
def get(key, sub_key='', shop_id='', fuzzy_key=False): the_key = '%s+%s+%s' % (key, sub_key, shop_id) if CacheAdpter.get(the_key, 'default'): return CacheAdpter.get(the_key, 'default') items = None if sub_key: if shop_id: if fuzzy_key: items = Config.objects.filter(key__contains=key, sub_key__contains=sub_key, shop_id=shop_id) else: items = Config.objects.filter(key=key, sub_key=sub_key, shop_id=shop_id) if not items: # 没有传入shop_id 或者 传入了shop_id但是没有对应shop_id的配置项 if fuzzy_key: items = Config.objects.filter(key__contains=key, sub_key__contains=sub_key) else: items = Config.objects.filter(key=key, sub_key=sub_key) else: if shop_id: if fuzzy_key: items = Config.objects.filter(key__contains=key, shop_id=shop_id) else: items = Config.objects.filter(key=key, shop_id=shop_id) if not items: # 没有传入shop_id 或者 传入了shop_id但是没有对应shop_id的配置项 if fuzzy_key: items = Config.objects.filter(key__contains=key) else: items = Config.objects.filter(key=key) item_list = [] for i in items: try: i.value = i.value.strip() if i.value == '': i.value = "''" i.value = eval(i.value) except Exception, e: i.value = '%s' % (i.value) item_list.append(i)
def execute(cls, poison): signal.signal(signal.SIGINT, signal.SIG_IGN) cachekey = CacheKey.ENGINE_REALTIME_OPTIMIZE_TASK default_last_start_time = datetime.datetime.now() - datetime.timedelta( hours=3) last_start_time, stop_shop_id = CacheAdpter.get( cachekey, 'default', [default_last_start_time, 0]) log.info( 'start engine for realtime_optimize, last_start_time=%s, stop_shop_id=%s' % (last_start_time, stop_shop_id)) while True: if poison.is_set(): break if not cls.is_time_todo(): time.sleep(2) continue if (datetime.datetime.now() - last_start_time ).seconds < 60 * 60 * 2 and (not stop_shop_id): time.sleep(2) continue adg_opt_count, kw_opt_count = 0, 0 log.info( '================================= realtime optimize start =================================' ) shop_id_list = cls.get_valid_shops() last_start_time = datetime.datetime.now() for shop_id in shop_id_list: log.info( 'is optimizing shop, shop_id=%s, total_shop_count=%s' % (shop_id, len(shop_id_list))) if poison.is_set(): stop_shop_id = shop_id log.info('stop shop_id=%s' % shop_id) break if shop_id < stop_shop_id: continue adg_list = ScreenRealtimeAdgroup(shop_id).execute() for adg in adg_list: if poison.is_set(): break cur_kw_count = RealtimeOptAdgroup(adg).execute() kw_opt_count += cur_kw_count adg_opt_count += 1 else: # 正常执行完后 stop_shop_id = 0 log.info( '======== realtime optimize end: shop_count=%s, adg_opt_count=%s, kw_opt_count=%s ========' % (len(shop_id_list), adg_opt_count, kw_opt_count)) log.info( 'end engine for realtime_optimize, last_start_time=%s, stop_shop_id=%s' % (last_start_time, stop_shop_id)) CacheAdpter.set(cachekey, [last_start_time, stop_shop_id], 'default', 60 * 60 * 2)
def get_valid_task(need_count): """获取符合条件的任务队列""" valid_id_list = [] is_mutual = CacheAdpter.get(CacheKey.ENGINE_SHOPMNG_TASK_MUTUAL_LOCK, 'web') if is_mutual: # 如果能够取到缓存,说明门关了 return valid_id_list else: # 否则,马上设置缓存,将门关了 try: CacheAdpter.set(CacheKey.ENGINE_SHOPMNG_TASK_MUTUAL_LOCK, True, 'web', 60 * 2) test_shop_id_list = Config.get_value('mnt.TEST_SHOP_IDLIST', default=[]) today_0time = date_2datetime(datetime.date.today()) recyle_time = datetime.datetime.today() - datetime.timedelta( hours=6) # 回收时间 cursor = shopmng_task_coll.find( { '_id': { '$nin': test_shop_id_list }, '$or': [{ 'status': 1 }, { 'status': { '$in': [-1, 2, 4] }, 'last_start_time': { '$lte': recyle_time, '$gte': today_0time } }] }, { '_id': 1 }).sort([('manual_priority', -1), ('priority', -1)]) cursor = cursor.limit(need_count) valid_id_list = [i['_id'] for i in cursor] if valid_id_list: shopmng_task_coll.update({'_id': { '$in': valid_id_list }}, { '$set': { 'status': -1, 'last_start_time': datetime.datetime.now() } }, multi=True) return valid_id_list except Exception, e: log.error("shopmng_task get_valid_task error, e=%s" % e) return valid_id_list finally:
def __is_mutual(self=None, *args, **kwargs): key_name = CacheKey.SUBWAY_DOWNLOAD_MUTUAL_LOCK % (self.shop_id, dl_type) cached_lock = CacheAdpter.get(key_name, 'web') if cached_lock: # 没有缓存,直接下载 return False else: CacheAdpter.set(key_name, True, 'web', 30 * 60) result = fn(self, *args, **kwargs) CacheAdpter.delete(key_name, 'web') return result
def bind_point(self): """【积分相关】积分数量""" point = 0 try: point = CacheAdpter.get(CacheKey.WEB_JLB_COUNT % self.shop_id, 'web', 'no_cache') if point == 'no_cache': from apps.web.point import PointManager point = PointManager.refresh_points_4shop(shop_id = self.shop_id) except Exception, e: log.error('bind_point error,e=%s, shop_id=%s' % (e, self.shop_id))
def get_config(cls, name): cachekey = CacheKey.MNT_CMDCFG % name cmd_cfg = CacheAdpter.get(cachekey, 'web', None) if not cmd_cfg: try: cmd_cfg = cls.objects.get(name=name) CacheAdpter.set(cachekey, cmd_cfg, 'web', 60 * 60 * 24) except DoesNotExist: # 取不到,使用默认值 cmd_cfg = None log.error('task config doesnot exsit, name = %s' % name) return cmd_cfg
def vip_home(request): from apps.web.point import PointManager, Sign from apps.web.utils import get_isneed_phone shop_id = request.user.shop_id customer = request.user.customer tapi = get_tapi(shop_id=shop_id) request.user.shop.sync_from_top(tapi) # 判断是否激活 is_need_phone = get_isneed_phone(request.user) # 获取积分 point_count = CacheAdpter.get(CacheKey.WEB_JLB_COUNT % shop_id, 'web', 'no_cache') if point_count == 'no_cache': point_count = PointManager.refresh_points_4shop( shop_id=request.user.shop_id) # 区分积分等级 grade, next_grade, parcent = Account.get_grade_display(shop_id=shop_id) # 历史最高积分 history_highest_point = Account.get_history_highest_point(shop_id=shop_id) # 判断今天是否签到 is_sign_today = Sign.is_sign_today(shop_id=shop_id) get_attendance_day = Sign.get_attendance_day(shop_id=shop_id) # 判断并获取用户收货信息 info_dict = customer.perfect_info if customer else {} # 改代码新版并没有用到,需要注销掉 # 获取兑换话费的人数 # phone_cost20_num = PointManager.get_exchange_gift_count(gift_type = 'phone_cost20') + 500 # 这里直接加了500 # discount_num = PointManager.get_exchange_gift_count(type = 'discount') data = { 'point_count': point_count, 'grade': grade, 'parcent': parcent, 'next_grade': next_grade, 'is_sign_today': is_sign_today, 'get_attendance_day': get_attendance_day, 'info_dict': info_dict, # 'phone_cost20_num': phone_cost20_num, # 'discount_num': discount_num, 'is_need_phone': is_need_phone, 'history_highest_point': history_highest_point } return render_to_response('vip_home.html', {'data': data}, context_instance=RequestContext(request))
def get_detail_rtrpt(cls, rpt_type, args_list, update_now=False, date=None): '''获取详细报表,包含每个平台的报表 rpt_type 可选值:'account', 'campaign', 'adgroup', 'keyword' args_list 与 rpt_type 对应关系, rpt_type = 'account', args_list = [shop_id] rpt_type = 'campaign', args_list = [shop_id] rpt_type = 'adgroup', args_list = [shop_id, campaign_id] rpt_type = 'keyword', args_list = [shop_id, campaign_id, adgroup_id] ''' config_dict = { 'account': { 'cachekey': CacheKey.SUBWAY_ACCOUNT_RTRPT, 'download_fun': '_download_account_rtrpt' }, 'campaign': { 'cachekey': CacheKey.SUBWAY_CAMPAIGN_RTRPT, 'download_fun': '_download_campaign_rtrpt' }, 'adgroup': { 'cachekey': CacheKey.SUBWAY_ADGROUP_RTRPT, 'download_fun': '_download_adgroup_rtrpt' }, 'keyword': { 'cachekey': CacheKey.SUBWAY_KEYWORD_RTRPT, 'download_fun': '_download_keyword_rtrpt' }, } # 从缓存或者淘宝api获取实时报表 rtrpt_dict = {} config = config_dict.get(rpt_type, {}) if not date: date = datetime.datetime.now() if config: cachekey = config['cachekey'] % (args_list[-1], date.date()) rtrpt_dict = CacheAdpter.get(cachekey, 'default', 'nocache') if update_now or rtrpt_dict == 'nocache': rtrpt_dict = getattr(cls, config['download_fun'])(*args_list, date=date) CacheAdpter.set(cachekey, rtrpt_dict, 'default', 60 * 5) else: log.warn('has no this rpt_type, rpt_type=%s' % rpt_type) # 将从字典转换成 ReportDictWrapper 对象,方便页面调用 result_rpt_dict = {} for k, rpt_list in rtrpt_dict.iteritems(): result_rpt_dict[k] = [ReportDictWrapper(rpt) for rpt in rpt_list] return result_rpt_dict
def get_jlb_count(shop_id): """获取精灵币个数""" try: jlb_count = CacheAdpter.get(CacheKey.WEB_JLB_COUNT % shop_id, 'web', 'no_cache') if jlb_count == 'no_cache': from apps.web.point import PointManager jlb_count = PointManager.refresh_points_4shop( shop_id=request.user.shop_id) return jlb_count except Exception, e: log.error('get_jlb_count error,e=%s, shop_id=%s' % (e, shop_id)) return 0
def platform(self): '''投放平台''' if not hasattr(self, '_platform'): platform = CacheAdpter.get( CacheKey.WEB_CAMPAIGN_PLATFORM % self.campaign_id, 'web', None) if not platform: platform = { 'pc_insite_search': 1, 'pc_insite_nonsearch': 0, 'pc_outsite_search': 0, 'pc_outsite_nonsearch': 0, 'yd_insite': 0, 'yd_outsite': 0, 'outside_discount': 100, 'mobile_discount': 100 } top_platform = None if self.tapi: try: top_platform = self.tapi.simba_campaign_platform_get( campaign_id=self.campaign_id) except TopError, e: log.error( 'get campaign platform error, shop_id=%s, campaign_id=%s, e=%s' % (self.shop_id, self.campaign_id, e)) if top_platform and hasattr(top_platform, 'campaign_platform'): pf = top_platform.campaign_platform platform['outside_discount'] = pf.outside_discount platform['mobile_discount'] = pf.mobile_discount if hasattr(pf.nonsearch_channels, 'number'): if 1 in pf.nonsearch_channels.number: platform['pc_insite_nonsearch'] = 1 if 2 in pf.nonsearch_channels.number: platform['pc_outsite_nonsearch'] = 1 if 8 in pf.nonsearch_channels.number: platform['yd_insite'] = 1 if 16 in pf.nonsearch_channels.number: platform['yd_outsite'] = 1 if hasattr(pf.search_channels, 'number'): if 2 in pf.search_channels.number: platform['pc_outsite_search'] = 1 if 8 in pf.search_channels.number: platform['yd_insite'] = 1 if 16 in pf.search_channels.number: platform['yd_outsite'] = 1 CacheAdpter.set( CacheKey.WEB_CAMPAIGN_PLATFORM % self.campaign_id, platform, 'web', 60 * 30) self._platform = platform
def get_account_balance(self): """优先从memcache中读取""" session = self.auth.session_key if session: balance = CacheAdpter.get(CacheKey.NCRM_ACCOUNT_BALANCE % session, 'web', None) if balance is None: tobj_balance = self.simba_account_balance_get() CacheAdpter.set(CacheKey.NCRM_ACCOUNT_BALANCE % session, tobj_balance.balance, 'web', 60 * 5) return tobj_balance.balance else: return balance else: raise (Exception('缺少session'))
def get_isneed_phone(user): shop_id = user.shop_id isneed_phone = CacheAdpter.get(CacheKey.WEB_ISNEED_PHONE % shop_id, 'web', 'no_cache') # isneed_phone = 'no_cache' if isneed_phone == 'no_cache': # 有手机号, 则放入缓存,避免多次访问数据库 # from apps.crm.models import Customer # customer, _ = Customer.objects.get_or_create(shop_id = shop_id, # defaults = {'user':user, 'nick':user.nick, 'tp_status':'untouched', 'jl_use_status':'using'}) from apps.ncrm.models import Customer customer, _ = Customer.objects.get_or_create(shop_id = shop_id, defaults = {'nick':user.nick}) if customer.phone: isneed_phone = 0 else: isneed_phone = 1 CacheAdpter.set(CacheKey.WEB_ISNEED_PHONE % shop_id, isneed_phone, 'web', 60 * 60 * 24 * 7) return isneed_phone