def delete(self, day_id): """ Update a day by ID """ app.logger.info("Deleting day entry with id: {}".format(day_id)) day = GlobalTime.find_by_id_or_404(day_id) day.delete() return make_response("", status.HTTP_204_NO_CONTENT)
def put(self, day_id): """ Update a day by ID """ app.logger.info("Updating day entry with id: {}".format(day_id)) day = GlobalTime.find_by_id_or_404(day_id) day.deserialize(request.get_json()) day.id = day_id day.save() return make_response(jsonify(day.serialize(), status.HTTP_200_OK))
def get(self, day_id): app.logger.info("Request for day with id: {}".format(day_id)) day = GlobalTime.find_by_id_or_404(day_id) if not day: raise NotFound( "Day with id: '{}' was not found in the database.".format( day_id)) return make_response(jsonify(day.serialize()), status.HTTP_200_OK)
def add_test_day(country_name, province, date, confirmed, deaths, recovered): day = GlobalTime(country_name=country_name, province=province, date=date, confirmed=confirmed, deaths=deaths, recovered=recovered) # add user to test db db.session.add(day) db.session.commit() return day
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
def get(self): country_name = request.args.get('country_name') date_input = request.args.get('date') app.logger.info( "Request for day with country_name: '{}' and date: '{}'".format( country_name, date_input)) days = GlobalTime.find_by_date_and_country_name( country_name, date_input) # due to all() filter, will return list of dicts if not days: raise NotFound( "Day with country_name: '{}' and date: '{}' was not found in the database" .format(country_name, date_input)) result = [day.serialize() for day in days] return make_response(jsonify(result), status.HTTP_200_OK)
def init_db(): """ Initialize SQLAlchemy app """ global app GlobalTime.init_db(app)