def test_datetime_ms_with_timezone(self):
        """
        Tests datetime with no colon

        """
        return_value = Survey.parse_timestamp("2017-01-11T17:18:53.020222+0000")
        self.assertIsInstance(return_value, datetime.datetime)
Пример #2
0
    def test_datetime_zulu(self):
        """
        Tests zulu datetime

        """
        return_value = Survey.parse_timestamp("2017-01-11T17:18:53Z")
        self.assertIsInstance(return_value, datetime.datetime)
    def test_datetime_zulu(self):
        """
        Tests zulu datetime

        """
        return_value = Survey.parse_timestamp("2017-01-11T17:18:53Z")
        self.assertIsInstance(return_value, datetime.datetime)
Пример #4
0
    def test_datetime_ms_with_timezone(self):
        """
        Tests datetime with no colon

        """
        return_value = Survey.parse_timestamp("2017-01-11T17:18:53.020222+0000")
        self.assertIsInstance(return_value, datetime.datetime)
Пример #5
0
    def test_date_iso(self):
        """
        Tests iso datetime format

        """
        return_value = Survey.parse_timestamp("2017-01-11")
        self.assertNotIsInstance(return_value, datetime.datetime)
        self.assertIsInstance(return_value, datetime.date)
Пример #6
0
    def test_date_diary(self):
        """
        Tests date date diary datetime format

        """
        return_value = Survey.parse_timestamp("11/07/2017")
        self.assertNotIsInstance(return_value, datetime.datetime)
        self.assertIsInstance(return_value, datetime.date)
    def test_date_iso(self):
        """
        Tests iso datetime format

        """
        return_value = Survey.parse_timestamp("2017-01-11")
        self.assertNotIsInstance(return_value, datetime.datetime)
        self.assertIsInstance(return_value, datetime.date)
    def test_date_diary(self):
        """
        Tests date date diary datetime format

        """
        return_value = Survey.parse_timestamp("11/07/2017")
        self.assertNotIsInstance(return_value, datetime.datetime)
        self.assertIsInstance(return_value, datetime.date)
Пример #9
0
    def events(qid, data, default, *args, group=[], **kwargs):
        """Return a sequence of time events from a question group.

        :param str qid: The question id.
        :param data: The full survey data.
        :type data: dict(str, str)
        :param default: The default value for the question.
        :param group: A sequence of question ids.

        """
        try:
            group_vals = [data.get(qid, None)] + [data.get(q, None) for q in group]
            data = sorted(filter(
                None, (Survey.parse_timestamp(i) for i in group_vals if i is not None)
            ))
            if all(isinstance(i, type(default)) for i in data):
                return data
            else:
                return type(default)(data)
        except (AttributeError, TypeError, ValueError):
            return default
    def events(qid, data, default, *args, group=[], **kwargs):
        """Return a sequence of time events from a question group.

        :param str qid: The question id.
        :param data: The full survey data.
        :type data: dict(str, str)
        :param default: The default value for the question.
        :param group: A sequence of question ids.

        """
        try:
            group_vals = [data.get(qid, None)
                          ] + [data.get(q, None) for q in group]
            data = sorted(
                filter(None, (Survey.parse_timestamp(i)
                              for i in group_vals if i is not None)))
            if all(isinstance(i, type(default)) for i in data):
                return data
            else:
                return type(default)(data)
        except (AttributeError, TypeError, ValueError):
            return default
Пример #11
0
    def get_identifiers(self, batch_nr=0, seq_nr=0):
        """Parse common metadata from the survey.

        Return a named tuple which code can use to access the various ids and discriminators.

        :param dict data: A survey reply.
        :param int batch_nr: A batch number for the reply.
        :param int seq_nr: An image sequence number for the reply.

        """

        logger.info("Parsing data from submission")

        ru_ref = self.response.get("metadata", {}).get("ru_ref", "")
        ts = datetime.datetime.now(datetime.timezone.utc)
        ids = {
            "batch_nr": batch_nr,
            "seq_nr": seq_nr,
            "ts": ts,
            "tx_id": self.response.get("tx_id"),
            "survey_id": self.response.get("survey_id"),
            "instrument_id": self.response.get("collection", {}).get("instrument_id"),
            "submitted_at": Survey.parse_timestamp(
                self.response.get("submitted_at", ts.isoformat())
            ),
            "user_id": self.response.get("metadata", {}).get("user_id"),
            "ru_ref": "".join(i for i in ru_ref if i.isdigit()),
            "ru_check": ru_ref[-1] if ru_ref and ru_ref[-1].isalpha() else "",
            "period": self.response.get("collection", {}).get("period"),
        }

        for key, value in ids.items():
            if value is None:
                logger.error("Missing value for: {0}".format(key))
                return None

        return ids
