Skip to content

Commit

Permalink
Merge pull request #155 from josmas/tags_count
Browse files Browse the repository at this point in the history
fix for issue #136 : tags counting
  • Loading branch information
dirkcuys committed Jun 25, 2012
2 parents 53f4ab8 + c64598a commit 5c67167
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lernanta/apps/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions lernanta/apps/projects/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5c67167

Please sign in to comment.