Exemplo n.º 1
0
 def get(request, pk):
     try:
         account = Account.objects.get(pk=pk)
         serializer = AccountSerializer(account)
         return XResponse(status_code=const.CODE_10000, msg=const.MSG_SUCCESS, data=serializer.data)
     except Account.DoesNotExist:
         return XResponse(status_code=const.CODE_10006, msg=const.MSG_FAIL)
Exemplo n.º 2
0
    def post(self, request, pk):
        feed_id = pk

        token = request.META.get(const.META_TOKEN, const.UNKNOWN)
        payload = decode_token(token)
        account_id = payload.get(const.ACCOUNT_ID)

        bill = Bill.objects.get(account=account_id)
        feed = Feed.objects.get(pk=pk)
        if bill.balance >= feed.diamond:
            data = {const.FEED: feed_id, const.ACCOUNT: account_id}
            serializer = BuyLogSerializer(data=data)
            if serializer.is_valid():
                serializer.save()

                bill.balance = bill.balance - feed.diamond
                bill.save()
                return XResponse(status_code=const.CODE_10000,
                                 msg=const.MSG_SUCCESS,
                                 data=None)
            else:
                return XResponse(status_code=const.CODE_10013,
                                 msg=const.MSG_FAIL)
        else:
            return XResponse(status_code=const.CODE_10014, msg=const.MSG_FAIL)
Exemplo n.º 3
0
 def post(request):
     serializer = FeedListSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return XResponse(status_code=const.CODE_10000,
                          msg=const.MSG_SUCCESS,
                          data=serializer.data)
     return XResponse(status_code=const.CODE_10007, msg=const.MSG_FAIL)
Exemplo n.º 4
0
 def delete(self, request, pk):
     try:
         feed = Feed.objects.get(pk=pk)
         serializer = FeedDetailSerializer(feed, data=request.data)
         feed.delete()
         return XResponse(status_code=const.CODE_10000,
                          msg=const.MSG_SUCCESS,
                          data=serializer.data)
     except Feed.DoesNotExist:
         return XResponse(status_code=const.CODE_10005, msg=const.MSG_FAIL)
Exemplo n.º 5
0
 def post(request):
     serializer = FeedbackSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return XResponse(status_code=const.CODE_10000,
                          msg=const.MSG_SUCCESS,
                          data=None)
     return XResponse(status_code=const.CODE_10007,
                      msg=const.MSG_FAIL,
                      data=serializer.error_messages)
Exemplo n.º 6
0
 def post(request):
     serializer = UploadImageSerializer(data=request.data.dict())
     if serializer.is_valid():
         serializer.save()
         return XResponse(status_code=const.CODE_10000,
                          msg=const.MSG_SUCCESS,
                          data=serializer.data)
     return XResponse(status_code=const.CODE_10007,
                      msg=const.MSG_FAIL,
                      data=serializer.error_messages)
Exemplo n.º 7
0
 def get(request):
     try:
         token = request.META.get(const.META_TOKEN, const.UNKNOWN)
         payload = decode_token(token)
         account_id = payload.get(const.ACCOUNT_ID)
         bill = Bill.objects.get(account=account_id)
         serializer = BillSerializer(bill)
         return XResponse(status_code=const.CODE_10000, msg=const.MSG_SUCCESS, data=serializer.data)
     except Bill.DoesNotExist:
         return XResponse(status_code=const.CODE_10006, msg=const.MSG_FAIL)
Exemplo n.º 8
0
 def patch(request, pk):
     try:
         account = Account.objects.get(pk=pk)
         serializer = AccountSerializer(account, data=request.data, partial=True)
         if serializer.is_valid():
             serializer.save()
             return XResponse(status_code=const.CODE_10000, msg=const.MSG_SUCCESS, data=serializer.data)
         else:
             return XResponse(status_code=const.CODE_10007, msg=const.MSG_FAIL, data=serializer.error_messages)
     except Account.DoesNotExist:
         return XResponse(status_code=const.CODE_10006, msg=const.MSG_FAIL)
Exemplo n.º 9
0
 def wrapped_f(self, request, pk):
     token = request.META.get(const.META_TOKEN, const.UNKNOWN)
     try:
         payload = decode_token(token)
         account_id = payload.get(const.ACCOUNT_ID)
         feed = Feed.objects.get(pk=pk)
         if account_id == feed.account_id:
             return f(self, request, pk)
         else:
             return XResponse(status_code=const.CODE_10007, msg=const.MSG_FAIL)
     except Feed.DoesNotExist:
         return XResponse(status_code=const.CODE_10004, msg=const.MSG_FAIL)
