Exemplo n.º 1
0
    def _PrintThreadReplies(self):
        replies = QueryManager.GetThreadReplies(self.contentID)

        print('PRINITING REPLIES FOR THEAD (CID ', self.contentID, ')')

        for re in replies:
            print(
                '--------------------------------------------------------------------------------'
            )
            print('REPLY (CID ', Utils.BinaryToUUID(re.cid.id), ')')
            print('TimeCreated: ',
                  Utils.GetPrettyFormatTimestamp(re.timeCreated))
            print('FromUser: '******'OpName: ', re.name)
            print('HasImage: ', bool(re.url))
            print('Text: ', re.text)

        Utils.PrintEndLine()
Exemplo n.º 2
0
def CreateUser(requestData):
    TAG = Const.Tags.Urls.SECURITY_CREATE

    securityProperties = RunThroughSecurityLayer(TAG, requestData)
    if (not securityProperties.isSecure):
        return securityProperties.httpResponse

    try:
        # Create the new user identity in the db
        with transaction.atomic():
            newUser = User.objects.create()
            newUserID = Utils.BinaryToUUID(newUser.id)
            identityID = ''

            # Create a new identity on Cognito with the new uuid
            # Only when in not in DEBUG. I don't want to get charged ;)
            if (not settings.DEBUG):
                identityID = AuthManager.CreateNewCognitoIdentity(newUserID)

            jsonDict = _CreateUserClientObject(newUserID,
                                               identityID).getOrderedDict()
            jsonString = json.dumps(jsonDict)
            logger.info(jsonString)

            # Update the URL hit and return
            DataCollector.UpdateURLHit(
                hitID=securityProperties.hitID,
                responseCode=Const.HttpResponseFactory.ResponseCodes.Success.
                CODE_OK,
                messageCode=Const.DataCollection.MessageCodes.SecurityCreate.
                CREATE_SUCCESSFUL)

            return HttpResponseFactory.MakeHttpResponse(
                Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK,
                jsonString, 'application/json')

    except Exception as e:
        DataCollector.logServerError(e)
        DataCollector.UpdateURLHit(
            hitID=securityProperties.hitID,
            responseCode=Const.HttpResponseFactory.ResponseCodes.ServerError.
            CODE_INTERNAL_SERVER_ERROR,
            messageCode=Const.DataCollection.MessageCodes.SecurityCreate.
            CREATE_FAILED_SERVER_ERROR)

        return HttpResponseFactory.MakeHttpResponse(
            Const.HttpResponseFactory.ResponseCodes.ServerError.
            CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes.
            SecurityCreate.CREATE_FAILED_SERVER_ERROR)
Exemplo n.º 3
0
def GetThreadListJsonString():
    
    # get the threads by time created
    threads = QueryManager.GetLiveThreadsByTimeLastActive()
        
    # Get the stuff we need from the thread, package and return to the client
    clientThreadsToReturn = []
    for index, thread in enumerate(threads):
        objectToReturn = _GetThreadClientObject(id=Utils.BinaryToUUID(thread.id),
                                                text=thread.text, 
                                                time=thread.timeCreated, 
                                                key=thread.key,
                                                order=index,
                                                replies=thread.replyCount,
                                                unique=thread.uniquePostersCount,
                                                arn=thread.arn) 
        clientThreadsToReturn.append(objectToReturn.getOrderedDict())   
           
    return json.dumps(clientThreadsToReturn)
Exemplo n.º 4
0
def GetReply(requestData):
    TAG = Const.Tags.Urls.GET_REPLY

    securityProperties = RunThroughSecurityLayer(TAG, requestData)
    if (not securityProperties.isSecure):
        return securityProperties.httpResponse

    try:

        clientUser = securityProperties.userObject
        clientThreadID = securityProperties.jsonRequestData[
            Const.Views.GetReply.JsonRequestKey.THREAD_ID]

        # Retrieve the thread and replies from the database
        thread = QueryManager.GetObjectByID(Thread, clientThreadID)
        threadReplies = Reply.objects.filter(parentThread=thread)

        if (not thread):
            DataCollector.UpdateURLHit(
                hitID=securityProperties.hitID,
                responseCode=Const.HttpResponseFactory.ResponseCodes.
                ClientError.CODE_NOT_FOUND,
                messageCode=Const.DataCollection.MessageCodes.GetReply.
                THREAD_NOT_FOUND)

            return HttpResponseFactory.MakeHttpResponse(
                Const.HttpResponseFactory.ResponseCodes.ClientError.
                CODE_NOT_FOUND,
                Const.DataCollection.MessageCodes.GetReply.THREAD_NOT_FOUND)

        # Package the thread replies and return
        clientReplyListToReturn = []
        for reply in threadReplies:
            replyClientObject = GetReplyClientObject(text=reply.text,
                                                     time=reply.timeCreated,
                                                     id=Utils.BinaryToUUID(
                                                         reply.id),
                                                     key=reply.key)
            clientReplyListToReturn.append(replyClientObject.getDict())

        jsonString = json.dumps(clientReplyListToReturn)

        # log and return on success
        DataCollector.UpdateURLHit(hitID=securityProperties.hitID,
                                   responseCode=Const.HttpResponseFactory.
                                   ResponseCodes.Success.CODE_OK,
                                   messageCode=Const.DataCollection.
                                   MessageCodes.GetReply.REQUEST_SUCCESSFUL)

        return HttpResponseFactory.MakeHttpResponse(
            Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK,
            jsonString, 'application/json')

    except Exception as e:
        DataCollector.logServerError(e)
        DataCollector.UpdateURLHit(
            hitID=securityProperties.hitID,
            responseCode=Const.HttpResponseFactory.ResponseCodes.ServerError.
            CODE_INTERNAL_SERVER_ERROR,
            messageCode=Const.DataCollection.MessageCodes.GetReply.
            REQUEST_FAILED_SERVER_ERROR)

        return HttpResponseFactory.MakeHttpResponse(
            Const.HttpResponseFactory.ResponseCodes.ServerError.
            CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes.
            GetReply.REQUEST_FAILED_SERVER_ERROR)
