Exemplo n.º 1
0
    def get( self, linkKeyStr ):

        logging.debug( 'getBudget.GetBudget() linkKeyStr=' + linkKeyStr )

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

        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.BUDGET_CLASS_NAME):
            return httpServer.outputJson( cookieData, responseData, self.response, errorMessage=conf.BAD_LINK )
        budgetId = linkKeyRecord.destinationId

        # Retrieve Budget by id, filter/transform fields for display.
        budgetRecord = budget.Budget.get_by_id( int(budgetId) )
        logging.debug( 'GetBudgetData() budgetRecord=' + str(budgetRecord) )

        budgetDisp = httpServerBudget.budgetToDisplay( budgetRecord, userId )
        logging.debug( 'GetBudgetData() budgetDisp=' + str(budgetDisp) )
        
        linkKeyDisplay = httpServer.linkKeyToDisplay( linkKeyRecord )
        
        # Store budget to user's recent (cookie).
        user.storeRecentLinkKey( linkKeyStr, cookieData )

        # Display budget data.
        responseData = { 'success':True , 'linkKey':linkKeyDisplay , 'budget':budgetDisp }
        httpServer.outputJson( cookieData, responseData, self.response )
Exemplo n.º 2
0
def createAndStoreLinkKey(destClassName, destinationId, loginRequired,
                          cookieData):
    linkKeyString = linkKey.createLinkKey()
    logging.debug('linkKeyString={}'.format(linkKeyString))

    linkKeyRecord = linkKey.LinkKey(id=linkKeyString,
                                    destinationType=destClassName,
                                    destinationId=destinationId,
                                    loginRequired=loginRequired)
    linkKeyRecordKey = linkKeyRecord.put()

    user.storeRecentLinkKey(linkKeyString, cookieData)

    return linkKeyRecord
Exemplo n.º 3
0
    def get(self, linkKeyStr, questionId):

        logging.debug('GetQuestion.get() linkKeyStr=' + str(linkKeyStr) +
                      ' questionId=' + str(questionId))

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

        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

        # Retrieve Question by id, filter/transform fields for display.
        questionRecord = question.Question.get_by_id(int(questionId))
        logging.debug('GetQuestion.get() questionRecord=' +
                      str(questionRecord))
        if questionRecord.surveyId != surveyId:
            return httpServer.outputJson(
                cookieData,
                responseData,
                self.response,
                errorMessage='questionRecord.surveyId != surveyId')

        questionDisp = httpServerAutocomplete.questionToDisplay(
            questionRecord, userId)
        logging.debug('GetQuestion.get() questionDisp=' + str(questionDisp))

        # Store question to user's recent (cookie).
        user.storeRecentLinkKey(linkKeyStr, cookieData)

        # Display question data.
        responseData = {'success': True, 'question': questionDisp}
        httpServer.outputJson(cookieData, responseData, self.response)
Exemplo n.º 4
0
    def get(self, linkKeyStr, proposalId):

        logging.debug('getProposalData.GetProposalData() linkKeyStr=' +
                      linkKeyStr + ' proposalId=' + str(proposalId))

        # Collect inputs.
        onlyTopReasons = (self.request.get('onlyTop', None) == 'true')
        logging.debug('onlyTopReasons=' + str(onlyTopReasons))

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

        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)
        requestId = None
        if linkKeyRecord is None:
            logging.debug('linkKeyRecord is None')
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.BAD_LINK)
        elif linkKeyRecord.destinationType == conf.PROPOSAL_CLASS_NAME:
            proposalId = linkKeyRecord.destinationId
        elif linkKeyRecord.destinationType == conf.REQUEST_CLASS_NAME:
            requestId = linkKeyRecord.destinationId
        else:
            logging.debug('linkKeyRecord has unhandled destinationType=' +
                          str(linkKeyRecord.destinationType))
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.BAD_LINK)

        # Retrieve Proposal by id, filter/transform fields for display.
        proposalRecord = proposal.Proposal.get_by_id(int(proposalId))
        logging.debug('GetProposalData() proposalRecord=' +
                      str(proposalRecord))
        if proposalRecord.requestId != requestId:
            logging.debug('proposalRecord.requestId=' +
                          str(proposalRecord.requestId) + '  !=  requestId=' +
                          str(requestId))
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.BAD_LINK)

        proposalDisp = httpServer.proposalToDisplay(proposalRecord, userId)
        logging.debug('GetProposalData() proposalDisp=' + str(proposalDisp))

        # Prepare parallel retrieve reasons, with votes.
        if onlyTopReasons:
            reasonRecordsFutures = reason.retrieveTopReasonsAsync(
                proposalId, conf.MAX_TOP_REASONS)
        else:
            reasonRecordsFutures = [
                reason.Reason.query(
                    reason.Reason.proposalId == proposalId).fetch_async()
            ]

        # If userId exists... retrieve user's ReasonVotes by KeyProperty proposalId x userId.
        voteRecordsFuture = reasonVote.ReasonVote.query(
            reasonVote.ReasonVote.proposalId == proposalId,
            reasonVote.ReasonVote.userId
            == userId).fetch_async() if userId else None

        # Run parallel retrieval.  Filter/transform fields for display.
        linkKeyDisplay = httpServer.linkKeyToDisplay(linkKeyRecord)
        logging.debug('GetProposalData() linkKeyDisplay=' +
                      str(linkKeyDisplay))

        reasonRecords = reason.fetchReasonRecordsFutures(reasonRecordsFutures)
        logging.debug('GetProposalData() reasonRecords=' + str(reasonRecords))

        reasonDisps = [
            httpServer.reasonToDisplay(r, userId) for r in reasonRecords
        ]
        logging.debug('GetProposalData() reasonDisps=' + str(reasonDisps))

        voteRecords = voteRecordsFuture.get_result(
        ) if voteRecordsFuture else None
        logging.debug('GetProposalData() voteRecords=' + str(voteRecords))

        # For each reason... lookup user vote ... set reason.myVote
        reasonToVoteRec = {v.reasonId: v
                           for v in voteRecords} if voteRecords else {}
        logging.debug('GetProposalData() reasonToVoteRec=' +
                      str(reasonToVoteRec))

        for r in reasonDisps:
            voteRec = reasonToVoteRec.get(r['id'])
            r['myVote'] = voteRec.voteUp if voteRec else False

        # Store proposal to user's recent (cookie).
        user.storeRecentLinkKey(linkKeyStr, cookieData)

        # Display proposal data.
        responseData = {
            'success': True,
            'linkKey': linkKeyDisplay,
            'proposal': proposalDisp,
            'reasons': reasonDisps,
        }
        logging.debug(
            'GetProposalData() responseData=' +
            json.dumps(responseData, indent=4, separators=(', ', ':')))
        httpServer.outputJson(cookieData, responseData, self.response)
