def get(args): """ 返回微品牌价值JSON数据 """ webapp_id = args.get('wid') start_date = args.get('start_date') end_date = args.get('end_date') freq_type = args.get('freq_type', 'W') try: periods = int(args.get('periods', DISPLAY_PERIODS_IN_CHARTS)) except: periods = DISPLAY_PERIODS_IN_CHARTS if freq_type == 'week' or freq_type == 'W': freq = 'W' elif freq_type == 'month' or freq_type == 'M': freq = 'M' else: freq = 'D' if end_date is None: end_date = utils_dateutil.now() else: try: end_date = utils_dateutil.parse_date(end_date) except: notify_msg = u"参数错误, cause:\n{}".format(unicode_full_stack()) watchdog_warning(notify_msg) end_date = utils_dateutil.now() #webapp_id = request.user_profile.webapp_id if start_date is not None: date_range = pd.date_range(start=start_date, end=end_date, freq=freq) else: # 如果不指定start_date,则以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=periods, freq=freq) #date_list = [] #values = [] # TODO: 需要优化。可以一次计算完成 data = {} for date in date_range: date_str = utils_dateutil.date2string(date.to_datetime()) # 将pd.Timestamp转成datetime #date_list.append(date_str) value = get_brand_value(webapp_id, date_str) #values.append() data[date_str] = value #print("date_list: {}".format(date_list)) #response = create_response(200) #response.data = data #return response.get_response() return data
def brand_value(request): """ 返回微品牌价值的EChart数据 """ freq_type = request.GET.get('freq_type','W') end_date = util_dateutil.now() webapp_id = request.user_profile.webapp_id # 以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=DISPLAY_PERIODS_IN_APP_CHARTS, freq=freq_type) date_list = [] values = [] # TODO: 需要优化。可以一次计算完成 for date in date_range: date_str = util_dateutil.date2string(date.to_datetime()) # 将pd.Timestamp转成datetime date_list.append(date_str[date_str.find('-')+1:]) values.append(get_brand_value(webapp_id, date_str)) response = create_line_chart_response( "", "", date_list, [{ "name": "品牌价值", "values" : values }] ) return response
def get_brand_value(webapp_id, date_str): """ 获取品牌价值的接口(如果数据库中有非今日的缓存值,则取出;否则计算并存入数据库) @retval brand_value(整型,单位元) """ cached, created = BrandValueHistory.objects.get_or_create( webapp_id=webapp_id, value_date=date_str) today = util_dateutil.date2string(util_dateutil.now()) # 如果有缓存的数据,从数据库取数据 #print("date_str: {}, count: {}".format(date_str, cached.count())) if date_str != today and not created: brand_value = int(cached.value) else: brand_value = 0 try: brand_value = compute_brand_value(webapp_id, date_str) #print("date_str: {}, value: {}".format(date_str, brand_value)) #BrandValueHistory.objects.create( # webapp_id=webapp_id, # value_date=date_str, # value=brand_value) cached.webapp_id = webapp_id cached.value_date = date_str cached.value = brand_value cached.save() brand_value = int(brand_value) except: notify_msg = u"存微品牌数据失败, cause:\n{}".format(unicode_full_stack()) watchdog_error(notify_msg) return brand_value
def get_brand_value_by_account(account_name, date_str=None): """ 根据account name获取微品牌价值 """ # 根据account_name获取webapp_id webapp_id = bdd_util.get_webapp_id_for(account_name) #print("webapp_id: {}".format(webapp_id)) date = date_str if date_str is None: date = util_dateutil.date2string(util_dateutil.now()) value = compute_brand_value(webapp_id, date) print("webapp_id:{}, date:{}, value:{}".format(webapp_id, date, value)) return value
def sales_chart(request): freq_type = request.GET.get('freq_type','W') priods = 7 if freq_type == 'W' else 30 end_date = util_dateutil.now() # 如果不指定start_date,则以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=priods, freq='D') start_date = util_dateutil.date2string(date_range[0].to_datetime()) try: webapp_id = request.user.get_profile().webapp_id orders = Order.by_webapp_id(webapp_id).filter(created_at__gte=start_date,created_at__lte=end_date,status__in=ORDER_SOURCE).order_by('created_at') order_price2time = OrderedDict() for order in orders: created_at = order.created_at.strftime("%Y-%m-%d") use_price = order.final_price + order.weizoom_card_money if not order_price2time.has_key(created_at): order_price2time[created_at] = use_price else: order_price2time[created_at] += use_price date_list = [] values =[] for date in date_range: date = util_dateutil.date2string(date.to_datetime()) date_list.append(date[date.find('-')+1:]) if not order_price2time.has_key(date): values.append(0) else: values.append(int(order_price2time[date])) response = create_line_chart_response( "", "", date_list, [{ "name": "销售额", "values" : values }] ) return response except: response = create_response(500) response.errMsg = u'系统繁忙,请稍后重试' return response.get_response()
def order_value(request): webapp_id = request.user.get_profile().webapp_id freq_type = request.GET.get('freq_type','W') priods = 7 if freq_type == 'W' else 30 end_date = util_dateutil.now() # 如果不指定start_date,则以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=priods, freq='D') start_date = util_dateutil.date2string(date_range[0].to_datetime()) try: orders = Order.by_webapp_id(webapp_id).filter(created_at__gte=start_date,created_at__lte=end_date,status__in=ORDER_SOURCE).order_by('created_at') order_items = OrderedDict() for order in orders: date = order.created_at.strftime('%Y-%m-%d') if not order_items.has_key(date): order_items[date] = 1 else: order_items[date] += 1 date_list = [] values =[] for date in date_range: date = util_dateutil.date2string(date.to_datetime()) date_list.append(date[date.find('-')+1:]) if not order_items.has_key(date): values.append(0) else: values.append(order_items[date]) response = create_line_chart_response( "", "", date_list, [{ "name": "订单量", "values" : values }] ) return response except: response = create_response(500) response.errMsg = u'系统繁忙,请稍后重试' return response.get_response()
def get(args): """ 获取微品牌价值 @param wid webapp_id @param dates 日期列表,多日期用逗号(,)分隔。默认是当天。 """ webapp_id = args.get('wid') #print("webapp_id: {}".format(webapp_id)) dates = args.get('dates') if dates is None or len(dates)<1: dates = [ utils_dateutil.date2string(utils_dateutil.now()) ] else: dates = dates.split(',') #values = [ {"date":date_str, "bv":get_brand_value(webapp_id, date_str)} for date_str in dates ] values = {date_str: get_brand_value(webapp_id, date_str) for date_str in dates} #return create_json_response(200, { # "values": values # }) return {"values": values}
def api_get(request): """ 获取微品牌价值 @param wid webapp_id @param dates 日期列表,多日期用逗号(,)分隔。默认是当天。 """ webapp_id = request.REQUEST.get('wid') print("webapp_id: {}".format(webapp_id)) dates = request.REQUEST.get('dates') if dates is None or len(dates) < 1: dates = [utils_dateutil.date2string(utils_dateutil.now())] else: dates = dates.split(',') #values = [ {"date":date_str, "bv":get_brand_value(webapp_id, date_str)} for date_str in dates ] values = { date_str: get_brand_value(webapp_id, date_str) for date_str in dates } response = create_response(200) response.data = {"values": values} return response.get_response()
def get_latest_brand_value(webapp_id): """ 获取当前微品牌值 @retval today_value 今日微品牌值 @retval yesterday_value 昨天的微品牌值 @retval is_increasing True表示上涨,False表示下降 @retval increase_percent 涨幅比例(%) """ today = util_dateutil.now() date_str = util_dateutil.date2string(today) yesterday = util_dateutil.get_date_after_days(today, -1) yesterday_date_str = util_dateutil.date2string(yesterday) today_value = get_brand_value(webapp_id, date_str) yesterday_value = get_brand_value(webapp_id, yesterday_date_str) delta = today_value - yesterday_value if today_value == 0: increase_percent = 0.0 else: increase_percent = abs(round((delta * 100.0 / today_value), 2)) return (today_value, yesterday_value, int(np.sign(delta)), increase_percent)
def _profunc_today_date(param): # 返回今天日期YYYY-MM-DD return utils_dateutil.date2string(utils_dateutil.now())
def api_get(request): """ 返回微品牌价值的EChart数据 """ start_date = request.GET.get('start_date') end_date = request.GET.get('end_date') freq_type = request.GET.get('freq_type') try: periods = int(request.GET.get('periods', DISPLAY_PERIODS_IN_CHARTS)) except: periods = DISPLAY_PERIODS_IN_CHARTS if freq_type == 'week': freq = 'W' elif freq_type == 'month': freq = 'M' else: freq = 'D' if end_date is None: end_date = util_dateutil.now() else: try: end_date = util_dateutil.parse_date(end_date) except: notify_msg = u"参数错误, cause:\n{}".format(unicode_full_stack()) watchdog_warning(notify_msg) end_date = util_dateutil.now() webapp_id = request.user_profile.webapp_id if start_date is not None: date_range = pd.date_range(start=start_date, end=end_date, freq=freq) else: # 如果不指定start_date,则以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=periods, freq=freq) date_list = [] values = [] # TODO: 需要优化。可以一次计算完成 for date in date_range: date_str = util_dateutil.date2string( date.to_datetime()) # 将pd.Timestamp转成datetime date_list.append(date_str) values.append(get_brand_value(webapp_id, date_str)) print("date_list: {}".format(date_list)) response = create_line_chart_response( "", "", #['2010-10-10', '2010-10-11', '2010-10-12', '2010-10-13'], date_list, [{ "name": "品牌价值", "values": values }]) return response
def get(request): webapp_id = request.user_profile.webapp_id member_id = request.GET.get('id', None) ship_infos = None orders = [] weizoom_card_total_money = 0 #try: if member_id: member = Member.objects.get(id=member_id, webapp_id=webapp_id) orders = get_member_orders(member) pay_money = 0 pay_times = 0 if orders.filter(status__gte=2).count() > 0: payment_time = orders.order_by('-payment_time')[0].payment_time member.last_pay_time = payment_time for order in orders: order.final_price = order.final_price + order.weizoom_card_money if order.status == 5: pay_money += order.final_price pay_times += 1 weizoom_card_total_money += order.weizoom_card_money member.pay_times = pay_times member.pay_money = pay_money try: member.unit_price = pay_money / pay_times except: member.unit_price = 0 try: member.friend_count = count_member_follow_relations(member) except: notify_message = u"更新会员好友数量失败:cause:\n{}".format( unicode_full_stack()) watchdog_error(notify_message) try: date_before_30 = get_date_after_days(now(), -30).strftime("%Y-%m-%d") webapp_user_ids = member.get_webapp_user_ids purchase_count_30days = Order.objects.filter( webapp_user_id__in=webapp_user_ids, payment_time__gte=date_before_30, status=ORDER_STATUS_SUCCESSED, origin_order_id__lte=0).count() member.purchase_frequency = purchase_count_30days except: notify_message = u"更新会员30天购买次数失败:cause:\n{}".format( unicode_full_stack()) watchdog_error(notify_message) member.save() else: raise Http404(u"不存在该会员") #except: # raise Http404(u"不存在该会员") from modules.member.member_info_util import update_member_basic_info if (not member.user_icon or not member.username_hexstr) and (settings.MODE != 'develop'): update_member_basic_info(request.user_profile, member) #完善会员的基本信息 if member.user_icon: member.user_icon = member.user_icon if len( member.user_icon.strip()) > 0 else DEFAULT_ICON else: member.user_icon = DEFAULT_ICON if member.unit_price > 0: member.unit_price = '%.2f' % member.unit_price if member.pay_money > 0: member.pay_money = '%.2f' % member.pay_money member_browse_records = MemberBrowseRecord.objects.filter( ~Q(title=''), member=member).order_by('-created_at') #会员标签 webapp_id = request.user_profile.webapp_id member_has_tags = MemberHasTag.get_member_has_tags(member) fans_count = MemberFollowRelation.get_follow_members_for( member.id, '1') #我的优惠券 coupons = get_member_coupons(member) # 组织盛景定制信息 shengjing_register_info = dict() if request.user.username == 'shengjing360': is_shengjing = True try: sj_binding_member = ShengjingBindingMember.objects.get( member_id=member_id) sj_binding_member_info = ShengjingBindingMemberInfo.objects.get( binding_id=sj_binding_member.id) sj_binding_member_companys = ShengjingBindingMemberHasCompanys.objects.filter( binding_id=sj_binding_member.id) shengjing_register_info[ 'phone_number'] = sj_binding_member.phone_number shengjing_register_info[ 'position'] = sj_binding_member_info.position shengjing_register_info[ 'status'] = sj_binding_member_info.status_name if sj_binding_member_info.status == LEADER: shengjing_register_info['is_leader'] = u'是' else: shengjing_register_info['is_leader'] = u'否' shengjing_register_info[ 'crm_name'] = sj_binding_member_info.name shengjing_register_info['crm_companys'] = [] for company in sj_binding_member_companys: shengjing_register_info['crm_companys'].append( company.name) shengjing_register_info['name'] = shengjing_register_info[ 'crm_name'] shengjing_register_info['companys'] = shengjing_register_info[ 'crm_companys'] # 如果未绑定CRM,则crm_name与crm_companys置空 if sj_binding_member_info.status == STAFF or sj_binding_member_info.status == LEADER: pass else: shengjing_register_info['crm_name'] = '' shengjing_register_info['crm_companys'] = [] shengjing_register_info[ 'phone_number'] = sj_binding_member.phone_number except: notify_message = u"shengjing360:cause:\n{}".format( unicode_full_stack()) watchdog_error(notify_message) else: is_shengjing = False webapp_user_ids = member.get_webapp_user_ids ship_infos = ShipInfo.objects.filter( webapp_user_id__in=webapp_user_ids) for ship_info in ship_infos: province = '' city = '' village = '' if ship_info.get_str_area: area_list = ship_info.get_str_area.split(' ') if len(area_list) == 3: province = area_list[0] city = area_list[1] village = area_list[2] elif len(area_list) == 2: province = area_list[0] city = area_list[1] else: province = area_list[0] ship_info.province = province ship_info.city = city ship_info.village = village shared_url_infos = get_member_shared_urls(member) numbers = shared_url_infos.aggregate(Sum("followers")) # shared_url_lead_number = 0 # if numbers["followers__sum"] is not None: # shared_url_lead_number = numbers["followers__sum"] numbers = shared_url_infos.aggregate(Sum("pv")) shared_url_pv = 0 if numbers["pv__sum"] is not None: shared_url_pv = numbers["pv__sum"] qrcode_friends = 0 purchased_fans = 0 if fans_count: qrcode_friends = fans_count.filter( source=SOURCE_MEMBER_QRCODE).count() purchased_fans = get_purchased_fans(fans_count) # for follow_member in fans_count: # if Order.get_orders_from_webapp_user_ids(follow_member.get_webapp_user_ids).filter(status=5).count() >0: # purchased_fans += 1 shared_url_lead_number = fans_count.count() - qrcode_friends if purchased_fans: conversion_rate = "%.2f" % (float(purchased_fans) / float(fans_count.count()) * 100) else: conversion_rate = 0 #微众卡使用金额 if member: member.weizoom_card_total_money = weizoom_card_total_money if member.fans_count != len(fans_count): Member.objects.filter(id=member.id).update( fans_count=len(fans_count)) c = RequestContext( request, { 'is_shengjing': is_shengjing, 'shengjing_register_info': shengjing_register_info, 'first_nav_name': export.MEMBER_FIRST_NAV, 'show_member': member, 'grades': MemberGrade.get_all_grades_list(member.webapp_id), 'orders': orders, 'ship_infos': ship_infos, 'shared_url_infos': shared_url_infos, 'show_member_info': get_member_info(member), 'member_browse_records': member_browse_records, 'member_has_tags': member_has_tags, 'fans_count': len(fans_count), 'coupons': coupons, 'second_navs': export.get_second_navs(request), 'second_nav_name': export.MEMBERS, 'shared_url_lead_number': shared_url_lead_number, 'shared_url_pv': shared_url_pv, 'qrcode_friends': qrcode_friends, 'purchased_fans': purchased_fans, 'conversion_rate': conversion_rate }) return render_to_response('member/editor/member_detail.html', c)