def blog(self, blog=None, tag=None, page=1, search=None, **opt): Blog = request.env['blog.blog'] if blog and not blog.can_access_from_current_website(): raise werkzeug.exceptions.NotFound() blogs = Blog.search(request.website.website_domain(), order="create_date asc, id asc") if not blog and len(blogs) == 1: return werkzeug.utils.redirect('/blog/%s' % slug(blogs[0]), code=302) date_begin, date_end, state = opt.get('date_begin'), opt.get( 'date_end'), opt.get('state') if tag and request.httprequest.method == 'GET': # redirect get tag-1,tag-2 -> get tag-1 tags = tag.split(',') if len(tags) > 1: url = QueryURL('' if blog else '/blog', ['blog', 'tag'], blog=blog, tag=tags[0], date_begin=date_begin, date_end=date_end, search=search)() return request.redirect(url, code=302) values = self._prepare_blog_values(blogs=blogs, blog=blog, date_begin=date_begin, date_end=date_end, tags=tag, state=state, page=page, search=search) # in case of a redirection need by `_prepare_blog_values` we follow it if isinstance(values, werkzeug.wrappers.Response): return values if blog: values['main_object'] = blog values['edit_in_backend'] = True values['blog_url'] = QueryURL('', ['blog', 'tag'], blog=blog, tag=tag, date_begin=date_begin, date_end=date_end, search=search) else: values['blog_url'] = QueryURL('/blog', ['tag'], date_begin=date_begin, date_end=date_end, search=search) return request.render("website_blog.blog_post_short", values)
def blogs(self, page=1, **post): Blog = request.env['blog.blog'] blogs = Blog.search([], limit=2) if len(blogs) == 1: return werkzeug.utils.redirect('/blog/%s' % slug(blogs[0]), code=302) BlogPost = request.env['blog.post'] total = BlogPost.search([], count=True) pager = request.website.pager( url='/blog', total=total, page=page, step=self._blog_post_per_page, ) posts = BlogPost.search([], offset=(page - 1) * self._blog_post_per_page, limit=self._blog_post_per_page) blog_url = QueryURL('', ['blog', 'tag']) return request.render("website_blog.latest_blogs", { 'posts': posts, 'pager': pager, 'blog_url': blog_url, })
def product(self, product, category='', search='', **kwargs): product_context = dict(request.env.context, active_id=product.id, partner=request.env.user.partner_id) ProductCategory = request.env['product.public.category'] if category: category = ProductCategory.browse(int(category)).exists() attrib_list = request.httprequest.args.getlist('attrib') attrib_values = [[int(x) for x in v.split("-")] for v in attrib_list if v] attrib_set = {v[1] for v in attrib_values} keep = QueryURL('/shop', category=category and category.id, search=search, attrib=attrib_list) categs = ProductCategory.search([('parent_id', '=', False)]) pricelist = request.website.get_current_pricelist() from_currency = request.env.user.company_id.currency_id to_currency = pricelist.currency_id compute_currency = lambda price: from_currency.compute( price, to_currency) if not product_context.get('pricelist'): product_context['pricelist'] = pricelist.id product = product.with_context(product_context) values = { 'search': search, 'category': category, 'pricelist': pricelist, 'attrib_values': attrib_values, 'compute_currency': compute_currency, 'attrib_set': attrib_set, 'keep': keep, 'categories': categs, 'main_object': product, 'product': product, 'get_attribute_value_ids': self.get_attribute_value_ids, } return request.render("website_sale.product", values)
def shop(self, page=0, category=None, search='', ppg=False, **post): if ppg: try: ppg = int(ppg) except ValueError: ppg = PPG post["ppg"] = ppg else: ppg = PPG attrib_list = request.httprequest.args.getlist('attrib') attrib_values = [[int(x) for x in v.split("-")] for v in attrib_list if v] attributes_ids = {v[0] for v in attrib_values} attrib_set = {v[1] for v in attrib_values} # For Tags tag_list = request.httprequest.args.getlist('tags') tag_values = [list(map(str, v)) for v in tag_list if v] tag_set = set([int(v[0]) for v in tag_values]) # For Brands brand_list = request.httprequest.args.getlist('brands') brand_values = [list(map(str, v)) for v in brand_list if v] brand_set = set([int(v[0]) for v in brand_values]) domain = self._get_search_domain(search, category, attrib_values, list(tag_set), list(brand_set)) keep = QueryURL('/shop', category=category and int(category), search=search, attrib=attrib_list, order=post.get('order')) compute_currency, pricelist_context, pricelist = self._get_compute_currency_and_context( ) request.context = dict(request.context, pricelist=pricelist.id, partner=request.env.user.partner_id) url = "/shop" if search: post["search"] = search if category: category = request.env['product.public.category'].browse( int(category)) website = [] for web in category.website_ids: website.append(web.id) if request.website.id not in website: return request.render('website.404') url = "/shop/category/%s" % slug(category) if attrib_list: post['attrib'] = attrib_list categs = request.env['product.public.category'].search([ ('parent_id', '=', False), ('website_ids', 'in', request.website.id) ]) Product = request.env['product.template'] parent_category_ids = [] if category: parent_category_ids = [category.id] current_category = category while current_category.parent_id: parent_category_ids.append(current_category.parent_id.id) current_category = current_category.parent_id product_count = Product.search_count(domain) pager = request.website.pager(url=url, total=product_count, page=page, step=ppg, scope=7, url_args=post) products = Product.search(domain, limit=ppg, offset=pager['offset'], order=self._get_search_order(post)) ProductAttribute = request.env['product.attribute'] ProductBrand = request.env['product.brand'] ProductTag = request.env['product.tags'] if products: attributes = ProductAttribute.search([ ('attribute_line_ids.product_tmpl_id', 'in', products.ids) ]) prod_brands = [] prod_tags = [] for product in products: if product.brand_id: prod_brands.append(product.brand_id.id) if product.tag_ids: for tag_id in product.tag_ids.ids: prod_tags.append(tag_id) brands = ProductBrand.browse(list(set(prod_brands))) tags = ProductTag.browse(list(set(prod_tags))) else: attributes = ProductAttribute.browse(attributes_ids) brands = ProductBrand.browse(brand_set) tags = ProductTag.browse(tag_set) limits = request.env['product.view.limit'].search([]) values = { 'search': search, 'category': category, 'attrib_values': attrib_values, 'attrib_set': attrib_set, 'pager': pager, 'pricelist': pricelist, 'products': products, 'tag_set': tag_set, 'brand_set': brand_set, 'search_count': product_count, # common for all searchbox 'bins': TableCompute().process(products, ppg), 'rows': PPR, 'categories': categs, 'attributes': attributes, 'compute_currency': compute_currency, 'keep': keep, 'limits': limits, 'parent_category_ids': parent_category_ids, 'get_attribute_value_ids': self.get_attribute_value_ids, 'tags': tags, 'brands': brands, } if category: values['main_object'] = category return request.render("website_sale.products", values)
def blog(self, blog=None, tag=None, page=1, **opt): """ Prepare all values to display the blog. :return dict values: values for the templates, containing - 'blog': current blog - 'blogs': all blogs for navigation - 'pager': pager of posts - 'active_tag_ids' : list of active tag ids, - 'tags_list' : function to built the comma-separated tag list ids (for the url), - 'tags': all tags, for navigation - 'state_info': state of published/unpublished filter - 'nav_list': a dict [year][month] for archives navigation - 'date': date_begin optional parameter, used in archives navigation - 'blog_url': help object to create URLs """ date_begin, date_end, state = opt.get('date_begin'), opt.get( 'date_end'), opt.get('state') published_count, unpublished_count = 0, 0 BlogPost = request.env['blog.post'] Blog = request.env['blog.blog'] blogs = Blog.search([], order="create_date asc") # build the domain for blog post to display domain = [] # retrocompatibility to accept tag as slug active_tag_ids = tag and [int(unslug(t)[1]) for t in tag.split(',')] or [] if active_tag_ids: domain += [('tag_ids', 'in', active_tag_ids)] if blog: domain += [('blog_id', '=', blog.id)] if date_begin and date_end: domain += [("post_date", ">=", date_begin), ("post_date", "<=", date_end)] if request.env.user.has_group('website.group_website_designer'): count_domain = domain + [("website_published", "=", True), ("post_date", "<=", fields.Datetime.now()) ] published_count = BlogPost.search_count(count_domain) unpublished_count = BlogPost.search_count(domain) - published_count if state == "published": domain += [("website_published", "=", True), ("post_date", "<=", fields.Datetime.now())] elif state == "unpublished": domain += [ '|', ("website_published", "=", False), ("post_date", ">", fields.Datetime.now()) ] else: domain += [("post_date", "<=", fields.Datetime.now())] blog_url = QueryURL('', ['blog', 'tag'], blog=blog, tag=tag, date_begin=date_begin, date_end=date_end) blog_posts = BlogPost.search(domain, order="post_date desc") pager = request.website.pager( url=request.httprequest.path.partition('/page/')[0], total=len(blog_posts), page=page, step=self._blog_post_per_page, url_args=opt, ) pager_begin = (page - 1) * self._blog_post_per_page pager_end = page * self._blog_post_per_page blog_posts = blog_posts[pager_begin:pager_end] all_tags = blog.all_tags()[blog.id] # function to create the string list of tag ids, and toggle a given one. # used in the 'Tags Cloud' template. def tags_list(tag_ids, current_tag): tag_ids = list(tag_ids) # required to avoid using the same list if current_tag in tag_ids: tag_ids.remove(current_tag) else: tag_ids.append(current_tag) tag_ids = request.env['blog.tag'].browse(tag_ids).exists() return ','.join(slug(tag) for tag in tag_ids) values = { 'blog': blog, 'blogs': blogs, 'main_object': blog, 'tags': all_tags, 'state_info': { "state": state, "published": published_count, "unpublished": unpublished_count }, 'active_tag_ids': active_tag_ids, 'tags_list': tags_list, 'blog_posts': blog_posts, 'blog_posts_cover_properties': [json.loads(b.cover_properties) for b in blog_posts], 'pager': pager, 'nav_list': self.nav_list(blog), 'blog_url': blog_url, 'date': date_begin, } response = request.render("website_blog.blog_post_short", values) return response
def blog_post(self, blog, blog_post, tag_id=None, page=1, enable_editor=None, **post): """ Prepare all values to display the blog. :return dict values: values for the templates, containing - 'blog_post': browse of the current post - 'blog': browse of the current blog - 'blogs': list of browse records of blogs - 'tag': current tag, if tag_id in parameters - 'tags': all tags, for tag-based navigation - 'pager': a pager on the comments - 'nav_list': a dict [year][month] for archives navigation - 'next_post': next blog post, to direct the user towards the next interesting post """ BlogPost = request.env['blog.post'] date_begin, date_end = post.get('date_begin'), post.get('date_end') pager_url = "/blogpost/%s" % blog_post.id pager = request.website.pager(url=pager_url, total=len(blog_post.website_message_ids), page=page, step=self._post_comment_per_page, scope=7) pager_begin = (page - 1) * self._post_comment_per_page pager_end = page * self._post_comment_per_page comments = blog_post.website_message_ids[pager_begin:pager_end] tag = None if tag_id: tag = request.env['blog.tag'].browse(int(tag_id)) blog_url = QueryURL('', ['blog', 'tag'], blog=blog_post.blog_id, tag=tag, date_begin=date_begin, date_end=date_end) if not blog_post.blog_id.id == blog.id: return request.redirect("/blog/%s/post/%s" % (slug(blog_post.blog_id), slug(blog_post))) tags = request.env['blog.tag'].search([]) # Find next Post all_post = BlogPost.search([('blog_id', '=', blog.id)]) if not request.env.user.has_group('website.group_website_designer'): all_post = all_post.filtered( lambda r: r.post_date <= fields.Datetime.now()) if blog_post not in all_post: return request.redirect("/blog/%s" % (slug(blog_post.blog_id))) # should always return at least the current post all_post_ids = all_post.ids current_blog_post_index = all_post_ids.index(blog_post.id) nb_posts = len(all_post_ids) next_post_id = all_post_ids[(current_blog_post_index + 1) % nb_posts] if nb_posts > 1 else None next_post = next_post_id and BlogPost.browse(next_post_id) or False values = { 'tags': tags, 'tag': tag, 'blog': blog, 'blog_post': blog_post, 'blog_post_cover_properties': json.loads(blog_post.cover_properties), 'main_object': blog_post, 'nav_list': self.nav_list(blog), 'enable_editor': enable_editor, 'next_post': next_post, 'next_post_cover_properties': json.loads(next_post.cover_properties) if next_post else {}, 'date': date_begin, 'blog_url': blog_url, 'pager': pager, 'comments': comments, } response = request.render("website_blog.blog_post_complete", values) request.session[request.session.sid] = request.session.get( request.session.sid, []) if not (blog_post.id in request.session[request.session.sid]): request.session[request.session.sid].append(blog_post.id) # Increase counter blog_post.sudo().write({ 'visits': blog_post.visits + 1, }) return response
def shop(self, page=0, category=None, search='', ppg=False, **post): if ppg: try: ppg = int(ppg) except ValueError: ppg = PPG post["ppg"] = ppg else: ppg = PPG attrib_list = request.httprequest.args.getlist('attrib') attrib_values = [[int(x) for x in v.split("-")] for v in attrib_list if v] attributes_ids = {v[0] for v in attrib_values} attrib_set = {v[1] for v in attrib_values} domain = self._get_search_domain(search, category, attrib_values) keep = QueryURL('/shop', category=category and int(category), search=search, attrib=attrib_list, order=post.get('order')) compute_currency, pricelist_context, pricelist = self._get_compute_currency_and_context( ) request.context = dict(request.context, pricelist=pricelist.id, partner=request.env.user.partner_id) url = "/shop" if search: post["search"] = search if category: category = request.env['product.public.category'].browse( int(category)) url = "/shop/category/%s" % slug(category) if attrib_list: post['attrib'] = attrib_list categs = request.env['product.public.category'].search([('parent_id', '=', False)]) Product = request.env['product.template'] parent_category_ids = [] if category: parent_category_ids = [category.id] current_category = category while current_category.parent_id: parent_category_ids.append(current_category.parent_id.id) current_category = current_category.parent_id product_count = Product.search_count(domain) pager = request.website.pager(url=url, total=product_count, page=page, step=ppg, scope=7, url_args=post) products = Product.search(domain, limit=ppg, offset=pager['offset'], order=self._get_search_order(post)) ProductAttribute = request.env['product.attribute'] if products: # get all products without limit selected_products = Product.search(domain, limit=False) attributes = ProductAttribute.search([ ('attribute_line_ids.product_tmpl_id', 'in', selected_products.ids) ]) else: attributes = ProductAttribute.browse(attributes_ids) values = { 'search': search, 'category': category, 'attrib_values': attrib_values, 'attrib_set': attrib_set, 'pager': pager, 'pricelist': pricelist, 'products': products, 'search_count': product_count, # common for all searchbox 'bins': TableCompute().process(products, ppg), 'rows': PPR, 'categories': categs, 'attributes': attributes, 'compute_currency': compute_currency, 'keep': keep, 'parent_category_ids': parent_category_ids, } if category: values['main_object'] = category return request.render("website_sale.products", values)
def blog_post(self, blog, blog_post, tag_id=None, page=1, enable_editor=None, **post): """ Prepare all values to display the blog. :return dict values: values for the templates, containing - 'blog_post': browse of the current post - 'blog': browse of the current blog - 'blogs': list of browse records of blogs - 'tag': current tag, if tag_id in parameters - 'tags': all tags, for tag-based navigation - 'pager': a pager on the comments - 'nav_list': a dict [year][month] for archives navigation - 'next_post': next blog post, to direct the user towards the next interesting post """ if not blog.can_access_from_current_website(): raise werkzeug.exceptions.NotFound() BlogPost = request.env['blog.post'] date_begin, date_end = post.get('date_begin'), post.get('date_end') domain = request.website.website_domain() blogs = blog.search(domain, order="create_date, id asc") tag = None if tag_id: tag = request.env['blog.tag'].browse(int(tag_id)) blog_url = QueryURL('', ['blog', 'tag'], blog=blog_post.blog_id, tag=tag, date_begin=date_begin, date_end=date_end) if not blog_post.blog_id.id == blog.id: return request.redirect("/blog/%s/%s" % (slug(blog_post.blog_id), slug(blog_post)), code=301) tags = request.env['blog.tag'].search([]) # Find next Post blog_post_domain = [('blog_id', '=', blog.id)] if not request.env.user.has_group('website.group_website_designer'): blog_post_domain += [('post_date', '<=', fields.Datetime.now())] all_post = BlogPost.search(blog_post_domain) if blog_post not in all_post: return request.redirect("/blog/%s" % (slug(blog_post.blog_id))) # should always return at least the current post all_post_ids = all_post.ids current_blog_post_index = all_post_ids.index(blog_post.id) nb_posts = len(all_post_ids) next_post_id = all_post_ids[(current_blog_post_index + 1) % nb_posts] if nb_posts > 1 else None next_post = next_post_id and BlogPost.browse(next_post_id) or False values = { 'tags': tags, 'tag': tag, 'blog': blog, 'blog_post': blog_post, 'blogs': blogs, 'main_object': blog_post, 'nav_list': self.nav_list(blog), 'enable_editor': enable_editor, 'next_post': next_post, 'date': date_begin, 'blog_url': blog_url, } response = request.render("website_blog.blog_post_complete", values) if blog_post.id not in request.session.get('posts_viewed', []): if sql.increment_field_skiplock(blog_post, 'visits'): if not request.session.get('posts_viewed'): request.session['posts_viewed'] = [] request.session['posts_viewed'].append(blog_post.id) request.session.modified = True return response
def events(self, page=1, **searches): Event = request.env['event.event'] SudoEventType = request.env['event.type'].sudo() searches.setdefault('search', '') searches.setdefault('date', 'all') searches.setdefault('tags', '') searches.setdefault('type', 'all') searches.setdefault('country', 'all') website = request.website today = fields.Datetime.today() def sdn(date): return fields.Datetime.to_string( date.replace(hour=23, minute=59, second=59)) def sd(date): return fields.Datetime.to_string(date) def get_month_filter_domain(filter_name, months_delta): first_day_of_the_month = today.replace(day=1) filter_string = _('This month') if months_delta == 0 \ else format_date(request.env, value=today + relativedelta(months=months_delta), date_format='LLLL', lang_code=get_lang(request.env).code).capitalize() return [ filter_name, filter_string, [("date_end", ">=", sd(first_day_of_the_month + relativedelta(months=months_delta))), ("date_begin", "<", sd(first_day_of_the_month + relativedelta(months=months_delta + 1)))], 0 ] dates = [ ['all', _('Upcoming Events'), [("date_end", ">", sd(today))], 0], [ 'today', _('Today'), [("date_end", ">", sd(today)), ("date_begin", "<", sdn(today))], 0 ], get_month_filter_domain('month', 0), ['old', _('Past Events'), [("date_end", "<", sd(today))], 0], ] # search domains domain_search = {'website_specific': website.website_domain()} if searches['search']: domain_search['search'] = [('name', 'ilike', searches['search'])] search_tags = self._extract_searched_event_tags(searches) if search_tags: # Example: You filter on age: 10-12 and activity: football. # Doing it this way allows to only get events who are tagged "age: 10-12" AND "activity: football". # Add another tag "age: 12-15" to the search and it would fetch the ones who are tagged: # ("age: 10-12" OR "age: 12-15") AND "activity: football grouped_tags = defaultdict(list) for tag in search_tags: grouped_tags[tag.category_id].append(tag) domain_search['tags'] = [] for group in grouped_tags: domain_search['tags'] = expression.AND([ domain_search['tags'], [('tag_ids', 'in', [tag.id for tag in grouped_tags[group]])] ]) current_date = None current_type = None current_country = None for date in dates: if searches["date"] == date[0]: domain_search["date"] = date[2] if date[0] != 'all': current_date = date[1] if searches["type"] != 'all': current_type = SudoEventType.browse(int(searches['type'])) domain_search["type"] = [("event_type_id", "=", int(searches["type"]))] if searches["country"] != 'all' and searches["country"] != 'online': current_country = request.env['res.country'].browse( int(searches['country'])) domain_search["country"] = [ '|', ("country_id", "=", int(searches["country"])), ("country_id", "=", False) ] elif searches["country"] == 'online': domain_search["country"] = [("country_id", "=", False)] def dom_without(without): domain = [] for key, search in domain_search.items(): if key != without: domain += search return domain # count by domains without self search for date in dates: if date[0] != 'old': date[3] = Event.search_count(dom_without('date') + date[2]) domain = dom_without('type') domain = dom_without('country') countries = Event.read_group(domain, ["id", "country_id"], groupby="country_id", orderby="country_id") countries.insert( 0, { 'country_id_count': sum([ int(country['country_id_count']) for country in countries ]), 'country_id': ("all", _("All Countries")) }) step = 12 # Number of events per page event_count = Event.search_count(dom_without("none")) pager = website.pager(url="/event", url_args=searches, total=event_count, page=page, step=step, scope=5) order = 'date_begin' if searches.get('date', 'all') == 'old': order = 'date_begin desc' order = 'is_published desc, ' + order events = Event.search(dom_without("none"), limit=step, offset=pager['offset'], order=order) keep = QueryURL( '/event', **{ key: value for key, value in searches.items() if (key == 'search' or value != 'all') }) values = { 'current_date': current_date, 'current_country': current_country, 'current_type': current_type, 'event_ids': events, # event_ids used in website_event_track so we keep name as it is 'dates': dates, 'categories': request.env['event.tag.category'].search([]), 'countries': countries, 'pager': pager, 'searches': searches, 'search_tags': search_tags, 'keep': keep, } if searches['date'] == 'old': # the only way to display this content is to set date=old so it must be canonical values['canonical_params'] = OrderedMultiDict([('date', 'old')]) return request.render("website_event.index", values)