Skip to content

python 2.4 and higher compatibility #13

Merged
merged 2 commits into from Jul 28, 2012
View
20 plugin-src/tracadvsearch/advsearch.py
@@ -32,6 +32,26 @@
from trac.web.chrome import add_stylesheet, add_warning, add_script
from trac.wiki.formatter import extract_link
+import operator
+
+# --- any() from Python 2.5 ---
+try:
+ from __builtin__ import any
+except ImportError:
+ def any(items):
+ for item in items:
+ if item:
+ return True
+ return False
+
+# ---all() from Python 2.5 ---
+try:
+ from __builtin__ import all
+except ImportError:
+ def all(items):
+ return reduce(operator.__and__, items)
+
+__all__ = ("any", "all")
class SearchBackendException(Exception):
"""
View
23 plugin-src/tracadvsearch/backend.py
@@ -12,6 +12,7 @@
from trac.core import Component
from trac.core import implements
from trac.search import shorten_result
+import time
class PySolrSearchBackEnd(Component):
"""AdvancedSearchBackend that uses pysolr lib to search Solr."""
@@ -85,7 +86,10 @@ def query_backend(self, criteria):
status = self._string_from_filters(criteria.get('ticket_statuses'))
q_parts.append("(status: %s OR source: wiki)" % status)
- q_string = " AND ".join(q_parts) if q_parts else '*:*'
+ if q_parts:
+ q_string = " AND ".join(q_parts)
+ else:
+ q_string = '*:*'
try:
results = self.conn.search(q_string, **params)
@@ -111,7 +115,7 @@ def _build_summary(self, text, query):
def _date_from_solr(self, date_string):
"""Return a human friendly date from solr date string."""
- date = datetime.datetime.strptime(date_string, self.SOLR_DATE_FORMAT)
+ date = self._strptime(date_string, self.SOLR_DATE_FORMAT)
return date.strftime(self.INPUT_DATE_FORMAT)
def _string_from_input(self, value):
@@ -140,17 +144,26 @@ def _date_from_range(self, start, end):
if not start and not end:
return None
- start_formatted = self._format_date(start) if start else "*"
- end_formatted = self._format_date(end) if end else "*"
@dnephin
Owner
dnephin added a note Jul 27, 2012

end_formatted was missed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if start:
+ start_formatted = self._format_date(start)
+ else:
+ start_formatted = "*"
+ if end:
+ end_formatted = self._format_date(end)
+ else:
+ end_formatted = "*"
return "[%s TO %s]" % (start_formatted, end_formatted)
def _format_date(self, date_string, default="*"):
"""Format a date as a solr date string."""
try:
- date = datetime.datetime.strptime(
+ date = self._strptime(
date_string, self.INPUT_DATE_FORMAT)
except ValueError:
self.log.warn("Invalid date format: %s" % date_string)
return default
return date.strftime(self.SOLR_DATE_FORMAT)
+ def _strptime(self, date_string, date_format):
+ return datetime.datetime(*(time.strptime(date_string, date_format)[0:6]))
+
View
6 plugin-src/tracadvsearch/templates/advsearch.html
@@ -23,7 +23,7 @@
<legend>${_('Source(s)')}:</legend>
<py:for each="filter in source">
<input type="checkbox" id="${filter.name}" name="${filter.name}"
- checked="${'checked' if filter.active else None}" />
+ checked="${((filter.active and 'checked') or None)}" />
<label for="${filter.name}">${filter.name}</label>
</py:for>
</fieldset>
@@ -58,7 +58,7 @@
<legend>${_('Ticket Status')}:</legend>
<py:for each="status in ticket_statuses">
<input type="checkbox" name="${status.field_name}"
- checked="${'checked' if status.active else None}" />
+ checked="${((status.active and 'checked') or None)}" />
<label for="${status.name}">${status.name}</label>
</py:for>
</fieldset>
@@ -86,7 +86,7 @@ <h2 py:if="results">
<div>
<div id="results">
<py:for each="idx, result in enumerate(results)">
- <div class="result ${'even' if not idx % 2 else None}">
+ <div class="result ${((not idx % 2 and 'even') or None)}">
<div class="result_header">
<span class="result_type ${result.status}">
${result.source}
Something went wrong with that request. Please try again.