|
| 1 | +# Copyright (C) 2020 - TODAY, Marcel Savegnago - Escodoo). |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | + |
| 5 | +from odoo import http |
| 6 | +from odoo.exceptions import AccessError, MissingError |
| 7 | +from odoo.http import request |
| 8 | +from odoo.osv.expression import OR |
| 9 | +from odoo.tools.translate import _ |
| 10 | + |
| 11 | +from odoo.addons.portal.controllers.portal import CustomerPortal, pager as portal_pager |
| 12 | + |
| 13 | + |
| 14 | +class CustomerPortal(CustomerPortal): |
| 15 | + def _prepare_portal_layout_values(self): |
| 16 | + values = super(CustomerPortal, self)._prepare_portal_layout_values() |
| 17 | + values["document_page_count"] = request.env["document.page"].search_count( |
| 18 | + [("type", "=", "content")] |
| 19 | + ) |
| 20 | + return values |
| 21 | + |
| 22 | + def _document_page_get_page_view_values( |
| 23 | + self, document_page, access_token, **kwargs |
| 24 | + ): |
| 25 | + values = { |
| 26 | + "page_name": "document_page", |
| 27 | + "document_page": document_page, |
| 28 | + } |
| 29 | + return self._get_page_view_values( |
| 30 | + document_page, |
| 31 | + access_token, |
| 32 | + values, |
| 33 | + "my_document_pages_history", |
| 34 | + False, |
| 35 | + **kwargs |
| 36 | + ) |
| 37 | + |
| 38 | + @http.route( |
| 39 | + ["/my/knowledge/documents/", "/my/knowledge/documents/page/<int:page>"], |
| 40 | + type="http", |
| 41 | + auth="user", |
| 42 | + website=True, |
| 43 | + ) |
| 44 | + def portal_my_knowledge_document_pages( |
| 45 | + self, |
| 46 | + page=1, |
| 47 | + date_begin=None, |
| 48 | + date_end=None, |
| 49 | + sortby=None, |
| 50 | + search=None, |
| 51 | + search_in="content", |
| 52 | + **kw |
| 53 | + ): |
| 54 | + values = self._prepare_portal_layout_values() |
| 55 | + domain = [("type", "=", "content")] |
| 56 | + |
| 57 | + searchbar_sortings = { |
| 58 | + "date": {"label": _("Newest"), "order": "create_date desc"}, |
| 59 | + "name": {"label": _("Name"), "order": "name"}, |
| 60 | + "parent": {"label": _("Category"), "order": "parent_id"}, |
| 61 | + } |
| 62 | + searchbar_inputs = { |
| 63 | + "content": { |
| 64 | + "input": "content", |
| 65 | + "label": _('Search <span class="nolabel"> (in Content)</span>'), |
| 66 | + }, |
| 67 | + "all": {"input": "all", "label": _("Search in All")}, |
| 68 | + } |
| 69 | + |
| 70 | + # default sort by value |
| 71 | + if not sortby: |
| 72 | + sortby = "date" |
| 73 | + order = searchbar_sortings[sortby]["order"] |
| 74 | + |
| 75 | + if date_begin and date_end: |
| 76 | + domain += [ |
| 77 | + ("create_date", ">", date_begin), |
| 78 | + ("create_date", "<=", date_end), |
| 79 | + ] |
| 80 | + |
| 81 | + # search |
| 82 | + if search and search_in: |
| 83 | + search_domain = [] |
| 84 | + if search_in in ("content", "all"): |
| 85 | + search_domain = OR( |
| 86 | + [ |
| 87 | + search_domain, |
| 88 | + ["|", ("name", "ilike", search), ("content", "ilike", search)], |
| 89 | + ] |
| 90 | + ) |
| 91 | + domain += search_domain |
| 92 | + |
| 93 | + # pager |
| 94 | + document_pages_count = request.env["document.page"].search_count(domain) |
| 95 | + pager = portal_pager( |
| 96 | + url="/my/knowledge/documents", |
| 97 | + url_args={"date_begin": date_begin, "date_end": date_end, "sortby": sortby}, |
| 98 | + total=document_pages_count, |
| 99 | + page=page, |
| 100 | + step=self._items_per_page, |
| 101 | + ) |
| 102 | + |
| 103 | + document_pages = request.env["document.page"].search( |
| 104 | + domain, order=order, limit=self._items_per_page, offset=pager["offset"] |
| 105 | + ) |
| 106 | + request.session["my_document_pages_history"] = document_pages.ids[:100] |
| 107 | + |
| 108 | + values.update( |
| 109 | + { |
| 110 | + "date": date_begin, |
| 111 | + "document_pages": document_pages, |
| 112 | + "page_name": "document_page", |
| 113 | + "default_url": "/my/knowledge/s", |
| 114 | + "pager": pager, |
| 115 | + "searchbar_sortings": searchbar_sortings, |
| 116 | + "searchbar_inputs": searchbar_inputs, |
| 117 | + "sortby": sortby, |
| 118 | + "search_in": search_in, |
| 119 | + "search": search, |
| 120 | + } |
| 121 | + ) |
| 122 | + return request.render( |
| 123 | + "document_page_portal.portal_my_knowledge_document_pages", values |
| 124 | + ) |
| 125 | + |
| 126 | + @http.route( |
| 127 | + [ |
| 128 | + "/knowledge/document/<int:document_page_id>", |
| 129 | + "/knowledge/document/<int:document_page_id>/<token>", |
| 130 | + "/my/knowledge/document/<int:document_page_id>", |
| 131 | + ], |
| 132 | + type="http", |
| 133 | + auth="public", |
| 134 | + website=True, |
| 135 | + ) |
| 136 | + def document_pages_followup(self, document_page_id=None, access_token=None, **kw): |
| 137 | + try: |
| 138 | + document_page_sudo = self._document_check_access( |
| 139 | + "document.page", document_page_id, access_token |
| 140 | + ) |
| 141 | + except (AccessError, MissingError): |
| 142 | + return request.redirect("/my") |
| 143 | + |
| 144 | + values = self._document_page_get_page_view_values( |
| 145 | + document_page_sudo, access_token, **kw |
| 146 | + ) |
| 147 | + return request.render("document_page_portal.document_pages_followup", values) |
0 commit comments