Exemplo n.º 5
0
def UploadReply(requestData):
    TAG = Const.Tags.Urls.UPLOAD_REPLY
     
    securityProperties = RunThroughSecurityLayer(TAG, requestData)
    if (not securityProperties.isSecure):
        return securityProperties.httpResponse
    
    try:
        clientUser = securityProperties.userObject
        clientSession = securityProperties.userSession
        clientThreadID = securityProperties.jsonRequestData[Const.Views.UploadReply.JsonRequestKey.THREAD_ID]
        clientReplyText= securityProperties.jsonRequestData[Const.Views.UploadReply.JsonRequestKey.REPLY_TEXT]
        clientReplyKey = securityProperties.jsonRequestData[Const.Views.UploadReply.JsonRequestKey.REPLY_URL]

        # Moderation - check if this user is posting replies too fast
        if (settings.RATE_LIMIT_LIVE and RateLimiter.UserReplyRateLimitExceeded(clientUser.id)):
            DataCollector.UpdateURLHit(hitID=securityProperties.hitID,
                                       responseCode=Const.HttpResponseFactory.ResponseCodes.ClientError.CODE_TOO_MANY_REQUESTS, 
                                       messageCode=Const.DataCollection.MessageCodes.UploadReply.RATE_LIMIT_EXCEEDED)  
            
            return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.ClientError.CODE_TOO_MANY_REQUESTS, 
                                                        Const.DataCollection.MessageCodes.UploadReply.RATE_LIMIT_EXCEEDED)
        
        # Find the parent thread to reply to in the DB  
        threadToReplyTo = QueryManager.GetObjectByID(Thread, clientThreadID)
        
        if (not threadToReplyTo):
            DataCollector.UpdateURLHit(hitID=securityProperties.hitID,
                                       responseCode=Const.HttpResponseFactory.ResponseCodes.ClientError.CODE_NOT_FOUND, 
                                       messageCode=Const.DataCollection.MessageCodes.UploadReply.THREAD_NOT_FOUND) 
            
            return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.ClientError.CODE_NOT_FOUND, 
                                                        Const.DataCollection.MessageCodes.UploadReply.THREAD_NOT_FOUND)

        # These fields are optional. Make sure that they go into the DB
        # as an empty string if they are not present 
        if (Utils.StringIsEmpty(clientReplyText)):
            clientReplyText = ''
        if (Utils.StringIsEmpty(clientReplyKey)):
            clientReplyKey = ''

        # Save the reply in the DB
        newReply = Reply.objects.create(fromUser=clientUser,
                             fromSession=clientSession,
                             contentType=Const.Tags.ContentTypes.REPLY,
                             parentThread=threadToReplyTo,
                             text=clientReplyText,
                             key=clientReplyKey)
        
        # Broadcast the reply out to this thread's subscribers using GCM
        # Create the client reply object
        newReplyClientObject = GetReplyClientObject(text=newReply.text, 
                                                    time=newReply.timeCreated, 
                                                    id=Utils.BinaryToUUID(newReply.id), 
                                                    key=newReply.key) 
        
        # Turn it into JSON and send it off
        googleResponseCode = GCMManager.BroadcastReplyToSubscribers(parentThreadID=clientThreadID,
                                               newReplyJSON=newReplyClientObject.getDict()) 
        
        # Check the response code from google
        # If it is not successful, return and log a warning, but still
        # return a 200 code to the client (since the reply saved ok)
        if (googleResponseCode != Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK):
                DataCollector.UpdateURLHit(hitID=securityProperties.hitID, 
                                   responseCode=Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK,
                                   messageCode=Const.DataCollection.MessageCodes.UploadReply.GCM_BROADCAST_FAILED)
                       
                return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK,
                                                            Const.DataCollection.MessageCodes.UploadReply.GCM_BROADCAST_FAILED)
        
        
        # log and return on success
        DataCollector.UpdateURLHit(hitID=securityProperties.hitID,
                                    responseCode=Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, 
                                    messageCode=Const.DataCollection.MessageCodes.UploadReply.POST_SUCCESSFUL) 
        
        return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, 
                                                    Const.DataCollection.MessageCodes.UploadReply.POST_SUCCESSFUL)
    
    except Exception as e:
        DataCollector.logServerError(e)
        DataCollector.UpdateURLHit(hitID=securityProperties.hitID,
                                    responseCode=Const.HttpResponseFactory.ResponseCodes.ServerError.CODE_INTERNAL_SERVER_ERROR, 
                                    messageCode=Const.DataCollection.MessageCodes.UploadReply.POST_FAILED_SERVER_ERROR)  
        
        return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.ServerError.CODE_INTERNAL_SERVER_ERROR, 
                                                    Const.DataCollection.MessageCodes.UploadReply.POST_FAILED_SERVER_ERROR)