Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
bug 650242,650917 fixing some sites snafus
  • Loading branch information
davedash committed Apr 21, 2011
1 parent 08902a7 commit db3b6b4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 79 deletions.
55 changes: 24 additions & 31 deletions apps/website_issues/forms.py
Expand Up @@ -8,8 +8,14 @@

from input import PLATFORMS, PRODUCTS, FIREFOX, MOBILE
from input.fields import SearchInput
from search.forms import (SENTIMENT_CHOICES, PLATFORM_CHOICES, PROD_CHOICES,
VERSION_CHOICES)
from search.forms import SENTIMENT_CHOICES, PLATFORM_CHOICES, PROD_CHOICES


VERSION_CHOICES = {
FIREFOX: [(v, v) for v in
FIREFOX.beta_versions + FIREFOX.release_versions],
MOBILE: [(v, v) for v in MOBILE.beta_versions + MOBILE.release_versions],
}

FieldDef = namedtuple("FieldDef", "default field keys")

Expand All @@ -32,9 +38,6 @@ def field_def(FieldType, default, widget=HiddenInput, choices=None):
)
),
"sentiment": field_def(ChoiceField, "", choices=SENTIMENT_CHOICES),
"version": field_def(ChoiceField,
VERSION_CHOICES[FIREFOX][0][0],
choices=VERSION_CHOICES[FIREFOX]),
"product": field_def(ChoiceField, FIREFOX.short, choices=PROD_CHOICES),
"platform": field_def(ChoiceField, "", choices=PLATFORM_CHOICES),
"show_one_offs": field_def(BooleanField, False),
Expand All @@ -49,41 +52,32 @@ class WebsiteIssuesSearchForm(forms.Form):
# Fields that are submitted on text search:
q = FIELD_DEFS['q'].field
sentiment = FIELD_DEFS['sentiment'].field
product = FIELD_DEFS['product'].field
product = forms.ChoiceField(choices=PROD_CHOICES, label=_lazy('Product:'),
initial=FIREFOX.short, required=False)
platform = FIELD_DEFS['platform'].field
version = FIELD_DEFS['version'].field
version = forms.ChoiceField(required=False, label=_lazy('Version:'),
choices=VERSION_CHOICES[FIREFOX])
show_one_offs = FIELD_DEFS['show_one_offs'].field

# These fields are reset on search:
page = FIELD_DEFS['page'].field
site = FIELD_DEFS['site'].field
cluster = FIELD_DEFS['cluster'].field

# TODO(michaelk) This is similar, but not identical to what happens with
# themes and search. During refactoring we probably want to
# unify this somewhat.
def __init__(self, *args, **kwargs):
"""Set available products/versions based on selected channel/product"""
super(WebsiteIssuesSearchForm, self).__init__(*args, **kwargs)
choices = VERSION_CHOICES
product_choices = \
[(p.short, p.pretty) for p in (FIREFOX, MOBILE) if choices[p]]
self.fields['product'].choices = product_choices
FIELD_DEFS['product'] = field_def(ChoiceField, product_choices[0][0],
choices=product_choices)
product = self.data.get('product', FIREFOX)
try:
if not choices[product]:
product = FIREFOX
except KeyError:
product = FIREFOX
version_choices = choices[product]
self.fields['version'].choices = version_choices
self.fields['version'].initial = version_choices[0][0]
FIELD_DEFS['version'] = field_def(ChoiceField,
version_choices[0][0],
choices=version_choices)
self.cleaned_data = {}
self.fields['version'].choices = VERSION_CHOICES[FIREFOX]
picked = None
if self.is_bound:
try:
picked = self.fields['product'].clean(self.data.get('product'))
except forms.ValidationError:
pass
if (picked == MOBILE.short):
# We default to Firefox. Only change if this is the mobile site.
self.fields['product'].initial = MOBILE.short
self.fields['version'].choices = VERSION_CHOICES[MOBILE]

def clean(self):
cleaned = super(WebsiteIssuesSearchForm, self).clean()
Expand All @@ -108,8 +102,7 @@ def clean(self):
cleaned['platform'] = FIELD_DEFS['platform'].default

if not cleaned.get('version'):
product = cleaned.get('product', FIREFOX)
cleaned['version'] = VERSION_CHOICES[product][0][0]
cleaned['version'] = VERSION_CHOICES[FIREFOX][0][0]

if cleaned.get('page') is not None:
cleaned['page'] = max(1, int(cleaned['page']))
Expand Down
39 changes: 6 additions & 33 deletions apps/website_issues/helpers.py
@@ -1,48 +1,21 @@
import utils

from django.utils.encoding import smart_unicode
from django.utils.http import urlencode

import jinja2
from jingo import register

from input.helpers import urlparams
from input.urlresolvers import reverse
from .forms import FIELD_DEFS


@register.function
@jinja2.contextfunction
def sites_url(context, form, url=None, **kwargs):
"""Return the current form values as URL parameters.
Values are taken from the given form and can be overriden using kwargs.
This is used to modify parts of a query without losing search context.
The 'page' is always reset if not explicitly given.
Parameters are only included if the values differ from the default.
"""
parameters = form.cleaned_data.copy()
# page is reset on every change of search
for name in form.cleaned_data.keys():
if name == 'page' or parameters[name] == FIELD_DEFS[name].default:
del parameters[name]
for name, value in kwargs.iteritems():
parameters[name] = value

if url or context.get('site'):
# single site URL
_baseurl = url or context['site'].url
_parsed = utils.urlparse(_baseurl)
parts = [reverse('single_site', args=[_parsed.scheme, _parsed.netloc])]
if 'q' in parameters:
del parameters['q']
else:
# regular sites search URL
parts = [reverse("website_issues")]

