def answer_question(self, answer, _ix=None):
        ix = self.parse_ix(_ix)
        event = self.cur_event if ix is None else self.__parse_event(ix)

        if event['type'] != 'question':
            raise ValueError('not currently on a question')

        datatype = event['datatype']
        if datatype == 'unrecognized':
            # don't commit answers to unrecognized questions, since we
            # couldn't parse what was originally there. whereas for
            # _unsupported_ questions, we're parsing and re-committing the
            # answer verbatim
            return {'status': 'success'}

        def multians(a):
            if hasattr(a, '__iter__'):
                return a
            else:
                return str(a).split()

        if answer == None or str(
                answer).strip() == '' or answer == [] or datatype == 'info':
            ans = None
        elif datatype == 'int':
            ans = IntegerData(int(answer))
        elif datatype == 'longint':
            ans = LongData(int(answer))
        elif datatype == 'float':
            ans = DecimalData(float(answer))
        elif datatype == 'str':
            ans = StringData(str(answer))
        elif datatype == 'date':
            ans = DateData(
                to_jdate(datetime.strptime(str(answer), '%Y-%m-%d').date()))
        elif datatype == 'time':
            ans = TimeData(
                to_jtime(datetime.strptime(str(answer), '%H:%M').time()))
        elif datatype == 'select':
            ans = SelectOneData(event['choices'][int(answer) - 1].to_sel())
        elif datatype == 'multiselect':
            ans = SelectMultiData(
                to_vect(event['choices'][int(k) - 1].to_sel()
                        for k in multians(answer)))
        elif datatype == 'geo':
            ans = GeoPointData(
                to_arr((float(x) for x in multians(answer)), 'd'))

        result = self.fec.answerQuestion(*([ans] if ix is None else [ix, ans]))
        if result == self.fec.ANSWER_REQUIRED_BUT_EMPTY:
            return {'status': 'error', 'type': 'required'}
        elif result == self.fec.ANSWER_CONSTRAINT_VIOLATED:
            q = self.fem.getQuestionPrompt(*([] if ix is None else [ix]))
            return {
                'status': 'error',
                'type': 'constraint',
                'reason': q.getConstraintText()
            }
        elif result == self.fec.ANSWER_OK:
            return {'status': 'success'}
Exemple #2
0
def case_from_json(data):
    c = Case()
    c.setCaseId(data['case_id'])
    c.setTypeId(data['properties']['case_type'])
    c.setName(data['properties']['case_name'])
    c.setClosed(data['closed'])
    if data['properties']['date_opened']:
        c.setDateOpened(to_jdate(
            datetime.strptime(
                data['properties']['date_opened'],
                '%Y-%m-%dT%H:%M:%S')))  # 'Z' in fmt string omitted due to jython bug
    owner_id = data['properties']['owner_id'] or data['user_id'] or ""
    c.setUserId(owner_id)  # according to clayton "there is no user_id, only owner_id"

    for k, v in data['properties'].iteritems():
        if v is not None and k not in ['case_name', 'case_type', 'date_opened']:
            c.setProperty(k, v)

    for k, v in data['indices'].iteritems():
        c.setIndex(k, v['case_type'], v['case_id'])

    for k, v in data['attachments'].iteritems():
        c.updateAttachment(k, v['url'])

    return c
Exemple #3
0
def case_from_json(data):
    c = Case()
    c.setCaseId(data['case_id'])
    c.setTypeId(data['properties']['case_type'])
    c.setName(data['properties']['case_name'])
    c.setClosed(data['closed'])
    if data['properties']['date_opened']:
        c.setDateOpened(
            to_jdate(
                datetime.strptime(data['properties']['date_opened'],
                                  '%Y-%m-%dT%H:%M:%S'))
        )  # 'Z' in fmt string omitted due to jython bug
    owner_id = data['properties']['owner_id'] or data['user_id'] or ""
    c.setUserId(
        owner_id)  # according to clayton "there is no user_id, only owner_id"

    for k, v in data['properties'].iteritems():
        if v is not None and k not in [
                'case_name', 'case_type', 'date_opened'
        ]:
            c.setProperty(k, v)

    for k, v in data['indices'].iteritems():
        c.setIndex(k, v['case_type'], v['case_id'])

    for k, v in data['attachments'].iteritems():
        c.updateAttachment(k, v['url'])

    return c
