From c64598a1e7dd48302c4750843caacedd46c83370 Mon Sep 17 00:00:00 2001 From: Josmas Date: Mon, 25 Jun 2012 14:30:08 +0100 Subject: [PATCH] fix for issue #136. adds a test and some refactoring --- lernanta/apps/projects/models.py | 12 ++++++++---- lernanta/apps/projects/tests.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lernanta/apps/projects/models.py b/lernanta/apps/projects/models.py index 34cc7c9f..e1f9539d 100644 --- a/lernanta/apps/projects/models.py +++ b/lernanta/apps/projects/models.py @@ -416,11 +416,16 @@ def get_non_started_next_projects(self, user): else: return Project.objects.none() + @classmethod + def get_projects_excluded_from_listing(cls): + return Project.objects.filter(Q(not_listed=True) + |Q(deleted=True)|Q(archived=True) + |Q(under_development=True)|Q(test=True)).values('id') + @classmethod def get_popular_tags(cls, max_count=10): ct = ContentType.objects.get_for_model(Project) - not_listed = Project.objects.filter( - Q(not_listed=True)|Q(deleted=True)).values('id') + not_listed = Project.get_projects_excluded_from_listing() return GeneralTaggedItem.objects.filter( content_type=ct).exclude(object_id__in=not_listed).values( 'tag__name').annotate(tagged_count=Count('object_id')).order_by( @@ -429,8 +434,7 @@ def get_popular_tags(cls, max_count=10): @classmethod def get_weighted_tags(cls, min_count=2, min_weight=1.0, max_weight=7.0): ct = ContentType.objects.get_for_model(Project) - not_listed = Project.objects.filter( - Q(not_listed=True)|Q(deleted=True)).values('id') + not_listed = Project.get_projects_excluded_from_listing() tags = GeneralTaggedItem.objects.filter( content_type=ct).exclude(object_id__in=not_listed).values( 'tag__name').annotate(tagged_count=Count('object_id')).filter( diff --git a/lernanta/apps/projects/tests.py b/lernanta/apps/projects/tests.py index 473f15fb..87338940 100644 --- a/lernanta/apps/projects/tests.py +++ b/lernanta/apps/projects/tests.py @@ -79,3 +79,33 @@ def test_challenge_creation(self): challenge = Project.objects.get(slug=slug) self.assertEqual(challenge.category, Project.CHALLENGE) self.assertEqual(challenge.duration_hours, 10) + + def test_get_projects_excluded_from_listing(self): + deleted_project = Project(deleted=True, test=False) + deleted_project.save() + + not_listed_project = Project(not_listed=True, test=False) + not_listed_project.save() + + under_dev_project = Project(name="under_dev:default", test=False) + under_dev_project.save() + + archived_project = Project(under_development=False, archived=True) + archived_project.save() + + test_project = Project(under_development=False, test=True) + test_project.save() + + project = Project(name="listed", under_development=False, test=False) + project.save() + + not_listed = Project.get_projects_excluded_from_listing() + not_listed_ids = [] + for project_entry in not_listed: + not_listed_ids.append(project_entry['id']) + + self.assertTrue(deleted_project.id in not_listed_ids) + self.assertTrue(not_listed_project.id in not_listed_ids) + self.assertTrue(under_dev_project.id in not_listed_ids) + self.assertTrue(test_project.id in not_listed_ids) + self.assertFalse(project.id in not_listed_ids)