Exemplo n.º 5
0
    def get(self, linkKeyStr):
        logging.debug('linkKeyStr=' + linkKeyStr)

        # Collect inputs
        cursor = self.request.get('cursor', None)
        cursor = Cursor(urlsafe=cursor) if cursor else None
        getReasons = (self.request.get('getReasons', 'true') == 'true')
        logging.debug('getReasons=' + str(getReasons))

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

        cookieData = httpServer.validate(self.request,
                                         self.request.GET,
                                         responseData,
                                         self.response,
                                         idRequired=False)
        userId = cookieData.id()

        # Retrieve requestId from linkKey.  destinationType must be RequestForProposals.
        linkKeyRecord = linkKey.LinkKey.get_by_id(linkKeyStr)
        logging.debug('GetRequestData.get() linkKeyRecord=' +
                      str(linkKeyRecord))

        if (linkKeyRecord == None) or (linkKeyRecord.destinationType !=
                                       conf.REQUEST_CLASS_NAME):
            return httpServer.outputJson(cookieData,
                                         responseData,
                                         self.response,
                                         errorMessage=conf.BAD_LINK)

        # Retrieve RequestForProposal by id, filter/transform fields for display.
        requestId = linkKeyRecord.destinationId
        requestRecordFuture = requestForProposals.RequestForProposals.get_by_id_async(
            int(requestId))

        requestRecord = requestRecordFuture.get_result(
        ) if requestRecordFuture else None

        logging.debug('GetRequestData.get() userId=' + str(userId) +
                      ' requestRecord.creator=' + str(requestRecord.creator))

        # If userId exists... async-retrieve user's ReasonVotes by KeyProperty requestId x userId.
        voteRecordsFuture = None
        if getReasons and userId:
            voteRecordsFuture = reasonVote.ReasonVote.query(
                reasonVote.ReasonVote.requestId == requestId,
                reasonVote.ReasonVote.userId == userId).fetch_async()

        # Retrieve Proposals by KeyProperty requestId
        # Get all data up to current page maximum length.  + Refreshes earlier proposal data.
        maxProposals = const.INITIAL_MAX_PROPOSALS
        proposalRecords, cursor, hasMore = proposal.retrieveTopProposals(
            requestId, maxProposals, cursor=cursor)
        cursor = cursor.urlsafe() if cursor else None

        # Async-retrieve top N reasons per proposal, equal number of pro/con reasons
        reasonRecordsFutures = []
        if getReasons:
            for proposalRec in proposalRecords:
                maxReasonsPerType = conf.MAX_TOP_REASONS / 2
                proRecordsFuture, conRecordsFuture = reason.retrieveTopReasonsAsync(
                    proposalRec.key.id(), maxReasonsPerType)
                reasonRecordsFutures.append(proRecordsFuture)
                reasonRecordsFutures.append(conRecordsFuture)

        # Wait for parallel retrievals
        logging.debug('GetRequestData.get() requestRecord=' +
                      str(requestRecord))

        reasonRecords = []
        for reasonRecordsFuture in reasonRecordsFutures:
            reasonRecordsForProp, cursor, hasMore = reasonRecordsFuture.get_result(
            )
            logging.debug('GetRequestData.get() reasonRecordsForProp=' +
                          str(reasonRecordsForProp))
            reasonRecords.extend(reasonRecordsForProp)
        reasonRecords = sorted(reasonRecords, key=lambda r: -r.score)
        logging.debug('GetRequestData.get() reasonRecords=' +
                      str(reasonRecords))

        voteRecords = voteRecordsFuture.get_result(
        ) if voteRecordsFuture else []
        logging.debug('GetRequestData.get() voteRecords=' + str(voteRecords))

        # Transform records for display.
        linkKeyDisp = httpServer.linkKeyToDisplay(linkKeyRecord)
        logging.debug('GetRequestData.get() linkKeyDisp=' + str(linkKeyDisp))

        requestDisp = httpServer.requestToDisplay(requestRecord, userId)
        logging.debug('GetRequestData.get() requestDisp=' + str(requestDisp))

        proposalDisps = [
            httpServer.proposalToDisplay(p,
                                         userId,
                                         requestRecord=requestRecord)
            for p in proposalRecords
        ]
        logging.debug('GetRequestData.get() proposalDisps=' +
                      str(proposalDisps))

        reasonDisps = [
            httpServer.reasonToDisplay(r, userId) for r in reasonRecords
        ]
        logging.debug('GetRequestData.get() reasonDisps=' + str(reasonDisps))

        # For each reason... collect user vote in reason.myVote
        reasonToVoteRec = {v.reasonId: v
                           for v in voteRecords} if voteRecords else {}
        logging.debug('GetRequestData.get() reasonToVoteRec=' +
                      str(reasonToVoteRec))

        for r in reasonDisps:
            voteRec = reasonToVoteRec.get(r['id'])
            r['myVote'] = voteRec.voteUp if voteRec else False

        # Store request to user's recent requests (cookie).
        user.storeRecentLinkKey(linkKeyStr, cookieData)

        # Display request data.
        responseData = {
            'success': True,
            'linkKey': linkKeyDisp,
            'request': requestDisp,
            'proposals': proposalDisps,
            'reasons': reasonDisps,
            'maxProposals': maxProposals,
            'cursor': cursor,
        }
        logging.debug(
            'GetRequestData.get() responseData=' +
            json.dumps(responseData, indent=4, separators=(', ', ':')))
        httpServer.outputJson(cookieData, responseData, self.response)