Exemple #4
0
    def answer_question(self, answer, _ix=None):
        ix = self.parse_ix(_ix)
        event = self.cur_event if ix is None else self.__parse_event(ix)

        if event["type"] != "question":
            raise ValueError("not currently on a question")

        datatype = event["datatype"]
        if datatype == "unrecognized":
            # don't commit answers to unrecognized questions, since we
            # couldn't parse what was originally there. whereas for
            # _unsupported_ questions, we're parsing and re-committing the
            # answer verbatim
            return {"status": "success"}

        def multians(a):
            if hasattr(a, "__iter__"):
                return a
            else:
                return str(a).split()

        if answer == None or str(answer).strip() == "" or answer == []:
            ans = None
        elif datatype == "int":
            if isinstance(int(answer), long):
                ans = LongData(int(answer))
            else:
                ans = IntegerData(int(answer))
        elif datatype == "longint":
            ans = LongData(int(answer))
        elif datatype == "float":
            ans = DecimalData(float(answer))
        elif datatype == "str" or datatype == "info" or datatype == "barcode":
            ans = StringData(str(answer))
        elif datatype == "date":
            ans = DateData(to_jdate(datetime.strptime(str(answer), "%Y-%m-%d").date()))
        elif datatype == "time":
            ans = TimeData(to_jtime(datetime.strptime(str(answer), "%H:%M").time()))
        elif datatype == "select":
            ans = SelectOneData(event["choices"][int(answer) - 1].to_sel())
        elif datatype == "multiselect":
            ans = SelectMultiData(to_vect(event["choices"][int(k) - 1].to_sel() for k in multians(answer)))
        elif datatype == "geo":
            ans = GeoPointData(to_arr((float(x) for x in multians(answer)), "d"))

        result = self.fec.answerQuestion(*([ans] if ix is None else [ix, ans]))
        if result == self.fec.ANSWER_REQUIRED_BUT_EMPTY:
            return {"status": "error", "type": "required"}
        elif result == self.fec.ANSWER_CONSTRAINT_VIOLATED:
            q = self.fem.getQuestionPrompt(*([] if ix is None else [ix]))
            return {"status": "error", "type": "constraint", "reason": q.getConstraintText()}
        elif result == self.fec.ANSWER_OK:
            return {"status": "success"}
Exemple #5
0
    def answer_question (self, answer, _ix=None):
        ix = self.parse_ix(_ix)
        event = self.cur_event if ix is None else self.__parse_event(ix)

        if event['type'] != 'question':
            raise ValueError('not currently on a question')

        datatype = event['datatype']
        if datatype == 'unrecognized':
            # don't commit answers to unrecognized questions, since we
            # couldn't parse what was originally there. whereas for
            # _unsupported_ questions, we're parsing and re-committing the
            # answer verbatim
            return {'status': 'success'}

        def multians(a):
            if hasattr(a, '__iter__'):
                return a
            else:
                return str(a).split()

        if answer == None or str(answer).strip() == '' or answer == []:
            ans = None
        elif datatype == 'int':
            if isinstance(int(answer), long):
                ans = LongData(int(answer))
            else:
                ans = IntegerData(int(answer))
        elif datatype == 'longint':
            ans = LongData(int(answer))
        elif datatype == 'float':
            ans = DecimalData(float(answer))
        elif datatype == 'str' or datatype == 'info':
            ans = StringData(str(answer))
        elif datatype == 'date':
            ans = DateData(to_jdate(datetime.strptime(str(answer), '%Y-%m-%d').date()))
        elif datatype == 'time':
            ans = TimeData(to_jtime(datetime.strptime(str(answer), '%H:%M').time()))
        elif datatype == 'select':
            ans = SelectOneData(event['choices'][int(answer) - 1].to_sel())
        elif datatype == 'multiselect':
            ans = SelectMultiData(to_vect(event['choices'][int(k) - 1].to_sel() for k in multians(answer)))
        elif datatype == 'geo':
            ans = GeoPointData(to_arr((float(x) for x in multians(answer)), 'd'))

        result = self.fec.answerQuestion(*([ans] if ix is None else [ix, ans]))
        if result == self.fec.ANSWER_REQUIRED_BUT_EMPTY:
            return {'status': 'error', 'type': 'required'}
        elif result == self.fec.ANSWER_CONSTRAINT_VIOLATED:
            q = self.fem.getQuestionPrompt(*([] if ix is None else [ix]))
            return {'status': 'error', 'type': 'constraint', 'reason': q.getConstraintText()}
        elif result == self.fec.ANSWER_OK:
            return {'status': 'success'}
Exemple #6
0
def case_from_json(data):
    c = Case()
    c.setCaseId(data['case_id'])
    c.setTypeId(data['properties']['case_type'])
    c.setName(data['properties']['case_name'])
    c.setClosed(data['closed'])
    if data['properties']['date_opened']:
        c.setDateOpened(to_jdate(datetime.strptime(data['properties']['date_opened'], '%Y-%m-%dT%H:%M:%S'))) # 'Z' in fmt string omitted due to jython bug
    c.setUserId(data['user_id'] or "")

    for k, v in data['properties'].iteritems():
        if v is not None and k not in ['case_name', 'case_type', 'date_opened']:
            c.setProperty(k, v)

    for k, v in data['indices'].iteritems():
        c.setIndex(k, v['case_type'], v['case_id'])

    return c
Exemple #7
0
def case_from_json(data):
    c = Case()
    c.setCaseId(data['case_id'])
    c.setTypeId(data['properties']['case_type'])
    c.setName(data['properties']['case_name'])
    c.setClosed(data['closed'])
    if data['properties']['date_opened']:
        c.setDateOpened(
            to_jdate(
                datetime.strptime(data['properties']['date_opened'],
                                  '%Y-%m-%dT%H:%M:%S'))
        )  # 'Z' in fmt string omitted due to jython bug
    c.setUserId(data['user_id'] or "")

    for k, v in data['properties'].iteritems():
        if v is not None and k not in [
                'case_name', 'case_type', 'date_opened'
        ]:
            c.setProperty(k, v)

    for k, v in data['indices'].iteritems():
        c.setIndex(k, v['case_type'], v['case_id'])

    return c