Skip to content

Commit 09f5989

Browse files
Nisha ChaubeNisha Chaube
Nisha Chaube
authored and
Nisha Chaube
committed
📦 Add: World API starter code with City schema
1 parent 618a732 commit 09f5989

15 files changed

+201
-0
lines changed

‎worldapi/__init__.py

Whitespace-only changes.
183 Bytes
Binary file not shown.
224 Bytes
Binary file not shown.
1.3 KB
Binary file not shown.
2.71 KB
Binary file not shown.

‎worldapi/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

‎worldapi/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class WorldapiConfig(AppConfig):
5+
name = 'worldapi'
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Generated by Django 2.1.7 on 2020-10-06 00:23
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = [
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='City',
17+
fields=[
18+
('city_id', models.AutoField(primary_key=True, serialize=False)),
19+
('city_name', models.CharField(max_length=100)),
20+
('country_code', models.CharField(max_length=100)),
21+
('city_district', models.CharField(max_length=100)),
22+
('city_population', models.IntegerField()),
23+
],
24+
),
25+
migrations.CreateModel(
26+
name='Country',
27+
fields=[
28+
('code', models.CharField(max_length=3, primary_key=True, serialize=False)),
29+
('name', models.CharField(max_length=100)),
30+
('continent', models.CharField(max_length=100)),
31+
('region', models.CharField(max_length=100)),
32+
('indepyear', models.IntegerField()),
33+
('population', models.IntegerField()),
34+
('localname', models.CharField(max_length=100)),
35+
('governmentform', models.CharField(max_length=100)),
36+
('headofstate', models.TextField()),
37+
('capital', models.IntegerField()),
38+
('code2', models.CharField(max_length=2)),
39+
],
40+
),
41+
migrations.CreateModel(
42+
name='CountryLanguage',
43+
fields=[
44+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
45+
('country_language', models.CharField(max_length=50)),
46+
('country_code', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='worldapi.City')),
47+
],
48+
),
49+
]

‎worldapi/migrations/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.

‎worldapi/models.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from django.db import models
2+
from django.conf import settings
3+
4+
5+
class Country(models.Model):
6+
code = models.CharField(primary_key=True, max_length=3)
7+
name = models.CharField(max_length=100)
8+
continent = models.CharField(max_length=100)
9+
region = models.CharField(max_length=100)
10+
# surfacearea real
11+
indepyear = models.IntegerField()
12+
population = models.IntegerField()
13+
# lifeexpectancy real,
14+
# gnp numeric(10,2),
15+
# gnpold numeric(10,2),
16+
localname = models.CharField(max_length=100)
17+
governmentform = models.CharField(max_length=100)
18+
headofstate = models.TextField()
19+
capital = models.IntegerField()
20+
code2 = models.CharField(max_length=2)
21+
22+
23+
class City(models.Model):
24+
city_id = models.AutoField(primary_key=True)
25+
city_name = models.CharField(max_length=100)
26+
# country_code = models.ForeignKey(Country, on_delete=models.CASCADE)
27+
country_code = models.CharField(max_length=100)
28+
city_district = models.CharField(max_length=100)
29+
city_population = models.IntegerField()
30+
31+
class CountryLanguage(models.Model):
32+
country_code = models.ForeignKey(City, on_delete=models.CASCADE)
33+
country_language = models.CharField(max_length=50)
34+
35+
# is_official boolean NOT NULL,
36+
# percentage real NOT NULL
37+
38+

‎worldapi/schema.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import graphene
2+
from graphene_django import DjangoObjectType
3+
from django.db.models import Q
4+
from graphene import ObjectType
5+
from .models import City
6+
7+
class CityType(DjangoObjectType):
8+
class Meta:
9+
model = City
10+
11+
class Query(graphene.ObjectType):
12+
cities = graphene.List(
13+
CityType,
14+
city_id=graphene.Int(),
15+
first=graphene.Int(),
16+
jump=graphene.Int(),
17+
)
18+
19+
def resolve_cities(self, info, city_id, first=None, jump=None, **kwargs):
20+
all_cities = City.objects.all()
21+
if city_id:
22+
filter = Q(city_id__icontains=city_id)
23+
filtered = all_cities.filter(filter)
24+
25+
if jump:
26+
filtered = filtered[jump:]
27+
if first:
28+
filtered = filtered[:first]
29+
else:
30+
filtered = City.objects.all()
31+
32+
return filtered
33+
34+
class AddCity(graphene.Mutation):
35+
addCity = graphene.Field(CityType)
36+
37+
class Arguments:
38+
city_id = graphene.Int(required=True)
39+
city_name = graphene.String(required=True)
40+
country_code = graphene.String(required=True)
41+
city_district = graphene.String(required=True)
42+
city_population = graphene.Int(required=True)
43+
44+
def mutate(
45+
self,
46+
info,
47+
city_id,
48+
city_name,
49+
country_code,
50+
city_district,
51+
city_population,
52+
**kwargs
53+
):
54+
55+
# user = info.context.user
56+
# if user.is_anonymous:
57+
# raise Exception("Not logged in!!")
58+
59+
city = City(
60+
city_id = city_id,
61+
city_name = city_name,
62+
country_code = country_code,
63+
city_district = city_district,
64+
city_population = city_population
65+
)
66+
67+
city.save()
68+
69+
return AddCity(addCity=city)
70+
71+
class DeleteCity(graphene.Mutation):
72+
deleteCity = graphene.Field(CityType)
73+
74+
class Arguments:
75+
city_id = graphene.Int(required=True)
76+
77+
def mutate(
78+
self,
79+
info,
80+
city_id,
81+
**kwargs
82+
):
83+
84+
# user = info.context.user
85+
# if user.is_anonymous:
86+
# raise Exception("Not logged in!!")
87+
88+
city = City(
89+
city_id = city_id
90+
)
91+
92+
print(city.delete())
93+
94+
return True
95+
96+
class Mutation(graphene.ObjectType):
97+
add_city = AddCity.Field()
98+
delete_city = DeleteCity.Field()
99+
100+
schema = graphene.Schema(query=Query, mutation=Mutation)

‎worldapi/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

‎worldapi/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.

0 commit comments

Comments
 (0)