Skip to content

Commit

Permalink
Merge pull request #291 from ropable/master
Browse files Browse the repository at this point in the history
Better error handling for emails, switch region layer on map
  • Loading branch information
ropable authored Sep 10, 2024
2 parents c7eb7aa + 33ce972 commit 497f53e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion kustomize/overlays/prod/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ patches:
- path: service_patch.yaml
images:
- name: ghcr.io/dbca-wa/resource_tracking
newTag: 1.4.15
newTag: 1.4.16
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "resource_tracking"
version = "1.4.15"
version = "1.4.16"
description = "DBCA internal corporate application to download and serve data from remote tracking devices."
authors = ["DBCA OIM <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -20,7 +20,7 @@ django-geojson = "4.1.0"
unicodecsv = "0.14.1"
whitenoise = {version = "6.7.0", extras = ["brotli"]}
azure-storage-blob = "12.22.0"
sentry-sdk = {version = "2.13.0", extras = ["django"]}
sentry-sdk = {version = "2.14.0", extras = ["django"]}

[tool.poetry.group.dev.dependencies]
ipython = "^8.27.0"
Expand Down
15 changes: 11 additions & 4 deletions tracking/email_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from django.conf import settings
import email
from imaplib import IMAP4_SSL
from imaplib import IMAP4, IMAP4_SSL
import logging

LOGGER = logging.getLogger("tracking")


def get_imap(mailbox="INBOX"):
"""Instantiate a new IMAP object, login, and connect to a mailbox."""
imap = IMAP4_SSL(settings.EMAIL_HOST)
imap.login(settings.EMAIL_USER, settings.EMAIL_PASSWORD)
imap.select(mailbox)
return imap
try:
imap.login(settings.EMAIL_USER, settings.EMAIL_PASSWORD)
imap.select(mailbox)
return imap
except IMAP4.error:
LOGGER.error("Unable to log into mailbox")
return None


def email_get_unread(imap, from_email_address):
Expand Down
14 changes: 12 additions & 2 deletions tracking/harvest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import csv
from django.conf import settings
from django.utils import timezone
from imaplib import IMAP4
import logging
import requests

Expand All @@ -24,6 +25,10 @@ def harvest_tracking_email(device_type, purge_email=False):
`device_type` should be one of: iriditrak, dplus, spot, mp70
"""
imap = email_utils.get_imap()
if not imap:
LOGGER.warning("Mailbox not available")
return

start = timezone.now()
created = 0
flagged = 0
Expand Down Expand Up @@ -80,8 +85,13 @@ def harvest_tracking_email(device_type, purge_email=False):
status, response = email_utils.email_delete(imap, uid)

LOGGER.info(f"Created {created} tracking points, flagged {flagged} emails")
imap.close()
imap.logout()

try:
imap.close()
imap.logout()
except IMAP4.abort:
LOGGER.warning("IMAP abort")
pass

delta = timezone.now() - start
start = start.astimezone(settings.TZ)
Expand Down
2 changes: 1 addition & 1 deletion tracking/static/js/resource_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const dfesBushfires = L.tileLayer(
}
);
const dbcaRegions = L.tileLayer(
geoserver_wmts_url_overlay + "&layer=cddp:dbca_regions",
geoserver_wmts_url_overlay + "&layer=cddp:kaartdijin-boodja-public_CPT_DBCA_REGIONS",
{
tileSize: 1024,
zoomOffset: -2,
Expand Down

0 comments on commit 497f53e

Please sign in to comment.