diff --git a/.gitignore b/.gitignore index 0d20b64..97aafd0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *.pyc +api-challenge/ +.idea diff --git a/README.md b/README updated.md similarity index 100% rename from README.md rename to README updated.md diff --git a/db.sqlite3 b/db.sqlite3 index d3ec30e..342269f 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/departures/filtered_departures.csv b/departures/filtered_departures.csv new file mode 100644 index 0000000..6c3c918 --- /dev/null +++ b/departures/filtered_departures.csv @@ -0,0 +1,25 @@ +name,start_date,finish_date,category +New Zealand Encompassed,2018-08-31,2018-09-10,Adventurous +Cambodia Overland,2018-08-31,2018-09-10,Adventurous +Peru Overland,2018-08-31,2018-09-10,Adventurous +Vietnam Safari,2018-08-31,2018-09-10,Adventurous +Brazil Trek,2018-08-31,2018-09-10,Adventurous +Cambodia Multisport,2018-08-31,2018-09-10,Adventurous +Morocco Safari,2018-08-31,2018-09-10,Adventurous +Galapagos Encompassed,2018-08-31,2018-09-10,Adventurous +Galapagos Discovery,2018-08-31,2018-09-10,Adventurous +New Zealand Adventure,2018-08-31,2018-09-10,Adventurous +Galapagos Trek,2018-08-31,2018-09-10,Adventurous +Brazil Encompassed,2018-08-31,2018-09-10,Adventurous +Kenya Express,2018-08-31,2018-09-10,Adventurous +Galapagos Discovery,2018-08-31,2018-09-10,Adventurous +Sri Lanka Adventure,2018-08-31,2018-09-10,Adventurous +Brazil Adventure,2018-08-31,2018-09-10,Adventurous +Brazil Overland,2018-08-31,2018-09-10,Adventurous +Kenya Encompassed,2018-08-31,2018-09-10,Adventurous +Morocco Discovery,2018-08-31,2018-09-10,Adventurous +Vietnam Trek,2018-08-31,2018-09-10,Adventurous +New Zealand Safari,2018-08-31,2018-09-10,Adventurous +Vietnam Encompassed,2018-08-31,2018-09-10,Adventurous +Ethopia Express,2018-08-31,2018-09-10,Adventurous +Sri Lanka Safari,2018-08-31,2018-09-10,Adventurous diff --git a/departures/migrations/0001_initial.py b/departures/migrations/0001_initial.py index 68f7fc4..75c3059 100644 --- a/departures/migrations/0001_initial.py +++ b/departures/migrations/0001_initial.py @@ -22,3 +22,5 @@ class Migration(migrations.Migration): ], ), ] + + diff --git a/departures/migrations/0002_auto_20200103_1902.py b/departures/migrations/0002_auto_20200103_1902.py new file mode 100644 index 0000000..da4b8d9 --- /dev/null +++ b/departures/migrations/0002_auto_20200103_1902.py @@ -0,0 +1,27 @@ +# Generated by Django 2.1.4 on 2020-01-03 19:02 + +from django.db import migrations +import json + + +def getDepartures(apps, schema_editor): + with open("departures.json") as json_file: + data = json.load(json_file) + Departure = apps.get_model("departures", "Departure") + db_alias = schema_editor.connection.alias + for departure in data: + Departure.objects.using(db_alias).create(name=departure["name"], start_date=departure["start_date"], finish_date=departure["finish_date"], category=departure["category"]) + + +class Migration(migrations.Migration): + + dependencies = [ + ('departures', '0001_initial'), + ] + + operations = [ + migrations.RunPython(getDepartures) + ] + + + diff --git a/departures/solution.py b/departures/solution.py new file mode 100644 index 0000000..9ed6c77 --- /dev/null +++ b/departures/solution.py @@ -0,0 +1,64 @@ +import requests +from datetime import datetime +import csv + +url = "http://localhost:8000/departures" +headers = {"Accept": "application/json"} + + +### First, we need to collect all departures referenced from the API: + +def getDepartures(departures_list, url, headers): + response = requests.get(url, headers) + ### List of HTTP status codes: https://www.restapitutorial.com/httpstatuscodes.html + if response.status_code == 200: + data = response.json() + for outcome in data["results"]: + departures_list.append(outcome) + + next_url = data["next"] + if next_url is None: + return None + else: + getDepartures(departures_list, next_url, headers) + + else: + data = response.status_code + format_string = "The following error occurred: %s" + print(format_string % data) + + return departures_list + + +### Next, we need to filter down the data to only include departures +### with a start_date after June 1st, 2018, and category = "adventurous": + +def filterDepartures(departures, date, category): + filtered_departures_list = [] + for departure in departures: + ### https://www.programiz.com/python-programming/datetime/strptime + start_date = datetime.strptime(departure["start_date"], "%Y-%m-%d") + style_category = departure["category"] + if start_date > date and style_category == category: + filtered_departures_list.append(departure) + return filtered_departures_list + + +### Finally, we need to create a CSV: + +def createCsv(departures): + + with open("filtered_departures.csv", mode="w") as file: + fieldnames = ["name", "start_date", "finish_date", "category"] + writer = csv.DictWriter(file, fieldnames=fieldnames) + + writer.writeheader() + for departure in departures: + writer.writerow(departure) + + +### https://stackoverflow.com/questions/419163/what-does-if-name-main-do +if __name__ == '__main__': + departures = getDepartures([], url, headers) + filtered_departures = filterDepartures(departures, datetime(2018, 6, 1), "Adventurous") + createCsv(filtered_departures)