if len(parameters):
parts.extend(["?", urlencode(parameters)])

return ''.join(parts)
def sites_url(context, url, **kw):
_parsed = utils.urlparse(url)
base_url = reverse('single_site', args=[_parsed.scheme, _parsed.netloc])
base_url = base_url + '?' + context['request'].META['QUERY_STRING']
return urlparams(base_url, page=None)


@register.filter
Expand Down

This file was deleted.

Expand Up @@ -2,15 +2,16 @@
<div class="pager">
{% with link_txt = _('&laquo; Previous Page')|safe %}
{% if page.has_previous() %}
<a class="prev" href="{{ sites_url(form, page=page.previous_page_number()) }}">{{ link_txt }}</a>
<a class="prev"
href="{{ base_url|urlparams(page=page.previous_page_number()) }}">{{ link_txt }}</a>
{% else %}
<span class="prev inactive">{{ link_txt }}</span>
{% endif %}
{% endwith %}

{% with link_txt = _('Next Page &raquo;')|safe %}
{% if page.has_next() %}
<a class="next" href="{{ sites_url(form, page=page.next_page_number()) }}">{{ link_txt }}</a>
<a class="next" href="{{ base_url|urlparams(page=page.next_page_number()) }}">{{ link_txt }}</a>
{% else %}
<span class="next inactive">{{ link_txt }}</span>
{% endif %}
Expand Down
Expand Up @@ -20,7 +20,7 @@ <h2>{{ _('Sites') }}</h2>
{{ fieldbox('q') }}
{# make hidden input fields for settings to keep on text-search #}
{{ fieldbox('sentiment') }}
{{ fieldbox('version') }}
<input type="hidden" name="version" value="{{ version }}" />
{{ fieldbox('show_one_offs') }}
</form>

Expand All @@ -35,24 +35,25 @@ <h3>{{ _('No results found!') }}</h3>
{% for site in sites %}
<li class="site">
<p class="name">
<a href="{{ sites_url(form, url=site.url) }}">{{ site.url|strip_protocol }}</a>
<a href="{{ sites_url(url=site.url) }}">
{{ site.url|strip_protocol }}</a>
{% if site.url|protocol == 'https' %}
<span class="ssl">(SSL)</span>
{% endif %}
</p>
<ul class="meta">
<li><a href="{{ sites_url(form, url=site.url)}}">
<li><a href="{{ sites_url(url=site.url) }}">
{% trans num=site.size, count=site.size|numberfmt %}
{{ count }} message
{% pluralize %}
{{ count }} messages
{% endtrans %}
</a></li>
{% if request.channel == 'beta' and site.positive is none %}
{% if not site.positive %}
{% with praise_perc=(site.praise_count/site.size*100)|int %}
<li><a href="{{ sites_url(form, sentiment='happy', url=site.url) }}">{{
<li><a href="{{ sites_url(url=site.url, sentiment='happy') }}">{{
_('{percentage}% praise')|f(percentage=praise_perc) }}</a></li>
<li><a href="{{ sites_url(form, sentiment='sad', url=site.url) }}">{{
<li><a href="{{ sites_url(url=site.url, sentiment='sad') }}">{{
_('{percentage}% issues')|f(percentage=100-praise_perc) }}</a></li>
{% endwith %}
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions apps/website_issues/templates/website_issues/sites.html
Expand Up @@ -10,7 +10,7 @@
{% macro button(form, name, value='', label='', title='', classes='') %}
{% set sel = (form.cleaned_data[name] == value) %}
<a class="{{ 'selected' if sel else ''}} {{ classes }}"
href="{{ sites_url(form,**{name: value}) }}">{{ label }}</a>
href="{{ base_url|urlparams(**{name: value, 'page': None}) }}">{{ label }}</a>
{% endmacro %}

{% block content %}
Expand Down Expand Up @@ -72,7 +72,7 @@ <h3>{{ _('Platforms') }}</h3>
<h3>{{ _('Uncommon Sites') }}</h3>
<ul>
{% for site in one_offs %}
<li><a href="{{ sites_url(form, url=site.url) }}">{{ site.url|strip_protocol }}</a></li>
<li><a href="{{ sites_url(url=site.url) }}">{{ site.url|strip_protocol }}</a></li>
{% endfor %}
<li class="more">{{ button(form, 'show_one_offs', True, label=_('More sites...')) }}</li>
</ul>
Expand Down
5 changes: 5 additions & 0 deletions apps/website_issues/views.py
Expand Up @@ -6,6 +6,7 @@
import jingo

from input import PLATFORMS, PRODUCTS
from input.urlresolvers import reverse
from input.decorators import cache_page
from feedback.models import Opinion

Expand Down Expand Up @@ -93,6 +94,8 @@ def website_issues(request):
one_offs, _ = _fetch_summaries(form, count=settings.TRENDS_COUNT,
one_offs=True)
data = dict(_common_data(form))
data['base_url'] = (reverse('website_issues') + '?' +
request.META['QUERY_STRING'])
data.update({"page": page, "sites": sites, "one_offs": one_offs})
return jingo.render(request, 'website_issues/sites.html', data)

Expand Down Expand Up @@ -124,6 +127,8 @@ def single_site(request, protocol, url_):
page = pager.page(pager.num_pages)

data = dict(_common_data(form))
data['base_url'] = (reverse('single_site', args=[protocol, url_]) + '?' +
request.META['QUERY_STRING'])
data.update({"page": page, "site": site})
return jingo.render(request, 'website_issues/sites.html', data)

Expand Down

0 comments on commit db3b6b4

Please sign in to comment.