-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by victor-champonnois
- Loading branch information
Showing
21 changed files
with
1,502 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
================ | ||
Document Hosting | ||
================ | ||
|
||
.. | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! This file is generated by oca-gen-addon-readme !! | ||
!! changes will be overwritten. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! source digest: sha256:4242f81ffab47caf4ef4883863af2ad4308c30d7148061aeef71ae875b404b02 | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png | ||
:target: https://odoo-community.org/page/development-status | ||
:alt: Beta | ||
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png | ||
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
:alt: License: AGPL-3 | ||
.. |badge3| image:: https://img.shields.io/badge/github-coopiteasy%2Faddons-lightgray.png?logo=github | ||
:target: https://github.com/coopiteasy/addons/tree/16.0/document_hosting | ||
:alt: coopiteasy/addons | ||
|
||
|badge1| |badge2| |badge3| | ||
|
||
|
||
**Table of contents** | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Known issues / Roadmap | ||
====================== | ||
|
||
Don't allow for recursive parent categories (a category cannot be it's own parent) | ||
Add unit tests | ||
|
||
Bug Tracker | ||
=========== | ||
|
||
Bugs are tracked on `GitHub Issues <https://github.com/coopiteasy/addons/issues>`_. | ||
In case of trouble, please check there if your issue has already been reported. | ||
If you spotted it first, help us to smash it by providing a detailed and welcomed | ||
`feedback <https://github.com/coopiteasy/addons/issues/new?body=module:%20document_hosting%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
||
Do not contact contributors directly about support or help with technical issues. | ||
|
||
Credits | ||
======= | ||
|
||
Authors | ||
~~~~~~~ | ||
|
||
* Coop IT Easy SC | ||
|
||
Maintainers | ||
~~~~~~~~~~~ | ||
|
||
This module is part of the `coopiteasy/addons <https://github.com/coopiteasy/addons/tree/16.0/document_hosting>`_ project on GitHub. | ||
|
||
You are welcome to contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import controllers | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2018 - Today Coop IT Easy SC (<http://www.coopiteasy.be>) | ||
# - Rémy Taymans <[email protected]> | ||
# - Elouan Le bars <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
||
{ | ||
"name": "Document Hosting", | ||
"summary": """ | ||
Manage documents that can be published on website with ??. | ||
""", | ||
"author": "Coop IT Easy SC", | ||
"license": "AGPL-3", | ||
"version": "16.0.1.0.0", | ||
"website": "https://github.com/coopiteasy/addons", | ||
"category": "Document", | ||
"depends": ["base", "web", "website", "mail"], | ||
"data": [ | ||
"security/ir.model.access.csv", | ||
"views/document_hosting_menu.xml", | ||
"views/document_hosting_views.xml", | ||
"views/document_hosting_website_templates.xml", | ||
"views/res_config_settings_views.xml", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
# Copyright 2018 Rémy Taymans <[email protected]> | ||
# Copyright 2015-2016 Odoo S.A. | ||
# Copyright 2016 Jairo Llopis <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
|
||
from odoo import http | ||
from odoo.fields import Date | ||
from odoo.http import request | ||
|
||
|
||
class DocumentWebsite(http.Controller): | ||
@http.route("/documents/<int:oid>", auth="public", website=True) | ||
def get_document(self, oid=-1): | ||
"""Render a http response for a document""" | ||
|
||
if not request.website.display_document_page: | ||
return request.not_found() | ||
|
||
document_mgr = request.env["document_hosting.document"] | ||
doc = document_mgr.sudo().browse(oid) | ||
return ( | ||
request.env["ir.binary"] | ||
._get_image_stream_from( | ||
doc, field_name="document", filename_field="filename" | ||
) | ||
.get_response() | ||
) | ||
|
||
@http.route("/documents", auth="public", website=True) | ||
def template_website_document(self, date_begin=None, date_end=None, **kw): | ||
if not request.website.display_document_page: | ||
return request.not_found() | ||
date_begin = Date.from_string(date_begin) | ||
date_end = Date.from_string(date_end) | ||
|
||
values = {} | ||
values.update( | ||
self.website_document_side_bar( | ||
date_begin=date_begin, date_end=date_end, user=request.env.user | ||
) | ||
) | ||
values.update( | ||
self.display_categories_and_documents( | ||
date_begin=date_begin, date_end=date_end, user=request.env.user | ||
) | ||
) | ||
values["size_to_str"] = self.size_to_str | ||
return request.render("document_hosting.template_website_document", values) | ||
|
||
def website_document_side_bar(self, date_begin=None, date_end=None, user=None): | ||
domains = [] | ||
# Show only doc that are published | ||
domains.append(("published", "=", True)) | ||
# Show only authorized documents | ||
if not self._is_authorized_user(user): | ||
domains.append(("public", "=", True)) | ||
# Show only doc in the time frame | ||
if date_begin and date_end: | ||
domains.append(("document_date", ">=", date_begin)) | ||
domains.append(("document_date", "<", date_end)) | ||
return { | ||
"archive_groups": self._get_archive_groups( | ||
"document_hosting.document", | ||
domains, | ||
fields=["name", "document_date"], | ||
groupby="document_date", | ||
order="document_date desc", | ||
) | ||
} | ||
|
||
def display_categories_and_documents( | ||
self, date_begin=None, date_end=None, user=None | ||
): | ||
"""Prepare value for display_categories_and_documents template""" | ||
data = self._data_tree() | ||
# Show only doc that are published | ||
data = self._data_filter_document(data, lambda r: r.published) | ||
# Show only authorized documents | ||
if not self._is_authorized_user(user): | ||
data = self._data_filter_document(data, lambda r: r.public) | ||
# Show only doc in the time frame | ||
if date_begin and date_end: | ||
data = self._data_filter_document( | ||
data, | ||
lambda r: ( | ||
r.document_date >= date_begin and r.document_date < date_end | ||
), | ||
) | ||
# After all the filter, remove the empty categories | ||
data = self._data_remove_empty_category(data) | ||
return {"category_tree": data} | ||
|
||
def size_to_str(self, size): | ||
units = ["o", "ko", "Mo", "Go", "To"] | ||
size_float = float(size) | ||
for unit in units: | ||
if size_float < 1000: | ||
return "{:.01f} {}".format(size_float, unit) | ||
size_float /= 1000 | ||
|
||
def _data_tree(self, category=None): | ||
"""Return a tree with categories and documents in it""" | ||
category_mgr = request.env["document_hosting.category"] | ||
document_mgr = request.env["document_hosting.document"] | ||
if category: | ||
categories = category.child_ids.sorted(key=lambda r: r.name) | ||
documents = category.document_ids | ||
else: | ||
categories = category_mgr.sudo().search( | ||
[("parent_id", "=", False)], order="name" | ||
) | ||
documents = document_mgr.sudo().search([("category", "=", False)]) | ||
if categories.ids: | ||
tree = [] | ||
for cat in categories: | ||
tree.append(self._data_tree(cat)) | ||
return (category, tree, documents) | ||
else: | ||
return (category, [], documents) | ||
|
||
def _data_filter_document(self, data, filter_fun): | ||
category, child_data, documents = data | ||
tree = [] | ||
for entry in child_data: | ||
tree.append(self._data_filter_document(entry, filter_fun)) | ||
return (category, tree, documents.filtered(filter_fun)) | ||
|
||
def _data_remove_empty_category(self, data): | ||
category, child_data, documents = data | ||
child_data = [ | ||
self._data_remove_empty_category(c) | ||
for c in child_data | ||
if not self._data_is_empty(c) | ||
] | ||
return (category, child_data, documents) | ||
|
||
def _data_is_empty(self, data): | ||
"""Return True if data is empty""" | ||
_, child_data, documents = data | ||
# If there is documents, it's not empty. | ||
if documents.ids: | ||
return False | ||
# We are sure there is no documents. | ||
# If there is no child, it's empty. | ||
if not child_data: | ||
return True | ||
# We are sure there is childs | ||
for entry in child_data: | ||
# If a child is not empty, it's not empty | ||
if not self._data_is_empty(entry): | ||
return False | ||
# Else it's empty | ||
return True | ||
|
||
def _is_authorized_user(self, user=None): | ||
return user is not None and ( | ||
user.has_group("base.group_portal") or user.has_group("base.group_user") | ||
) | ||
|
||
def _get_archive_groups( | ||
self, | ||
model, | ||
domain=None, | ||
fields=None, | ||
groupby="create_date", | ||
order="create_date desc", | ||
): | ||
if not model: | ||
return [] | ||
if domain is None: | ||
domain = [] | ||
if fields is None: | ||
fields = ["name", "create_date"] | ||
groups = [] | ||
for group in ( | ||
request.env[model] | ||
.sudo() | ||
.read_group(domain, fields=fields, groupby=groupby, orderby=order) | ||
): | ||
label = group[groupby] | ||
date_begin = date_end = None | ||
for leaf in group["__domain"]: | ||
if leaf[0] == groupby: | ||
if leaf[1] == ">=": | ||
date_begin = leaf[2] | ||
elif leaf[1] == "<": | ||
date_end = leaf[2] | ||
groups.append( | ||
{ | ||
"date_begin": date_begin, | ||
"date_end": date_end, | ||
"name": label, | ||
"item_count": group[groupby + "_count"], | ||
} | ||
) | ||
return groups |
Oops, something went wrong.