Skip to content
Browse files

tags now act as filters rather than static links

  • Loading branch information...
1 parent bda3c65 commit ce3e7ee9d80b0ad4d081fd163f78aea4f4722414 @dirkcuys committed
View
20 lernanta/apps/discover/forms.py
@@ -18,25 +18,7 @@ class ProjectsFilterForm(forms.Form):
archived = forms.BooleanField(required=False, widget=forms.HiddenInput)
under_development = forms.BooleanField(required=False, widget=forms.HiddenInput)
closed_signup = forms.BooleanField(required=False, widget=forms.HiddenInput)
- # Filter Links
- COMMUNITY = 'community'
- SHOWCASE = 'showcase'
- FRESH = 'fresh'
- POPULAR = 'popular'
- UPDATED = 'updated'
- FEATURED_CHOICES = (
- (COMMUNITY, _('Community Picks')),
- (SHOWCASE, _('Showcase')),
- (FRESH, _('Fresh Additions')),
- (POPULAR, _('Popular')),
- (UPDATED, _('Last Updated'))
- )
- featured = forms.ChoiceField(required=False, widget=forms.HiddenInput,
- choices=FEATURED_CHOICES)
- school = forms.ModelChoiceField(required=False, queryset=School.objects.all(),
- widget=forms.HiddenInput)
- tag = forms.CharField(required=False, widget=forms.HiddenInput)
- # Filter Form
+
language = forms.ChoiceField(required=False, choices=[('all', 'All')] + get_active_languages())
reviewed = forms.BooleanField(required=False)
View
31 lernanta/apps/discover/models.py
@@ -21,11 +21,11 @@ def search(keyword, limit = 10):
"""
results = []
results += Project.objects.filter(name__icontains=keyword)[:limit]
- results += get_tagged_projects(keyword)[:limit]
+ results += get_courses_by_tag(keyword)[:limit]
return list(set(results))[:limit]
-def get_listed_projects():
+def get_listed_courses():
""" return all the projects that should be listed """
listed = Project.objects.filter(
not_listed=False,
@@ -39,7 +39,7 @@ def get_listed_projects():
def get_popular_tags(max_count=10):
""" return a list of popular tags """
ct = ContentType.objects.get_for_model(Project)
- listed = list(get_listed_projects().values_list('id', flat=True))
+ listed = list(get_listed_courses().values_list('id', flat=True))
return GeneralTaggedItem.objects.filter(
content_type=ct, object_id__in=listed).values(
'tag__name').annotate(tagged_count=Count('object_id')).order_by(
@@ -48,7 +48,7 @@ def get_popular_tags(max_count=10):
def get_weighted_tags(min_count=2, min_weight=1.0, max_weight=7.0):
ct = ContentType.objects.get_for_model(Project)
- listed = get_listed_projects().values('id')
+ listed = get_listed_courses().values('id')
tags = GeneralTaggedItem.objects.filter(
content_type=ct, object_id__in=listed).values(
'tag__name').annotate(tagged_count=Count('object_id')).filter(
@@ -66,7 +66,17 @@ def get_weighted_tags(min_count=2, min_weight=1.0, max_weight=7.0):
return tags
-def get_tagged_projects(tag_name, projects=None):
+def get_tags_for_courses(courses, exclude=[], max_tags=6):
+ ct = ContentType.objects.get_for_model(Project)
+ course_ids = courses.values('id')
+ tags = GeneralTaggedItem.objects.filter(
+ content_type=ct, object_id__in=course_ids).values(
+ 'tag__name').exclude(tag__name__in=exclude).annotate(
+ tagged_count=Count('object_id'))
+ return tags.order_by('-tagged_count')[:max_tags]
+
+
+def get_courses_by_tag(tag_name, projects=None):
ct = ContentType.objects.get_for_model(Project)
items = GeneralTaggedItem.objects.filter(
content_type=ct, tag__name=tag_name).values(
@@ -76,7 +86,16 @@ def get_tagged_projects(tag_name, projects=None):
return projects.filter(id__in=items)
-def get_course_list(list_name, projects=None):
+def get_courses_by_tags(tag_list, courses=None):
+ "this will return courses that have all the tags in tag_list"
+ if not courses:
+ courses = Project.objects
+ for tag in tag_list:
+ courses = get_courses_by_tag(tag, courses)
+ return courses
+
+
+def get_courses_by_list(list_name, projects=None):
""" return a list of projects
if projects != None, only the courses in projects and the list
will be returned.
View
0 lernanta/apps/discover/templatetags/__init__.py
No changes.
View
10 lernanta/apps/discover/templatetags/learn_tags.py
@@ -0,0 +1,10 @@
+from django import template
+
+register = template.Library()
+
+@register.simple_tag
+def tag_string(filter_tags, tag):
+ filter_list = []
+ filter_list += filter_tags
+ filter_list += [tag]
+ return '+'.join(filter_list)
View
5 lernanta/apps/discover/tests.py
@@ -3,6 +3,7 @@
from users.models import create_profile
from projects.models import Project
+from discover.models import get_listed_courses
from test_utils import TestCase
@@ -24,7 +25,7 @@ def setUp(self):
self.user.set_password(self.test_password)
self.user.save()
- def test_get_listed_projects(self):
+ def test_get_listed_courses(self):
deleted_project = Project(deleted=True, test=False)
deleted_project.save()
@@ -43,7 +44,7 @@ def test_get_listed_projects(self):
project = Project(name="listed", under_development=False, test=False)
project.save()
- listed_projects = Project.get_listed_projects()
+ listed_projects = get_listed_courses()
self.assertFalse(deleted_project in listed_projects)
self.assertFalse(not_listed_project in listed_projects)
View
143 lernanta/apps/discover/views.py
@@ -19,8 +19,10 @@
from discover import forms as project_forms
from discover.models import get_popular_tags
from discover.models import get_weighted_tags
-from discover.models import get_tagged_projects
-from discover.models import get_course_list
+from discover.models import get_courses_by_tag
+from discover.models import get_courses_by_tags
+from discover.models import get_courses_by_list
+from discover.models import get_tags_for_courses
from l10n.urlresolvers import reverse
from relationships.models import Relationship
from schools.models import School
@@ -31,6 +33,40 @@
log = logging.getLogger(__name__)
+def _filter_and_return(request, context, projects, max_count):
+ tag_string = request.GET.get('filter_tags')
+ filter_tags = []
+ if tag_string:
+ filter_tags = tag_string.split(' ')
+ context['filter_tags'] = filter_tags
+
+ if filter_tags:
+ projects = get_courses_by_tags(filter_tags, projects)
+
+ context['popular_tags'] = get_tags_for_courses(projects, filter_tags)
+ context['projects'] = projects
+ context.update(get_pagination_context(request, projects, max_count))
+ if request.is_ajax():
+ projects_html = render_to_string('discover/_learn_projects.html',
+ context, context_instance=RequestContext(request))
+ projects_pagination = render_to_string('discover/_learn_pagination.html',
+ context, context_instance=RequestContext(request))
+ learn_header = render_to_string('discover/_learn_header.html',
+ context, context_instance=RequestContext(request))
+ learn_filters = render_to_string('discover/_learn_filters.html',
+ context, context_instance=RequestContext(request))
+ data = {
+ 'projects_html': projects_html,
+ 'projects_pagination': projects_pagination,
+ 'learn_header': learn_header,
+ 'learn_filters': learn_filters,
+ }
+ json = simplejson.dumps(data)
+ return http.HttpResponse(json, mimetype="application/json")
+ return render_to_response('discover/discover.html', context,
+ context_instance=RequestContext(request))
+
+
def learn(request, max_count=24):
projects = Project.objects.filter(not_listed=False,
deleted=False).order_by('-created_on')
@@ -39,6 +75,7 @@ def learn(request, max_count=24):
language = request.session.get('search_language') or 'all'
get_params['language'] = language
form = project_forms.ProjectsFilterForm(get_params)
+
context = {
'schools': School.objects.all(),
'popular_tags': get_popular_tags(),
@@ -50,20 +87,6 @@ def learn(request, max_count=24):
projects = projects.filter(Q(category=Project.CHALLENGE)
| Q(sign_up__status=Signup.MODERATED)
| Q(sign_up__status=Signup.NON_MODERATED))
-
- featured = form.cleaned_data['featured']
- projects = get_course_list(featured, projects)
- if featured == project_forms.ProjectsFilterForm.SHOWCASE:
- context['learn_showcase'] = True
- elif featured == project_forms.ProjectsFilterForm.COMMUNITY:
- context['learn_community'] = True
- elif featured == project_forms.ProjectsFilterForm.FRESH:
- context['learn_fresh'] = True
-
- tag = form.cleaned_data['tag']
- if tag:
- context['learn_tag'] = tag
- projects = get_tagged_projects(tag, projects)
language = form.cleaned_data['language']
request.session['search_language'] = language
@@ -76,27 +99,7 @@ def learn(request, max_count=24):
accepted=True).values('project_id')
projects = projects.filter(id__in=accepted_reviews)
- context['projects'] = projects
- context.update(get_pagination_context(request, projects, max_count))
- if request.is_ajax():
- projects_html = render_to_string('discover/_learn_projects.html',
- context, context_instance=RequestContext(request))
- projects_pagination = render_to_string('discover/_learn_pagination.html',
- context, context_instance=RequestContext(request))
- learn_header = render_to_string('discover/_learn_header.html',
- context, context_instance=RequestContext(request))
- learn_filters = render_to_string('discover/_learn_filters.html',
- context, context_instance=RequestContext(request))
- data = {
- 'projects_html': projects_html,
- 'projects_pagination': projects_pagination,
- 'learn_header': learn_header,
- 'learn_filters': learn_filters,
- }
- json = simplejson.dumps(data)
- return http.HttpResponse(json, mimetype="application/json")
- return render_to_response('discover/discover.html', context,
- context_instance=RequestContext(request))
+ return _filter_and_return(request, context, projects, max_count)
def schools(request, school_slug, max_count=24):
@@ -130,30 +133,10 @@ def schools(request, school_slug, max_count=24):
request.session['search_language'] = language
if language != 'all':
projects = projects.filter(language__startswith=language)
+
+ return _filter_and_return(request, context, projects, max_count)
- context['projects'] = projects
- context.update(get_pagination_context(request, projects, max_count))
- if request.is_ajax():
- projects_html = render_to_string('discover/_learn_projects.html',
- context, context_instance=RequestContext(request))
- projects_pagination = render_to_string('discover/_learn_pagination.html',
- context, context_instance=RequestContext(request))
- learn_header = render_to_string('discover/_learn_header.html',
- context, context_instance=RequestContext(request))
- learn_filters = render_to_string('discover/_learn_filters.html',
- context, context_instance=RequestContext(request))
- data = {
- 'projects_html': projects_html,
- 'projects_pagination': projects_pagination,
- 'learn_header': learn_header,
- 'learn_filters': learn_filters,
- }
- json = simplejson.dumps(data)
- return http.HttpResponse(json, mimetype="application/json")
- return render_to_response('discover/discover.html', context,
- context_instance=RequestContext(request))
-
-
+
def featured(request, feature, max_count=24):
projects = Project.objects.filter(not_listed=False,
deleted=False).order_by('-created_on')
@@ -163,7 +146,6 @@ def featured(request, feature, max_count=24):
language = request.session.get('search_language') or 'all'
get_params['language'] = language
- get_params['featured'] = feature
form = project_forms.ProjectsFilterForm(get_params)
context = {
@@ -177,45 +159,18 @@ def featured(request, feature, max_count=24):
| Q(sign_up__status=Signup.MODERATED)
| Q(sign_up__status=Signup.NON_MODERATED))
- if form.is_valid():
- featured = form.cleaned_data['featured']
- projects = get_course_list(featured, projects)
- if featured == project_forms.ProjectsFilterForm.SHOWCASE:
- context['learn_showcase'] = True
- elif featured == project_forms.ProjectsFilterForm.COMMUNITY:
- context['learn_community'] = True
- elif featured == project_forms.ProjectsFilterForm.FRESH:
- context['learn_fresh'] = True
+ projects = get_courses_by_list(feature, projects)
+ context['learn_{0}'.format(feature)] = True
if form.is_valid():
language = form.cleaned_data['language']
request.session['search_language'] = language
if language != 'all':
projects = projects.filter(language__startswith=language)
+
+ return _filter_and_return(request, context, projects, max_count)
- context['projects'] = projects
- context.update(get_pagination_context(request, projects, max_count))
- if request.is_ajax():
- projects_html = render_to_string('discover/_learn_projects.html',
- context, context_instance=RequestContext(request))
- projects_pagination = render_to_string('discover/_learn_pagination.html',
- context, context_instance=RequestContext(request))
- learn_header = render_to_string('discover/_learn_header.html',
- context, context_instance=RequestContext(request))
- learn_filters = render_to_string('discover/_learn_filters.html',
- context, context_instance=RequestContext(request))
- data = {
- 'projects_html': projects_html,
- 'projects_pagination': projects_pagination,
- 'learn_header': learn_header,
- 'learn_filters': learn_filters,
- }
- json = simplejson.dumps(data)
- return http.HttpResponse(json, mimetype="application/json")
- return render_to_response('discover/discover.html', context,
- context_instance=RequestContext(request))
-
-
+
def learn_tags(request):
tags = get_weighted_tags()
return render_to_response('discover/discover_tags.html', {'tags': tags},
@@ -260,5 +215,3 @@ def find(request, max_count=24):
return render_to_response('discover/find.html', context,
context_instance=RequestContext(request))
-
-
View
3 lernanta/templates/discover/_learn_filters.html
@@ -1,4 +1,5 @@
{% load l10n_tags %}
+{% load learn_tags %}
<a class="filter {% if not learn_showcase and not learn_community and not learn_fresh and not learn_school and not learn_tag %}active{% endif %}" href="{% locale_url discover_learn %}">{{ _('View All') }}</a>
<h2><i class="icon-star-empty icon-large"></i>{{ _('Featured') }}</h2>
<a class="filter {% if learn_community %}active{% endif %}" href="{% locale_url discover_featured feature='community' %}">
@@ -18,7 +19,7 @@
{% endfor %}
<h2><i class="icon-tags icon-large"></i>{{ _('Tags') }}</h2>
{% for tag in popular_tags %}
- <a class="filter {% if learn_tag == tag.tag__name %}active{% endif %}" href="{% locale_url discover_learn %}?tag={{ tag.tag__name }}">
+ <a class="filter {% if learn_tag == tag.tag__name %}active{% endif %}" href="./?filter_tags={% tag_string filter_tags tag.tag__name %}">
{{ tag.tag__name }} ({{ tag.tagged_count }})
</a>
{% endfor %}

0 comments on commit ce3e7ee

Please sign in to comment.
Something went wrong with that request. Please try again.