Skip to content

Commit 2150c21

Browse files
committed
Added command to import all starships
1 parent 998d50a commit 2150c21

6 files changed

Lines changed: 68 additions & 0 deletions

File tree

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ psycopg2>=2.6.2,<2.7
44
djangorestframework
55
django-filter
66
django-extensions
7+
requests

shiptrader/management/__init__.py

Whitespace-only changes.

shiptrader/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import requests
2+
from django.core.management.base import BaseCommand, CommandError
3+
4+
from shiptrader.models import Starship
5+
6+
7+
class Command(BaseCommand):
8+
help = 'Retrieves starship data from the swapi.co api '
9+
10+
def handle(self, *args, **options):
11+
12+
url = 'https://swapi.co/api/starships/'
13+
starships = []
14+
15+
while url is not None:
16+
response = requests.get(url)
17+
response_json = response.json()
18+
url = response_json['next']
19+
starships += [build_starship(result) for result in response_json['results']]
20+
21+
Starship.objects.bulk_create(starships)
22+
23+
24+
def build_starship(data):
25+
return Starship(name=data['name'],
26+
model=data['model'],
27+
starship_class=data['starship_class'],
28+
manufacturer=data['manufacturer'],
29+
length=float(convert_unknown(data['length'])),
30+
hyperdrive_rating=float(convert_unknown(data['hyperdrive_rating'])),
31+
cargo_capacity=int(convert_unknown(data['cargo_capacity'])),
32+
crew=int(convert_unknown(data['crew'])),
33+
passengers=int(convert_unknown(data['passengers'])))
34+
35+
36+
def convert_unknown(value):
37+
return value.replace(',', '.') if value != 'unknown' else 0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.core.management import call_command
2+
from django.test import TestCase
3+
from rest_framework.reverse import reverse
4+
5+
from shiptrader.models import Starship
6+
7+
8+
class TestImportStarships(TestCase):
9+
10+
def test_can_import_starships(self):
11+
12+
call_command('import_starships')
13+
starships = Starship.objects.all()
14+
self.assertGreater(len(starships), 0)
15+
16+
executor = next(filter(lambda s: s.name == 'Executor', starships))
17+
18+
self.assertEqual(executor.name, 'Executor')
19+
self.assertEqual(executor.model, 'Executor-class star dreadnought')
20+
self.assertEqual(executor.starship_class, 'Star dreadnought')
21+
self.assertEqual(executor.manufacturer, 'Kuat Drive Yards, Fondor Shipyards')
22+
23+
self.assertEqual(executor.length, 19000.0)
24+
self.assertEqual(executor.hyperdrive_rating, 2.0)
25+
self.assertEqual(executor.cargo_capacity, 250000000)
26+
27+
self.assertEqual(executor.crew, 279144)
28+
self.assertEqual(executor.passengers, 38000)

shiptrader/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ class Meta:
1010

1111

1212
class StarshipViewSet(viewsets.ReadOnlyModelViewSet):
13+
"""View that allows all starships to be listed"""
14+
1315
queryset = Starship.objects.all()
1416
serializer_class = StarshipSerializer

0 commit comments

Comments
 (0)