[PATCH 1/7] trivial: Cleanup of 'filters.py'
Stephen Finucane
stephen.finucane at intel.com
Wed Sep 9 22:59:08 AEST 2015
Resolve some issues with the aforementioned file. These are mostly
stylistic changes.
Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
patchwork/filters.py | 183 +++++++++++++++++++++++++++------------------------
1 file changed, 97 insertions(+), 86 deletions(-)
diff --git a/patchwork/filters.py b/patchwork/filters.py
index 8c9690e..fe35dae 100644
--- a/patchwork/filters.py
+++ b/patchwork/filters.py
@@ -17,19 +17,26 @@
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+from urllib import quote
-from patchwork.models import Person, State
from django.utils.safestring import mark_safe
from django.utils.html import escape
from django.contrib.auth.models import User
-from urllib import quote
+
+from patchwork.models import Person, State
+
class Filter(object):
+ param = None
+
def __init__(self, filters):
self.filters = filters
self.applied = False
self.forced = False
+ def _set_key(self, key):
+ raise NotImplementedError()
+
def name(self):
"""The 'name' of the filter, to be displayed in the filter UI"""
return self.name
@@ -50,10 +57,10 @@ class Filter(object):
that user's delegated patches"""
pass
- def parse(self, dict):
- if self.param not in dict.keys():
+ def parse(self, filters):
+ if self.param not in filters.keys():
return
- self._set_key(dict[self.param])
+ self._set_key(filters[self.param])
def url_without_me(self):
return self.filters.querystring_without_filter(self)
@@ -61,11 +68,13 @@ class Filter(object):
def form_function(self):
return 'function(form) { return "unimplemented" }'
+ def _form(self):
+ raise NotImplementedError()
+
def form(self):
if self.forced:
- return mark_safe('<input type="hidden" value="%s">%s' % (self.param,
- self.condition()))
- return self.condition()
+ return mark_safe('<input type="hidden" value="%s">%s' % (
+ self.param, self.condition()))
return self._form()
def kwargs(self):
@@ -77,35 +86,35 @@ class Filter(object):
class SubmitterFilter(Filter):
param = 'submitter'
+
def __init__(self, filters):
super(SubmitterFilter, self).__init__(filters)
self.name = 'Submitter'
self.person = None
self.person_match = None
- def _set_key(self, str):
+ def _set_key(self, key):
self.person = None
self.person_match = None
submitter_id = None
try:
- submitter_id = int(str)
+ submitter_id = int(key)
except ValueError:
pass
except:
return
if submitter_id:
- self.person = Person.objects.get(id = int(str))
+ self.person = Person.objects.get(id=int(key))
self.applied = True
return
-
- people = Person.objects.filter(name__icontains = str)
+ people = Person.objects.filter(name__icontains=key)
if not people:
return
- self.person_match = str
+ self.person_match = key
self.applied = True
def kwargs(self):
@@ -113,7 +122,7 @@ class SubmitterFilter(Filter):
user = self.person.user
if user:
return {'submitter__in':
- Person.objects.filter(user = user).values('pk').query}
+ Person.objects.filter(user=user).values('pk').query}
return {'submitter': self.person}
if self.person_match:
@@ -131,17 +140,18 @@ class SubmitterFilter(Filter):
name = ''
if self.person:
name = self.person.name
- return mark_safe(('<input onKeyUp="submitter_field_change(this)" ' +
- 'name="submitter" id="submitter_input" ' +
- 'value="%s"> ' % escape(name)) +
- '<select id="submitter_select" ' +
- 'disabled="true"></select>')
+ return mark_safe(
+ '<input onKeyUp="submitter_field_change(this)"'
+ ' name="submitter" id="submitter_input" value="%s"> '
+ '<select id="submitter_select" disabled="true"></select>'
+ % escape(name))
def key(self):
if self.person:
return self.person.id
return self.person_match
+
class StateFilter(Filter):
param = 'state'
any_key = '*'
@@ -153,15 +163,15 @@ class StateFilter(Filter):
self.state = None
self.applied = True
- def _set_key(self, str):
+ def _set_key(self, key):
self.state = None
- if str == self.any_key:
+ if key == self.any_key:
self.applied = False
return
try:
- self.state = State.objects.get(id=int(str))
+ self.state = State.objects.get(id=int(key))
except:
return
@@ -171,9 +181,8 @@ class StateFilter(Filter):
if self.state is not None:
return {'state': self.state}
else:
- return {'state__in': \
- State.objects.filter(action_required = True) \
- .values('pk').query}
+ return {'state__in': State.objects.filter(
+ action_required=True).values('pk').query}
def condition(self):
if self.state:
@@ -188,51 +197,53 @@ class StateFilter(Filter):
return None
def _form(self):
- str = '<select name="%s">' % self.param
+ out = '<select name="%s">' % self.param
selected = ''
if not self.applied:
selected = 'selected'
- str += '<option %s value="%s">any</option>' % (selected, self.any_key)
+ out += '<option %s value="%s">any</option>' % (selected, self.any_key)
selected = ''
- if self.applied and self.state == None:
+ if self.applied and self.state is None:
selected = 'selected'
- str += '<option %s value="">%s</option>' % \
- (selected, self.action_req_str)
+ out += '<option %s value="">%s</option>' % (
+ selected, self.action_req_str)
for state in State.objects.all():
selected = ''
if self.state and self.state == state:
selected = ' selected="true"'
- str += '<option value="%d" %s>%s</option>' % \
- (state.id, selected, state.name)
- str += '</select>'
- return mark_safe(str);
+ out += '<option value="%d" %s>%s</option>' % (
+ state.id, selected, state.name)
+ out += '</select>'
+ return mark_safe(out)
def form_function(self):
return 'function(form) { return form.x.value }'
def url_without_me(self):
- qs = self.filters.querystring_without_filter(self)
- if qs != '?':
- qs += '&'
- return qs + '%s=%s' % (self.param, self.any_key)
+ query = self.filters.querystring_without_filter(self)
+ if query != '?':
+ query += '&'
+ return query + '%s=%s' % (self.param, self.any_key)
+
class SearchFilter(Filter):
param = 'q'
+
def __init__(self, filters):
super(SearchFilter, self).__init__(filters)
self.name = 'Search'
self.param = 'q'
self.search = None
- def _set_key(self, str):
- str = str.strip()
- if str == '':
+ def _set_key(self, key):
+ key = key.strip()
+ if key == '':
return
- self.search = str
+ self.search = key
self.applied = True
def kwargs(self):
@@ -248,14 +259,16 @@ class SearchFilter(Filter):
value = ''
if self.search:
value = escape(self.search)
- return mark_safe('<input name="%s" value="%s">' %\
- (self.param, value))
+ return mark_safe('<input name="%s" value="%s">' % (
+ self.param, value))
def form_function(self):
return mark_safe('function(form) { return form.x.value }')
+
class ArchiveFilter(Filter):
param = 'archive'
+
def __init__(self, filters):
super(ArchiveFilter, self).__init__(filters)
self.name = 'Archived'
@@ -272,17 +285,17 @@ class ArchiveFilter(Filter):
None: 'Both'
}
- def _set_key(self, str):
+ def _set_key(self, key):
self.archive_state = False
self.applied = True
for (k, v) in self.param_map.iteritems():
- if str == v:
+ if key == v:
self.archive_state = k
- if self.archive_state == None:
+ if self.archive_state is None:
self.applied = False
def kwargs(self):
- if self.archive_state == None:
+ if self.archive_state is None:
return {}
return {'archived': self.archive_state}
@@ -290,32 +303,30 @@ class ArchiveFilter(Filter):
return self.description_map[self.archive_state]
def key(self):
- if self.archive_state == False:
+ if self.archive_state is False:
return None
return self.param_map[self.archive_state]
def _form(self):
- s = ''
- for b in [False, True, None]:
- label = self.description_map[b]
+ out = ''
+ for archive_state in [False, True, None]:
selected = ''
- if self.archive_state == b:
+ if self.archive_state == archive_state:
selected = 'checked="true"'
- s += ('<input type="radio" name="%(param)s" ' + \
- '%(selected)s value="%(value)s">%(label)s' + \
- ' ') % \
- {'label': label,
- 'param': self.param,
- 'selected': selected,
- 'value': self.param_map[b]
- }
- return mark_safe(s)
+ out += ('<input type="radio" name="%(param)s" '
+ '%(selected)s value="%(value)s">%(label)s'
+ ' ') % {
+ 'label': self.description_map[archive_state],
+ 'param': self.param,
+ 'selected': selected,
+ 'value': self.param_map[archive_state]}
+ return mark_safe(out)
def url_without_me(self):
- qs = self.filters.querystring_without_filter(self)
- if qs != '?':
- qs += '&'
- return qs + 'archive=both'
+ query = self.filters.querystring_without_filter(self)
+ if query != '?':
+ query += '&'
+ return query + 'archive=both'
class DelegateFilter(Filter):
@@ -330,15 +341,15 @@ class DelegateFilter(Filter):
self.param = 'delegate'
self.delegate = None
- def _set_key(self, str):
- if str == self.no_delegate_key:
+ def _set_key(self, key):
+ if key == self.no_delegate_key:
self.applied = True
self.delegate = None
return
applied = False
try:
- self.delegate = User.objects.get(id = str)
+ self.delegate = User.objects.get(id=key)
self.applied = True
except:
pass
@@ -354,34 +365,34 @@ class DelegateFilter(Filter):
return self.no_delegate_str
def _form(self):
- delegates = User.objects.filter(profile__maintainer_projects =
- self.filters.project)
+ delegates = User.objects.filter(
+ profile__maintainer_projects=self.filters.project)
- str = '<select name="delegate">'
+ out = '<select name="delegate">'
selected = ''
if not self.applied:
selected = 'selected'
- str += '<option %s value="">------</option>' % selected
+ out += '<option %s value="">------</option>' % selected
selected = ''
if self.applied and self.delegate is None:
selected = 'selected'
- str += '<option %s value="%s">%s</option>' % \
- (selected, self.no_delegate_key, self.no_delegate_str)
+ out += '<option %s value="%s">%s</option>' % (
+ selected, self.no_delegate_key, self.no_delegate_str)
for d in delegates:
selected = ''
if d == self.delegate:
selected = ' selected'
- str += '<option %s value="%s">%s</option>' % (selected,
- d.id, d.profile.name())
- str += '</select>'
+ out += '<option %s value="%s">%s</option>' % (
+ selected, d.id, d.profile.name())
+ out += '</select>'
- return mark_safe(str)
+ return mark_safe(out)
def key(self):
if self.delegate:
@@ -398,7 +409,7 @@ class DelegateFilter(Filter):
self.applied = False
self.forced = True
-filterclasses = [SubmitterFilter, \
+filterclasses = [SubmitterFilter,
StateFilter,
SearchFilter,
ArchiveFilter,
@@ -431,10 +442,10 @@ class Filters:
return queryset.filter(**kwargs)
def params(self):
- return [ (f.param, f.key()) for f in self._filters \
- if f.key() is not None ]
+ return [
+ (f.param, f.key()) for f in self._filters if f.key() is not None]
- def querystring(self, remove = None):
+ def querystring(self, remove=None):
params = dict(self.params())
for (k, v) in self.dict.iteritems():
@@ -452,8 +463,8 @@ class Filters:
s = unicode(s)
return quote(s.encode('utf-8'))
- return '?' + '&'.join(['%s=%s' % (sanitise(k), sanitise(v))
- for (k, v) in pairs])
+ return '?' + '&'.join([
+ '%s=%s' % (sanitise(k), sanitise(v)) for (k, v) in pairs])
def querystring_without_filter(self, filter):
return self.querystring(filter)
--
2.0.0
More information about the Patchwork
mailing list