aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2020-10-24 16:50:16 +0100
committerMatthew Lemon <lemon@matthewlemon.com>2020-10-24 16:50:16 +0100
commitf845b310778e693cc85ca763bb1f9a6b892acbc4 (patch)
tree23e04a144e2c0affda1f4d9f79183580c9e71b59
parent57f241264d15aac3a015dd8a29c994c1ee691932 (diff)
person contact history now filters out private events but the code needs refactoring badly
-rw-r--r--ctrack/organisations/tests/test_views.py63
-rw-r--r--ctrack/organisations/views.py13
2 files changed, 73 insertions, 3 deletions
diff --git a/ctrack/organisations/tests/test_views.py b/ctrack/organisations/tests/test_views.py
index c1eb3ef..c5418f5 100644
--- a/ctrack/organisations/tests/test_views.py
+++ b/ctrack/organisations/tests/test_views.py
@@ -9,7 +9,8 @@ from ctrack.organisations.tests.factories import (
OrganisationFactory,
SingleDateTimeEventFactory,
)
-from ctrack.organisations.views import IncidentReportCreateView, OrganisationDetailView, oes_list
+from ctrack.organisations.views import IncidentReportCreateView, OrganisationDetailView, oes_list, \
+ person_contact_history
from ..utils import filter_private_events
from ..views import OrganisationListView
@@ -70,6 +71,66 @@ def test_meetings_in_organisation_detail_view(user, client, org_with_people):
assert "First Meeting" in html
+# TODO - parameterize this test with the almost identical test below
+def test_private_event_filter_for_contact_history_page(user, client, org_with_people):
+ """
+ In this test we are creating five events, using two different users.
+ Each event will be set to either private or not private. We are testing
+ a function that will only allow private notes belonging to the logged in,
+ or request.user user to be added to the view context. The context is not
+ referred to here - only the utility function under test. The output from
+ that filter function will go forward into the view context.
+ """
+ person = org_with_people.person_set.first()
+ e1_user = SingleDateTimeEventFactory(
+ type_descriptor="MEETING",
+ short_description="First Event with user",
+ private=True,
+ user=user,
+ )
+ e2_user = SingleDateTimeEventFactory(
+ type_descriptor="MEETING",
+ short_description="Second Event with user",
+ private=False,
+ user=user,
+ )
+ e3_user = SingleDateTimeEventFactory(
+ type_descriptor="MEETING",
+ short_description="Third Event with user",
+ private=True,
+ user=user,
+ )
+ e1_user.participants.add(person)
+ e1_user.save()
+ e2_user.participants.add(person)
+ e2_user.save()
+ e3_user.participants.add(person)
+ e3_user.save()
+ user2 = get_user_model().objects.create(username="sam", email="asd@asdsd.com", password="123")
+ e1_user2 = SingleDateTimeEventFactory(
+ type_descriptor="MEETING",
+ short_description="First Event with USER2",
+ private=False,
+ user=user2,
+ )
+ e2_user2 = SingleDateTimeEventFactory(
+ type_descriptor="MEETING",
+ short_description="Second Event with USER2",
+ private=True,
+ user=user2,
+ )
+ e1_user2.participants.add(person)
+ e1_user2.save()
+ e2_user2.participants.add(person)
+ e2_user2.save()
+ client.force_login(user2)
+ response = client.get(reverse("organisations:person_contact_history", kwargs={"person_id": person.pk}))
+ assert response.status_code == 200
+ html = response.content.decode("utf-8")
+ assert "First Event with user" not in html
+ assert "Third Event with user" not in html
+
+
def test_private_event_filter(user, org_with_people):
"""
In this test we are creating five events, using two different users.
diff --git a/ctrack/organisations/views.py b/ctrack/organisations/views.py
index 52d226a..51c8810 100644
--- a/ctrack/organisations/views.py
+++ b/ctrack/organisations/views.py
@@ -33,10 +33,20 @@ def person_contact_history(request, person_id):
"-date"
)
person = get_object_or_404(Person, id=person_id)
+
+ _sdes = [
+ filter_private_events(
+ person.get_single_datetime_events(), request.user
+ )
+ ]
+ _all = list(itertools.chain.from_iterable(_sdes))
+ _all = set(_all)
+ flat_sdes = sorted(_all, key=lambda e: e.date, reverse=True)
+
return render(
request,
"organisations/contact_history.html",
- {"events": events, "person": person},
+ {"events": flat_sdes, "person": person},
)
@@ -125,7 +135,6 @@ class OrganisationDetailView(PermissionRequiredMixin, DetailView):
cafs = org.caf_set.all()
# simple datetime events for org
- # TODO - a note is not getting registered on org detail page after renamed datetime field so fix!
notes = NoteEvent.objects.filter(
user=self.request.user, organisation=self.object
).order_by("-created_date")