Skip to content

Commit

Permalink
Merge pull request #2183 from pbanaszkiewicz/feature/2072-2138-counti…
Browse files Browse the repository at this point in the history
…ng-roles

[#2072][#2138] Counting person's roles correctly
  • Loading branch information
pbanaszkiewicz committed May 29, 2022
2 parents 2c930c1 + 36b64c3 commit c5135b5
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 301 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -47,3 +47,6 @@ docker/redis/*

# mkdocs output folder
site/

# environment variables
.env
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -27,11 +27,12 @@ fast_test_fail:
dev_database :
${MANAGE} reset_db
${MANAGE} migrate
${MANAGE} loaddata amy/workshops/fixtures/badges.json
${MANAGE} loaddata amy/autoemails/fixtures/templates_triggers.json
${MANAGE} loaddata amy/communityroles/fixtures/inactivations.json
${MANAGE} loaddata amy/communityroles/fixtures/configs.json
${MANAGE} create_superuser
${MANAGE} fake_database
${MANAGE} loaddata amy/communityroles/fixtures/configs.json
${MANAGE} createinitialrevisions

## node_modules : install front-end dependencies using Yarn
Expand Down
10 changes: 6 additions & 4 deletions amy/dashboard/tests/test_instructor_recruitment_views.py
Expand Up @@ -148,9 +148,11 @@ def test_get_queryset(self):
view = UpcomingTeachingOpportunitiesList(request=request)
qs = view.get_queryset()
# Assert
self.assertEqual(list(qs), [recruitment1, recruitment2])
self.assertEqual(set(qs), {recruitment1, recruitment2})
# `person_signup` is an additional attribute created using `Prefetch()`
self.assertEqual(list(qs[0].person_signup), [signup])
self.assertTrue(
any(recruitment.person_signup == [signup] for recruitment in qs)
)

def test_get_context_data(self):
"""Context data is extended only with person object, but it includes pre-counted
Expand Down Expand Up @@ -187,7 +189,7 @@ def test_get_context_data(self):
with self.assertNumQueries(2):
data = view.get_context_data(object_list=[])
# Assert
self.assertEqual(data["person"].num_taught, 1)
self.assertEqual(data["person"].num_instructor, 1)
self.assertEqual(data["person"].num_supporting, 2)
self.assertEqual(data["person"].num_helper, 3)
self.assertEqual(list(data["person_instructor_tasks_slugs"]), [event1.slug])
Expand Down Expand Up @@ -282,7 +284,7 @@ def test_get_context_data(self):
# Assert
self.assertEqual(data["title"], f"Signup for workshop {event}")
# `num_*` are special fields added through `QuerySet.annotate`
self.assertEqual(data["person"].num_taught, 1)
self.assertEqual(data["person"].num_instructor, 1)
self.assertEqual(data["person"].num_supporting, 0)
self.assertEqual(data["person"].num_helper, 0)

Expand Down
42 changes: 2 additions & 40 deletions amy/dashboard/views.py
Expand Up @@ -324,26 +324,7 @@ def get_context_data(self, **kwargs):

# person details with tasks counted
context["person"] = (
Person.objects.annotate(
num_taught=Count(
Case(
When(task__role__name="instructor", then=Value(1)),
output_field=IntegerField(),
)
),
num_supporting=Count(
Case(
When(task__role__name="supporting-instructor", then=Value(1)),
output_field=IntegerField(),
)
),
num_helper=Count(
Case(
When(task__role__name="helper", then=Value(1)),
output_field=IntegerField(),
)
),
)
Person.objects.annotate_with_role_count()
.select_related("airport")
.get(pk=self.request.user.pk)
)
Expand Down Expand Up @@ -404,26 +385,7 @@ def get_context_data(self, **kwargs):

# person details with tasks counted
context["person"] = (
Person.objects.annotate(
num_taught=Count(
Case(
When(task__role__name="instructor", then=Value(1)),
output_field=IntegerField(),
)
),
num_supporting=Count(
Case(
When(task__role__name="supporting-instructor", then=Value(1)),
output_field=IntegerField(),
)
),
num_helper=Count(
Case(
When(task__role__name="helper", then=Value(1)),
output_field=IntegerField(),
)
),
)
Person.objects.annotate_with_role_count()
.select_related("airport")
.get(pk=self.request.user.pk)
)
Expand Down
2 changes: 1 addition & 1 deletion amy/templates/includes/instructor_profile_snapshot.html
Expand Up @@ -10,7 +10,7 @@ <h5 class="card-title">Profile snapshot</h5>
<ul>
<li><strong>Helper:</strong> {{ person.num_helper }} time{{ person.num_helper|pluralize }}</li>
<li><strong>Supporting Instructor:</strong> {{ person.num_supporting }} time{{ person.num_supporting|pluralize }}</li>
<li><strong>Instructor:</strong> {{ person.num_taught }} time{{ person.num_taught|pluralize }}</li>
<li><strong>Instructor:</strong> {{ person.num_instructor }} time{{ person.num_instructor|pluralize }}</li>
</ul>
</li>
</ul>
Expand Down
108 changes: 0 additions & 108 deletions amy/templates/reports/all_activity_over_time.html

This file was deleted.

52 changes: 0 additions & 52 deletions amy/templates/reports/instructors_by_date.html

This file was deleted.

3 changes: 2 additions & 1 deletion amy/templates/workshops/person.html
Expand Up @@ -152,7 +152,8 @@
{% endwith %}
<p>Summary:</p>
<ul>
<li>Instructor: {{ person.num_taught }} times</li>
<li>Instructor: {{ person.num_instructor }} times</li>
<li>Trainer: {{ person.num_trainer }} times</li>
<li>Supporting Instructor: {{ person.num_supporting }} times</li>
<li>Helper: {{ person.num_helper }} times</li>
<li>Learner: {{ person.num_learner }} times</li>
Expand Down
4 changes: 2 additions & 2 deletions amy/templates/workshops/workshop_staff.html
Expand Up @@ -19,7 +19,7 @@ <h3>Filter</h3>
title="Only Instructor or Trainer badges shown."></i></th>
<th>Has Trainer badge</th>
<th>Person</th>
<th>Taught</th>
<th>Taught (inc. TTT)</th>
<th>Trainee</th>
<th>Airport</th>
<th>Country</th>
Expand All @@ -34,7 +34,7 @@ <h3>Filter</h3>
{% empty %}&mdash;{% endfor %}</td>
<td>{{ p.is_trainer|yesno }}</td>
<td><a href="{{ p.get_absolute_url }}">{{ p.full_name }}</a>{% if p.email and p.may_contact %} &lt;{{ p.email|urlize }}&gt;{% endif %}</td>
<td>{{ p.num_taught }}</td>
<td>{{ p.num_instructor|add:p.num_trainer }}</td>
<td>{{ p.is_trainee|yesno }}</td>
<td>{% if p.airport %}<a href="{{ p.airport.get_absolute_url }}">{{ p.airport }}</a>{% else %}&mdash;{% endif %}</td>
<td>{% if p.country %}{{ p.country.name }}{% else %}&mdash;{% endif %}</td>
Expand Down

0 comments on commit c5135b5

Please sign in to comment.