Skip to content

Commit

Permalink
Send email when there is a new site submitted from app (#3445)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput authored Aug 19, 2023
1 parent af94cda commit 0873a1a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
3 changes: 3 additions & 0 deletions bims/serializers/location_site_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class LocationSiteSerializer(serializers.ModelSerializer):
river_name = serializers.SerializerMethodField()
owner = serializers.SerializerMethodField()
description = serializers.SerializerMethodField()
user_site_code = serializers.CharField(source='legacy_site_code', read_only=True)
user_river_name = serializers.CharField(source='legacy_river_name', read_only=True)

def get_description(self, obj):
return obj.site_description
Expand Down Expand Up @@ -43,6 +45,7 @@ class Meta:
'name', 'geometry',
'location_type',
'record_type', 'river_name',
'user_site_code', 'user_river_name',
'owner',
'description'
]
Expand Down
6 changes: 4 additions & 2 deletions bims/views/location_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def handle_location_site_post_data(
owner = None
latitude = post_data.get('latitude', None)
longitude = post_data.get('longitude', None)
legacy_site_code = post_data.get('legacy_site_code', '')
legacy_site_code = post_data.get('user_site_code', '')
if not legacy_site_code:
legacy_site_code = post_data.get('legacy_site_code', '')
additional_data = post_data.get('additional_data', None)
date = post_data.get('date', datetime.now())
site_geometry = post_data.get('site-geometry', None)
Expand Down Expand Up @@ -126,7 +128,7 @@ def handle_location_site_post_data(
'owner': owner,
'latitude': latitude,
'longitude': longitude,
'site_description': site_description,
'site_description': site_description if site_description else '',
'geometry_point': geometry_point,
'location_type': location_type,
'site_code': site_code,
Expand Down
75 changes: 72 additions & 3 deletions mobile/api_views/add_location_site.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import copy
import os
from datetime import datetime

from django.contrib.auth import get_user_model
from django.core.mail import EmailMessage
from django.contrib.sites.models import Site
import csv

import pytz

from bims.location_site.river import fetch_river_name
from bims.models.location_site import generate_site_code
from bims.models.location_site import generate_site_code, LocationSite
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
Expand All @@ -15,12 +21,72 @@
class AddLocationSiteView(APIView):
permission_classes = (IsAuthenticated,)

def send_location_site_email(self, location_site: LocationSite):
"""
Send the new location site data to the user and staff.
Create CSV from location site and attach it to email.
"""
current_site = Site.objects.get_current()

csv_data = [
["ID", "Site Code", "User Site Code", "River Name",
"User River Name", "Description", "Owner", "URL"],
[location_site.id,
location_site.site_code,
location_site.legacy_site_code,
location_site.river.name if location_site.river else '-',
location_site.legacy_river_name,
location_site.site_description,
location_site.owner.username,
'http://{url}/location-site-form/update/?id={id}'.format(
url=current_site,
id=location_site.id
)
]]

# Write CSV data to a file
csv_file_name = f"location_site_{location_site.id}.csv"
with open(csv_file_name, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(csv_data)

email_body = """
You have received the following notice from {current_site}:
A new location site has been submitted through the mobile app.
Details of the submission are attached in the CSV file.
""".format(current_site=current_site)

bcc_recipients = list(
get_user_model().objects.filter(is_superuser=True).values_list('email', flat=True)
)

owner_email = self.request.user.email

if owner_email in bcc_recipients:
bcc_recipients.remove(owner_email)

# Send an email with the attached CSV
email = EmailMessage(
'[{}] New Location Site Data Submission'.format(current_site),
email_body,
'from@example.com',
[self.request.user.email],
bcc=bcc_recipients
)
email.attach_file(csv_file_name)
email.send()

os.remove(csv_file_name)

def post(self, request, *args, **kwargs):
post_data = copy.deepcopy(request.data)

# Fetch river name
river_name = fetch_river_name(
post_data.get('latitude'), post_data.get('longitude'))
river_name = post_data.get('river_name', '')
if not river_name:
river_name = fetch_river_name(
post_data.get('latitude'), post_data.get('longitude'))

# Generate site code
site_code, catchments_data = generate_site_code(
Expand All @@ -44,6 +110,9 @@ def post(self, request, *args, **kwargs):
post_data,
self.request.user
)

self.send_location_site_email(location_site)

return Response(
{
'id': location_site.id,
Expand Down

0 comments on commit 0873a1a

Please sign in to comment.