Esempio n. 1
0
    def post(self):
        logging.debug('SubmitNewSurvey.post() request.body=' +
                      self.request.body)

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

        introduction = text.formTextToStored(inputData.get('introduction', ''))
        browserCrumb = inputData.get('crumb', '')
        loginCrumb = inputData.get('crumbForLogin', '')
        loginRequired = inputData.get('loginRequired', False)
        logging.debug('SubmitNewSurvey.post() introduction=' +
                      str(introduction) + ' browserCrumb=' +
                      str(browserCrumb) + ' loginCrumb=' + str(loginCrumb) +
                      ' loginRequired=' + str(loginRequired))

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

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

        # Check survey introduction length.
        if not httpServer.isLengthOk(introduction, '',
                                     conf.minLengthSurveyIntro):
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.TOO_SHORT)

        # Construct and store new survey record.
        surveyRecord = survey.Survey(creator=userId,
                                     introduction=introduction,
                                     allowEdit=True)
        surveyRecordKey = surveyRecord.put()
        logging.debug('surveyRecordKey.id={}'.format(surveyRecordKey.id()))

        # Construct and store link key.
        surveyId = str(surveyRecordKey.id())
        linkKeyRecord = httpServer.createAndStoreLinkKey(
            conf.SURVEY_CLASS_NAME, surveyId, loginRequired, cookieData)

        # Display survey.
        surveyDisplay = httpServerAutocomplete.surveyToDisplay(
            surveyRecord, userId)
        linkKeyDisplay = httpServer.linkKeyToDisplay(linkKeyRecord)
        responseData.update({
            'success': True,
            'linkKey': linkKeyDisplay,
            'survey': surveyDisplay
        })
        httpServer.outputJson(cookieData, responseData, self.response)
Esempio n. 2
0
    def post(self):
        logging.debug('SubmitNewBudget.post() request.body=' +
                      self.request.body)

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

        title = text.formTextToStored(inputData.get('title', ''))
        introduction = text.formTextToStored(inputData.get('introduction', ''))
        total = float(inputData.get('total', None))
        loginRequired = inputData.get('loginRequired', False)
        experimentalPassword = inputData.get('experimentalPassword', None)
        hideReasons = bool(inputData.get('hideReasons', False))
        logging.debug('SubmitNewBudget.post() title=' + str(title) +
                      ' total=' + str(total) + ' introduction=' +
                      str(introduction) + ' loginRequired=' +
                      str(loginRequired) + ' hideReasons=' + str(hideReasons))

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

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

        # Check budget introduction length
        if not httpServer.isLengthOk(title, introduction,
                                     conf.minLengthBudgetIntro):
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.TOO_SHORT)

        # Check budget-total
        if (total <= 0):
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage='Budget total must be a positive number')

        # Check experimental password (low-risk secret)
        if (hideReasons or loginRequired or experimentalPassword) and (
                experimentalPassword != secrets.experimentalPassword):
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage=conf.EXPERIMENT_NOT_AUTHORIZED)

        # Construct and store new budget record
        budgetRecord = budget.Budget(creator=userId,
                                     title=title,
                                     introduction=introduction,
                                     total=total,
                                     allowEdit=True,
                                     hideReasons=hideReasons)
        budgetRecordKey = budgetRecord.put()
        logging.debug('budgetRecordKey.id={}'.format(budgetRecordKey.id()))

        # Construct and store link key
        budgetId = str(budgetRecordKey.id())
        linkKeyRecord = httpServer.createAndStoreLinkKey(
            conf.BUDGET_CLASS_NAME, budgetId, loginRequired, cookieData)

        # Display budget
        budgetDisplay = httpServerBudget.budgetToDisplay(budgetRecord, userId)
        linkKeyDisplay = httpServer.linkKeyToDisplay(linkKeyRecord)
        responseData.update({
            'success': True,
            'linkKey': linkKeyDisplay,
            'budget': budgetDisplay
        })
        httpServer.outputJson(cookieData, responseData, self.response)
Esempio n. 3
0
    def post(
        self
    ):  # Not a transaction, because it is ok to fail link creation, and orphan the request.

        logging.debug(
            LogMessage('SubmitNewRequest', 'request.body=', self.request.body))

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

        title = text.formTextToStored(inputData.get('title', ''))
        detail = text.formTextToStored(inputData.get('detail', ''))
        loginRequired = bool(inputData.get('loginRequired', False))
        experimentalPassword = inputData.get('experimentalPassword', None)
        hideReasons = bool(inputData.get('hideReasons', False))
        browserCrumb = inputData.get('crumb', '')
        loginCrumb = inputData.get('crumbForLogin', '')
        logging.debug(
            LogMessage('SubmitNewRequest', 'title=', title, 'detail=', detail,
                       'browserCrumb=', browserCrumb, 'loginCrumb=',
                       loginCrumb, 'loginRequired=', loginRequired,
                       'hideReasons=', hideReasons))

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

        # Check request length
        if not httpServer.isLengthOk(title, detail, conf.minLengthRequest):
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.TOO_SHORT)

        # Check experimental password (low-risk secret)
        if (hideReasons or loginRequired or experimentalPassword) and (
                experimentalPassword != secrets.experimentalPassword):
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage=conf.EXPERIMENT_NOT_AUTHORIZED)

        # Construct new request record
        requestRecord = requestForProposals.RequestForProposals(
            creator=userId,
            title=title,
            detail=detail,
            allowEdit=True,
            hideReasons=hideReasons)
        # Store request record
        requestRecordKey = requestRecord.put()
        logging.debug(
            LogMessage('SubmitNewRequest', 'requestRecordKey.id=',
                       requestRecordKey.id()))

        # Construct and store link key.
        requestId = str(requestRecordKey.id())
        linkKeyRecord = httpServer.createAndStoreLinkKey(
            conf.REQUEST_CLASS_NAME, requestId, loginRequired, cookieData)

        # Send response data.
        linkKeyDisplay = httpServer.linkKeyToDisplay(linkKeyRecord)
        requestDisplay = httpServer.requestToDisplay(requestRecord, userId)
        responseData.update({
            'success': True,
            'linkKey': linkKeyDisplay,
            'request': requestDisplay
        })
        httpServer.outputJson(cookieData, responseData, self.response)
