Skip to content

Commit

Permalink
Add date to the crop filename (#167)
Browse files Browse the repository at this point in the history
* Add date to the crop filename

* Fix flake
  • Loading branch information
meomancer authored Oct 2, 2024
1 parent a75b440 commit be2d41d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 101 deletions.
31 changes: 26 additions & 5 deletions django_project/gap/models/crop_insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import os.path
import uuid
from datetime import date, timedelta
from datetime import date, timedelta, datetime, tzinfo

from django.conf import settings
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -585,10 +585,16 @@ def run(self):
self._generate_report()

@property
def title(self) -> str:
"""Return the title of the request."""
def time_description(self) -> (tzinfo, datetime):
"""Return time description."""
east_africa_timezone = Preferences.east_africa_timezone()
east_africa_time = self.requested_at.astimezone(east_africa_timezone)
return east_africa_timezone, east_africa_time

@property
def title(self) -> str:
"""Return the title of the request."""
east_africa_timezone, east_africa_time = self.time_description
group = ''
if self.farm_group:
group = f' {self.farm_group} -'
Expand All @@ -598,6 +604,18 @@ def title(self) -> str:
f"({east_africa_timezone})"
)

@property
def filename(self):
"""Return the filename of the request."""
east_africa_timezone, east_africa_time = self.time_description
group = ''
if self.farm_group:
group = f' {self.farm_group} -'
return (
f"{group} {east_africa_time.strftime('%Y-%m-%d')} "
f'({self.unique_id}).csv'
)

