-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
238 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
terraso_backend/apps/soil_id/migrations/0014_soilidcache_soilidcache_coordinate_index.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright © 2024 Technology Matters | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
||
# Generated by Django 5.0.6 on 2024-06-27 23:06 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("soil_id", "0013_remove_projectsoilsettings_measurement_units"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="SoilIdCache", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
), | ||
), | ||
("latitude", models.FloatField()), | ||
("longitude", models.FloatField()), | ||
("failure_reason", models.TextField(null=True)), | ||
("soil_list_json", models.JSONField(null=True)), | ||
("rank_data_csv", models.TextField(null=True)), | ||
("map_unit_component_data_csv", models.TextField(null=True)), | ||
], | ||
), | ||
migrations.AddConstraint( | ||
model_name="soilidcache", | ||
constraint=models.UniqueConstraint( | ||
fields=("latitude", "longitude"), name="coordinate_index" | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright © 2024 Technology Matters | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
||
|
||
from django.db import models | ||
from soil_id.us_soil import SoilListOutputData | ||
|
||
|
||
class SoilIdCache(models.Model): | ||
latitude = models.FloatField() | ||
longitude = models.FloatField() | ||
failure_reason = models.TextField(null=True) | ||
soil_list_json = models.JSONField(null=True) | ||
rank_data_csv = models.TextField(null=True) | ||
map_unit_component_data_csv = models.TextField(null=True) | ||
|
||
class Meta: | ||
constraints = [ | ||
models.UniqueConstraint(fields=["latitude", "longitude"], name="coordinate_index") | ||
] | ||
|
||
@classmethod | ||
def round_coordinate(cls, coord: float): | ||
return round(coord, 6) | ||
|
||
@classmethod | ||
def save_data(cls, latitude: float, longitude: float, data: SoilListOutputData | str): | ||
if isinstance(data, str): | ||
data_to_save = {"failure_reason": data} | ||
else: | ||
data_to_save = { | ||
"soil_list_json": data.soil_list_json, | ||
"rank_data_csv": data.rank_data_csv, | ||
"map_unit_component_data_csv": data.map_unit_component_data_csv, | ||
} | ||
|
||
cls.objects.update_or_create( | ||
latitude=cls.round_coordinate(latitude), | ||
longitude=cls.round_coordinate(longitude), | ||
create_defaults=data_to_save, | ||
) | ||
|
||
@classmethod | ||
def get_data(cls, latitude: float, longitude: float) -> SoilListOutputData | str: | ||
try: | ||
prev_result = cls.objects.get( | ||
latitude=cls.round_coordinate(latitude), longitude=cls.round_coordinate(longitude) | ||
) | ||
if prev_result.failure_reason is not None: | ||
return prev_result.failure_reason | ||
|
||
return SoilListOutputData( | ||
soil_list_json=prev_result.soil_list_json, | ||
rank_data_csv=prev_result.rank_data_csv, | ||
map_unit_component_data_csv=prev_result.map_unit_component_data_csv, | ||
) | ||
except cls.DoesNotExist: | ||
return None |
Oops, something went wrong.