예제 #1
0
 def partners_detail(self, partner_id, **post):
     _, partner_id = unslug(partner_id)
     current_grade, current_country = None, None
     grade_id = post.get('grade_id')
     country_id = post.get('country_id')
     if grade_id:
         current_grade = request.env['res.partner.grade'].browse(
             int(grade_id)).exists()
     if country_id:
         current_country = request.env['res.country'].browse(
             int(country_id)).exists()
     if partner_id:
         partner = request.env['res.partner'].sudo().browse(partner_id)
         is_website_publisher = request.env['res.users'].has_group(
             'website.group_website_publisher')
         if partner.exists() and (partner.website_published
                                  or is_website_publisher):
             values = {
                 'main_object': partner,
                 'partner': partner,
                 'current_grade': current_grade,
                 'current_country': current_country
             }
             return request.render("website_crm_partner_assign.partner",
                                   values)
     return self.partners(**post)
예제 #2
0
 def partners_detail(self, partner_id, **post):
     _, partner_id = unslug(partner_id)
     if partner_id:
         partner = request.env['res.partner'].sudo().browse(partner_id)
         if partner.exists() and partner.website_published:
             values = {}
             values['main_object'] = values['partner'] = partner
             return request.render("website_customer.details", values)
     return self.customers(**post)
예제 #3
0
 def partners_detail(self, partner_id, **post):
     _, partner_id = unslug(partner_id)
     if partner_id:
         partner = request.env['res.partner'].sudo().browse(partner_id)
         if partner.exists(
         ) and partner.website_published:  # TODO should be done with access rules
             values = {}
             values['main_object'] = values['partner'] = partner
             return request.render("website_membership.partner", values)
     return self.members(**post)
예제 #4
0
 def partners_detail(self, partner_id, **post):
     _, partner_id = unslug(partner_id)
     if partner_id:
         partner_sudo = request.env['res.partner'].sudo().browse(partner_id)
         is_website_publisher = request.env['res.users'].has_group(
             'website.group_website_publisher')
         if partner_sudo.exists() and (partner_sudo.website_published
                                       or is_website_publisher):
             values = {
                 'main_object': partner_sudo,
                 'partner': partner_sudo,
                 'edit_page': False
             }
             return request.render("website_partner.partner_page", values)
     return request.not_found()
예제 #5
0
    def test_unslug(self):
        tests = {
            '': (None, None),
            'foo': (None, None),
            'foo-': (None, None),
            '-': (None, None),
            'foo-1': ('foo', 1),
            'foo-bar-1': ('foo-bar', 1),
            'foo--1': ('foo', -1),
            '1': (None, 1),
            '1-1': ('1', 1),
            '--1': (None, None),
            'foo---1': (None, None),
            'foo1': (None, None),
        }

        for slug, expected in tests.items():
            self.assertEqual(unslug(slug), expected)
예제 #6
0
    def customers(self, country=None, industry=None, page=0, **post):
        Tag = request.env['res.partner.tag']
        Partner = request.env['res.partner']
        search_value = post.get('search')

        domain = [('website_published', '=', True),
                  ('assigned_partner_id', '!=', False)]
        if search_value:
            domain += [
                '|',
                '|',
                ('name', 'ilike', search_value),
                ('website_description', 'ilike', search_value),
                ('industry_id.name', 'ilike', search_value),
            ]

        tag_id = post.get('tag_id')
        if tag_id:
            tag_id = unslug(tag_id)[1] or 0
            domain += [('website_tag_ids', 'in', tag_id)]

        # group by industry, based on customers found with the search(domain)
        industries = Partner.sudo().read_group(domain, ["id", "industry_id"],
                                               groupby="industry_id",
                                               orderby="industry_id")
        partners_count = Partner.sudo().search_count(domain)

        if industry:
            domain.append(('industry_id', '=', industry.id))
            if industry.id not in (x['industry_id'][0] for x in industries
                                   if x['industry_id']):
                if industry.exists():
                    industries.append({
                        'industry_id_count':
                        0,
                        'industry_id': (industry.id, industry.name)
                    })

        industries.sort(key=lambda d: (d.get('industry_id') or (0, ''))[1])

        industries.insert(
            0, {
                'industry_id_count': partners_count,
                'industry_id': (0, _("All Sectors of Activity"))
            })

        # group by country, based on customers found with the search(domain)
        countries = Partner.sudo().read_group(domain, ["id", "country_id"],
                                              groupby="country_id",
                                              orderby="country_id")
        country_count = Partner.sudo().search_count(domain)

        if country:
            domain += [('country_id', '=', country.id)]
            if country.id not in (x['country_id'][0] for x in countries
                                  if x['country_id']):
                if country.exists():
                    countries.append({
                        'country_id_count': 0,
                        'country_id': (country.id, country.name)
                    })
                    countries.sort(
                        key=lambda d: (d['country_id'] or (0, ""))[1])

        countries.insert(
            0, {
                'country_id_count': country_count,
                'country_id': (0, _("All Countries"))
            })

        # search customers to display
        partner_count = Partner.sudo().search_count(domain)

        # pager
        url = '/customers'
        if industry:
            url += '/industry/%s' % industry.id
        if country:
            url += '/country/%s' % country.id
        pager = request.website.pager(url=url,
                                      total=partner_count,
                                      page=page,
                                      step=self._references_per_page,
                                      scope=7,
                                      url_args=post)

        partners = Partner.sudo().search(domain,
                                         offset=pager['offset'],
                                         limit=self._references_per_page)
        google_map_partner_ids = ','.join(str(it) for it in partners.ids)
        google_maps_api_key = request.website.google_maps_api_key

        tags = Tag.search([('website_published', '=', True),
                           ('partner_ids', 'in', partners.ids)],
                          order='classname, name ASC')
        tag = tag_id and Tag.browse(tag_id) or False

        values = {
            'countries': countries,
            'current_country_id': country.id if country else 0,
            'current_country': country or False,
            'industries': industries,
            'current_industry_id': industry.id if industry else 0,
            'current_industry': industry or False,
            'partners': partners,
            'google_map_partner_ids': google_map_partner_ids,
            'pager': pager,
            'post': post,
            'search_path': "?%s" % werkzeug.url_encode(post),
            'tag': tag,
            'tags': tags,
            'google_maps_api_key': google_maps_api_key,
        }
        return request.render("website_customer.index", values)
