Commit eed0ce9d authored by Patrick Kimber's avatar Patrick Kimber

Merge branch '3923-replace-report-with-report-app' into 'master'

Add the "report" app to the project and update existing reports

See merge request !6
parents 9810401c 99d90811
Pipeline #55188416 passed with stage
in 2 minutes and 29 seconds
......@@ -13,6 +13,8 @@ then
source $VE/bin/activate
# Set environment variables for this VE
export DATABASE_HOST=""
export DATABASE_USER=""
export DEFAULT_FROM_EMAIL="web@pkimber.net"
export DJANGO_SETTINGS_MODULE="example_enquiry.dev_`id -nu`"
export MAIL_TEMPLATE_TYPE="django"
......@@ -20,7 +22,5 @@ then
export NORECAPTCHA_SITE_KEY="your public key"
export NORECAPTCHA_SECRET_KEY="your private key"
export SECRET_KEY="the_secret_key"
export STRIPE_PUBLISH_KEY="your_stripe_publish_key"
export STRIPE_SECRET_KEY="your_stripe_secret_key"
source .private
fi
# Set environment variables for this VE
source venv-enquiry/bin/activate.fish
set -x DATABASE_HOST ""
set -x DATABASE_USER ""
set -x DEFAULT_FROM_EMAIL "web@pkimber.net"
set -x DJANGO_SETTINGS_MODULE "example_enquiry.dev_patrick"
set -x MAIL_TEMPLATE_TYPE "django"
......@@ -7,6 +9,4 @@ set -x MAILGUN_SERVER_NAME "pkimber.net"
set -x NORECAPTCHA_SECRET_KEY "your private key"
set -x NORECAPTCHA_SITE_KEY "your public key"
set -x SECRET_KEY "the_secret_key"
set -x STRIPE_PUBLISH_KEY "your_stripe_publish_key"
set -x STRIPE_SECRET_KEY "your_stripe_secret_key"
source .private
......@@ -9,5 +9,5 @@ test:
- export MAILGUN_SERVER_NAME="pkimber.net"
- export NORECAPTCHA_SECRET_KEY="your secret key"
- export NORECAPTCHA_SITE_KEY="your site key"
- pip3 install -r requirements/ci.txt -i https://devpi.kbsoftware.co.uk/kb/dev/+simple/
- py.test --create-db --cov
- pip3 install -r requirements/ci.txt -i https://pypi.kbsoftware.co.uk/kb/dev/+simple/
- pytest --create-db --cov
......@@ -10,10 +10,11 @@ class Command(BaseCommand):
help = "Initialise 'enquiry' application"
def handle(self, *args, **options):
self.stdout.write("{}".format(self.help))
Consent.objects.init_consent(
Enquiry.GDPR_CONTACT_SLUG,
"Enquiries",
"Enquiry (Contact) Form",
True,
)
print("Initialised 'enquiry' app...")
self.stdout.write("{} - Complete".format(self.help))
# -*- encoding: utf-8 -*-
from datetime import date
from dateutil.relativedelta import relativedelta
from django.utils import timezone
from enquiry.models import Enquiry
from report.models import ReportError
from report.service import ReportMixin
class EnquiryMonthReport(ReportMixin):
"""Enquiries received.
This report was originally called ``enquiry_month``.
"""
REPORT_SLUG = "enquiry-month"
REPORT_TITLE = "Enquiries received"
def _check_parameters(self, parameters):
if not parameters:
raise ReportError(
"Enquiries received needs year and month parameters"
)
if not "month" in parameters:
raise ReportError("Enquiries received needs a month parameter")
if not "year" in parameters:
raise ReportError("Enquiries received needs a year parameter")
month = int(parameters.get("month"))
year = int(parameters.get("year"))
return year, month
def run_csv_report(self, csv_writer, parameters=None):
count = 0
csv_writer.writerow(
("created", "name", "description", "email", "phone")
)
year, month = self._check_parameters(parameters)
d = date(year, month, 1)
startdate = d + relativedelta(day=1)
enddate = d + relativedelta(months=+1, day=1, days=-1)
enquiries = Enquiry.objects.filter(
created__gte=startdate, created__lte=enddate
).order_by("created")
for x in enquiries:
count = count + 1
csv_writer.writerow(
[
timezone.localtime(x.created).strftime("%d/%m/%Y %H:%M:%S"),
x.name,
x.description,
x.email,
x.phone,
]
)
return count
......@@ -5,7 +5,6 @@ from enquiry.models import Enquiry
class EnquiryFactory(factory.django.DjangoModelFactory):
class Meta:
model = Enquiry
......
# -*- encoding: utf-8 -*-
import csv
import pytest
import pytz
from datetime import datetime
from freezegun import freeze_time
from enquiry.reports import EnquiryMonthReport
from enquiry.tests.factories import EnquiryFactory
from login.tests.factories import UserFactory
from report.models import ReportSchedule, ReportSpecification
@pytest.mark.django_db
def test_enquiry_month():
EnquiryMonthReport().init_report()
freeze_date = datetime(2017, 5, 21, 18, 54, 12, tzinfo=pytz.utc)
with freeze_time(freeze_date):
# report data
EnquiryFactory(
name="Pat", description="Farm", email="pat@test.com", phone="01363"
)
EnquiryFactory(
name="Jon", description="Baker", email="jon@test.com", phone="01837"
)
# schedule report
report_specification = ReportSpecification.objects.get(
slug=EnquiryMonthReport.REPORT_SLUG
)
parameters = {"year": 2017, "month": 5}
report_specification.schedule(UserFactory(), parameters=parameters)
# Run the report and find the schedule::
schedule_pks = ReportSchedule.objects.run_reports()
assert 1 == len(schedule_pks)
schedule_pk = schedule_pks[0]
schedule = ReportSchedule.objects.get(pk=schedule_pk)
# Check the report output::
reader = csv.reader(open(schedule.output_file.path), "excel")
first_row = None
result = []
for row in reader:
if not first_row:
first_row = row
else:
result.append(row)
assert ["created", "name", "description", "email", "phone"] == first_row
assert [
["21/05/2017 19:54:12", "Pat", "Farm", "pat@test.com", "01363"],
["21/05/2017 19:54:12", "Jon", "Baker", "jon@test.com", "01837"],
] == result
......@@ -140,7 +140,15 @@ DJANGO_APPS = (
THIRD_PARTY_APPS = ("captcha", "reversion")
LOCAL_APPS = ("base", "enquiry", "example_enquiry", "gdpr", "login", "mail")
LOCAL_APPS = (
"base",
"enquiry",
"example_enquiry",
"gdpr",
"login",
"mail",
"report",
)
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
......@@ -164,7 +172,9 @@ LOGGING = {
},
"loggers": {
"django.request": {
"handlers": ["mail_admins"], "level": "ERROR", "propagate": True
"handlers": ["mail_admins"],
"level": "ERROR",
"propagate": True,
}
},
}
......
# -*- encoding: utf-8 -*-
from .base import *
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "temp.db",
"USER": "",
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "dev_test_enquiry",
"USER": get_env_variable("DATABASE_USER"),
"PASSWORD": "",
"HOST": "",
"HOST": get_env_variable("DATABASE_HOST"),
"PORT": "",
}
}
# http://docs.celeryproject.org/en/2.5/django/unit-testing.html
CELERY_ALWAYS_EAGER = True
......@@ -125,7 +125,7 @@ def test_create_gdpr_checkbox_not_ticked(client):
response = client.post(reverse("example.enquiry.create"), data)
finally:
del os.environ["RECAPTCHA_TESTING"]
assert HTTPStatus.OK == response.status_code, response.url
assert HTTPStatus.OK == response.status_code
assert "form" in response.context
form = response.context["form"]
assert {"consent_checkbox": ["I am sorry"]} == form.errors
......
bleach==2.1.4
bleach==3.1.0
celery==3.1.26.post2
django-braces==1.13.0
django-recaptcha==1.4.0
django-reversion==3.0.0
django-reversion==3.0.3
django-sendfile==0.3.11
Django==2.1.1
python-dateutil==2.7.3
pytz==2018.5
Django==2.1.7
python-dateutil==2.7.5
pytz==2018.9
......@@ -4,9 +4,11 @@
-e git+https://gitlab.com/kb/gdpr.git#egg=gdpr
-e git+https://gitlab.com/kb/login.git#egg=login
-e git+https://gitlab.com/kb/mail.git#egg=mail
-e git+https://gitlab.com/kb/report.git#egg=report
django-debug-toolbar
factory-boy
freezegun
psycopg2
pytest-cov
pytest-django
pytest-flakes
pytest-pep8
......@@ -4,8 +4,10 @@
-e ../gdpr
-e ../login
-e ../mail
-e ../report
black
factory-boy
freezegun
psycopg2-binary
pytest-cov
pytest-django
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment