Commit dc0a0232 authored by Patrick Kimber's avatar Patrick Kimber

Create 'enquiry' reports

parent 23e22600
Pipeline #54929681 failed with stage
in 37 seconds
# -*- encoding: utf-8 -*-
from datetime import date, datetime
from dateutil.relativedelta import relativedelta
from django.utils import timezone
from enquiry.models import Enquiry
from report.models import ReportError, ReportSpecification
from report.service import ReportMixin
class EnquiryMonthReport(ReportMixin):
"""Enquiries received.
This report was originally called ``enquiry_month``.
REPORT_SLUG = "dash-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
("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
for x in enquiries:
count = count + 1
timezone.localtime(x.created).strftime("%d/%m/%Y %H:%M:%S"),,
return count
# -*- 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
def test_enquiry_month():
freeze_date = datetime(2017, 5, 21, 18, 54, 12, tzinfo=pytz.utc)
with freeze_time(freeze_date):
# report data
name="Pat", description="Farm", email="", phone="01363"
name="Jon", description="Baker", email="", phone="01837"
# schedule report
report_specification = ReportSpecification.objects.get(
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
assert ["created", "name", "description", "email", "phone"] == first_row
assert [
["21/05/2017 19:54:12", "Pat", "Farm", "", "01363"],
["21/05/2017 19:54:12", "Jon", "Baker", "", "01837"],
] == result
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