Ejemplo n.º 1
0
    def deserialize(self, data):
        """
        Deserializes GlobalTime 'day' from dictionary to object
        
        Args:
            data (dict): dictionary containing 'day' data
        """
        dc = DateConverter()

        try:
            self.country_name = data['country_name']
            self.province = data['province']
            self.date = dc.to_python(value=data['date'])
            self.confirmed = data['confirmed']
            self.deaths = data['deaths']
            self.recovered = data['recovered']

        except KeyError as error:
            raise DataValidationError("KeyError: Missing field '" +
                                      error.args[0] + "'")
        except TypeError as error:
            raise DataValidationError(
                "TypeError: Body of request contained bad data")

        return self
Ejemplo n.º 2
0
 def find_by_date_range(cls, start_date, end_date):
     """ Returns all days that fall within a date range by start date and end date """
     dc = DateConverter()
     logger.info("Processing lookup for days within range {} and {}".format(
         start_date, end_date))
     return cls.query.filter(
         cls.date.between(dc.to_python(value=start_date),
                          dc.to_python(value=end_date)))
Ejemplo n.º 3
0
    def find_by_date_and_country_name(cls, country_name, date):
        """ Returns one day that match given country name and date """
        dc = DateConverter()
        logger.info(
            "Processing lookup for days with country_name: '{}' and date: '{}'"
            .format(country_name, date))

        date_converted = dc.to_python(date)
        return cls.query.filter(cls.country_name == country_name,
                                cls.date == date_converted).all()
Ejemplo n.º 4
0
def parse_value(column, value):
    dc = DateConverter()
    """ Parses the value into desired type based on column """
    if column == 'confirmed' or column == 'deaths' or column == 'recovered':
        return int(value)
    elif column == 'date':
        # parse datetime
        return dc.to_python(value)
    else:
        return value
Ejemplo n.º 5
0
def test_date_converter_to_python_correct_str_to_datetime():

    dc = DateConverter()
    test_input = "1994-05-10"
    expected_result = datetime(1994, 5, 10)

    result = dc.to_python(test_input)

    assert result == expected_result
    assert type(result) == type(expected_result)
Ejemplo n.º 6
0
def test_date_converter_to_url_correct_datetime_to_str():

    dc = DateConverter()
    test_input = datetime(1994, 5, 10)
    expected_result = "1994-05-10"

    result = dc.to_url(test_input)

    assert result == expected_result
    assert type(result) == type(expected_result)
Ejemplo n.º 7
0
 def serialize(self):
     """
     Serializes GlobalTime 'day' from object into a dictionary 
     """
     dc = DateConverter()
     return {
         "id": self.id,
         "country_name": self.country_name,
         "province": self.province,
         "date": dc.to_url(self.date),
         "confirmed": self.confirmed,
         "deaths": self.deaths,
         "recovered": self.recovered
     }
Ejemplo n.º 8
0
    def post(self):
        """
        Creates a day in the db 
        """
        check_content_type("application/json")

        # create Day instance
        day = GlobalTime()

        # check if any missing keys or invalid type
        try:
            day.deserialize(request.get_json())

        except DataValidationError as error:
            abort(400, str(error))

        dc = DateConverter()

        check_country_name = day.country_name
        check_date = dc.to_url(day.date)  # convert back to str

        res = GlobalTime.find_by_date_and_country_name(check_country_name,
                                                       check_date)

        if res:
            abort(400, "Duplicate date for given country and date")

        app.logger.info("Creating day in db")
        day.create()

        # transform object into json
        day_json = day.serialize()

        # return of single day by searching for id
        location_url = url_for(
            "get_day_by_id", day_id=day.id,
            _external=True)  # _external - generates absolute

        response = make_response(jsonify(day_json), status.HTTP_201_CREATED)

        # Attach custom headers to response.headers
        response.headers['Location'] = location_url
        response.headers['Message'] = "day created"

        return response