Skip to content

Commit

Permalink
Merge PR #696 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Jul 27, 2023
2 parents ca7b582 + 73d281a commit d7261ba
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
47 changes: 47 additions & 0 deletions announcement/models/announcement.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2022 Tecnativa - David Vidal
# Copyright 2022 Tecnativa - Pilar Vargas
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models

Expand Down Expand Up @@ -27,6 +28,11 @@ class Announcement(models.Model):
string="Tags",
)
is_general_announcement = fields.Boolean("General Announcement")
attachment_ids = fields.Many2many(
comodel_name="ir.attachment",
string="Attachments",
help="You can attach the copy of your Letter",
)
announcement_type = fields.Selection(
selection=[
("specific_users", "Specific users"),
Expand Down Expand Up @@ -144,6 +150,47 @@ def _search_in_date(self, operator, value):
("notification_expiry_date", ">=", now),
]

def _process_attachments(self, vals):
"""Assign attachments owner (if not yet set) or create a copy of the added
attachments for making sure that they are accessible to the users that read
the announcement.
"""
if self.env.context.get("bypass_attachment_process"):
return
for command in vals.get("attachment_ids", []):
to_process = []
if command[0] == 4:
to_process.append(command[1])
elif command[0] == 6:
to_process += command[2]
for attachment_id in to_process:
attachment = self.env["ir.attachment"].browse(attachment_id)
for record in self:
if not attachment.res_id:
attachment.res_id = record.id
attachment.res_model = record._name
else:
new_attachment = attachment.copy(
{"res_id": record.id, "res_model": record._name}
)
record.with_context(
bypass_attachment_process=True
).attachment_ids = [(3, attachment_id), (4, new_attachment.id)]

@api.model_create_multi
def create(self, vals_list):
"""Adjust attachments for being accesible to receivers of the announcement."""
records = super().create(vals_list)
for vals in vals_list:
records._process_attachments(vals)
return records

def write(self, vals):
"""Adjust attachments for being accesible to receivers of the announcement."""
res = super().write(vals)
self._process_attachments(vals)
return res

@api.onchange("announcement_type")
def _onchange_announcement_type(self):
"""We want to reset the values on screen"""
Expand Down
26 changes: 25 additions & 1 deletion announcement/models/res_users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Copyright 2022 Tecnativa - David Vidal
# Copyright 2022 Tecnativa - Pilar Vargas
# Copyright 2022 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from markupsafe import Markup

from odoo import api, fields, models


Expand Down Expand Up @@ -44,11 +48,31 @@ def announcement_user_count(self):
{
"id": announcement.id,
"name": announcement.name,
"content": announcement.content,
"content": self._add_attachment_links(announcement),
}
for announcement in announcements.sorted(lambda k: k.sequence)
]

def _add_attachment_links(self, announcement):
"""In case the announcement has attachments, show the list below the
modal content"""
content = announcement.content
attachment_links = ""
if announcement.attachment_ids:
attachment_links += "<div class='list-group'>"
for attachment in announcement.attachment_ids:
attachment_url = "/web/content/%s?download=false" % attachment.id
attachment_link = """<a href="%s" class="list-group-item list-group-item-action"
target="_blank"><i class="fa fa-download" /> %s</a>""" % (
attachment_url,
attachment.name,
)
attachment_links += attachment_link
attachment_links += "</div>"
if attachment_links:
content += Markup("<br/>") + Markup(attachment_links)
return content

@api.model
def mark_announcement_as_read(self, announcement_id):
"""Used as a controller for the widget"""
Expand Down
3 changes: 3 additions & 0 deletions announcement/views/announcement_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
/>
</group>
</group>
<group>
<field name="attachment_ids" widget="many2many_binary" />
</group>
<notebook>
<page name="content" string="Content">
<group>
Expand Down

0 comments on commit d7261ba

Please sign in to comment.