diff --git a/cap_feed/country.json b/cap_feed/country.json index 92432b3..6fcd59d 100644 --- a/cap_feed/country.json +++ b/cap_feed/country.json @@ -68,7 +68,7 @@ "iso": null, "iso3": null, "society_url": "", - "region": null, + "region": 2, "key_priorities": null, "inform_score": null, "id": 343, @@ -282,7 +282,7 @@ "iso": "AI", "iso3": "AIA", "society_url": "", - "region": null, + "region": 1, "key_priorities": null, "inform_score": null, "id": 217, @@ -426,7 +426,7 @@ "iso": "AW", "iso3": "ABW", "society_url": "", - "region": null, + "region": 1, "key_priorities": null, "inform_score": null, "id": 231, @@ -927,7 +927,7 @@ "iso": "BM", "iso3": "BMU", "society_url": "", - "region": null, + "region": 1, "key_priorities": null, "inform_score": null, "id": 233, @@ -992,7 +992,7 @@ "iso": null, "iso3": null, "society_url": "", - "region": null, + "region": 0, "key_priorities": null, "inform_score": null, "id": 346, @@ -3054,7 +3054,7 @@ "iso": "GI", "iso3": "GIB", "society_url": "", - "region": null, + "region": 3, "key_priorities": null, "inform_score": null, "id": 241, @@ -3421,7 +3421,7 @@ "iso": null, "iso3": null, "society_url": "", - "region": null, + "region": 0, "key_priorities": null, "inform_score": null, "id": 351, @@ -3472,7 +3472,7 @@ "iso": "VA", "iso3": "VAT", "society_url": "", - "region": null, + "region": 3, "key_priorities": null, "inform_score": null, "id": 274, @@ -3664,7 +3664,7 @@ "iso": null, "iso3": null, "society_url": "", - "region": null, + "region": 0, "key_priorities": null, "inform_score": null, "id": 352, @@ -4616,7 +4616,7 @@ "iso": "MO", "iso3": "MAC", "society_url": "http://www.redcross.org.mo/", - "region": null, + "region": 2, "key_priorities": null, "inform_score": null, "id": 279, @@ -5311,7 +5311,7 @@ "iso": "NR", "iso3": "NRU", "society_url": "", - "region": null, + "region": 2, "key_priorities": null, "inform_score": null, "id": 251, @@ -5571,7 +5571,7 @@ "iso": "NU", "iso3": "NIU", "society_url": "", - "region": null, + "region": 2, "key_priorities": null, "inform_score": null, "id": 253, @@ -5752,7 +5752,7 @@ "iso": "OM", "iso3": "OMN", "society_url": "", - "region": null, + "region": 2, "key_priorities": null, "inform_score": null, "id": 258, @@ -6060,7 +6060,7 @@ "iso": "PN", "iso3": "PCN", "society_url": "", - "region": null, + "region": 2, "key_priorities": null, "inform_score": null, "id": 256, @@ -6156,7 +6156,7 @@ "iso": "PR", "iso3": "PRI", "society_url": "", - "region": null, + "region": 1, "key_priorities": null, "inform_score": null, "id": 257, @@ -6365,7 +6365,7 @@ "iso": "SH", "iso3": "SHN", "society_url": "", - "region": null, + "region": 0, "key_priorities": null, "inform_score": null, "id": 262, @@ -6616,7 +6616,7 @@ "iso": null, "iso3": null, "society_url": "", - "region": null, + "region": 2, "key_priorities": null, "inform_score": null, "id": 358, @@ -7148,7 +7148,7 @@ "iso": null, "iso3": null, "society_url": "", - "region": null, + "region": 2, "key_priorities": null, "inform_score": null, "id": 360, @@ -8230,7 +8230,7 @@ "iso": "EH", "iso3": "ESH", "society_url": "", - "region": null, + "region": 0, "key_priorities": null, "inform_score": null, "id": 269, diff --git a/cap_feed/migrations/0002_country_region.py b/cap_feed/migrations/0002_country_region.py new file mode 100644 index 0000000..b3f2374 --- /dev/null +++ b/cap_feed/migrations/0002_country_region.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.2 on 2023-06-23 11:38 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cap_feed', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='country', + name='region', + field=models.ForeignKey(blank=True, default='', on_delete=django.db.models.deletion.SET_DEFAULT, to='cap_feed.region'), + ), + ] diff --git a/cap_feed/models.py b/cap_feed/models.py index d6bb221..64db806 100644 --- a/cap_feed/models.py +++ b/cap_feed/models.py @@ -32,15 +32,16 @@ class Region(models.Model): centroid = models.CharField(max_length=255, blank=True, default='') def __str__(self): - return self.id + return self.name class Country(models.Model): id = models.CharField(max_length=255, primary_key=True) name = models.CharField(max_length=255) - iso = models.CharField(max_length=255, blank = True, default='') - iso3 = models.CharField(max_length=255, blank = True, default='') + iso = models.CharField(max_length=255, blank=True, default='') + iso3 = models.CharField(max_length=255, blank=True, default='') polygon = models.TextField(max_length=16383, blank=True, default='') centroid = models.CharField(max_length=255, blank=True, default='') + region = models.ForeignKey(Region, blank=True, on_delete=models.SET_DEFAULT, default='') def __str__(self): - return self.id \ No newline at end of file + return self.name \ No newline at end of file diff --git a/cap_feed/views.py b/cap_feed/views.py index cd51f15..a0ca630 100644 --- a/cap_feed/views.py +++ b/cap_feed/views.py @@ -1,3 +1,4 @@ +import json import requests import xml.etree.ElementTree as ET @@ -7,10 +8,6 @@ from .models import Alert, Region, Country -import json -with open('cap_feed/region.json') as file: - data = json.load(file) - region_centroids = ["17.458740234362434 -2.677413176352464", "-80.83261851536723 -2.6920536197633442", "117.78896429869648 -3.1783208418475954", "30.64725652750233 45.572165430308736", "21.18749859869599 31.264366696701767"] @@ -20,6 +17,7 @@ def index(request): getAlerts() saveRegions() + saveCountries() latest_alert_list = Alert.objects.order_by("-sent")[:10] template = loader.get_template("cap_feed/index.html") context = { @@ -29,17 +27,45 @@ def index(request): def saveRegions(): - count = 0 - for region_entry in data: - region = Region() - region.id = region_entry["id"] - region.name = region_entry["region_name"] - coordinates = region_entry["bbox"]["coordinates"][0] - for coordinate in coordinates: - region.polygon += str(coordinates[0]) + "," + str(coordinates[1]) + " " - region.centroid = region_centroids[count] - count += 1 - region.save() + with open('cap_feed/region.json') as file: + region_data = json.load(file) + count = 0 + for region_entry in region_data: + region = Region() + region.id = region_entry["id"] + region.name = region_entry["region_name"] + coordinates = region_entry["bbox"]["coordinates"][0] + for coordinate in coordinates: + region.polygon += str(coordinate[0]) + "," + str(coordinate[1]) + " " + region.centroid = region_centroids[count] + count += 1 + region.save() + + +def saveCountries(): + with open('cap_feed/country.json') as file: + country_data = json.load(file) + for country_entry in country_data: + country = Country() + country.id = country_entry["id"] + country.name = country_entry["name"] + region_id = country_entry["region"] + if ("Region" in country.name) or ("Cluster" in country.name): + continue + if region_id is not None: + country.region = Region.objects.get(id=country_entry["region"]) + if country_entry["iso"] is not None: + country.iso = country_entry["iso"] + if country_entry["iso3"] is not None: + country.iso3 = country_entry["iso3"] + if country_entry["bbox"] is not None: + coordinates = country_entry["bbox"]["coordinates"][0] + for coordinate in coordinates: + country.polygon += str(coordinate[0]) + "," + str(coordinate[1]) + " " + if country_entry["centroid"] is not None: + coordinates = country_entry["centroid"]["coordinates"] + country.centroid = str(coordinates[0]) + "," + str(coordinates[1]) + country.save() # sources = [ # ("https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-atom-france", {'atom': 'http://www.w3.org/2005/Atom', 'cap': 'urn:oasis:names:tc:emergency:cap:1.2'}),