Example #1
0
def UploadMessage(requestData):
    TAG = Const.Tags.Urls.UPLOAD_MESSAGE

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

    try:
        clientUser = securityProperties.clientUserObject
        clientRecipientUserUUID = securityProperties.jsonRequestData[
            Const.Views.UploadMessage.JsonRequestKey.TO_USER_ID]
        clientMessageText = securityProperties.jsonRequestData[
            Const.Views.UploadMessage.JsonRequestKey.TEXT]
        clientMessageURL = securityProperties.jsonRequestData[
            Const.Views.UploadMessage.JsonRequestKey.URL]

        # Find the recipient user in the DB
        try:
            recipientUser = User.objects.get(
                uuid=Utils.ConvertUUIDToBinary(clientRecipientUserUUID))
        except ObjectDoesNotExist:
            DataCollector.logURL(
                TAG, {
                    Const.DataCollection.ParamNames.RESPONSE_CODE:
                    Const.HttpResponseFactory.ResponseCodes.ClientError.
                    CODE_UNPROCESSABLE_ENTITY,
                    Const.DataCollection.ParamNames.MESSAGE_CODE:
                    Const.DataCollection.MessageCodes.UploadMessage.
                    RECIPIENT_NOT_FOUND,
                    Const.DataCollection.ParamNames.FROM_USER:
                    Utils.ConvertBinaryToUUID(clientUser.uuid),
                    Const.DataCollection.ParamNames.TO_USER:
                    Utils.ConvertBinaryToUUID(recipientUser.uuid),
                    Const.DataCollection.ParamNames.HAS_TEXT:
                    (not Utils.StringIsEmpty(clientMessageText))
                })

            return HttpResponseFactory.MakeHttpResponse(
                Const.HttpResponseFactory.ResponseCodes.ClientError.
                CODE_UNPROCESSABLE_ENTITY, Const.DataCollection.MessageCodes.
                UploadMessage.RECIPIENT_NOT_FOUND)

        # Save the message in the DB
        newMessage = Message(toUser=recipientUser,
                             fromUser=clientUser,
                             text=clientMessageText,
                             url=clientMessageURL,
                             contentType=Const.Tags.ContentTypes.MESSAGE)

        # If there is an exception, roll back this db transaction
        with transaction.atomic():
            newMessage.save()

        # log and return on success
        DataCollector.logURL(
            TAG, {
                Const.DataCollection.ParamNames.RESPONSE_CODE:
                Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK,
                Const.DataCollection.ParamNames.MESSAGE_CODE:
                Const.DataCollection.MessageCodes.UploadMessage.
                POST_SUCCESSFUL,
                Const.DataCollection.ParamNames.FROM_USER:
                Utils.ConvertBinaryToUUID(clientUser.uuid),
                Const.DataCollection.ParamNames.TO_USER:
                Utils.ConvertBinaryToUUID(recipientUser.uuid),
                Const.DataCollection.ParamNames.HAS_TEXT:
                (not Utils.StringIsEmpty(clientMessageText))
            })

        return HttpResponseFactory.MakeHttpResponse(
            Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK,
            Const.DataCollection.MessageCodes.UploadMessage.POST_SUCCESSFUL)

    except Exception as e:
        DataCollector.logServerError(e)
        DataCollector.logURL(
            TAG, {
                Const.DataCollection.ParamNames.RESPONSE_CODE:
                Const.HttpResponseFactory.ResponseCodes.ServerError.
                CODE_INTERNAL_SERVER_ERROR,
                Const.DataCollection.ParamNames.MESSAGE_CODE:
                Const.DataCollection.MessageCodes.UploadMessage.
                POST_FAILED_SERVER_ERROR,
                Const.DataCollection.ParamNames.FROM_USER:
                Utils.ConvertBinaryToUUID(clientUser.uuid),
                Const.DataCollection.ParamNames.TO_USER:
                Utils.ConvertBinaryToUUID(recipientUser.uuid),
                Const.DataCollection.ParamNames.HAS_TEXT:
                (not Utils.StringIsEmpty(clientMessageText))
            })

        return HttpResponseFactory.MakeHttpResponse(
            Const.HttpResponseFactory.ResponseCodes.ServerError.
            CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes.
            UploadMessage.POST_FAILED_SERVER_ERROR)