Esempio n. 4
0
    def post(
        self
    ):  # Not a transaction, because it is ok to fail link creation, and orphan the request.

        logging.debug('SubmitNewRequest.post() request.body=' +
                      self.request.body)

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

        title = text.formTextToStored(inputData.get('title', ''))
        detail = text.formTextToStored(inputData.get('detail', ''))
        loginRequired = inputData.get('loginRequired', False)
        browserCrumb = inputData.get('crumb', '')
        loginCrumb = inputData.get('crumbForLogin', '')
        logging.debug('SubmitNewRequest.post() title=' + str(title) +
                      ' detail=' + str(detail) + ' browserCrumb=' +
                      str(browserCrumb) + ' loginCrumb=' + str(loginCrumb) +
                      ' loginRequired=' + str(loginRequired))

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

        # Check request length
        if not httpServer.isLengthOk(title, detail, conf.minLengthRequest):
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.TOO_SHORT)

        # Construct new request record
        requestRecord = requestForProposals.RequestForProposals(
            creator=userId,
            title=title,
            detail=detail,
            allowEdit=True,
        )
        # Store request record
        requestRecordKey = requestRecord.put()
        logging.debug('requestRecordKey.id={}'.format(requestRecordKey.id()))

        # Construct and store link key.
        requestId = str(requestRecordKey.id())
        linkKeyRecord = httpServer.createAndStoreLinkKey(
            conf.REQUEST_CLASS_NAME, requestId, loginRequired, cookieData)

        # Send response data.
        linkKeyDisplay = httpServer.linkKeyToDisplay(linkKeyRecord)
        requestDisplay = httpServer.requestToDisplay(requestRecord, userId)
        responseData.update({
            'success': True,
            'linkKey': linkKeyDisplay,
            'request': requestDisplay
        })
        httpServer.outputJson(cookieData, responseData, self.response)
Esempio n. 5
0
    def post(self):

        logging.debug('SubmitNewProposal.post() request.body=' +
                      self.request.body)

        # Collect inputs
        requestLogId = os.environ.get(conf.REQUEST_LOG_ID)
        responseData = {'success': False, 'requestLogId': requestLogId}
        inputData = json.loads(self.request.body)
        logging.debug('SubmitNewProposal.post() inputData=' + str(inputData))

        title = text.formTextToStored(inputData.get('title', ''))
        detail = text.formTextToStored(inputData.get('detail', ''))
        loginRequired = inputData.get('loginRequired', False)
        browserCrumb = inputData.get('crumb', '')
        loginCrumb = inputData.get('crumbForLogin', '')
        logging.debug('SubmitNewProposal.post() title=' + str(title) +
                      ' detail=' + str(detail) + ' browserCrumb=' +
                      str(browserCrumb) + ' loginCrumb=' + str(loginCrumb) +
                      ' loginRequired=' + str(loginRequired))

        # Voter login not required to create initial proposal, though login may be required to use proposal
        cookieData = httpServer.validate(self.request,
                                         inputData,
                                         responseData,
                                         self.response,
                                         loginRequired=loginRequired)
        if not cookieData.valid(): return
        userId = cookieData.id()

        # Check proposal length.
        if not httpServer.isLengthOk(title, detail, conf.minLengthProposal):
            return httpServer.outputJson(responseData,
                                         self.response,
                                         errorMessage=conf.TOO_SHORT)

        # Construct new proposal record.
        proposalRecord = proposal.Proposal(
            creator=userId,
            title=title,
            detail=detail,
            allowEdit=True,
        )
        # Store proposal record.
        proposalRecordKey = proposalRecord.put()
        logging.debug('proposalRecordKey.id={}'.format(proposalRecordKey.id()))

        # Construct and store link key.
        proposalId = str(proposalRecordKey.id())
        proposalLinkKeyRecord = httpServer.createAndStoreLinkKey(
            conf.PROPOSAL_CLASS_NAME, proposalId, loginRequired, cookieData)

        # Display proposal
        linkKeyDisplay = httpServer.linkKeyToDisplay(proposalLinkKeyRecord)
        proposalDisplay = httpServer.proposalToDisplay(proposalRecord, userId)
        responseData.update({
            'success': True,
            'linkKey': linkKeyDisplay,
            'proposal': proposalDisplay
        })
        httpServer.outputJson(cookieData, responseData, self.response)