Пример #12
0
    def get_identifiers(self, batch_nr=0, seq_nr=0):
        """Parse common metadata from the survey.

        Return a named tuple which code can use to access the various ids and discriminators.

        :param int batch_nr: A batch number for the reply.
        :param int seq_nr: An image sequence number for the reply.

        """

        logger.info("Parsing data from submission")

        ru_ref = self.response.get("metadata", {}).get("ru_ref", "")
        ts = datetime.datetime.now(datetime.timezone.utc)
        ids = {
            "batch_nr": batch_nr,
            "seq_nr": seq_nr,
            "ts": ts,
            "tx_id": self.response.get("tx_id"),
            "survey_id": self.response.get("survey_id"),
            "instrument_id": self.response.get("collection", {}).get("instrument_id"),
            "submitted_at": Survey.parse_timestamp(
                self.response.get("submitted_at", ts.isoformat())
            ),
            "user_id": self.response.get("metadata", {}).get("user_id"),
            "ru_ref": "".join(i for i in ru_ref if i.isdigit()),
            "ru_check": ru_ref[-1] if ru_ref and ru_ref[-1].isalpha() else "",
            "period": self.response.get("collection", {}).get("period"),
        }

        for key, value in ids.items():
            if value is None:
                logger.error(f"Missing value for: {key}")
                return None

        return ids
Пример #13
0
 def test_date_diary(self):
     rv = Survey.parse_timestamp("11/07/2017")
     self.assertNotIsInstance(rv, datetime.datetime)
     self.assertIsInstance(rv, datetime.date)
 def test_date_diary(self):
     rv = Survey.parse_timestamp("11/07/2017")
     self.assertNotIsInstance(rv, datetime.datetime)
     self.assertIsInstance(rv, datetime.date)
 def test_date_iso(self):
     rv = Survey.parse_timestamp("2017-01-11")
     self.assertNotIsInstance(rv, datetime.datetime)
     self.assertIsInstance(rv, datetime.date)
 def test_datetime_ms_with_colon_in_timezone(self):
     rv = Survey.parse_timestamp("2017-01-11T17:18:53.020222+00:00")
     self.assertIsInstance(rv, datetime.datetime)
 def test_date_period(self):
     rv = Survey.parse_timestamp("201605")
     self.assertNotIsInstance(rv, datetime.datetime)
     self.assertIsInstance(rv, datetime.date)
Пример #18
0
 def test_datetime_zulu(self):
     rv = Survey.parse_timestamp("2017-01-11T17:18:53Z")
     self.assertIsInstance(rv, datetime.datetime)
Пример #19
0
 def test_date_iso(self):
     rv = Survey.parse_timestamp("2017-01-11")
     self.assertNotIsInstance(rv, datetime.datetime)
     self.assertIsInstance(rv, datetime.date)
 def test_datetime_zulu(self):
     rv = Survey.parse_timestamp("2017-01-11T17:18:53Z")
     self.assertIsInstance(rv, datetime.datetime)
Пример #21
0
 def test_datetime_ms_with_colon_in_timezone(self):
     rv = Survey.parse_timestamp("2017-01-11T17:18:53.020222+00:00")
     self.assertIsInstance(rv, datetime.datetime)
Пример #22
0
 def test_date_period(self):
     rv = Survey.parse_timestamp("201605")
     self.assertNotIsInstance(rv, datetime.datetime)
     self.assertIsInstance(rv, datetime.date)