Skip to content

Commit

Permalink
Merge pull request #410 from ccnmtl/project-list-optimization
Browse files Browse the repository at this point in the history
responses_by_course: fix collaboration/project sorts
  • Loading branch information
thraxil committed Nov 5, 2015
2 parents 36975ab + 74ed978 commit dee24d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mediathread/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Q
import reversion
from threadedcomments.models import ThreadedComment

from mediathread.assetmgr.models import Asset
from mediathread.djangosherd.models import SherdNote
from mediathread.main.course_details import cached_course_is_faculty
import reversion
from structuredcollaboration.models import Collaboration


Expand Down Expand Up @@ -152,17 +152,21 @@ def responses_by_course(self, course, viewer):
projects = Project.objects.filter(
course=course, project_type=PROJECT_TYPE_COMPOSITION)

# filter down to responses only based on the collaboration parent state
collaborations = Collaboration.objects.get_for_object_list(projects)
collaborations = collaborations.filter(_parent__isnull=False)
collaborations = collaborations.order_by('object_pk')

# get all the content objects at once
ids = [int(c.object_pk) for c in collaborations]
responses = Project.objects.filter(id__in=ids)
responses = responses.select_related('author')
responses = list(responses.select_related('author'))
responses.sort(reverse=False, key=lambda p: str(p.id))

visible = []
hidden = []
for idx, r in enumerate(responses):
assert str(r.id) == collaborations[idx].object_pk
if r.can_read(course, viewer, collaborations[idx]):
visible.append(r)
else:
Expand Down
27 changes: 27 additions & 0 deletions mediathread/projects/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def test_responses_by_course(self):
response = ProjectFactory.create(
course=self.sample_course, author=self.student_one,
policy='PrivateEditorsAreOwners', parent=self.assignment)

self.assert_responses_by_course(self.student_one, [response], [])
self.assert_responses_by_course(self.instructor_one, [], [response])
self.assert_responses_by_course(self.student_two, [], [response])
Expand Down Expand Up @@ -267,6 +268,32 @@ def test_responses_by_course(self):
self.assert_responses_by_course(self.student_two,
[response, response2], [])

def test_many_responses_by_course(self):
# additional responses ensure selected collaborations/projects
# don't line-up by default

response1 = ProjectFactory.create(
title='Zeta', course=self.sample_course, author=self.student_three,
date_submitted=datetime.now(), policy='PublicEditorsAreOwners',
parent=self.assignment)

# private response
response2 = ProjectFactory.create(
title='Omega', course=self.sample_course, author=self.student_one,
policy='PrivateEditorsAreOwners', parent=self.assignment)

response3 = ProjectFactory.create(
title='Beta', course=self.sample_course, author=self.student_two,
date_submitted=datetime.now(), policy='PublicEditorsAreOwners',
parent=self.assignment)

self.assert_responses_by_course(self.student_one,
[response1, response2, response3], [])
self.assert_responses_by_course(self.instructor_one,
[response1, response3], [response2])
self.assert_responses_by_course(self.student_two,
[response1, response3], [response2])

def test_project_clean_date_field(self):
try:
self.assignment.due_date = datetime(2012, 3, 13, 0, 0)
Expand Down

0 comments on commit dee24d6

Please sign in to comment.