Exemplo n.º 6
0
    def get(self, linkKeyStr, proposalId):

        if conf.isDev:
            logging.debug('TopReasons.get() linkKeyStr=' + str(linkKeyStr) +
                          ' proposalId=' + str(proposalId))

        # Collect inputs
        preview = self.request.get('preview', None) is not None
        cursorPro = self.request.get('cursorPro', None)
        cursorPro = Cursor(urlsafe=cursorPro) if cursorPro else None
        cursorCon = self.request.get('cursorCon', None)
        cursorCon = Cursor(urlsafe=cursorCon) if cursorCon else None
        httpRequestId = os.environ.get(conf.REQUEST_LOG_ID)
        responseData = {'success': False, 'httpRequestId': httpRequestId}
        cookieData = httpServer.validate(self.request,
                                         self.request.GET,
                                         responseData,
                                         self.response,
                                         idRequired=False)
        userId = cookieData.id()

        # Retrieve top-level records
        (linkKeyRecord, proposalRecord,
         requestRecord) = retrieveRequestAndProposal(linkKeyStr, proposalId)
        if not linkKeyRecord: return  # Bad link
        proposalId = str(proposalRecord.key.id())

        # Retrieve reasons and vote, in parallel
        voteRecordsFuture = reasonVote.ReasonVote.query(
            reasonVote.ReasonVote.proposalId == proposalId,
            reasonVote.ReasonVote.userId
            == userId).fetch_async() if userId else None
        maxReasonsPerType = (conf.MAX_TOP_REASONS / 2) if preview else 10
        proRecordsFuture, conRecordsFuture = reason.retrieveTopReasonsAsync(
            proposalId,
            maxReasonsPerType,
            cursorPro=cursorPro,
            cursorCon=cursorCon)
        proRecords, cursorPro, morePros = proRecordsFuture.get_result()
        conRecords, cursorCon, moreCons = conRecordsFuture.get_result()
        cursorPro = cursorPro.urlsafe() if cursorPro else None
        cursorCon = cursorCon.urlsafe() if cursorCon else None
        voteRecords = voteRecordsFuture.get_result(
        ) if voteRecordsFuture else None

        # Filter/transform fields for display
        linkKeyDisplay = httpServer.linkKeyToDisplay(linkKeyRecord)
        proposalDisp = httpServer.proposalToDisplay(
            proposalRecord, userId, requestRecord=requestRecord)
        reasonDisps = [
            httpServer.reasonToDisplay(r, userId)
            for r in (proRecords + conRecords)
        ]
        mergeReasonVotes(voteRecords, reasonDisps)

        # Store request/proposal to user's recent (cookie)
        user.storeRecentLinkKey(linkKeyStr, cookieData)

        # Display proposal data
        responseData = {
            'success': True,
            'linkKey': linkKeyDisplay,
            'proposal': proposalDisp,
            'reasons': reasonDisps,
            'cursorPro': cursorPro,
            'cursorCon': cursorCon
        }
        httpServer.outputJson(cookieData, responseData, self.response)