def test_0030_model_pagination_serialization(self): """ Test serialization of pagination for models """ with Transaction().start(DB_NAME, USER, CONTEXT): self.setup_defaults() # Create a 100 nereid users for id in xrange(0, 100): self.nereid_user_obj.create([{ 'party': self.guest_party, 'display_name': 'User %s' % id, 'email': '*****@*****.**' % id, 'password': '******', 'company': self.company.id, }]) pagination = Pagination(self.nereid_user_obj, [], 1, 10) serialized = pagination.serialize() self.assertEqual(serialized['count'], 100) self.assertEqual(serialized['pages'], 10) self.assertEqual(serialized['page'], 1) self.assertEqual(len(serialized['items']), 10) self.assert_('display_name' in serialized['items'][0])
def get_categories(cls, page=1): """Return list of categories """ return Pagination(cls, [ ('displayed_on_eshop', '=', True), ('sites', '=', request.nereid_website.id) ], page, cls.per_page)
def render(cls, uri, page=1): """ Renders the template 'category.jinja' with the category and the products of the category paginated in the context :param uri: URI of the product category :param page: Integer value of the page """ ProductTemplate = Pool().get('product.template') categories = cls.search([ ('displayed_on_eshop', '=', True), ('uri', '=', uri), ('sites', '=', request.nereid_website.id) ]) if not categories: return NotFound('Product Category Not Found') # if only one category is found then it is rendered and # if more than one are found then the first one is rendered category = categories[0] products = Pagination(ProductTemplate, [ ('products.displayed_on_eshop', '=', True), ('category', '=', category.id), ], page=page, per_page=cls.per_page) return render_template( 'category.jinja', category=category, products=products )
def test_0020_model_pagination(self): """ Test pagination for models """ with Transaction().start(DB_NAME, USER, CONTEXT): self.setup_defaults() # Create a 100 nereid users for id in xrange(0, 100): self.nereid_user_obj.create([{ 'party': self.guest_party, 'display_name': 'User %s' % id, 'email': '*****@*****.**' % id, 'password': '******', 'company': self.company.id, }]) pagination = Pagination(self.nereid_user_obj, [], 1, 10) self.assertEqual(pagination.count, 100) self.assertEqual(pagination.pages, 10) self.assertEqual(pagination.begin_count, 1) self.assertEqual(pagination.end_count, 10)
def sales(cls, page=1): 'All sales' Sale = Pool().get('sale.sale') sales = Pagination(Sale, [('party', '=', current_user.party.id), ('state', '!=', 'draft')], page, cls.per_page) return render_template('sales.jinja', sales=sales)
def render(self, uri, page=1): """ Renders the template """ product_obj = Pool().get('product.product') category_ids = self.search([('displayed_on_eshop', '=', True), ('uri', '=', uri), ('sites', '=', request.nereid_website.id)]) if not category_ids: return NotFound('Product Category Not Found') # if only one product is found then it is rendered and # if more than one are found then the first one is rendered category = self.browse(category_ids[0]) child_categories = self.search([('childs', 'child_of', [category.id])]) print child_categories products = Pagination(product_obj, [ ('displayed_on_eshop', '=', True), ('category', 'in', child_categories + [category.id]), ], page=page, per_page=self.per_page) return render_template( 'category.jinja', category=category, products=products, )
def render_list(cls, page=1): """Render all orders """ filter_by = request.args.get('filter_by', None) domain = [ ('party', '=', current_user.party.id), ] req_date = (date.today() + relativedelta(months=-3)) if filter_by == 'done': domain.append(('state', '=', 'done')) elif filter_by == 'canceled': domain.append(('state', '=', 'cancel')) elif filter_by == 'archived': domain.append(('state', 'not in', ('draft', 'quotation'))) # Add a sale_date domain for recent orders. domain.append(('sale_date', '<', req_date)) else: domain.append( ('state', 'not in', ('draft', 'quotation', 'cancel'))) # Add a sale_date domain for recent orders. domain.append(('sale_date', '>=', req_date)) # Handle order duration sales = Pagination(cls, domain, page, cls.per_page) return render_template('sales.jinja', sales=sales)
def get_root_categories(cls, page=1): """Return list of Root Categories.""" return Pagination(cls, [ ('displayed_on_eshop', '=', True), ('sites', '=', request.nereid_website.id), ('parent', '=', None), ], page, cls.per_page)
def cms_static_list(cls, page=1): """ Return JSON with list of all files inside cms static folder """ StaticFile = Pool().get("nereid.static.file") files = Pagination( StaticFile, [('folder', '=', current_website.cms_static_folder.id)], page, 10) return jsonify(items=[item.serialize() for item in files])
def test_0040_model_pagination_serialization(self): """ Test serialization of pagination for model which does not have serialize method """ self.setup_defaults() # Create a 100 addresses for id in xrange(0, 100): self.address_obj.create([{ 'party': self.guest_party, 'name': 'User %s' % id, }]) pagination = Pagination(self.address_obj, [], 1, 10) serialized = pagination.serialize() self.assert_('id' in serialized['items'][0]) self.assert_('rec_name' in serialized['items'][0])
def render_list(cls, page=1): """ Renders the list of all categories which are displayed_on_shop=True paginated. :param page: Integer ID of the page """ categories = Pagination(cls, [ ('displayed_on_eshop', '=', True), ('sites', '=', request.nereid_website.id), ], page, cls.per_page) return render_template('category-list.jinja', categories=categories)
def my_posts(self, page=1): """Render all the posts of the logged in user """ posts = Pagination(self, [ ('nereid_user', '=', request.nereid_user.id), ], page, self.per_page) if request.is_xhr: return jsonify({ 'has_next': posts.has_next, 'has_prev': posts.has_prev, 'items': [post.serialize() for post in posts], }) return render_template('my_blog_posts.jinja', posts=posts)
def quick_search(cls): """A quick and dirty search which searches through the product.product for an insensitive like and returns a pagination object the same. """ page = request.args.get('page', 1, type=int) query = request.args.get('q', '') categories = request.nereid_website.get_categories() + [None] products = Pagination(cls, [ ('displayed_on_eshop', '=', True), ('category', 'in', categories), ('template.active', '=', True), ('name', 'ilike', '%' + query + '%'), ], page, cls.per_page) return render_template('search-results.jinja', products=products)
def quick_search(cls): """A quick and dirty search which searches through the product.product for an insensitive like and returns a pagination object the same. """ Product = Pool().get('product.product') page = request.args.get('page', 1, type=int) query = request.args.get('q', '') products = Pagination(Product, [ ('displayed_on_eshop', '=', True), ('template.active', '=', True), ('name', 'ilike', '%' + query + '%'), ], page, Product.per_page) return render_template('search-results.jinja', products=products)
def render_list(cls, page=1): """Render all orders """ filter_by = request.args.get('filter_by', None) domain = [ ('party', '=', current_user.party.id), ] req_date = ( date.today() + relativedelta(months=-3) ) if filter_by == 'done': domain.append(('state', '=', 'done')) elif filter_by == 'canceled': domain.append(('state', '=', 'cancel')) elif filter_by == 'archived': # only done and cancelled orders should be in archive # irrespective of the date. Pre orders for example # could be over 3 months old and still be in the # processing state domain.append( ('state', 'in', ('done', 'cancel')) ) # Add a sale_date domain for recent orders. domain.append(( 'sale_date', '<', req_date )) else: domain.append([ 'OR', ('state', 'in', ('confirmed', 'processing')), [ ('state', 'in', ('done', 'cancel')), ('sale_date', '>=', req_date), ] ]) # Handle order duration sales = Pagination(cls, domain, page, cls.per_page) return render_template('sales.jinja', sales=sales)
def render(cls, uri, page=1): """ Renders the category """ Article = Pool().get('nereid.cms.article') # Find in cache or load from DB try: category, = cls.search([('unique_name', '=', uri)]) except ValueError: return NotFound() articles = Pagination(Article, [('category', '=', category.id)], page, cls.per_page) return render_template(category.template, category=category, articles=articles)
def render_list(cls, user_id, page=1): """Render the blog posts for a user This should render the list of only published posts of the user """ NereidUser = Pool().get('nereid.user') user = NereidUser(user_id) posts = Pagination(cls, [ ('nereid_user', '=', user.id), ('state', '=', 'Published'), ], page, cls.per_page) if request.is_xhr: return jsonify({ 'has_next': posts.has_next, 'has_prev': posts.has_prev, 'items': [post.serialize() for post in posts], }) return render_template('blog_posts.jinja', posts=posts, poster=user)