Skip to content

Commit

Permalink
Add button to country to fetch geoshape from wikidata.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoWill authored and alexdutton committed Feb 4, 2019
1 parent fe23c20 commit 4499a82
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions commons_api/wikidata/templates/wikidata/country_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
{% if country.iso_3166_1_code %}
<input type="submit" name="update-boundaries" value="Update boundaries">
{% endif %}
{% if country.geoshape_url %}
<input type="submit" name="refresh-geoshape" value="Refresh boundary from Wikidata">
{% endif %}
</form>
</div>
{% endblock %}
Expand Down
12 changes: 12 additions & 0 deletions commons_api/wikidata/tests/geoshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ def testWithLastModified(self, requests_get):
requests_get.called_once_with(geoshape_url, headers={'If-Modified-Since': 'Fri, 25 May 2018 14:44:24 GMT'})
# Has done nothing, because it assumes a current boundary is up to date
self.assertEqual(0, Boundary.objects.count())

@unittest.mock.patch('commons_api.wikidata.tasks.refresh_geoshape')
def testPostCountry(self, refresh_geoshape):
queued_at = timezone.now()
geoshape_url = 'http://example.org/some/geoshape.map'
country = models.Country.objects.create(id='Q142',
refresh_geoshape_last_queued=queued_at,
geoshape_url=geoshape_url)
response = self.client.post('/country/Q142', {'refresh-geoshape': None})
country.refresh_from_db()
refresh_geoshape.delay.assert_called_once_with('wikidata', 'country', 'Q142',
queued_at=country.refresh_geoshape_last_queued)
10 changes: 10 additions & 0 deletions commons_api/wikidata/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def post(self, request, *args, **kwargs):
messages.info(request, "Boundaries for {} will "
"be refreshed".format(self.object))

if 'refresh-geoshape' in request.POST:
from commons_api.wikidata.tasks import refresh_geoshape
from django.contrib.contenttypes.models import ContentType
last_queued = timezone.now()
self.model.objects.filter(pk=self.object.pk).update(refresh_geoshape_last_queued=last_queued)
ct = ContentType.objects.get_for_model(self.model)
refresh_geoshape.delay(ct.app_label, ct.model, self.object.id, queued_at=last_queued)
messages.info(request, "Getting boundary from wikidata "
"for {}".format(self.object))

return redirect(self.object.get_absolute_url())


Expand Down

0 comments on commit 4499a82

Please sign in to comment.