Permalink
Browse files

Added proper pagination

  • Loading branch information...
jbohman committed Jun 27, 2010
1 parent 3b7f1a7 commit 194ab939ed334baf4e00a63776d774e8e19c642d
Showing with 90 additions and 79 deletions.
  1. +31 −8 logsandra/controllers/log.py
  2. +11 −2 logsandra/templates/log_view.html
  3. +48 −69 logsandra/utils/model.py
@@ -29,36 +29,59 @@ def view(self):
if search_keyword:
keyword = search_keyword
+ current_prev = None
current_next = None
if 'next' in request.GET:
current_next = long(request.GET['next'])
- if date_from and date_to:
+ if 'prev' in request.GET:
+ current_prev = long(request.GET['prev'])
+
+ if date_from:
try:
date_from = datetime.datetime.strptime(date_from, '%Y-%m-%d %H:%M:%S')
+ except ValueError:
+ redirect(url(controller='log', action='error'))
+ else:
+ date_from = ''
+
+ if date_to:
+ try:
date_to = datetime.datetime.strptime(date_to, '%Y-%m-%d %H:%M:%S')
except ValueError:
redirect(url(controller='log', action='error'))
else:
- date_from = None
- date_to = None
+ date_to = ''
- # TODO: Move this to a final that could be used by other controllers and make sure it uses the config file
+ # TODO: Move this to a final that could be used by other controllers
+ # and make sure it uses the config file
client = Cassandra('', 'localhost', 9160, 5)
if current_next:
- entries, next = client.get_entries_by_keyword(keyword, date_from, date_to, action_next=current_next)
+ entries, last, first = client.get_entries_by_keyword(keyword,
+ date_from, date_to, action_next=current_next)
+ elif current_prev:
+ entries, last, first = client.get_entries_by_keyword(keyword,
+ date_from, date_to, action_prev=current_prev)
else:
- entries, next = client.get_entries_by_keyword(keyword, date_from, date_to)
+ entries, last, first = client.get_entries_by_keyword(keyword,
+ date_from, date_to)
c.entries = entries
- if next:
+ if last:
c.next_url = url(controller='log', action='view',
search_keyword=request.GET['search_keyword'],
status=request.GET['status'],
date_from=request.GET['date_from'],
- date_to=request.GET['date_to'], next=next)
+ date_to=request.GET['date_to'], next=last)
+
+ if first:
+ c.prev_url = url(controller='log', action='view',
+ search_keyword=request.GET['search_keyword'],
+ status=request.GET['status'],
+ date_from=request.GET['date_from'],
+ date_to=request.GET['date_to'], prev=first)
return render('/log_view.html')
@@ -2,10 +2,19 @@
{% block content %}
-{% if c.next_url %}
-<a href="{{ c.next_url }}">Next</a>
+{% if c.entries %}
+ Displaying {{ c.entries|length }} entries
+
+ {% if c.prev_url %}
+ <a href="{{ c.prev_url }}">Prev</a>
+ {% endif %}
+
+ {% if c.next_url %}
+ <a href="{{ c.next_url }}">Next</a>
+ {% endif %}
{% endif %}
+
{% for entry in c.entries %}
<pre style="white-space: normal;">
{{ entry['entry'] }}<br />
View
@@ -44,89 +44,68 @@ def add_entry(self, date, entry, source, keywords):
return True
- def get_entries_by_keyword(self, keyword, column_start=None, column_finish=None, column_count=30, action_next=None):
- if action_next:
- column_start = long_struct.pack(action_next)
- elif column_start:
- column_start = to_long(column_start)
-
- try:
- if column_start and not column_finish:
- result = self.cf_by_date.get(str(keyword), column_count=column_count, column_start=column_start)
- elif column_start and column_finish:
- result = self.cf_by_date.get(str(keyword), column_count=column_count, column_start=column_start, column_finish=to_long(column_finish, 0))
- else:
- result = self.cf_by_date.get(str(keyword), column_count=column_count)
- except NotFoundException:
- return [], None
-
- try:
- entries = self.cf_entries.multiget(result.values())
- except NotFoundException:
- return [], None
-
- return_value = []
- return_next = None
- for k, v in result.items():
- if v in entries:
- return_value.append(entries[v])
- return_next = k
-
- return return_value, long_struct.unpack(return_next)[0] + 1
-
-
- """
- def get_entries_by_keyword(self, keyword, column_start=None, column_finish=None, column_count=10, action_next=None, action_prev=None, prev=[]):
+ def get_entries_by_keyword(self, keyword, column_start='', column_finish='', column_count=30, action_next=None, action_prev=None):
if action_next and action_prev:
raise AttributeError('action_next and action_prev is mutually exclusive')
column_reversed = False
- if not column_start and not column_finish:
- if action_next:
- column_start = long_struct.pack(action_next)
- if action_prev:
- column_start = long_struct.pack(action_prev[-2])
+ if column_start:
+ column_start = to_long(column_start, 0)
+
+ if column_finish:
+ column_finish = to_long(column_finish, 0)
+
+ if action_next:
+ column_start = long_struct.pack(action_next)
+
+ if action_prev:
+ if column_start:
+ column_finish = column_start
+ column_start = long_struct.pack(action_prev)
+ column_reversed = True
try:
- if column_start and not column_finish:
- result = self.cf_by_date.get(str(keyword),
- column_count=column_count,
- column_reversed=column_reversed,
- column_start=column_start)
- elif column_start and column_finish:
- result = self.cf_by_date.get(str(keyword),
- column_count=column_count,
- column_reversed=column_reversed,
- column_start=column_start,
- column_finish=column_finish)
- else:
- result = self.cf_by_date.get(str(keyword),
- column_count=column_count,
- column_reversed=column_reversed)
+ result = self.cf_by_date.get(str(keyword),
+ column_reversed=column_reversed, column_count=column_count+1,
+ column_start=column_start, column_finish=column_finish)
except NotFoundException:
- return OrderedDict(), None
+ return [], None, None
try:
entries = self.cf_entries.multiget(result.values())
except NotFoundException:
- return OrderedDict(), None
+ return [], None, None
- return_value = OrderedDict()
- return_next = None
- return_prev = None
- for k, v in result.items():
+ loop = result.items()
+ if column_reversed:
+ loop = reversed(loop)
+
+ values = []
+ keys = []
+ for k, v in loop:
if v in entries:
- if not return_prev:
- return_prev = k
- return_value[v] = entries[v]
- return_next = k
+ values.append(entries[v])
+ keys.append(k)
- if action_prev:
- prev.pop()
+ return_next = None
+ return_prev = None
+ if len(keys) == column_count+1:
+ if action_prev:
+ values = values[1:]
+ return_next = long_struct.unpack(keys[-1])[0] + 1
+ return_prev = long_struct.unpack(keys[1])[0] - 1
+ elif action_next:
+ values = values[0:-1]
+ return_next = long_struct.unpack(keys[-2])[0] + 1
+ return_prev = long_struct.unpack(keys[0])[0] - 1
+ else:
+ return_next = long_struct.unpack(keys[-2])[0] + 1
+ values = values[0:-1]
else:
- prev.append(long_struct.unpack(return_prev)[0])
-
- return return_value, (long_struct.unpack(return_next)[0] + 1), prev
- """
+ if action_prev:
+ return_next = long_struct.unpack(keys[-1])[0] + 1
+ elif action_next:
+ return_prev = long_struct.unpack(keys[0])[0] - 1
+ return values, return_next, return_prev

0 comments on commit 194ab93

Please sign in to comment.