def _generate_report(self):
"""Generate reports."""
from spw.generator.crop_insight import CropInsightFarmGenerator
Expand Down Expand Up @@ -646,7 +664,10 @@ def _generate_report(self):
csv_content += ','.join(map(str, row)) + '\n'
content_file = ContentFile(csv_content)
self.file.save(
os.path.join(f'{self.farm_group.id}', f'{self.unique_id}.csv'),
os.path.join(
f'{self.farm_group.id}',
self.filename
),
content_file
)
self.save()
Expand All @@ -667,7 +688,7 @@ def _generate_report(self):
to=self.farm_group.email_recipients()
)
email.attach(
f'{self.unique_id}.csv',
self.filename,
self.file.open('rb').read(),
'text/csv'
)
Expand Down
207 changes: 111 additions & 96 deletions django_project/spw/tests/test_crop_insight_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,102 +286,117 @@ def create_timeline_data(
request = CropInsightRequestFactory.create()
generate_insight_report(request.id)

# Crop insight report
self.request = CropInsightRequestFactory.create(
farm_group=self.farm_group
)
generate_insight_report(self.request.id)
self.request.refresh_from_db()

# Check the if of farm group in the path
self.assertTrue(f'{self.farm_group.id}/' in self.request.file.path)

with self.request.file.open(mode='r') as csv_file:
csv_reader = csv.reader(csv_file)
row_num = 1
for row in csv_reader:
# Header
if row_num == 1:
self.assertEqual(row, self.csv_headers)

# Farm 1
elif row_num == 2:
# Farm Unique ID
self.assertEqual(row[0], self.farm.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm.phone_number)
self.assertEqual(row[2], '10.1111') # Latitude
self.assertEqual(row[3], '11.1111') # Longitude
self.assertEqual(row[4], 'Plant Now')
self.assertEqual(
row[5],
'Both current forecast '
'historical rains have good signal to plant.'
)
self.assertEqual(row[6], '10.0') # Precip (daily)
self.assertEqual(row[7], '50.0') # Precip % chance
self.assertEqual(row[8], 'Light rain') # Precip Type

# Farm 2
elif row_num == 3:
# Farm Unique ID
self.assertEqual(row[0], self.farm_2.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm_2.phone_number)
self.assertEqual(row[2], '100.1111') # Latitude
self.assertEqual(row[3], '22.2222') # Longitude
self.assertEqual(row[4], 'DO NOT PLANT')
self.assertEqual(
row[5], 'Wait for more positive forecast.'
)
self.assertEqual(row[6], '0.5') # Precip (daily)
self.assertEqual(row[7], '10.0') # Precip % chance
self.assertEqual(row[8], 'No Rain') # Precip Type

# Farm 3
elif row_num == 4:
# Farm Unique ID
self.assertEqual(row[0], self.farm_3.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm_3.phone_number)
self.assertEqual(row[2], '50.1111') # Latitude
self.assertEqual(row[3], '50.2222') # Longitude
self.assertEqual(row[4], '')
self.assertEqual(row[5], '')
self.assertEqual(row[6], '') # Precip (daily)
self.assertEqual(row[7], '') # Precip % chance
self.assertEqual(row[8], '') # Precip Type

# Farm 4 has same grid with farm 2
elif row_num == 5:
# Farm Unique ID
self.assertEqual(row[0], self.farm_4.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm_4.phone_number)
self.assertEqual(row[2], '100.0') # Latitude
self.assertEqual(row[3], '22.2222') # Longitude
self.assertEqual(row[4], 'DO NOT PLANT')
self.assertEqual(
row[5], 'Wait for more positive forecast.'
)
self.assertEqual(row[6], '0.5') # Precip (daily)
self.assertEqual(row[7], '10.0') # Precip % chance
self.assertEqual(row[8], 'No Rain') # Precip Type

# Farm 5 has same grid with farm 3
elif row_num == 6:
# Farm Unique ID
self.assertEqual(row[0], self.farm_5.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm_5.phone_number)
self.assertEqual(row[2], '50.0') # Latitude
self.assertEqual(row[3], '50.2222') # Longitude
self.assertEqual(row[4], '')
self.assertEqual(row[5], '')
self.assertEqual(row[6], '') # Precip (daily)
self.assertEqual(row[7], '') # Precip % chance
self.assertEqual(row[8], '') # Precip Type
row_num += 1
# We mock the send email to get the attachments
attachments = []

def mock_send_fn(self, fail_silently=False):
"""Mock send messages."""
for attachment in self.attachments:
attachments.append(attachment)
return 0

# Mock the send email
with patch("django.core.mail.EmailMessage.send", mock_send_fn):
# Crop insight report
self.request = CropInsightRequestFactory.create(
farm_group=self.farm_group
)
generate_insight_report(self.request.id)
self.request.refresh_from_db()

# Check the if of farm group in the path
self.assertTrue(f'{self.farm_group.id}/' in self.request.file.path)

# Check the attachment on email
self.assertEqual(attachments[0][0], self.request.filename)

# Check the file content
with self.request.file.open(mode='r') as csv_file:
csv_reader = csv.reader(csv_file)
row_num = 1
for row in csv_reader:
# Header
if row_num == 1:
self.assertEqual(row, self.csv_headers)

# Farm 1
elif row_num == 2:
# Farm Unique ID
self.assertEqual(row[0], self.farm.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm.phone_number)
self.assertEqual(row[2], '10.1111') # Latitude
self.assertEqual(row[3], '11.1111') # Longitude
self.assertEqual(row[4], 'Plant Now')
self.assertEqual(
row[5],
'Both current forecast '
'historical rains have good signal to plant.'
)
self.assertEqual(row[6], '10.0') # Precip (daily)
self.assertEqual(row[7], '50.0') # Precip % chance
self.assertEqual(row[8], 'Light rain') # Precip Type

# Farm 2
elif row_num == 3:
# Farm Unique ID
self.assertEqual(row[0], self.farm_2.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm_2.phone_number)
self.assertEqual(row[2], '100.1111') # Latitude
self.assertEqual(row[3], '22.2222') # Longitude
self.assertEqual(row[4], 'DO NOT PLANT')
self.assertEqual(
row[5], 'Wait for more positive forecast.'
)
self.assertEqual(row[6], '0.5') # Precip (daily)
self.assertEqual(row[7], '10.0') # Precip % chance
self.assertEqual(row[8], 'No Rain') # Precip Type

# Farm 3
elif row_num == 4:
# Farm Unique ID
self.assertEqual(row[0], self.farm_3.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm_3.phone_number)
self.assertEqual(row[2], '50.1111') # Latitude
self.assertEqual(row[3], '50.2222') # Longitude
self.assertEqual(row[4], '')
self.assertEqual(row[5], '')
self.assertEqual(row[6], '') # Precip (daily)
self.assertEqual(row[7], '') # Precip % chance
self.assertEqual(row[8], '') # Precip Type

# Farm 4 has same grid with farm 2
elif row_num == 5:
# Farm Unique ID
self.assertEqual(row[0], self.farm_4.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm_4.phone_number)
self.assertEqual(row[2], '100.0') # Latitude
self.assertEqual(row[3], '22.2222') # Longitude
self.assertEqual(row[4], 'DO NOT PLANT')
self.assertEqual(
row[5], 'Wait for more positive forecast.'
)
self.assertEqual(row[6], '0.5') # Precip (daily)
self.assertEqual(row[7], '10.0') # Precip % chance
self.assertEqual(row[8], 'No Rain') # Precip Type

# Farm 5 has same grid with farm 3
elif row_num == 6:
# Farm Unique ID
self.assertEqual(row[0], self.farm_5.unique_id)
# Phone Number
self.assertEqual(row[1], self.farm_5.phone_number)
self.assertEqual(row[2], '50.0') # Latitude
self.assertEqual(row[3], '50.2222') # Longitude
self.assertEqual(row[4], '')
self.assertEqual(row[5], '')
self.assertEqual(row[6], '') # Precip (daily)
self.assertEqual(row[7], '') # Precip % chance
self.assertEqual(row[8], '') # Precip Type
row_num += 1

@patch('spw.generator.crop_insight.CropInsightFarmGenerator.generate_spw')
def test_generate_crop_plan(
Expand Down

0 comments on commit be2d41d

Please sign in to comment.