Ejemplo n.º 1
0
    def post(self):
        logging.debug('SetAnswer.post() request.body=' + self.request.body)

        # Collect inputs
        requestLogId = os.environ.get(conf.REQUEST_LOG_ID)
        inputData = json.loads(self.request.body)
        logging.debug('SetAnswer.post() inputData=' + str(inputData))

        content = answer.standardizeContent(inputData.get('content', None))
        linkKeyString = inputData['linkKey']
        questionId = str(int(inputData['questionId']))
        browserCrumb = inputData.get('crumb', None)
        logging.debug('SetAnswer.post() content=' + str(content) +
                      ' browserCrumb=' + str(browserCrumb) +
                      ' linkKeyString=' + str(linkKeyString))

        responseData = {'success': False, 'requestLogId': requestLogId}
        cookieData = httpServer.validate(self.request, inputData, responseData,
                                         self.response)
        if not cookieData.valid(): return
        userId = cookieData.id()

        surveyId, loginRequired = retrieveSurveyIdFromLinkKey(
            cookieData, linkKeyString, responseData, self.response)
        if surveyId is None: return

        # Check answer length
        if (content is not None) and not httpServer.isLengthOk(
                content, '', conf.minLengthAnswer):
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.TOO_SHORT)

        # Retrieve question record.
        questionRec = question.Question.get_by_id(int(questionId))
        logging.debug('SetAnswer.post() questionRec=' + str(questionRec))

        if questionRec is None:
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage='question not found')
        if questionRec.surveyId != surveyId:
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage='questionRec.surveyId != surveyId')

        # Update answer and vote.
        answerRec, voteRecord = answer.vote(questionId, surveyId, content,
                                            userId, questionRec.creator)

        # Display updated answer.
        responseData.update({'success': True, 'answerContent': content})
        httpServer.outputJson(cookieData, responseData, self.response)
Ejemplo n.º 2
0
    def post(self):
        logging.debug(('SetAnswer', 'request.body=', self.request.body))

        # Collect inputs
        requestLogId = os.environ.get( conf.REQUEST_LOG_ID )
        inputData = json.loads( self.request.body )
        logging.debug(('SetAnswer', 'inputData=', inputData))

        content = answer.standardizeContent( inputData.get( 'content', None ) )
        linkKeyString = inputData['linkKey']
        questionId = str( int( inputData['questionId'] ) )
        browserCrumb = inputData.get( 'crumb', None )
        logging.debug(('SetAnswer', 'content=', content, 'browserCrumb=', browserCrumb, 'linkKeyString=', linkKeyString))

        responseData = { 'success':False, 'requestLogId':requestLogId }
        cookieData = httpServer.validate( self.request, inputData, responseData, self.response )
        if not cookieData.valid():  return
        userId = cookieData.id()

        # Retrieve link-key record
        surveyId, loginRequired = retrieveSurveyIdFromLinkKey( cookieData, linkKeyString, responseData, self.response )
        if surveyId is None:  return

        # Check answer length
        if ( content is not None ) and not httpServer.isLengthOk( content, '', conf.minLengthAnswer ):
            return httpServer.outputJson( cookieData, responseData, self.response, errorMessage=conf.TOO_SHORT )

        # Retrieve survey record to check whether survey is frozen
        surveyRec = survey.Survey.get_by_id( int(surveyId) )
        if surveyRec is None:  return httpServer.outputJson( cookieData, responseData, self.response, errorMessage='survey record not found' )
        if surveyRec.freezeUserInput:  return httpServer.outputJson( cookieData, responseData, self.response, errorMessage=conf.FROZEN )

        # Require that answer and reason both exist, or both can be empty
        answerStr, reason = answer.parseAnswerAndReason( content )
        if surveyRec.hideReasons and reason:  return httpServer.outputJson( cookieData, responseData, self.response, errorMessage='reasons hidden' )
        if (not answerStr) and reason:  return httpServer.outputJson( cookieData, responseData, self.response, errorMessage=conf.TOO_SHORT )
        if answerStr and (not reason) and (not surveyRec.hideReasons):  return httpServer.outputJson( cookieData, responseData, self.response, errorMessage=conf.REASON_TOO_SHORT )

        # Retrieve question record.
        questionRec = question.Question.get_by_id( int(questionId) )
        logging.debug(('SetAnswer', 'questionRec=', questionRec))

        if questionRec is None:  return httpServer.outputJson( cookieData, responseData, self.response, errorMessage='question not found' )
        if questionRec.surveyId != surveyId:  return httpServer.outputJson( cookieData, responseData, self.response, errorMessage='questionRec.surveyId != surveyId' )
        
        # Update answer and vote.
        answerRec, voteRecord = answer.vote( questionId, surveyId, content, userId, questionRec.creator )

        # Display updated answer.
        responseData.update(  { 'success':True, 'answerContent':content }  )
        httpServer.outputJson( cookieData, responseData, self.response )