Exemplo n.º 10
0
 def patch(self, request, pk):
     try:
         feed = Feed.objects.get(pk=pk)
         serializer = FeedDetailSerializer(feed,
                                           data=request.data,
                                           partial=True)
         if serializer.is_valid():
             serializer.save()
             return XResponse(status_code=const.CODE_10000,
                              msg=const.MSG_SUCCESS,
                              data=serializer.data)
         else:
             return XResponse(status_code=const.CODE_10007,
                              msg=const.MSG_FAIL)
     except Feed.DoesNotExist:
         return XResponse(status_code=const.CODE_10005, msg=const.MSG_FAIL)
Exemplo n.º 11
0
 def delete(request, pk):
     notification = Notification.objects.get(pk=pk)
     serializer = NotificationSerializer(notification, data=request.data)
     notification.delete()
     return XResponse(status_code=const.CODE_10000,
                      msg=const.MSG_SUCCESS,
                      data=serializer.data)
Exemplo n.º 12
0
    def delete(self, request, pk):
        try:
            feed = pk

            token = request.META.get(const.META_TOKEN, const.UNKNOWN)
            payload = decode_token(token)
            account_id = payload.get(const.ACCOUNT_ID)
            account = account_id

            like = Like.objects.get(feed=feed, account=account)
            like.delete()

            feed = Feed.objects.get(pk=feed)
            feed.like_count = feed.like_count - 1
            feed.save()
            return XResponse(status_code=const.CODE_10000,
                             msg=const.MSG_SUCCESS)
        except Like.DoesNotExist:
            return XResponse(status_code=const.CODE_10007, msg=const.MSG_FAIL)
Exemplo n.º 13
0
    def post(self, request, pk):
        feed = pk

        token = request.META.get(const.META_TOKEN, const.UNKNOWN)
        payload = decode_token(token)
        account_id = payload.get(const.ACCOUNT_ID)
        account = account_id

        data = {const.FEED: feed, const.ACCOUNT: account}
        serializer = LikeSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            feed = Feed.objects.get(pk=feed)
            feed.like_count = feed.like_count + 1
            feed.save()
            return XResponse(status_code=const.CODE_10000,
                             msg=const.MSG_SUCCESS)
        else:
            return XResponse(status_code=const.CODE_10012, msg=const.MSG_FAIL)
Exemplo n.º 14
0
def error_response(message=u'服务器异常', status_code=const.CODE_10010):
    http_status = status.HTTP_200_OK

    if status_code == const.CODE_10001:
        http_status = status.HTTP_401_UNAUTHORIZED

    if status_code == const.CODE_10002:
        http_status = status.HTTP_403_FORBIDDEN

    return XResponse(status_code=status_code, msg=message, status=http_status)
Exemplo n.º 15
0
 def post(self, request, pk):
     feed = pk
     token = request.META.get(const.META_TOKEN, const.UNKNOWN)
     payload = decode_token(token)
     account = payload.get(const.ACCOUNT_ID)
     content = request.data[const.COMMENT_CONTENT]
     data = {
         const.FEED: feed,
         const.ACCOUNT: account,
         const.COMMENT_CONTENT: content
     }
     serializer = CommentDetailSerializer(data=data)
     if serializer.is_valid():
         serializer.save()
         data = serializer.data
         del (data['account'])
         del (data['feed'])
         return XResponse(status_code=const.CODE_10000,
                          msg=const.MSG_SUCCESS,
                          data=data)
     return XResponse(status_code=const.CODE_10007, msg=const.MSG_FAIL)
Exemplo n.º 16
0
    def post(request):
        openid = request.data[const.OPENID]

        if openid is not None:
            try:
                third_party = ThirdParty.objects.get(openid=openid)
                account_serializer = AccountSerializer(third_party.account)

                data_wrapper = {const.TOKEN: create_token(third_party.account.id, const.TOKEN_EXPIRE_TIME),
                                const.REFRESH_TOKEN: create_token(third_party.account.id,
                                                                  const.REFRESH_TOKEN_EXPIRE_TIME),
                                const.ACCOUNT: account_serializer.data}
                return XResponse(status_code=const.CODE_10000, msg=const.MSG_SUCCESS, data=data_wrapper)
            except ThirdParty.DoesNotExist:
                account = {const.NICKNAME: request.data[const.NICKNAME],
                           const.AVATAR: request.data[const.AVATAR]}
                create_account_serializer = CreateAccountSerializer(data=account)

                if create_account_serializer.is_valid():
                    account_instance = create_account_serializer.save()
                    third_party = ThirdParty(openid=openid,
                                             bind_type=request.data[const.BIND_TYPE],
                                             account=account_instance)
                    third_party.save(force_insert=True)

                    bill = Bill(account=account_instance)
                    bill.total = 10
                    bill.balance = 10
                    bill.save(force_insert=True)

                    data_wrapper = {const.TOKEN: create_token(account_instance.id, const.TOKEN_EXPIRE_TIME),
                                    const.REFRESH_TOKEN: create_token(third_party.account.id,
                                                                      const.REFRESH_TOKEN_EXPIRE_TIME),
                                    const.ACCOUNT: AccountSerializer(account_instance).data}
                    return XResponse(status_code=const.CODE_10000, msg=const.MSG_SUCCESS, data=data_wrapper)
                else:
                    return XResponse(status_code=const.CODE_10007, msg=const.MSG_FAIL,
                                     data=create_account_serializer.error_messages)
        else:
            return XResponse(status_code=const.CODE_10007, msg=const.MSG_FAIL)
