def self_rank(self, request): if request.user.is_anonymous: raise exceptions.PermissionDenied(u'用户未登录或并非小鹿妈妈') mama = request.user.customer.get_xiaolumm() if not mama: raise exceptions.ValidationError(u'用户未登录或并非小鹿妈妈') stat_time = request.GET.get('stat_time') if stat_time: try: stat_time = datetime.datetime.strptime(stat_time, '%Y%m%d') except: raise exceptions.ValidationError(make_response(u'提供的统计时间不正确')) myteam = WeekMamaTeamCarryTotal.get_by_mama_id(mama.id, stat_time) if not myteam: res = { 'mama': mama.id, 'mama_nick': mama.nick, 'thumbnail': mama.thumbnail, 'total': 0, 'num': 0, 'total_display': '0', 'rank': 0 } return Response(res) return Response(self.get_serializer(myteam).data)
def bind_mama(self, request, pk): try: pk=int(pk) except: raise exceptions.ValidationError(make_response(u'妈妈id必须为整数')) cus = request.user.customer mama = get_object_or_404(XiaoluMama, pk=pk) try: XlmmFans.bind_mama(cus, mama) except Exception, e0: # raise exceptions.ValidationError(e0.message) raise exceptions.ValidationError(make_response(e0.message))
def retrieve(self, request, pk): stat_time = request.GET.get('stat_time') if stat_time: try: stat_time = datetime.datetime.strptime(stat_time, '%Y%m%d') except: raise exceptions.ValidationError(make_response(u'提供的统计时间不正确')) mama = get_object_or_404(XiaoluMama, pk=pk) wmm = WeekMamaTeamCarryTotal.get_by_mama_id(mama.id, stat_time) if not wmm: raise exceptions.ValidationError(make_response(u'用户未参与团队妈妈排名')) res = self.get_serializer(wmm).data res['rank_add'] = WeekMamaTeamCarryTotal.get_rank_add(pk) return Response(res)
def self_rank(self, request): if request.user.is_anonymous: raise exceptions.PermissionDenied(u'用户未登录或并非小鹿妈妈') mama = request.user.customer.get_xiaolumm() if not mama: raise exceptions.PermissionDenied(u'用户未登录或并非小鹿妈妈') stat_time = request.GET.get('stat_time') if stat_time: try: stat_time = datetime.datetime.strptime(stat_time, '%Y%m%d') except: raise exceptions.ValidationError(make_response(u'提供的统计时间不正确')) if not stat_time: stat_time = WeekRank.this_week_time() wmama = WeekMamaCarryTotal.objects.filter(mama_id=mama.id, stat_time=stat_time).first() if not wmama: res = { 'mama': mama.id, 'mama_nick': mama.nick, 'thumbnail': mama.thumbnail, 'total': 0, 'num': 0, 'total_display': '0', 'rank': 0 } return Response(res) res = self.get_serializer(wmama).data res['rank_add'] = 0 res['rank'] = wmama.total_rank return Response(res)
def get_team_members(self, request, pk): stat_time = request.GET.get('stat_time') if stat_time: try: stat_time = datetime.datetime.strptime(stat_time, '%Y%m%d') except: raise exceptions.ValidationError(make_response(u'提供的统计时间不正确')) stat_time = stat_time if WeekRank.check_week_begin( stat_time) else WeekRank.this_week_time() team = WeekMamaTeamCarryTotal.get_by_mama_id(pk, stat_time) if not team: raise exceptions.ValidationError(make_response(u'用户尚未参与团队排名统计')) else: records = WeekMamaCarryTotal.objects.filter( mama_id__in=team.mama_ids, stat_time=stat_time) return Response(self.get_serializer(records, many=True).data)
def activity_self_rank(self, request): if request.user.is_anonymous: raise exceptions.PermissionDenied(u'用户未登录或并非小鹿妈妈') mama = request.user.customer.getXiaolumm() if not mama: raise exceptions.ValidationError(u'用户未登录或并非小鹿妈妈') myteam = MamaTeamCarryTotal.get_by_mama_id(mama.id) return Response(ActivityMamaTeamCarryTotalSerializer(myteam).data)
def change_mama(self, request): new_mama_id = request.data.get('new_mama_id') fans = XlmmFans.get_by_customer_id(request.user.customer.id) new_mama = get_object_or_404(XiaoluMama, pk=new_mama_id) if not fans: try: XlmmFans.bind_mama(request.user.customer, new_mama) except Exception, e0: raise exceptions.ValidationError(make_response(e0.message))
def carry_duration_rank(self, request): stat_time = request.GET.get('stat_time') if stat_time: try: stat_time = datetime.datetime.strptime(stat_time, '%Y%m%d') except: raise exceptions.ValidationError(make_response(u'提供的统计时间不正确')) top = WeekMamaCarryTotal.get_duration_ranking_list(stat_time)[0:10] return Response( WeekMamaCarryTotalDurationSerializer(top, many=True).data)
def activity_self_rank(self, request, pk): if request.user.is_anonymous: raise exceptions.PermissionDenied(u'用户未登录或并非小鹿妈妈') activity = RankActivity.objects.filter( id=pk).first() or RankActivity.now_activity() mama = request.user.customer.getXiaolumm() if not mama: raise exceptions.ValidationError(u'用户未登录或并非小鹿妈妈') myteam = ActivityMamaTeamCarryTotal.get_by_mama_id(mama.id) return Response(ActivityMamaTeamCarryTotalSerializer(myteam).data)
def carry_total_rank(self, request): stat_time = request.GET.get('stat_time') if stat_time: try: stat_time = datetime.datetime.strptime(stat_time, '%Y%m%d') except: raise exceptions.ValidationError(make_response(u'提供的统计时间不正确')) top = WeekMamaCarryTotal.get_ranking_list(stat_time)[0:10] return Response(self.get_serializer(top, many=True).data) queryset = WeekMamaCarryTotal.get_ranking_list() page = self.paginate_queryset(queryset) serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data)
def carry_total_rank(self, request): stat_time = request.GET.get('stat_time') if stat_time: try: stat_time = datetime.datetime.strptime(stat_time, '%Y%m%d') except: raise exceptions.ValidationError(make_response(u'提供的统计时间不正确')) top = WeekMamaTeamCarryTotal.get_ranking_list(stat_time)[0:10] top = list(top) i = 1 for t in top: t._rank_ = i i += 1 return Response(self.get_serializer(top, many=True).data)
class XlmmFansViewSet(viewsets.ModelViewSet): """ """ paginate_by = 10 page_query_param = 'page' paginate_by_param = 'page_size' max_paginate_by = 100 queryset = XlmmFans.objects.all() serializer_class = serializers.XlmmFansSerializer authentication_classes = (authentication.SessionAuthentication, WeAppAuthentication, authentication.BasicAuthentication) permission_classes = (permissions.IsAuthenticated, perms.IsOwnerOnly) # renderer_classes = (renderers.JSONRenderer, renderers.BrowsableAPIRenderer) def get_owner_queryset(self, request): customer_id = get_customer_id(request.user) mama_id = get_mama_id(request.user) task_mama_daily_tab_visit_stats.delay(mama_id,MamaTabVisitStats.TAB_FANS_LIST) return self.queryset.filter(xlmm_cusid=customer_id).order_by('-created') def list(self, request, *args, **kwargs): dt_str = datetime.datetime.now().strftime('%Y.%m.%d') statsd.incr('xiaolumm.mama_fans_count.%s'%dt_str) datalist = self.get_owner_queryset(request) datalist = self.paginate_queryset(datalist) serializer = serializers.XlmmFansSerializer(datalist, many=True) return self.get_paginated_response(serializer.data) def create(self, request, *args, **kwargs): raise exceptions.APIException('METHOD NOT ALLOWED') @list_route(methods=['POST']) def change_mama(self, request): new_mama_id = request.data.get('new_mama_id') fans = XlmmFans.get_by_customer_id(request.user.customer.id) new_mama = get_object_or_404(XiaoluMama, pk=new_mama_id) if not fans: try: XlmmFans.bind_mama(request.user.customer, new_mama) except Exception, e0: raise exceptions.ValidationError(make_response(e0.message)) if new_mama.id == fans.xlmm: raise exceptions.ValidationError(make_response(u'更换的新妈妈ID与原小鹿妈妈ID必须不一致')) fans.change_mama(new_mama) return Response(SUCCESS_RESPONSE)
def activity_self_rank(self, request): if request.user.is_anonymous: raise exceptions.PermissionDenied(u'用户未登录或并非小鹿妈妈') mama = request.user.customer.getXiaolumm() if not mama: raise exceptions.ValidationError(u'用户未登录或并非小鹿妈妈') mama_carry = MamaCarryTotal.objects.get(pk=mama.id) res = ActivityMamaCarryTotalSerializer(mama_carry).data res['rank'] = mama_carry.activite_rank res['rank_add'] = 0 team = MamaTeamCarryTotal.objects.get(pk=mama_carry.mama_id) res['recommended_quantity'] = len(mama.get_invite_potential_mama_ids()) res['team_total'] = team.expect_total res['team_rank'] = team.activite_rank res['team_total_display'] = float('%.2f' % (team.expect_total * 0.01)) res['duration_total'] = mama_carry.duration_total + mama_carry.expect_total res['duration_total_display'] = float('%.2f' % (mama_carry.expect_total * 0.01)) res['team_num'] = team.duration_num res['activite_num'] = len(mama.get_active_invite_potential_mama_ids()) # res['invitate_num'] = 0 return Response(res)