Esempio n. 1
0
 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))
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 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)
Esempio n. 5
0
 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)
Esempio n. 6
0
 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))
Esempio n. 7
0
 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)
Esempio n. 8
0
 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)
Esempio n. 9
0
 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)
Esempio n. 10
0
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)