Skip to content

Commit

Permalink
Merge 3cd2ae1 into d6886c1
Browse files Browse the repository at this point in the history
  • Loading branch information
ropable committed Aug 4, 2016
2 parents d6886c1 + 3cd2ae1 commit 19c144d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Variables below may also need to be defined (context-dependent):
REFERRAL_EMAIL_HOST="outlook.office365.com"
REFERRAL_EMAIL_USER="referrals@email.address"
REFERRAL_EMAIL_PASSWORD="password"
REFERRAL_ASSIGNEE_FALLBACK="admin"
DOP_EMAIL="referrals@planning.wa.gov.au"
SITE_URL="prs.dpaw.wa.gov.au"
GEOSERVER_WMS_URL="//kmi.dpaw.wa.gov.au/geoserver/gwc/service/wms"
Expand Down
11 changes: 11 additions & 0 deletions prs2/harvester/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.conf import settings
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
import xmltodict

from referral.models import Referral, Record, Region

Expand Down Expand Up @@ -39,6 +40,16 @@ class EmailAttachment(models.Model):
def __str__(self):
return self.name

def get_xml_data(self):
"""Convenience function to conditionally return XML data from the
attachment (returns None if not an XML file).
"""
d = None
if self.name.startswith('Application.xml'):
self.attachment.seek(0)
d = xmltodict.parse(self.attachment.read())
return d


@python_2_unicode_compatible
class RegionAssignee(models.Model):
Expand Down
56 changes: 36 additions & 20 deletions prs2/harvester/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from referral.models import (
Region, Referral, ReferralType, Agency, Organisation, DopTrigger, Record,
TaskType, Task)
from .models import EmailedReferral, EmailAttachment
from .models import EmailedReferral, EmailAttachment, RegionAssignee


User = get_user_model()
Expand Down Expand Up @@ -121,7 +121,7 @@ def harvest_email(uid, message):
from_email=from_e, subject=message.get('Subject'),
body=message_body.get_payload())
em_new.save()
logger.info('Email UID {} harvested as EmailedReferral {}'.format(uid, em_new.pk))
logger.info('Email UID {} harvested: {}'.format(uid, em_new.subject))
for a in attachments:
att_new = EmailAttachment(
emailed_referral=em_new, name=a.get_filename(),
Expand All @@ -131,7 +131,7 @@ def harvest_email(uid, message):
att_new.attachment.save(a.get_filename(), new_file)
att_new.save()
data.close()
logger.info('Email attachment created as EmailAttachment {}'.format(uid, att_new.pk))
logger.info('Email attachment created: {}'.format(att_new.name))
except Exception as e:
logger.error('Email UID {} generated exception during harvest'.format(uid))
logger.exception(e)
Expand Down Expand Up @@ -198,6 +198,7 @@ def import_harvested_refs():
dpaw = Agency.objects.get(slug='dpaw')
wapc = Organisation.objects.get(slug='wapc')
assess_task = TaskType.objects.get(name='Assess a referral')
assignee_default = User.objects.get(username=settings.REFERRAL_ASSIGNEE_FALLBACK)
# Process harvested refs that are unprocessed at present.
for er in EmailedReferral.objects.filter(referral__isnull=True, processed=False):
attachments = er.emailattachment_set.all()
Expand Down Expand Up @@ -233,34 +234,49 @@ def import_harvested_refs():
continue
else:
# Import the harvested referral.
logger.info('Importing harvested referral reference {}'.format(ref))
ref_type = ReferralType.objects.current().get(name__istartswith=app['APP_TYPE'])
logger.info('Importing harvested referral ref {}'.format(ref))
try:
ref_type = ReferralType.objects.filter(name__istartswith=app['APP_TYPE'])[0]
except:
logger.warning('Referral type {} is not recognised type; skipping import'.format(app['APP_TYPE']))
er.processed = True
er.save()
continue
# Determine the intersecting region(s).
regions = []
assigned = None
# ADDRESS_DETAIL may or may not be a list :/
if not isinstance(app['ADDRESS_DETAIL']['DOP_ADDRESS_TYPE'], list):
addresses = [app['ADDRESS_DETAIL']['DOP_ADDRESS_TYPE']]
else:
addresses = app['ADDRESS_DETAIL']['DOP_ADDRESS_TYPE']
# Business rule cludge: for a referral with multiple addresses, just
# use the first one to assign the referral to a DPaW region.
p = Point(x=float(addresses[0]['LONGITUDE']), y=float(addresses[0]['LATITUDE']))
region = None
assigned = None
for reg in Region.objects.all():
if reg.region_mpoly and reg.region_mpoly.intersects(p):
region = reg
# TODO: determine the user to assign, by region.
# assigned = <SOME USER>
for a in addresses:
try:
p = Point(x=float(a['LONGITUDE']), y=float(a['LATITUDE']))
for r in Region.objects.all():
if r.region_mpoly and r.region_mpoly.intersects(p):
regions.append(r)
except:
logger.warning('Address long/lat could not be parsed({}, {})'.format(a['LONGITUDE'], a['LATITUDE']))
# Business rules:
# Didn't intersect a region? Might be bad geometry in the XML.
# Default to Swan Region and the designated fallback user.
if not region:
# Likewise if >1 region was intersected, default to Swan Region
# and the designated fallback user.
if len(regions) == 0 or len(regions) > 1:
region = Region.objects.get(name='Swan')
# TODO: determine the fallback user.
# assigned = <FALLBACK USER>
assigned = assignee_default
else:
region = regions[0]
try:
assigned = RegionAssignee.objects.get(region=region).user
except:
logger.warning('No default assignee set for {}, defaulting to {}'.format(region, assignee_default))
assigned = assignee_default
# Create the referral in PRS.
new_ref = Referral.objects.create(
type=ref_type, agency=dpaw, referring_org=wapc,
reference=ref, description=app['DEVELOPMENT_DESCRIPTION'],
referral_date=er.received, address=app['LOCATION'], point=p)
referral_date=er.received, address=app['LOCATION'])
# Assign to a region.
new_ref.region.add(region)
logger.info('New PRS referral generated: {}'.format(new_ref))
Expand Down
1 change: 1 addition & 0 deletions prs2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
REFERRAL_EMAIL_HOST = env('REFERRAL_EMAIL_HOST', 'host')
REFERRAL_EMAIL_USER = env('REFERRAL_EMAIL_USER', 'referrals')
REFERRAL_EMAIL_PASSWORD = env('REFERRAL_EMAIL_PASSWORD', 'password')
REFERRAL_ASSIGNEE_FALLBACK = env('REFERRAL_ASSIGNEE_FALLBACK', 'admin')
DOP_EMAIL = env('DOP_EMAIL', 'referrals@planning.wa.gov.au')

# Database configuration
Expand Down

0 comments on commit 19c144d

Please sign in to comment.