Ejemplo n.º 3
0
    def post(self, linkKeyStr, questionId):

        logging.debug(
            LogMessage('QuestionAnswersForPrefix', 'linkKeyStr=', linkKeyStr,
                       'questionId=', questionId))

        # Collect inputs
        inputData = json.loads(self.request.body)
        logging.debug(
            LogMessage('QuestionAnswersForPrefix', 'inputData=', inputData))
        answerStart = answer.standardizeContent(
            inputData.get('answerStart', None))
        logging.debug(
            LogMessage('QuestionAnswersForPrefix', 'answerStart=',
                       answerStart))

        httpRequestId = os.environ.get(conf.REQUEST_LOG_ID)
        responseData = {'success': False, 'httpRequestId': httpRequestId}

        # No user-id required, works for any user with the link-key
        cookieData = httpServer.validate(self.request,
                                         self.request.GET,
                                         responseData,
                                         self.response,
                                         idRequired=False)
        userId = cookieData.id()

        # Retrieve and check linkKey.
        linkKeyRecord = linkKey.LinkKey.get_by_id(linkKeyStr)
        if (linkKeyRecord is None) or (linkKeyRecord.destinationType !=
                                       conf.SURVEY_CLASS_NAME):
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.BAD_LINK)
        surveyId = linkKeyRecord.destinationId

        # No need to enforce login-required in GET calls, only on write operations that create/use link-key
        # But enforcing here because the search is expensive, and part of a write flow
        if linkKeyRecord.loginRequired and not cookieData.loginId:
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.NO_LOGIN)

        # Check that question is part of survey, because the answer query may be too expensive to allow unnecessary calls.
        questionRecord = question.Question.get_by_id(int(questionId))
        if questionRecord is None:
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage='questionRecord is null')
        if questionRecord.surveyId != surveyId:
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage='questionRecord.surveyId != surveyId')

        # Retrieve survey record
        surveyRecord = survey.Survey.get_by_id(int(surveyId))
        if surveyRecord is None:
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage='survey record not found')
        # Check that survey is not frozen, to reduce the cost of abuse via search queries
        if surveyRecord.freezeUserInput:
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.FROZEN)

        # Retrieve best suggested answers for this question and creator.
        answersOrdered = answer.retrieveTopAnswers(
            surveyId,
            questionId,
            answerStart=answerStart,
            hideReasons=surveyRecord.hideReasons)
        logging.debug(
            LogMessage('QuestionAnswersForPrefix', 'answersOrdered=',
                       answersOrdered))

        answerDisplays = [
            httpServerAutocomplete.answerToDisplay(a, userId)
            for a in answersOrdered
        ]

        # Display answers data.
        responseData.update({'success': True, 'answers': answerDisplays})
        httpServer.outputJson(cookieData, responseData, self.response)
Ejemplo n.º 4
0
    def post(self):
        logging.debug('EditAnswer.post() request.body=' + self.request.body)

        # Collect inputs
        requestLogId = os.environ.get(conf.REQUEST_LOG_ID)
        inputData = json.loads(self.request.body)
        logging.debug('EditAnswer.post() inputData=' + str(inputData))

        responseData = {'success': False, 'requestLogId': requestLogId}

        cookieData = httpServer.validate(self.request, inputData, responseData,
                                         self.response)
        if not cookieData.valid(): return
        userId = cookieData.id()

        content = answer.standardizeContent(
            text.formTextToStored(inputData['content']))
        linkKeyString = inputData['linkKey']
        answerId = inputData['answerId']
        questionId = str(int(inputData['questionId']))
        browserCrumb = inputData.get('crumb', None)
        logging.debug('EditAnswer.post() content=' + str(content) +
                      ' browserCrumb=' + str(browserCrumb) +
                      ' linkKeyString=' + str(linkKeyString) + ' answerId=' +
                      str(answerId))

        surveyId, loginRequired = retrieveSurveyIdFromLinkKey(
            cookieData, linkKeyString, responseData, self.response)
        if surveyId is None: return

        # Retrieve survey record to check survey creator
        surveyRec = survey.Survey.get_by_id(int(surveyId))
        if surveyRec is None:
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage='survey record not found')
        if surveyRec.creator != userId:
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage='surveyRec.creator != userId')

        # Split answers by newline
        contentLines = content.split('\n') if content else []
        # For each non-empty answer...
        answerDisplays = []
        for contentLine in contentLines:
            logging.debug('EditAnswer.post() contentLine=' + str(contentLine))
            if not contentLine: continue

            # Check answer length
            if not httpServer.isLengthOk(contentLine, '',
                                         conf.minLengthAnswer):
                return httpServer.outputJson(cookieData,
                                             responseData,
                                             self.response,
                                             errorMessage=conf.TOO_SHORT)

            # If new answer value already exists... error.  If new answer is same as old answer value... no problem?
            newAnswerId = answer.toKeyId(questionId, contentLine)
            answerRec = answer.Answer.get_by_id(newAnswerId)
            if answerRec: continue

            # Delete old answer
            if answerId:
                oldAnswerRec = answer.Answer.get_by_id(answerId)
                if oldAnswerRec is None:
                    return httpServer.outputJson(
                        cookieData,
                        responseData,
                        self.response,
                        errorMessage='answer record not found')
                if oldAnswerRec.surveyId != surveyId:
                    return httpServer.outputJson(
                        cookieData,
                        responseData,
                        self.response,
                        errorMessage='oldAnswerRec.surveyId != surveyId')
                oldAnswerRec.key.delete()
            # Create new answer
            answerRec = answer.newAnswer(questionId,
                                         surveyId,
                                         contentLine,
                                         userId,
                                         voteCount=0,
                                         fromEditPage=True)
            answerDisplay = httpServerAutocomplete.answerToDisplay(
                answerRec, userId)
            answerDisplays.append(answerDisplay)

        # Display updated answers
        responseData.update({'success': True, 'answers': answerDisplays})
        httpServer.outputJson(cookieData, responseData, self.response)