예제 #7
0
    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
        """
        if not blog.can_access_from_current_website():
            raise werkzeug.exceptions.NotFound()

        date_begin, date_end, state = opt.get('date_begin'), opt.get(
            'date_end'), opt.get('state')
        published_count, unpublished_count = 0, 0

        domain = request.website.website_domain()

        BlogPost = request.env['blog.post']

        Blog = request.env['blog.blog']
        blogs = Blog.search(domain, order="create_date asc")

        # 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)

        tag_category = sorted(all_tags.mapped('category_id'),
                              key=lambda category: category.name.upper())
        other_tags = sorted(all_tags.filtered(lambda x: not x.category_id),
                            key=lambda tag: tag.name.upper())

        values = {
            'blog':
            blog,
            'blogs':
            blogs,
            'main_object':
            blog,
            'other_tags':
            other_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,
            'tag_category':
            tag_category,
        }
        response = request.render("website_blog.blog_post_short", values)
        return response
예제 #8
0
    def _prepare_blog_values(self, blogs, blog=False, date_begin=False, date_end=False, tags=False, state=False, page=False):
        """ Prepare all values to display the blogs index page or one specific blog"""
        BlogPost = request.env['blog.post']

        # prepare domain
        domain = request.website.website_domain()

        if blog:
            domain += [('blog_id', '=', blog.id)]

        if date_begin and date_end:
            domain += [("post_date", ">=", date_begin), ("post_date", "<=", date_end)]

        active_tag_ids = tags and [unslug(tag)[1] for tag in tags.split(',')] or []
        if active_tag_ids:
            fixed_tag_slug = ",".join(slug(t) for t in request.env['blog.tag'].browse(active_tag_ids))
            if fixed_tag_slug != tags:
                return request.redirect(request.httprequest.full_path.replace("/tag/%s/" % tags, "/tag/%s/" % fixed_tag_slug, 1), 301)

            domain += [('tag_ids', 'in', active_tag_ids)]

        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())]

        use_cover = request.website.viewref('website_blog.opt_blog_cover_post').active
        fullwidth_cover = request.website.viewref('website_blog.opt_blog_cover_post_fullwidth_design').active

        # if blog, we show blog title, if use_cover and not fullwidth_cover we need pager + latest always
        offset = (page - 1) * self._blog_post_per_page
        first_post = BlogPost
        if not blog:
            first_post = BlogPost.search(domain + [('website_published', '=', True)], order="post_date desc, id asc", limit=1)
            if use_cover and not fullwidth_cover:
                offset += 1

        posts = BlogPost.search(domain, offset=offset, limit=self._blog_post_per_page, order="is_published desc, post_date desc, id asc")
        total = BlogPost.search_count(domain)

        pager = request.website.pager(
            url=request.httprequest.path.partition('/page/')[0],
            total=total,
            page=page,
            step=self._blog_post_per_page,
        )

        all_tags = blog and blogs.all_tags()[blog.id] or blogs.all_tags(join=True)
        tag_category = sorted(all_tags.mapped('category_id'), key=lambda category: category.name.upper())
        other_tags = sorted(all_tags.filtered(lambda x: not x.category_id), key=lambda tag: tag.name.upper())

        # for performance prefetch the first post with the others
        post_ids = (first_post | posts).ids

        return {
            'date_begin': date_begin,
            'date_end': date_end,
            'first_post': first_post.with_prefetch(post_ids),
            'other_tags': other_tags,
            'tag_category': tag_category,
            'nav_list': self.nav_list(),
            'tags_list': self.tags_list,
            'pager': pager,
            'posts': posts.with_prefetch(post_ids),
            'tag': tags,
            'active_tag_ids': active_tag_ids,
            'domain': domain,
            'state_info': state and {"state": state, "published": published_count, "unpublished": unpublished_count},
            'blogs': blogs,
            'blog': blog,
        }