Skip to content

Commit

Permalink
Merge pull request #676 from akvo/hotfix/673_pages_filtering
Browse files Browse the repository at this point in the history
[#673] Fix filter drop-downs for Pages site using keywords
  • Loading branch information
KasperBrandt committed Jul 9, 2014
2 parents cd3afda + 746a780 commit 7c182eb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
49 changes: 29 additions & 20 deletions akvo/rsr/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ def filter_by_budget_range(qs, what):
currency = django_filters.ChoiceFilter()

def __init__(self, *args, **kwargs):
organisation_id = kwargs.pop('organisation_id', None)
request = kwargs.pop('request', None)
super(ProjectFilterSet, self).__init__(*args, **kwargs)
projects = kwargs.pop('queryset', None)

self.filters['title'].field.widget.input_type = 'search'
self.filters['title'].field.widget.attrs = {'results': '5', 'autosave': 'project_search', 'placeholder': _(u'Project title or subtitle')}
Expand All @@ -83,29 +84,37 @@ def __init__(self, *args, **kwargs):
choices.extend(list(CONTINENTS))
self.filters['continent'].extra.update({'choices': choices})

self.filters['locations__country'].extra.update({'empty_label': _(u'All countries')})

if organisation_id:
org = Organisation.objects.get(pk=organisation_id)

countries = org.countries_where_active()

continents = [('', _(u'All continents'))]
choices = []
for country in countries:
if not country.continent_code in [code[0] for code in choices]:
choices.append((country.continent_code, _(country.continent)))
continents.extend(sorted(choices))

self.filters['continent'].extra.update({'choices': continents})
self.filters['locations__country'].extra.update({'queryset': countries})

qs = org.partners()
# The request is sent when the call is made from a partner site view
if request:
partner_site = request.partner_site
organisation = partner_site.organisation
# If the partner site is "normal"
if partner_site.partner_projects:
countries = organisation.countries_where_active()

continents = [('', _(u'Active continents'))]
choices = []
for country in countries:
if not country.continent_code in [code[0] for code in choices]:
choices.append((country.continent_code, _(country.continent)))
continents.extend(sorted(choices))

self.filters['continent'].extra.update({'choices': continents})
self.filters['locations__country'].extra.update({'queryset': countries})
self.filters['locations__country'].extra.update({'empty_label': _(u'Active countries')})

organisations = organisation.partners()
# Keyword driven partner site.
# Performance issues preclude filtering of filter drop-downs right now
else:
organisations = Organisation.objects.all()
self.filters['locations__country'].extra.update({'empty_label': _(u'All countries')})
self.filters['organisation'].extra.update({'empty_label': _(u'All partners')})
else:
qs = Organisation.objects.all()
organisations = Organisation.objects.all()
self.filters['organisation'].extra.update({'empty_label': _(u'All organisations')})
self.filters['organisation'].extra.update({'queryset': qs})
self.filters['organisation'].extra.update({'queryset': organisations})

self.filters['andor'].field_class = BooleanField
self.filters['status'].field_class = CheckboxMultipleChoiceField
Expand Down
1 change: 1 addition & 0 deletions akvo/rsr/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def process_request(self, request, cname_domain=False, partner_site=None):
)
request.akvoapp_root_url = "http://%s" % request.app_domain
request.organisation_id = partner_site.organisation.id
request.partner_site = partner_site
request.default_language = partner_site.default_language

request.domain_url = "http://%s" % settings.RSR_DOMAIN
Expand Down
21 changes: 15 additions & 6 deletions akvo/rsr/templatetags/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def global_organisation_map(width, height, dynamic='dynamic'):


@register.inclusion_tag('inclusion_tags/maps.html')
def organisation_projects_map(organisation_id, width, height, dynamic='dynamic'):
def organisation_projects_map(projects, width, height, dynamic='dynamic'):
"""
params:
organisation_id: id of organisation.
Expand All @@ -187,18 +187,27 @@ def organisation_projects_map(organisation_id, width, height, dynamic='dynamic')

locations = []

projects = Project.objects.filter(partnerships__organisation=organisation_id).active()
# projects = Project.objects.filter(partnerships__organisation=organisation_id).active()

for project in projects:
proj_locations = ProjectLocation.objects.filter(location_target=project)
for location in proj_locations:
try:
thumbnail = project.current_image.extra_thumbnails['map_thumb'].absolute_url
locations.append([location.latitude,
location.longitude,
[str(project.pk),project.title.encode('utf8'), thumbnail, 'project']])
except:
pass
thumbnail = ""
locations.append(
[
location.latitude,
location.longitude,
[
str(project.pk),
project.title.encode('utf8'),
thumbnail,
'project'
]
]
)

template_context = {
'map_id': map_id,
Expand Down
2 changes: 1 addition & 1 deletion akvo/rsr/views_partner_sites/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,5 @@ def get_queryset(self):
return ProjectFilterSet(
self.request.GET.copy() or None,
queryset=projects,
organisation_id=self.request.organisation_id
request=self.request
)
2 changes: 1 addition & 1 deletion akvo/templates/partner_sites/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</div>
</div>
<div class="two_col_right whitebox">
{% organisation_projects_map request.organisation_id 470 250 'dynamic' %}
{% organisation_projects_map sorted_projects 470 250 'dynamic' %}
</div>
<div class="clear"></div>
<h1 class="marg_top20">{% trans "Find projects" %}</h1>
Expand Down

0 comments on commit 7c182eb

Please sign in to comment.