Exemplo n.º 17
0
    def get(request, pk):
        try:
            feed = Feed.objects.get(pk=pk)
            feed.watch_count = feed.watch_count + 1
            feed.save()
            serializer = FeedDetailSerializer(feed)

            def is_like():
                token = request.META.get(const.META_TOKEN, const.UNKNOWN)

                if token.strip() != const.UNKNOWN:
                    payload = decode_token(token)
                    account_id = payload.get(const.ACCOUNT_ID)
                    likes = Like.objects.filter(account=account_id).filter(
                        feed=pk)
                    return likes.exists()
                else:
                    return False

            def is_bought():
                token = request.META.get(const.META_TOKEN, const.UNKNOWN)

                if token.strip() != const.UNKNOWN:
                    payload = decode_token(token)
                    account_id = payload.get(const.ACCOUNT_ID)
                    buy_logs = BuyLog.objects.filter(
                        account=account_id).filter(feed=pk)
                    return buy_logs.exists()
                else:
                    return False

            data = serializer.data
            data["is_like"] = is_like()
            data["is_bought"] = is_bought()
            return XResponse(status_code=const.CODE_10000,
                             msg=const.MSG_SUCCESS,
                             data=data)
        except Feed.DoesNotExist:
            return XResponse(status_code=const.CODE_10005, msg=const.MSG_FAIL)
Exemplo n.º 18
0
def api_paging(objects, request, Serializer):
    try:
        page_size = int(request.GET.get('page_size', const.PAGE_SIZE))
        page = int(request.GET.get('page', 1))
    except (TypeError, ValueError):
        return XResponse(status_code=const.CODE_10007, msg=const.MSG_FAIL)

    paginator = Paginator(objects, page_size)
    total = paginator.count
    try:
        objects = paginator.page(page)
    except PageNotAnInteger:
        objects = paginator.page(1)
    except EmptyPage:
        objects = paginator.page(paginator.num_pages)

    serializer = Serializer(objects, many=True)
    list_wrapper = {
        'list': serializer.data,
        'page': page,
        'total': total
    }
    return XResponse(status_code=const.CODE_10000, msg=const.MSG_SUCCESS, data=list_wrapper)
Exemplo n.º 19
0
 def post(request):
     refresh_token = request.META[const.REFRESH_TOKEN]
     try:
         payload = decode_token(refresh_token)
         account_id = payload.get(const.ACCOUNT_ID)
         token_wrapper = {
             const.TOKEN:
             create_token(account_id, const.TOKEN_EXPIRE_TIME),
             const.REFRESH_TOKEN:
             create_token(account_id, const.REFRESH_TOKEN_EXPIRE_TIME)
         }
         return XResponse(status_code=const.CODE_10000,
                          msg=const.MSG_SUCCESS,
                          data=token_wrapper)
     except (DecodeError, ExpiredSignatureError):
         raise PermissionDenied
Exemplo n.º 20
0
 def get(request):
     denominations = Denomination.objects.all()
     serializer = DenominationSerializer(denominations, many=True)
     return XResponse(status_code=const.CODE_10000,
                      msg=const.MSG_SUCCESS,
                      data=serializer.data)
Exemplo n.º 21
0
 def get(request):
     version = Version.objects.last()
     serializer = VersionSerializer(version, many=False)
     return XResponse(status_code=const.CODE_10000, msg=const.MSG_SUCCESS, data=serializer.data)
Exemplo n.º 22
0
 def wrapped_f(self, request, pk):
     token = request.META.get(const.META_TOKEN, const.UNKNOWN)
     if token.strip() == const.UNKNOWN:
         return XResponse(status_code=const.CODE_10011, msg=const.MSG_LOGIN_REQUIRED)
     else:
         return f(self, request, pk)
Exemplo n.º 23
0
 def get(request):
     feeds = Feed.objects.order_by('?')[:const.PAGE_SIZE]
     serializer = FeedListSerializer(feeds, many=True)
     return XResponse(status_code=const.CODE_10000,
                      msg=const.MSG_SUCCESS,
                      data=serializer.data)
Exemplo n.º 24
0
 def get(request):
     recommends = Recommend.objects.all()
     serializer = RecommendSerializer(recommends, many=True)
     return XResponse(status_code=const.CODE_10000,
                      msg=const.MSG_SUCCESS,
                      data=serializer.data)
Exemplo n.º 25
0
 def get(request):
     segments = Segment.objects.all()
     serializer = SegmentSerializer(segments, many=True)
     return XResponse(status_code=const.CODE_10000,
                      msg=const.MSG_SUCCESS,
                      data=serializer.data)