-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcities.py
More file actions
101 lines (78 loc) · 3.6 KB
/
cities.py
File metadata and controls
101 lines (78 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from dal.dao import create_cursor
import string
import os
from .filters.city_filter import CityFilter
from Json.Query import convertJson
class City:
@staticmethod
def getCities(country_name="", state_name="", filters=CityFilter()):
"""
it takes 3 optional parameters and return JSON object.
Default:
By default it will return all the cities with all the fields including
- city_id
Args:
country_name (str): Optional - To get the cities of some specific country.
state_name (str): Optional - To get the cities of some specific state.
filters (CityFilter): Optional - A class object specifying which fields to include in the results.
Returns:
list: A json object containing the requested information about the cities.
Example:
- To get all cities
```
hawqal.getCities()
```
- To get all cities of some country
```
hawqal.getCities(country_name="Pakistan")
```
- To get all cities of some state in a country
```
hawqal.getCities(country_name="Pakistan",state_name="Punjab")
```
- To apply filters and exclude state_name
```
hawqal.getCities(filter=hawqal.CityFilter(state_name=False))
```
"""
cursor = create_cursor()
query = "SELECT " + str(filters) + " FROM cities"
if country_name != "":
query = query + \
f" WHERE country_name = '{string.capwords(country_name)}'"
elif state_name != "":
query = query + \
f" WHERE state_name = '{string.capwords(state_name)}'"
cursor.execute(query)
return convertJson(cursor)
@staticmethod
def getCity(country_name="", state_name="", city_name="", filters=CityFilter()):
"""
Retrieves information about a city from the database.
Parameters:
country_name (str): The name of the country where the city is located. Optional.
state_name (str): The name of the state or province where the city is located. Optional.
city_name (str): The name of the city. Required.
filters (CityFilter): An object specifying which fields to include in the results. Optional.
Returns:
A Json object containing the requested information about the city.
Raises:
ValueError: If `city_name` is not specified.
"""
cursor = create_cursor()
country_name = string.capwords(country_name)
state_name = string.capwords(state_name)
city_name = string.capwords(city_name)
if city_name == "":
raise ValueError("City name must be set")
query = "SELECT " + str(filters) + " FROM CITIES "
if country_name != "":
query = query + f"WHERE country_name='{country_name}' AND city_name='{city_name}' "
elif state_name != "":
query = query + f"Where state_name='{state_name}' and city_name='{city_name}'"
elif country_name != "" and state_name != "":
query = query + f"Where state_name='{state_name}' and country_name='{country_name} and city_name='{city_name}' '"
else:
query = query + f"where city_name='{city_name}'"
cursor.execute(query)
return convertJson(cursor)