def build_conversation_list(user_id, conversation_id, conversation_info, msg_id, get_new): """ :param user_id: :param conversation_id: :param conversation_info: :param msg_id: :param get_new: :return: """ my_info = get_user_info_by_user_id_db(user_id) receiver_id = conversation_info.user_1_id if conversation_info.user_1_id != user_id else conversation_info.user_2_id receiver_info = get_user_info_by_user_id_db(receiver_id) query = Q(conversation_id=conversation_id) result = {} if get_new: query &= Q(id__gt=msg_id) chat_record = ChatRecord.objects.filter(query).order_by('-created_time') else: if msg_id: query &= Q(id__lt=msg_id) chat_record = ChatRecord.objects.filter(query).order_by('-created_time')[0: 21] result = {'has_more': len(chat_record) > 20} chat_record = chat_record[:20] result.update({ 'content_list': [{'content': json.loads(chat.content), 'is_me': user_id == chat.addresser_id, 'created_time': datetime_to_str(chat.created_time, FORMAT_DATETIME), 'msg_id': chat.id} for chat in chat_record] }) result.update({'my_info': {'user_id': user_id, 'avatar': my_info.avatar}, 'receiver_info': {'user_id': receiver_id, 'avatar': receiver_info.avatar}}) return result
def build_user_coupon_info(coupon): """ 构造单个用户优惠券信息: 商家头像 商家名称 优惠券金额 优惠券名称 优惠券到期日期 """ coupon_money = 0 if coupon.template.template_type == CouponTemplateChoices.GENERAL else coupon.template.money return { 'club_id': coupon.template.club.id, 'avatar': "https://zongz.cn" + coupon.template.club.avatar.url, 'club_name': coupon.template.club.name, 'address': coupon.template.club.address, 'coupon_id': coupon.id, 'coupon_money': coupon_money, 'coupon_type': coupon.template.template_type, 'coupon_name': coupon.template.name, 'coupon_deadline': datetime_to_str(coupon.template.deadline), 'coupon_code': coupon.coupon_code, 'coupon_is_used': coupon.is_used }
def build_comment(comment): return { 'avatar': comment.avatar, 'name': comment.name, 'created_time': datetime_to_str(comment.created_time), 'content': comment.comment, 'user_id': comment.user_id }
def get_next_explore_post_view(request): """ 获取信息流的下一个: 1. 信息流包含: 普通信息(足迹, 求助帖) + 优惠券信息 2. [3, 9, 20, 30] 这四个位置是优惠券, 如果没有足够的普通信息, 都填充优惠券就可以了 3. 0~10km、10~30km、30~100km、100km 及以上 GET: /footprint/next_explore_post/ 一共有四种返回类型: text, image_text, coupon_template, help """ lon = float(request.GET.get('lon', 0)) lat = float(request.GET.get('lat', 0)) user_info = get_user_info_by_user_id_db(request.user.id) current_date = datetime_to_str(datetime.datetime.now(), date_format=FORMAT_DATE_WITHOUT_SEPARATOR) next_explore_times = int( ExploreSurplusTimesManager.get_times(current_date, user_info.id)) + 1 if next_explore_times > ExploreSurplusTimesManager.EXPLORE_DAY_LIMIT: return json_http_error(u'今天的次数已经用完了') # 增加一次探索次数 ExploreSurplusTimesManager.add_times(current_date, user_info.id) # 随机返回一张优惠券, 但是, 注意位置 (获取不到优惠券, 返回用户足迹吧) if next_explore_times in [3, 9, 20, 30]: coupon_template = get_next_explore_template(next_explore_times) if coupon_template: # 返回的信息里面带有 type, 前端识别不同的 type 去展示 return json_http_success( build_coupon_template_info(coupon_template)) else: footprint = get_next_explore_footprint(next_explore_times, lon, lat, request.user) if footprint: return json_http_success( build_footprint_info_for_explore(request.user, footprint)) return json_http_error(u'没有更多的探索啦') else: # 如果不是优惠券的位置, 返回足迹 footprint = get_next_explore_footprint(next_explore_times, lon, lat, request.user) if footprint: return json_http_success( build_footprint_info_for_explore(request.user, footprint)) # 如果获取不到足迹, 再尝试返回优惠券 coupon_template = get_next_explore_template(next_explore_times, ignore_index=True) if coupon_template: return json_http_success( build_coupon_template_info(coupon_template)) # 如果优惠券也获取不到 return json_http_error(u'没有更多的探索啦')
def build_activity_for_flow(activity, user_id, lon, lat): club = activity.club return { 'flow_id': activity.id, 'flow_type': FlowType.ACTIVITY, 'avatar': club.avatar.url, 'name': club.name, 'distance': geodesic((lat, lon), (activity.lat, activity.lon)).meters, 'location': activity.address, 'post_time': datetime_to_str(activity.created_time), 'content': activity.introduction, 'image_list': activity.image_list, 'favored': is_user_favored(user_id, activity.id, FlowType.ACTIVITY), 'favor_num': activity.favor_num, }
def build_footprint_for_flow(footprint, user_id, lon, lat): return { 'flow_id': footprint.id, 'flow_type': FlowType.FOOTPRINT, 'avatar': footprint.avatar, 'name': footprint.name, 'distance': geodesic((lat, lon), (footprint.lat, footprint.lon)).meters, 'location': footprint.location, 'post_time': datetime_to_str(footprint.created_time), 'content': footprint.content, 'image_list': footprint.image_list, 'user_id': footprint.user_id, 'favored': is_user_favored(user_id, footprint.id, FlowType.FOOTPRINT), 'favor_num': footprint.favor_num, }
def build_footprint_info_for_explore(user, footprint): """ 构造用户足迹信息 """ # 先确定足迹的类型 if footprint.post_type == PostType.HELP: footprint_type = 'help' else: if not footprint.image_list: footprint_type = 'text' else: footprint_type = 'image_text' coupon_template = {} if footprint_type == 'help': template = ClubCouponTemplate.objects.get(id=footprint.template_id) coupon_money = 0 if template.template_type == CouponTemplateChoices.GENERAL else template.money coupon_template = { 'club_id': template.club_id, 'avatar': "https://zongz.cn" + template.club.avatar.url, 'club_name': template.club.name, 'address': template.club.address, 'template_id': template.id, 'template_name': template.name, 'deadline': datetime_to_str(template.deadline), 'coupon_money': coupon_money, 'has_count': get_user_coupon_count(user, template), 'lon': footprint.lon, 'lat': footprint.lat } return { # text, image_text 'footprint_id': footprint.id, 'time': time_format(footprint.created_time), 'user_id': footprint.user_id, 'name': footprint.name, 'avatar': footprint.avatar, 'location': footprint.location, 'content': footprint.content, 'image_list': footprint.image_list, # help 'coupon_template': coupon_template, # type 'type': footprint_type, 'lon': footprint.lon, 'lat': footprint.lat }
def get_explore_surplus_times_view(request): """ 获取用户探索模式剩余的次数 URL[GET]: /commercial/get_explore_surplus_times/ :return int """ user_info = get_user_info_by_user_id_db(request.user.id) current_date = datetime_to_str(datetime.datetime.now(), date_format=FORMAT_DATE_WITHOUT_SEPARATOR) surplus_times = ExploreSurplusTimesManager.get_explore_day_limit() \ - ExploreSurplusTimesManager.get_times(current_date, user_info.id) return json_http_success( {"surplus_times": surplus_times if surplus_times >= 0 else 0})
def get_my_profile_view(request): """ 获取我的资料 URL[GET]: /user_info/my_profile/ :param request: :return: """ user_info = get_user_info_by_user_id_db(request.user.id) result = { 'avatar': user_info.avatar or '', 'nickname': user_info.nickname or '', 'birthday': datetime_to_str(user_info.birthday) if user_info.birthday else '', 'location': user_info.location or '', 'sex': user_info.sex, 'wechat_no': user_info.wechat_no, 'show_wechat_no': user_info.show_wechat_no, 'signature': user_info.signature, 'user_id': request.user.id, } return json_http_success(result)
def build_coupon_template_info(template): """ 构造优惠券模板信息 商户 id、商户头像、商户名称、商户地址、优惠券名称、优惠券截止日期、优惠券金额 """ coupon_money = 0 if template.template_type == CouponTemplateChoices.GENERAL else template.money return { 'club_id': template.club_id, 'avatar': "https://zongz.cn" + template.club.avatar.url, 'club_name': template.club.name, 'address': template.club.address, 'template_id': template.id, 'template_name': template.name, 'deadline': datetime_to_str(template.deadline), 'coupon_money': coupon_money, 'type': 'coupon_template', 'lon': template.club.lon, 'lat': template.club.lat }
def add_message(cls, to_user_id, from_user_id, conversation_id, content): """ 包括添加自己的消息和另外那个人的消息 message信息包括: {conversation_id, avatar, content, created_time, has_badge} :param from_user_id: :param to_user_id: :param conversation_id: :param content: :return: """ user_info = get_user_info_by_user_id_db(to_user_id) message = { 'conversation_id': conversation_id, 'avatar': user_info.avatar, 'last_message': content, 'has_new': 1, 'time': datetime_to_str(datetime.datetime.now(), FORMAT_DATETIME), 'username': user_info.nickname } redis.hset_pickle(cls.build_redis_key(to_user_id), conversation_id, message) # 更新信息 message['has_new'] = 0 redis.hset_pickle(cls.build_redis_key(from_user_id), conversation_id, message)
def build_user_coupon_info_for_charge_off(club_id, coupon_code): """ 构造用户优惠券信息 --> 核销展示使用 """ club = get_club_by_id_db(club_id) if not club: return None coupon = get_user_coupon_by_coupon_code(coupon_code) if not coupon: return None user_info = get_user_info_by_user_id_db(coupon.user.id) coupon_money = 0 if coupon.template.template_type == CouponTemplateChoices.GENERAL else coupon.template.money return { 'club_info': { 'avatar': "https://zongz.cn" + club.avatar.url, 'name': club.name, 'address': club.address, 'telephone': club.telephone, 'club_id': club.id, }, 'coupon_info': { 'coupon_id': coupon.id, 'coupon_money': coupon_money, 'coupon_type': coupon.template.template_type, 'coupon_name': coupon.template.name, 'coupon_deadline': datetime_to_str(coupon.template.deadline) }, 'user_info': { 'avatar': user_info.avatar, 'nickname': user_info.nickname, 'consume_count': CouponChargeOffRecord.objects.filter( user_id=coupon.user.id).count() } }
def build_club_consume_infos(charge_off_records): """ 构造商户消耗优惠券的信息 """ money = 0 consume_infos = [] for record in charge_off_records: coupon = get_user_coupon_by_id(record.coupon_id) coupon_money = 0 if coupon.template.template_type == CouponTemplateChoices.GENERAL \ else coupon.template.money consume_infos.append({ 'nickname': get_user_info_by_user_id_db(coupon.user_id).nickname, 'coupon_money': coupon_money, 'confirm_user': record.club_user.user_info.nickname, 'confirm_time': datetime_to_str(record.created_time, '%m-%d %H:%M') }) money += coupon_money return money, consume_infos