23 |
23 |
verbose_name=_('Service URL'),
|
24 |
24 |
help_text=_('Base Adresse Web Service URL'))
|
25 |
25 |
|
|
26 |
api_geo_url = models.CharField(
|
|
27 |
max_length=128, blank=False,
|
|
28 |
default='https://geo.api.gouv.fr/',
|
|
29 |
verbose_name=_('API Geo URL'),
|
|
30 |
help_text=_('Base Adresse API Geo URL'))
|
|
31 |
|
26 |
32 |
category = _('Geographic information system')
|
27 |
33 |
|
28 |
34 |
zipcode = models.CharField(
|
... | ... | |
30 |
36 |
blank=True,
|
31 |
37 |
verbose_name=_('Postal codes or county number to get streets, separated with commas'))
|
32 |
38 |
|
|
39 |
county = models.PositiveSmallIntegerField(
|
|
40 |
blank=True,
|
|
41 |
null=True,
|
|
42 |
verbose_name=_('County code to get cities from'))
|
|
43 |
|
|
44 |
region = models.PositiveSmallIntegerField(
|
|
45 |
blank=True,
|
|
46 |
null=True,
|
|
47 |
verbose_name=_('Region code to get cities from'))
|
|
48 |
|
33 |
49 |
class Meta:
|
34 |
50 |
verbose_name = _('Base Adresse Web Service')
|
35 |
51 |
|
... | ... | |
137 |
153 |
|
138 |
154 |
return {'data': result}
|
139 |
155 |
|
|
156 |
@endpoint(description=_('Cities from zipcode'),
|
|
157 |
parameters={
|
|
158 |
'zipcode': {'description': 'TODO', 'example_value': '75014'}
|
|
159 |
})
|
|
160 |
def cities(self, request, id=None, zipcode=None, q=None, insee_code=None, **kwargs):
|
|
161 |
if id is not None:
|
|
162 |
insee_code, zipcode = id.split('.')
|
|
163 |
|
|
164 |
request_data = {
|
|
165 |
'format': 'json',
|
|
166 |
'boost': 'population',
|
|
167 |
}
|
|
168 |
|
|
169 |
if insee_code:
|
|
170 |
request_data['code'] = insee_code
|
|
171 |
elif zipcode:
|
|
172 |
request_data['codePostal'] = zipcode
|
|
173 |
|
|
174 |
if q:
|
|
175 |
request_data['nom'] = q
|
|
176 |
if self.county:
|
|
177 |
request_data['codeDepartement'] = self.county
|
|
178 |
if self.region:
|
|
179 |
request_data['codeRegion'] = self.region
|
|
180 |
|
|
181 |
scheme, netloc, path, params, query, fragment = urlparse.urlparse(self.api_geo_url)
|
|
182 |
path = os.path.join(path, 'communes/')
|
|
183 |
query = urlencode(request_data)
|
|
184 |
url = urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
|
|
185 |
|
|
186 |
result_response = self.requests.get(url)
|
|
187 |
|
|
188 |
data = []
|
|
189 |
for city in result_response.json():
|
|
190 |
zipcodes = [zipcode] if zipcode else city['codesPostaux']
|
|
191 |
for code in zipcodes:
|
|
192 |
new_city = city.copy()
|
|
193 |
new_city['codePostal'] = code
|
|
194 |
new_city['id'] = '.'.join((city['code'], code))
|
|
195 |
new_city['text'] = '%s (%s)' % (city['nom'], code)
|
|
196 |
data.append(new_city)
|
|
197 |
|
|
198 |
return {'data': data, 'err': 0}
|
|
199 |
|
140 |
200 |
def check_status(self):
|
141 |
201 |
if self.service_url == 'https://api-adresse.data.gouv.fr/':
|
142 |
202 |
result = self.search(None, '169 rue du chateau, paris')
|
143 |
|
-
|