-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
58 lines (51 loc) · 1.81 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Some utilities
"""
import pickle
import logging
import math
from . import admin_settings
def getBlobProperties(item, fieldName):
props = getattr(item, fieldName + admin_settings.BLOB_FIELD_META_SUFFIX, None)
if props:
return pickle.loads(props)
else:
return None
class Http404(Exception):
code = 404
class Http500(Exception):
code = 500
class Page(object):
def __init__(self, modelAdmin, itemsPerPage = 20, currentPage = 1):
self.modelAdmin = modelAdmin
self.model = self.modelAdmin.model
self.itemsPerPage = int(itemsPerPage)
self.current = int(currentPage) # comes in as unicode
self.setPageNumbers()
logging.info("Paging: Maxpages: %r" % self.maxpages)
logging.info("Paging: Current: %r" % self.current)
def setPageNumbers(self):
nItems = float(self.model.all().count())
logging.info('Paging: Items per page: %s' % self.itemsPerPage)
logging.info('Paging: Number of items %s' % int(nItems))
self.maxpages = int(math.ceil(nItems / float(self.itemsPerPage)))
if self.maxpages < 1:
self.maxpages = 1
# validate current page number
if self.current > self.maxpages or self.current < 1:
self.current = 1
if self.current > 1:
self.prev = self.current - 1
else:
self.prev = None
if self.current < self.maxpages:
self.next = self.current + 1
else:
self.next = None
self.first = 1
self.last = self.maxpages
def getDataForPage(self):
offset = int((self.current - 1) * self.itemsPerPage)
query = self.modelAdmin.listGql + ' LIMIT %i, %i' % (offset, self.itemsPerPage)
logging.info("Paging: GQL: %s" % query)
return self.model.gql(query)