From 679e29f4da20c683f20f8f5f00b9478f3260cbed Mon Sep 17 00:00:00 2001 From: Marc Durepos Date: Thu, 21 Sep 2023 18:15:42 -0400 Subject: [PATCH] got portal working to at least give a download button and chatter on documents. No preview yet. --- bemade_documents_portal/__manifest__.py | 2 +- bemade_documents_portal/controllers/portal.py | 33 +++++++-- bemade_documents_portal/models/documents.py | 5 ++ .../views/document_portal_templates.xml | 72 ++++++++++++++++++- 4 files changed, 105 insertions(+), 7 deletions(-) diff --git a/bemade_documents_portal/__manifest__.py b/bemade_documents_portal/__manifest__.py index ca097a9..b5e503b 100644 --- a/bemade_documents_portal/__manifest__.py +++ b/bemade_documents_portal/__manifest__.py @@ -24,7 +24,7 @@ 'author': 'Bemade Inc.', 'website': 'https://www.bemade.org', 'license': 'OPL-1', - 'depends': ['documents', 'portal'], + 'depends': ['documents', 'portal', 'mail_enterprise', 'im_livechat'], 'data': ['views/document_portal_templates.xml'], 'demo': [], 'installable': True, diff --git a/bemade_documents_portal/controllers/portal.py b/bemade_documents_portal/controllers/portal.py index 64a96b2..0e0c917 100644 --- a/bemade_documents_portal/controllers/portal.py +++ b/bemade_documents_portal/controllers/portal.py @@ -1,5 +1,7 @@ from odoo.addons.portal.controllers.portal import CustomerPortal from odoo.http import request, route +from odoo.exceptions import AccessError, MissingError +from odoo import _ class DocumentCustomerPortal(CustomerPortal): @@ -17,16 +19,12 @@ class DocumentCustomerPortal(CustomerPortal): domain = self._prepare_documents_domain() documents_count = Documents.search_count(domain) documents = Documents.search(domain) - print(f"Documents: {documents_count}...") - for doc in documents: - print(doc.name) values.update({ 'documents_count': documents_count, 'documents': documents.sudo(), 'default_url': '/my/documents', 'page_name': 'my_documents', }) - print return request.render("bemade_documents_portal.portal_my_documents", values) def _prepare_documents_domain(self): @@ -37,3 +35,30 @@ class DocumentCustomerPortal(CustomerPortal): ('partner_id', '=', partner.id), ('owner_id', '=', user.id), ] + + def _render_record_template(self, values): + """ Override this method to apply a different template for a single document + record on the portal. """ + return request.render("bemade_documents_portal.document_portal_template", values) + + @route('/my/documents/', type='http', auth='user', website=True) + def portal_document_page(self, document_id, download=False, **kwargs): + document = request.env['documents.document'].browse(document_id) + if not document: + raise MissingError(_('This document does not exist.')) + if download: + return self._download_attachment(document) + values={ + 'document': document, + 'page_name': 'my_documents', + 'action': document._get_portal_return_action(), + } + return self._render_record_template(values) + + def _download_attachment(self, document): + attachment = document.attachment_id + headers = [ + ('content-type', attachment.mimetype), + ('content-length', attachment.file_size), + ] + return request.make_response(attachment.raw, headers) diff --git a/bemade_documents_portal/models/documents.py b/bemade_documents_portal/models/documents.py index 0395240..adcef6a 100644 --- a/bemade_documents_portal/models/documents.py +++ b/bemade_documents_portal/models/documents.py @@ -10,3 +10,8 @@ class Document(models.Model): for document in self: document.access_url = f'/my/documents/{document.id}' + def _get_portal_return_action(self): + """ Return the action used to display documents when returning from customer + portal.""" + self.ensure_one() + return self.env.ref('documents.document_action') \ No newline at end of file diff --git a/bemade_documents_portal/views/document_portal_templates.xml b/bemade_documents_portal/views/document_portal_templates.xml index 55c50ff..b78f2db 100644 --- a/bemade_documents_portal/views/document_portal_templates.xml +++ b/bemade_documents_portal/views/document_portal_templates.xml @@ -21,17 +21,85 @@ - + + + + + +