Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #143 from praekelt/feature/issue-143-update-to-mol…
Browse files Browse the repository at this point in the history
…o-v6

Update to Molo version 6
  • Loading branch information
nathanbegbie authored Feb 13, 2018
2 parents 8a6f7ad + 4507a68 commit f821130
Show file tree
Hide file tree
Showing 20 changed files with 145 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ source =
omit =
*/migrations/*
*/settings/*
nurseconnect/wsgi.py
*/wagtailsearch/*
nurseconnect/wsgi.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pip-log.txt
.coverage
.tox
nosetests.xml
nurseconnect_test.db
.pytest_cache/

# Translations
*.mo
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
sudo: false
language: python
python:
- "2.7"
cache:
- pip
before_install:
Expand All @@ -20,8 +18,10 @@ after_success:
jobs:
include:
- stage: test
- stage: docker
python: "2.7"
- python: "3.6"
- stage: docker
python: "3.6"
sudo: required
dist: trusty
services: [docker]
Expand Down
5 changes: 2 additions & 3 deletions nurseconnect/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ def _clinic_code(self, obj, *args, **kwargs):


def download_as_csv(NurseConnectEndUsersModelAdmin, request, queryset):
response = HttpResponse(content_type="text/csv")
response = HttpResponse(content_type="text/csv", charset='utf-8')
response["Content-Disposition"] = "attachment;filename=export.csv"
writer = csv.writer(response)
writer = csv.writer(response, encoding='utf-8')
field_names = NurseConnectEndUsersModelAdmin.list_display
writer.writerow(field_names)
for obj in queryset:
obj.username = obj.username.encode("utf-8")
obj.date_joined = obj.date_joined.strftime("%Y-%m-%d %H:%M")
writer.writerow(
[getattr(obj, field) for field in field_names]
Expand Down
16 changes: 9 additions & 7 deletions nurseconnect/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,19 @@ def __init__(self, *args, **kwargs):
# Security questions fields are created dynamically.
# This allows any number of security questions to be specified
for index, question in enumerate(questions):
self.fields["question_%s" % index] = forms.CharField(
self.fields["question_{}".format(index)] = forms.CharField(
label=_(str(question)),
widget=forms.TextInput(
attrs={
"max_length": 150,
"class": "Form-input",
"placeholder": "Enter " + str(question).lower(),
"for": "sq" + str(index)
"placeholder": "Enter {}".format(
str(question).lower()),
"for": "sq{}".format(str(index))
}
)
)
self.fields["question_%s" % index].required = (
self.fields["question_{}".format(index)].required = (
profile_settings.show_security_question_fields and
profile_settings.security_questions_required
)
Expand Down Expand Up @@ -361,14 +362,15 @@ def __init__(self, *args, **kwargs):
super(ForgotPasswordForm, self).__init__(*args, **kwargs)

for index, question in enumerate(questions):
self.fields["question_%s" % index] = forms.CharField(
self.fields["question_{}".format(index)] = forms.CharField(
label=_(str(question)),
widget=forms.TextInput(
attrs={
"max_length": 150,
"class": "Form-input",
"placeholder": "Enter " + str(question).lower(),
"for": "sq" + str(index)
"placeholder": "Enter {}".format(
str(question).lower()),
"for": "sq{}".format(str(index))
}
)
)
Expand Down
12 changes: 7 additions & 5 deletions nurseconnect/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from os.path import abspath, dirname, join
from os import environ
import sys
from django.conf import global_settings, locale
import django.conf.locale
from django.conf import global_settings
from django.utils.translation import ugettext_lazy as _
import dj_database_url # noqa
import djcelery
Expand Down Expand Up @@ -61,6 +62,7 @@
"molo.yourwords",
"django_comments",
"molo.commenting",
"wagtail_personalisation",

'wagtail.wagtailcore',
'wagtail.wagtailadmin',
Expand Down Expand Up @@ -168,7 +170,7 @@

# SQLite (simplest install)
DATABASES = {"default": dj_database_url.config(
default="sqlite:///%s" % (join(PROJECT_ROOT, "db.sqlite3"),))}
default="sqlite:///{}".format(join(PROJECT_ROOT, "db.sqlite3"),))}

# PostgreSQL (Recommended, but requires the psycopg2 library and Postgresql
# development headers)
Expand Down Expand Up @@ -277,7 +279,7 @@
}
}

locale.LANG_INFO = dict(locale.LANG_INFO.items() + EXTRA_LANG_INFO.items())
django.conf.locale.LANG_INFO.update(EXTRA_LANG_INFO)

LOCALE_PATHS = [
join(PROJECT_ROOT, "locale"),
Expand Down Expand Up @@ -416,8 +418,8 @@
AWS_STORAGE_BUCKET_NAME = environ.get('AWS_STORAGE_BUCKET_NAME', '')
AWS_ACCESS_KEY_ID = environ.get('AWS_ACCESS_KEY_ID', '')
AWS_SECRET_ACCESS_KEY = environ.get('AWS_SECRET_ACCESS_KEY', '')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_CUSTOM_DOMAIN = '{}.s3.amazonaws.com'.format(AWS_STORAGE_BUCKET_NAME)

if AWS_STORAGE_BUCKET_NAME and AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:
MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
MEDIA_URL = "https://{}/".format(AWS_S3_CUSTOM_DOMAIN)
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
8 changes: 3 additions & 5 deletions nurseconnect/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.contrib.auth.models import User
from django.core.management import call_command

from nurseconnect.utils import get_period_date_format
from nurseconnect.services import get_clinic_code

from wagtail.wagtailsearch.backends.db import DatabaseSearchBackend
Expand Down Expand Up @@ -53,8 +54,7 @@ def nurses_registered():
"dataValues": [
{
"dataElement": settings.JEMBI["num_nurses"]["dataElement"],
"period":
str(datetime.now().year) + "%02d" % datetime.now().month,
"period": get_period_date_format(),
"value": str(num_nurses)
},
]
Expand Down Expand Up @@ -83,9 +83,7 @@ def nurses_registered_per_clinic():
{
"dataElement": settings.JEMBI["nurses_per_facility"][
"dataElement"],
"period":
str(datetime.now().year) +
"%02d" % datetime.now().month,
"period": get_period_date_format(),
"orgUnit": clinic[1],
"value": str(v)
},
Expand Down
2 changes: 1 addition & 1 deletion nurseconnect/templates/profiles/editprofile.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% csrf_token %}
{% if settings_form.errors %}
<p class="error errorlist">
{% for key, errors in settings_form.non_field_errors.iteritems %}
{% for key, errors in settings_form.non_field_errors.items %}
{% for error in errors %}
{% trans error %}<br/>
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions nurseconnect/tests/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def test_passwords_can_be_changed(self):
},
follow=True
)
self.assertEqual(response.request["PATH_INFO"], "/view/myprofile/")
self.assertContains(response, "Password successfully changed!")

def test_unmatching_passwords_raises_error(self):
Expand Down
48 changes: 28 additions & 20 deletions nurseconnect/tests/test_services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import responses

from django.test import TestCase, override_settings
Expand Down Expand Up @@ -54,42 +55,49 @@
@override_settings(CLINIC_CODE_API=FAKE_URL)
class ServicesTestCase(TestCase):

def setUp(self):
logging.disable(logging.CRITICAL)
self.clinic_code = 123456
self.complete_url = "{}?criteria=value:{}".format(FAKE_URL,
self.clinic_code)

def tearDown(self):
logging.disable(logging.NOTSET)

@override_settings(FAKE_CLINIC_CODE_VALIDATION=True, DEBUG=True)
def test_get_clinic_code_returns_fake_data(self):
clinic_code = 123456

self.assertEqual(
get_clinic_code(clinic_code),
get_clinic_code(self.clinic_code),
[0, 1, "fake_clinic_name"]
)

@responses.activate
def test_get_clinic_code_returns_none(self):
clinic_code = 123456
complete_url = "{}?criteria=value:{}".format(FAKE_URL, clinic_code)

responses.add(responses.GET, FAKE_URL, status=400)
self.assertEqual(get_clinic_code(clinic_code), None)
def test_get_clinic_code_returns_none_from_400(self):
responses.add(responses.GET, self.complete_url, status=400)
self.assertEqual(get_clinic_code(self.clinic_code), None)

responses.add(responses.GET, FAKE_URL, status=200)
self.assertEqual(get_clinic_code(clinic_code), None)
@responses.activate
def test_get_clinic_code_returns_none_no_json(self):
responses.add(responses.GET, self.complete_url, status=200)
self.assertEqual(get_clinic_code(self.clinic_code), None)

responses.add(responses.GET, complete_url,
@responses.activate
def test_get_clinic_code_returns_none_not_found(self):
responses.add(responses.GET, self.complete_url,
json={'error': 'not found'}, status=200)
self.assertEqual(get_clinic_code(clinic_code), None)
self.assertEqual(get_clinic_code(self.clinic_code), None)

responses.add(responses.GET, complete_url,
@responses.activate
def test_get_clinic_code_returns_none_invalid_response(self):
responses.add(responses.GET, self.complete_url,
json=FAKE_ENDPOINT_INVALID_PARAM_RESPONSE,
status=200)
self.assertEqual(get_clinic_code(clinic_code), None)
self.assertEqual(get_clinic_code(self.clinic_code), None)

@responses.activate
def test_get_clinic_code_returns_values(self):
clinic_code = 123456
complete_url = "{}?criteria=value:{}".format(FAKE_URL, clinic_code)

responses.add(responses.GET, complete_url,
responses.add(responses.GET, self.complete_url,
json=FAKE_ENDPOINT_RESPONSE, status=200)
self.assertEqual(
get_clinic_code(clinic_code),
get_clinic_code(self.clinic_code),
FAKE_ENDPOINT_RESPONSE["rows"][0])
18 changes: 14 additions & 4 deletions nurseconnect/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

import json
import responses

from django.conf import settings
Expand All @@ -8,6 +9,8 @@

from molo.core.tests.base import MoloTestCaseMixin
from .constants import FACILITIES

from nurseconnect.utils import get_period_date_format
from nurseconnect import tasks


Expand Down Expand Up @@ -39,12 +42,19 @@ def test_num_nurses_sent_successfully(self):
body="<Response [200]>", status=200,)

tasks.nurses_registered()
date = str(datetime.now().year) + "%02d" % datetime.now().month
date = get_period_date_format()

expected_response_body = {
"dataValues": [{
"dataElement": "uaQ8nZ2z8sl",
"period": date,
"value": "2"
}]
}

self.assertEqual(
responses.calls[-1].request.body,
'{"dataValues": [{"dataElement": '
'"uaQ8nZ2z8sl", "period": "' + date + '", "value": "2"}]}'
json.loads(responses.calls[-1].request.body),
expected_response_body
)
self.assertEqual(responses.calls[-1].response.status_code, 200)

Expand Down
22 changes: 22 additions & 0 deletions nurseconnect/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from freezegun import freeze_time

from django.test import TestCase

from nurseconnect.utils import get_period_date_format


class UtilsTestCase(TestCase):

@freeze_time("2018-02-01")
def test_get_period_date_format_1(self):
self.assertEqual(
get_period_date_format(),
"201802"
)

@freeze_time("2012-12-01")
def test_get_period_date_format_2(self):
self.assertEqual(
get_period_date_format(),
"201212"
)
16 changes: 7 additions & 9 deletions nurseconnect/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.auth.decorators import login_required
Expand All @@ -16,19 +16,17 @@
from nurseconnect import forms, views
from nurseconnect.forms import NurseconnectAuthenticationForm

urlpatterns = []

# implement CAS URLs in a production setting
if settings.ENABLE_SSO: # pragma: no cover
urlpatterns = patterns(
"",
urlpatterns += [
url(r"^admin/login/", "django_cas_ng.views.login"),
url(r"^admin/logout/", "django_cas_ng.views.logout"),
url(r"^admin/callback/", "django_cas_ng.views.callback"),
)
else:
urlpatterns = patterns("", )
]

urlpatterns += patterns(
"",
urlpatterns += [
url(r"^django-admin/", include(admin.site.urls)),
url(r"^admin/", include(wagtailadmin_urls)),
url(r"^documents/", include(wagtaildocs_urls)),
Expand Down Expand Up @@ -146,7 +144,7 @@
url(r'', include('django_comments.urls')),
url(r"", include("molo.core.urls")),
url(r"", include(wagtail_urls)),
)
]

if settings.DEBUG: # pragma: no cover
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
Expand Down
12 changes: 12 additions & 0 deletions nurseconnect/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import datetime


def get_period_date_format():
"""
Returns the current year and month concatenated into a string
e.g.
datetime.date(2018, 2, 1) -> 201802
datetime.date(2012, 11, 29) -> 201211
"""
return datetime.date.today().strftime('%Y%m')
Loading

0 comments on commit f821130

Please sign in to comment.