def test_pagination_values(self): page = ptah.Pagination(10) self.assertEqual(page.page_size, 10) self.assertEqual(page.left_neighbours, 3) self.assertEqual(page.right_neighbours, 3) page = ptah.Pagination(11, 5, 2) self.assertEqual(page.page_size, 11) self.assertEqual(page.left_neighbours, 5) self.assertEqual(page.right_neighbours, 2) self.assertEqual(page.offset(3), (page.page_size * 2, page.page_size))
def test_pagination_zero_nb(self): page = ptah.Pagination(10, 0, 0) self.assertEqual(page(1000, 1)[0], [1, None, 100]) self.assertEqual(page(1000, 2)[0], [1, 2, None, 100]) self.assertEqual(page(1000, 3)[0], [1, None, 3, None, 100])
def test_pagination_one_nb(self): page = ptah.Pagination(10, 1, 1) self.assertEqual(page(1000, 1)[0], [1, 2, None, 100]) self.assertEqual(page(1000, 2)[0], [1, 2, 3, None, 100]) self.assertEqual(page(1000, 3)[0], [1, 2, 3, 4, None, 100]) self.assertEqual(page(1000, 4)[0], [1, None, 3, 4, 5, None, 100])
def test_pagination_prev_next(self): page = ptah.Pagination(10, 3, 3) self.assertEqual(page(1000, 1)[1], None) self.assertEqual(page(1000, 2)[1], 1) self.assertEqual(page(1000, 3)[1], 2) self.assertEqual(page(1000, 100)[1], 99) self.assertEqual(page(1000, 100)[2], None) self.assertEqual(page(1000, 99)[2], 100) self.assertEqual(page(1000, 98)[2], 99) self.assertEqual(page(1000, 1)[2], 2)
class GalleryView(ptah.View): """ Main gallery view """ themes_tmpl = 'ploud.themegallery:templates/listthemes.pt' status = 'public' page = ptah.Pagination(8, 3, 3) def render_tmpl(self, themes, url): return render(self.themes_tmpl, {'themes': themes, 'url': url}) def getCurrent(self): id = 'gallery-batch-%s' % self.status request = self.request try: current = int(request.params.get('batch', None)) if not current: current = request.session.get(id) if not current: current = 1 else: request.session[id] = current except: current = request.session.get(id) if not current: current = 1 return current def getCount(self): return ptah.get_session().query(Theme)\ .filter(Theme.status == self.status).count() def listThemes(self, offset, size): return ptah.get_session().query(Theme)\ .filter(Theme.status == self.status).order_by(Theme.modified)\ .offset(offset).limit(size).all() def update(self): self.actions = [ ac for ac in ptah.list_uiactions(self.context, self.request) if ac['id'] != 'view' ] size = self.getCount() self.current = current = self.getCurrent() self.pages, self.prev, self.next = self.page(size, current) self.themes = self.listThemes(*self.page.offset(current))
def test_pagination_nb_end(self): page = ptah.Pagination(10, 2, 2) self.assertEqual( page(1000, 95)[0], [1, None, 93, 94, 95, 96, 97, None, 100]) self.assertEqual( page(1000, 96)[0], [1, None, 94, 95, 96, 97, 98, None, 100]) self.assertEqual(page(1000, 97)[0], [1, None, 95, 96, 97, 98, 99, 100]) self.assertEqual(page(1000, 98)[0], [1, None, 96, 97, 98, 99, 100]) self.assertEqual(page(1000, 99)[0], [1, None, 97, 98, 99, 100]) self.assertEqual(page(1000, 100)[0], [1, None, 98, 99, 100])
def test_pagination_nb_begin(self): page = ptah.Pagination(10, 2, 2) self.assertRaises(ValueError, page, 1000, 0) self.assertEqual(page(1000, 1)[0], [1, 2, 3, None, 100]) self.assertEqual(page(1000, 2)[0], [1, 2, 3, 4, None, 100]) self.assertEqual(page(1000, 3)[0], [1, 2, 3, 4, 5, None, 100]) self.assertEqual(page(1000, 4)[0], [1, 2, 3, 4, 5, 6, None, 100]) self.assertEqual(page(1000, 5)[0], [1, None, 3, 4, 5, 6, 7, None, 100]) self.assertEqual(page(1000, 6)[0], [1, None, 4, 5, 6, 7, 8, None, 100])
class CrowdGroupsView(ptah.View): __doc__ = 'List groups view' pages = () page = ptah.Pagination(15) def update(self): request = self.request self.manage_url = ptah.manage.get_manage_url(request) Session = ptah.get_session() uids = request.POST.getall('uid') if 'remove' in request.POST and uids: for grp in Session.query(CrowdGroup).\ filter(CrowdGroup.__uri__.in_(uids)): grp.delete() self.request.add_message( _("The selected groups have been removed."), 'info') self.size = Session.query(CrowdGroup).count() try: current = int(request.params.get('batch', None)) if not current: current = 1 request.session['crowd-grp-batch'] = current except: current = request.session.get('crowd-grp-batch') if not current: current = 1 self.current = current self.pages, self.prev, self.next = self.page(self.size, self.current) offset, limit = self.page.offset(current) self.groups = Session.query(CrowdGroup)\ .offset(offset).limit(limit).all()
class ModelView(form.Form): csrf = True page = ptah.Pagination(15) def update(self): tinfo = self.context.tinfo cls = tinfo.cls self.fields = tinfo.fieldset result = super(ModelView, self).update() request = self.request try: current = int(request.params.get('batch', None)) if not current: current = 1 request.session['table-current-batch'] = current except: current = request.session.get('table-current-batch') if not current: current = 1 Session = ptah.get_session() self.size = Session.query(cls).count() self.current = current self.pages, self.prev, self.next = self.page(self.size, self.current) offset, limit = self.page.offset(current) self.data = Session.query(cls)\ .order_by(cls.__id__).offset(offset).limit(limit).all() return result def get_record_info(self, item): res = {} for field in self.fields.values(): val = getattr(item, field.name, field.default) res[field.name] = val return res #@form.button('Add', actype=form.AC_PRIMARY) #def add(self): # return HTTPFound(location='add.html') @form.button('Remove', actype=form.AC_DANGER) def remove(self): self.validate_csrf_token() ids = [] for id in self.request.POST.getall('rowid'): try: ids.append(int(id)) except: # pragma: no cover pass if not ids: self.request.add_message('Please select records for removing.', 'warning') return Session = ptah.get_session() for rec in Session.query(self.context.tinfo.cls).filter( self.context.tinfo.cls.__id__.in_(ids)).all(): Session.delete(rec) self.request.add_message('Select records have been removed.') return HTTPFound(location = self.request.url)
class TableView(ptah.form.Form): __doc__ = "List table records." csrf = True page = ptah.Pagination(15) def update(self): table = self.table = self.context.table self.primary = None self.pcolumn = None self.uris = [] self.inheritance = get_inheritance(table) if table.name == 'ptah_nodes' or self.inheritance: self.show_actions = False else: self.show_actions = True names = [] for cl in table.columns: names.append(cl.name) if cl.primary_key: self.primary = cl.name self.pcolumn = cl if cl.info.get('uri'): self.uris.append(len(names)-1) res = super(TableView, self).update() request = self.request try: current = int(request.params.get('batch', None)) if not current: current = 1 request.session['table-current-batch'] = current except: current = request.session.get('table-current-batch') if not current: current = 1 self.size = Session.query(table).count() self.current = current self.pages, self.prev, self.next = self.page(self.size, self.current) offset, limit = self.page.offset(current) self.data = Session.query(table).offset(offset).limit(limit).all() return res def quote(self, val): return url_quote_plus(val) def val(self, val): try: if isinstance(val, str): # pragma: no cover val = unicode(val, 'utf-8', 'ignore') elif not isinstance(val, unicode): val = str(val) except: # pragma: no cover val = "Can't show" return val[:100] @ptah.form.button('Add', actype=ptah.form.AC_PRIMARY) def add(self): return HTTPFound(location='add.html') @ptah.form.button('Remove', actype=ptah.form.AC_DANGER) def remove(self): self.validate_csrf_token() ids = [] for id in self.request.POST.getall('rowid'): try: ids.append(int(id)) except: pass if not ids: self.message('Please select records for removing.', 'warning') return self.table.delete(self.pcolumn.in_(ids)).execute() self.message('Select records have been removed.')
class CrowdModuleView(ptah.form.Form, ptah.View): __doc__ = 'List/search users view' csrf = True fields = ptah.form.Fieldset( ptah.form.TextField( 'term', title=_('Search term'), description=_('Ptah searches users by login and email'), missing='', default='')) users = None external = {} pages = () page = ptah.Pagination(15) def form_content(self): return {'term': self.request.session.get('ptah-search-term', '')} def update(self): super(CrowdModuleView, self).update() request = self.request self.manage_url = ptah.manage.get_manage_url(self.request) Session = ptah.get_session() uids = request.POST.getall('uid') if 'create' in request.POST: return HTTPFound('create.html') if 'activate' in request.POST and uids: Session.query(CrowdUser).filter(CrowdUser.id.in_(uids))\ .update({'suspended': False}, False) self.request.add_message( _("The selected accounts have been activated."), 'info') if 'suspend' in request.POST and uids: Session.query(CrowdUser).filter(CrowdUser.id.in_(uids)).update( {'suspended': True}, False) self.request.add_message( _("The selected accounts have been suspended."), 'info') if 'validate' in request.POST and uids: Session.query(CrowdUser).filter(CrowdUser.id.in_(uids))\ .update({'validated': True}, False) self.request.add_message( _("The selected accounts have been validated."), 'info') if 'remove' in request.POST and uids: for user in Session.query(CrowdUser).filter( CrowdUser.id.in_(uids)): Session.delete(user) self.request.add_message( _("The selected accounts have been removed."), 'info') term = request.session.get('ptah-search-term', '') if term: self.users = Session.query(CrowdUser) \ .filter(sqla.sql.or_( CrowdUser.email.contains('%%%s%%' % term), CrowdUser.username.contains('%%%s%%' % term)))\ .order_by(sqla.sql.asc('fullname')).all() else: self.size = Session.query(CrowdUser).count() try: current = int(request.params.get('batch', None)) if not current: current = 1 request.session['crowd-current-batch'] = current except: current = request.session.get('crowd-current-batch') if not current: current = 1 self.current = current self.pages, self.prev, self.next = \ self.page(self.size, self.current) offset, limit = self.page.offset(current) self.users = Session.query(CrowdUser) \ .offset(offset).limit(limit).all() auths = Session.query(Storage).filter( Storage.uri.in_([u.__uri__ for u in self.users])).all() self.external = extr = {} for entry in auths: data = extr.get(entry.uri) if data is None: data = [] data.append(entry.domain) extr[entry.uri] = data @ptah.form.button(_('Search'), actype=ptah.form.AC_PRIMARY) def search(self): data, error = self.extract() if not data['term']: self.request.add_message('Please specify search term', 'warning') return self.request.session['ptah-search-term'] = data['term'] @ptah.form.button(_('Clear term'), name='clear') def clear(self): if 'ptah-search-term' in self.request.session: del self.request.session['ptah-search-term']