Esempio n. 1
0
 def process_request(request):
     token = request.META.get(const.META_TOKEN, const.UNKNOWN)
     if token.strip() != const.UNKNOWN:
         try:
             decode_token(token)
         except (DecodeError, ExpiredSignatureError):
             error = {
                 const.STATUS_CODE: const.CODE_10001,
                 const.MSG: const.MSG_FAIL
             }
             return JsonResponse(error, status=status.HTTP_401_UNAUTHORIZED)
     return None
Esempio 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)
Esempio n. 3
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)
Esempio n. 4
0
            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
Esempio n. 5
0
            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
Esempio n. 6
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)
Esempio n. 7
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
Esempio n. 8
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)
Esempio n. 9
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)
Esempio n. 10
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)