Example #2
0
def Block(requestData):

    TAG = Const.Tags.Urls.MODERATION_BLOCK

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

    try:

        clientUser = securityProperties.clientUserObject
        clientBlockUser = securityProperties.jsonRequestData[
            Const.Views.Block.JsonRequestKey.USER_TO_BLOCK_ID]

        # Try to find the user to block in the Db
        try:
            blockedUser = User.objects.get(
                uuid=Utils.ConvertUUIDToBinary(clientBlockUser))
        except ObjectDoesNotExist:
            DataCollector.logURL(
                TAG, {
                    Const.DataCollection.ParamNames.RESPONSE_CODE:
                    Const.HttpResponseFactory.ResponseCodes.ClientError.
                    CODE_UNPROCESSABLE_ENTITY,
                    Const.DataCollection.ParamNames.MESSAGE_CODE:
                    Const.DataCollection.MessageCodes.ModerationBlock.
                    TARGET_USER_NOT_FOUND,
                    Const.DataCollection.ParamNames.BLOCKED_USER:
                    clientBlockUser,
                    Const.DataCollection.ParamNames.BLOCKER_USER:
                    Utils.ConvertBinaryToUUID(clientUser.uuid)
                })

            return HttpResponseFactory.MakeHttpResponse(
                Const.HttpResponseFactory.ResponseCodes.ClientError.
                CODE_UNPROCESSABLE_ENTITY, Const.DataCollection.MessageCodes.
                ModerationBlock.TARGET_USER_NOT_FOUND)

        # Make sure that this block does not already exist
        block = Block.objects.filter(blockerUser=clientUser,
                                     blockedUser=blockedUser)
        if block:
            DataCollector.logURL(
                TAG, {
                    Const.DataCollection.ParamNames.RESPONSE_CODE:
                    Const.HttpResponseFactory.ResponseCodes.ClientError.
                    CODE_CONFLICT,
                    Const.DataCollection.ParamNames.MESSAGE_CODE:
                    Const.DataCollection.MessageCodes.ModerationBlock.
                    BLOCK_EXISTS,
                    Const.DataCollection.ParamNames.BLOCKED_USER:
                    clientBlockUser,
                    Const.DataCollection.ParamNames.BLOCKER_USER:
                    Utils.ConvertBinaryToUUID(clientUser.uuid)
                })

            return HttpResponseFactory.MakeHttpResponse(
                Const.HttpResponseFactory.ResponseCodes.ClientError.
                CODE_CONFLICT,
                Const.DataCollection.MessageCodes.ModerationBlock.BLOCK_EXISTS)

        # Save the block in the DB
        # If there is an exception, roll back this db transaction
        with transaction.atomic():
            Block.objects.create(blockerUser=clientUser,
                                 blockedUser=blockedUser)

        # log and return on success
        DataCollector.logURL(
            TAG, {
                Const.DataCollection.ParamNames.RESPONSE_CODE:
                Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK,
                Const.DataCollection.ParamNames.MESSAGE_CODE:
                Const.DataCollection.MessageCodes.ModerationBlock.
                REQUEST_SUCCESSFUL,
                Const.DataCollection.ParamNames.BLOCKED_USER:
                clientBlockUser,
                Const.DataCollection.ParamNames.BLOCKER_USER:
                Utils.ConvertBinaryToUUID(clientUser.uuid)
            })

        return HttpResponseFactory.MakeHttpResponse(
            Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.
            DataCollection.MessageCodes.ModerationBlock.REQUEST_SUCCESSFUL)

    except Exception as e:
        DataCollector.logServerError(e)
        DataCollector.logURL(
            TAG, {
                Const.DataCollection.ParamNames.RESPONSE_CODE:
                Const.HttpResponseFactory.ResponseCodes.ServerError.
                CODE_INTERNAL_SERVER_ERROR,
                Const.DataCollection.ParamNames.MESSAGE_CODE:
                Const.DataCollection.MessageCodes.ModerationBlock.
                REQUEST_FAILED_SERVER_ERROR,
                Const.DataCollection.ParamNames.BLOCKED_USER:
                clientBlockUser,
                Const.DataCollection.ParamNames.BLOCKER_USER:
                Utils.ConvertBinaryToUUID(clientUser.uuid)
            })

        return HttpResponseFactory.MakeHttpResponse(
            Const.HttpResponseFactory.ResponseCodes.ServerError.
            CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes.
            ModerationBlock.REQUEST_FAILED_SERVER_ERROR)