Skip to content

Commit

Permalink
Update farm ingestor to have to receive farm_group_id (#160)
Browse files Browse the repository at this point in the history
* Update farm ingestor to have to receive farm_group_id

* Fix flake
  • Loading branch information
meomancer authored Sep 27, 2024
1 parent 5c420c1 commit 73fe0e5
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 14 deletions.
2 changes: 1 addition & 1 deletion django_project/gap/admin/farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FarmGroupAdmin(AbstractDefinitionAdmin):
"""FarmGroup admin."""

list_display = (
'name', 'description', 'farm_count'
'id', 'name', 'description', 'farm_count'
)

filter_horizontal = ('farms', 'users')
Expand Down
8 changes: 8 additions & 0 deletions django_project/gap/ingestor/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ class ApiKeyNotFoundException(Exception):
def __init__(self): # noqa
self.message = 'Api key not found.'
super().__init__(self.message)


class AdditionalConfigNotFoundException(Exception):
"""Error when additional config is not found."""

def __init__(self, key): # noqa
self.message = f'{key} is required in additional_config.'
super().__init__(self.message)
20 changes: 16 additions & 4 deletions django_project/gap/ingestor/farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

import pandas as pd

from gap.ingestor.base import BaseIngestor
from gap.ingestor.exceptions import (
FileNotFoundException, FileIsNotCorrectException
FileNotFoundException, FileIsNotCorrectException,
AdditionalConfigNotFoundException
)
from gap.models import (
IngestorSession, Farm, Crop, FarmCategory, FarmRSVPStatus, Village
IngestorSession, Farm, FarmGroup, Crop, FarmCategory, FarmRSVPStatus,
Village
)
from gap.ingestor.base import BaseIngestor
from gap.utils.dms import dms_string_to_point

COLUMN_COUNT = 9
Expand All @@ -38,6 +40,15 @@ class FarmIngestor(BaseIngestor):
def __init__(self, session: IngestorSession, working_dir: str = '/tmp'):
"""Initialize the ingestor."""
super().__init__(session, working_dir)
self.farm_group = None
try:
self.farm_group = FarmGroup.objects.get(
id=session.additional_config['farm_group_id']
)
except KeyError:
raise AdditionalConfigNotFoundException('farm_group_id')
except FarmGroup.DoesNotExist:
raise Exception('Farm group does not exist')

def _run(self):
"""Run the ingestor."""
Expand Down Expand Up @@ -67,7 +78,7 @@ def _run(self):
village, _ = Village.objects.get_or_create(
name=row[Keys.VILLAGE_NAME]
)
Farm.objects.update_or_create(
farm, _ = Farm.objects.update_or_create(
unique_id=farm_id,
geometry=geometry,
defaults={
Expand All @@ -78,6 +89,7 @@ def _run(self):
'phone_number': phone_number
}
)
self.farm_group.farms.add(farm)
except KeyError as e:
raise FileIsNotCorrectException(
f'Row {idx + HEADER_IDX + 2} does not have {e}'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.7 on 2024-09-27 01:57

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('gap', '0020_farmgroup_remove_cropinsightrequest_farms_and_more'),
]

operations = [
migrations.AlterField(
model_name='farmgroup',
name='farms',
field=models.ManyToManyField(blank=True, null=True, to='gap.farm'),
),
migrations.AlterField(
model_name='farmgroup',
name='users',
field=models.ManyToManyField(blank=True, null=True, to=settings.AUTH_USER_MODEL),
),
]
8 changes: 6 additions & 2 deletions django_project/gap/models/farm_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
class FarmGroup(Definition):
"""Model representing group of farms."""

farms = models.ManyToManyField(Farm)
users = models.ManyToManyField(User)
farms = models.ManyToManyField(
Farm, blank=True, null=True
)
users = models.ManyToManyField(
User, blank=True, null=True
)

def email_recipients(self) -> list:
"""Return list of email addresses of farm recipients."""
Expand Down
2 changes: 1 addition & 1 deletion django_project/gap/tests/farm_group/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_delete_object(self):
self.assertFalse(self.Model.objects.filter(id=_id).exists())


class FarmGroupFUnctionalityCRUDTest(TestCase):
class FarmGroupFunctionalityCRUDTest(TestCase):
"""Farm Group test case."""

Factory = FarmGroupFactory
Expand Down
73 changes: 68 additions & 5 deletions django_project/gap/tests/ingestor/test_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase

from gap.ingestor.exceptions import FileNotFoundException
from gap.factories import FarmGroupFactory
from gap.ingestor.exceptions import (
FileNotFoundException, AdditionalConfigNotFoundException
)
from gap.ingestor.farm import Keys, Farm
from gap.models.ingestor import (
IngestorSession, IngestorSessionStatus, IngestorType
Expand All @@ -21,10 +24,57 @@ class FarmIngestorTest(TestCase):

fixtures = []

def setUp(self) -> None:
"""Set test class."""
self.farm_group = FarmGroupFactory()

def test_error_no_configuration(self):
"""Test when ingestor error no farm_group_id."""
filepath = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'data', 'farms', 'ErrorColumn.xlsx'
)
_file = open(filepath, 'rb')
session = IngestorSession.objects.create(
file=SimpleUploadedFile(_file.name, _file.read()),
ingestor_type=IngestorType.FARM
)
session.run()
session.delete()
self.assertEqual(
session.notes,
AdditionalConfigNotFoundException('farm_group_id').message
)
self.assertEqual(session.status, IngestorSessionStatus.FAILED)

def test_error_farm_group_not_found(self):
"""Test when ingestor error does not find farm_group_id."""
filepath = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'data', 'farms', 'ErrorColumn.xlsx'
)
_file = open(filepath, 'rb')
session = IngestorSession.objects.create(
file=SimpleUploadedFile(_file.name, _file.read()),
ingestor_type=IngestorType.FARM,
additional_config={
'farm_group_id': 0
}
)
session.run()
session.delete()
self.assertEqual(
session.notes, 'Farm group does not exist'
)
self.assertEqual(session.status, IngestorSessionStatus.FAILED)

def test_no_file(self):
"""Test no file ingestor."""
session = IngestorSession.objects.create(
ingestor_type=IngestorType.FARM
ingestor_type=IngestorType.FARM,
additional_config={
'farm_group_id': self.farm_group.id
}
)
session.run()
self.assertEqual(session.notes, FileNotFoundException().message)
Expand All @@ -39,7 +89,10 @@ def test_error_column(self):
_file = open(filepath, 'rb')
session = IngestorSession.objects.create(
file=SimpleUploadedFile(_file.name, _file.read()),
ingestor_type=IngestorType.FARM
ingestor_type=IngestorType.FARM,
additional_config={
'farm_group_id': self.farm_group.id
}
)
session.run()
session.delete()
Expand All @@ -57,7 +110,10 @@ def test_error_coordinate(self):
_file = open(filepath, 'rb')
session = IngestorSession.objects.create(
file=SimpleUploadedFile(_file.name, _file.read()),
ingestor_type=IngestorType.FARM
ingestor_type=IngestorType.FARM,
additional_config={
'farm_group_id': self.farm_group.id
}
)
session.run()
session.delete()
Expand All @@ -75,7 +131,10 @@ def test_working(self):
_file = open(filepath, 'rb')
session = IngestorSession.objects.create(
file=SimpleUploadedFile(_file.name, _file.read()),
ingestor_type=IngestorType.FARM
ingestor_type=IngestorType.FARM,
additional_config={
'farm_group_id': self.farm_group.id
}
)
session.run()
self.assertEqual(
Expand Down Expand Up @@ -115,3 +174,7 @@ def test_working(self):
self.assertEqual(farms[2].crop.name, 'Wheat')
self.assertEqual(farms[2].geometry.y, -0.2940277777777778)
self.assertEqual(farms[2].geometry.x, 35.885)

# Farm in correct group
for farm in farms:
self.farm_group.farms.get(id=farm.id)
2 changes: 1 addition & 1 deletion django_project/gap/tests/ingestor/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setUp(self):
def test_no_file(self):
"""Test no file ingestor."""
session = IngestorSession.objects.create(
ingestor_type=IngestorType.FARM
ingestor_type=IngestorType.GRID
)
session.run()
self.assertEqual(session.notes, FileNotFoundException().message)
Expand Down

0 comments on commit 73fe0e5

Please sign in to comment.