Skip to content

Commit

Permalink
Merge pull request #37 from ropable/v2.1
Browse files Browse the repository at this point in the history
Update dpaw-utils to 0.3a11, add API call to Borg Collector to run harvest for PRS locations
  • Loading branch information
dbca-asi committed May 23, 2016
2 parents 2a5b774 + b7cd321 commit 5ab6e2d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Variables below may also need to be defined (context-dependent):
GEOSERVER_WFS_URL="//kmi.dpaw.wa.gov.au/geoserver/ows"
PRS_USER_GROUP="PRS user"
PRS_PWUSER_GROUP="PRS power user"
BORGCOLLECTOR_API="https://borg.dpaw.wa.gov.au/api/"
# debug-toolbar settings:
INTERNAL_IP="x.x.x.x"

Expand Down
20 changes: 17 additions & 3 deletions prs2/referral/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Django imports
from confy import env
from datetime import datetime
from django.apps import apps
from django.db.models import Q
from django.db.models.base import ModelBase
from django.utils.safestring import mark_safe
from django.contrib import admin
import re
from django.utils.encoding import smart_str
from datetime import datetime
import json
import re

from dpaw_utils.requests.api import post as post_sso


def is_model_or_string(model):
Expand Down Expand Up @@ -223,3 +225,15 @@ def is_superuser(request):

def prs_user(request):
return is_prs_user(request) or is_prs_power_user(request) or is_superuser(request)


def borgcollector_harvest(request, publishes=['prs_locations']):
"""Convenience function to manually run a Borg Collector harvest
job for the PRS locations layer.
Docs: https://github.com/parksandwildlife/borgcollector
"""
api_url = env('BORGCOLLECTOR_API', 'https://borg.dpaw.wa.gov.au/api/') + 'jobs/'
# Send a POST request to the API endpoint.
r = post_sso(request, api_url, data=json.dumps({'publishes': publishes}))
return r
16 changes: 15 additions & 1 deletion prs2/referral/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View, ListView, TemplateView, FormView
import json
import logging
import re

from referral.models import (
Expand All @@ -25,7 +26,7 @@
from referral.utils import (
is_model_or_string, breadcrumbs_li, smart_truncate, get_query,
user_task_history, user_referral_history, filter_queryset,
prs_user, is_prs_power_user)
prs_user, is_prs_power_user, borgcollector_harvest)
from referral.forms import (
ReferralCreateForm, NoteForm, NoteAddExistingForm,
RecordCreateForm, RecordAddExistingForm, TaskCreateForm,
Expand All @@ -41,6 +42,8 @@
from django_downloadview import ObjectDownloadView
from taggit.models import Tag

logger = logging.getLogger('prs.log')


class SiteHome(LoginRequiredMixin, ListView):
"""Site home page view. Returns an object list of tasks (ongoing or stopped).
Expand Down Expand Up @@ -737,6 +740,13 @@ def post(self, request, *args, **kwargs):
locations.append(l)

messages.success(request, '{} location(s) created.'.format(len(forms)))

# Call the Borg Collector publish API endpoint to create a manual job
# to update the prs_locations layer.
resp = borgcollector_harvest(self.request)
logger.info('Borg Collector API response status was {}'.format(resp.status_code))

# Test for intersecting locations.
intersecting_locations = self.polygon_intersects(locations)
if intersecting_locations:
# Redirect to a view where users can create relationships between referrals.
Expand Down Expand Up @@ -1278,6 +1288,10 @@ def post(self, request, *args, **kwargs):
i.delete()
ref.delete()
messages.success(request, '{0} deleted.'.format(self.model._meta.object_name))
# Call the Borg Collector publish API endpoint to create a manual job
# to update the prs_locations layer.
resp = borgcollector_harvest(self.request)
logger.info('Borg Collector API response status was {}'.format(resp.status_code))
return redirect('site_home')


Expand Down
15 changes: 14 additions & 1 deletion prs2/referral/views_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
from django.views.generic import (
View, ListView, DetailView, CreateView, UpdateView, DeleteView)
import json
import logging
from reversion.revisions import get_for_object
from taggit.models import Tag

from referral.forms import FORMS_MAP, ReferralForm
from referral.utils import is_model_or_string, breadcrumbs_li, get_query, prs_user
from referral.utils import (
is_model_or_string, breadcrumbs_li, get_query, prs_user,
borgcollector_harvest)

logger = logging.getLogger('prs.log')


class PrsObjectList(LoginRequiredMixin, ListView):
Expand Down Expand Up @@ -389,6 +394,14 @@ def delete(self, request, *args, **kwargs):
else:
success_url = self.get_success_url()
obj.delete()

# For Location objects:
# Call the Borg Collector publish API endpoint to create a manual job
# to update the prs_locations layer.
if obj._meta.object_name == 'Location':
resp = borgcollector_harvest(self.request)
logger.info('Borg Collector API response status was {}'.format(resp.status_code))

messages.success(self.request, '{0} has been deleted.'.format(obj))
return HttpResponseRedirect(success_url)

Expand Down
15 changes: 8 additions & 7 deletions prs2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
('Cho Lamb', 'cho.lamb@dpaw.wa.gov.au', '9442 0309'),
)
LOGIN_URL = '/login/'
#LOGIN_REDIRECT_URL = '/'
APPLICATION_TITLE = 'Planning Referral System'
APPLICATION_ACRONYM = 'PRS'
APPLICATION_VERSION_NO = '2.1'
Expand Down Expand Up @@ -177,17 +176,18 @@
os.mkdir(os.path.join(BASE_DIR, 'logs'))
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(message)s'
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'file': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_DIR, 'logs', 'prs.log'),
'formatter': 'verbose',
'formatter': 'simple',
'maxBytes': 1024 * 1024 * 5
},
},
Expand All @@ -196,17 +196,18 @@
'handlers': ['file'],
'level': 'INFO'
},
'log': {
'prs.log': {
'handlers': ['file'],
'level': 'INFO'
},
}
}
DEBUG_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
'format': '%(levelname)s %(asctime)s %(module)s %(message)s'
},
},
'handlers': {
Expand All @@ -223,7 +224,7 @@
'handlers': ['file'],
'level': 'DEBUG'
},
'log': {
'prs.log': {
'handlers': ['file'],
'level': 'DEBUG'
},
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Project requirements
Django==1.9.5
django-extensions==1.6.1
https://static.dpaw.wa.gov.au/static/py/dpaw-utils/dist/dpaw-utils-0.3a3.tar.gz
https://static.dpaw.wa.gov.au/static/py/dpaw-utils/dist/dpaw-utils-0.3a11.tar.gz
django-reversion==1.10.1
django-debug-toolbar==1.4
django-tastypie==0.13.3
Expand Down

0 comments on commit 5ab6e2d

Please sign in to comment.