Skip to content

Commit 9434069

Browse files
fix(spp_session_tracking): harden security, views, and test coverage
- Fix ACL allowing session users to write own facilitated sessions - Add co-facilitator write access and multi-company record rules - Add attendance record rules scoped to facilitated sessions - Add state transition guards, write() override, and time validation - Add ondelete=restrict on session_type_id, facilitator_id, company_id - Fix N+1 query in _compute_counts using read_group - Remove PII from log messages (log rec.id only) - Rename required_attendance_pct to required_attendance_percentage - Restructure form with oe_title, button_box, ribbons, named groups - Add graph, pivot views; improve list, kanban, calendar, search views - Fix spp_case_session button_box XPath to use hasclass() - Add 63 tests (security, constraints, coverage) for 95%+ coverage - Update DESCRIPTION.md to reflect security and view changes
1 parent 31cf2d2 commit 9434069

16 files changed

Lines changed: 1219 additions & 113 deletions

spp_case_session/views/session_views.xml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
<field name="model">spp.session</field>
77
<field name="inherit_id" ref="spp_session_tracking.view_session_form" />
88
<field name="arch" type="xml">
9-
<xpath expr="//sheet/group[1]" position="before">
10-
<div class="oe_button_box" name="button_box">
11-
<button
12-
name="action_view_cases"
13-
type="object"
14-
class="oe_stat_button"
15-
icon="fa-folder-open"
16-
>
17-
<field name="case_count" widget="statinfo" string="Cases" />
18-
</button>
19-
</div>
9+
<xpath expr="//div[hasclass('oe_button_box')]" position="inside">
10+
<button
11+
name="action_view_cases"
12+
type="object"
13+
class="oe_stat_button"
14+
icon="fa-folder-open"
15+
>
16+
<field name="case_count" widget="statinfo" string="Cases" />
17+
</button>
2018
</xpath>
2119
</field>
2220
</record>

spp_session_tracking/data/session_data.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
>General training and capacity building sessions for beneficiaries</field>
1010
<field name="frequency">monthly</field>
1111
<field name="duration_hours">3.0</field>
12-
<field name="required_attendance_pct">80.0</field>
12+
<field name="required_attendance_percentage">80.0</field>
1313
<field name="track_topics">True</field>
1414
</record>
1515

@@ -21,7 +21,7 @@
2121
>Regular support group meetings for community members</field>
2222
<field name="frequency">biweekly</field>
2323
<field name="duration_hours">2.0</field>
24-
<field name="required_attendance_pct">75.0</field>
24+
<field name="required_attendance_percentage">75.0</field>
2525
<field name="track_topics">False</field>
2626
</record>
2727

@@ -33,7 +33,7 @@
3333
>Family Development Sessions for conditional cash transfer programs</field>
3434
<field name="frequency">monthly</field>
3535
<field name="duration_hours">2.5</field>
36-
<field name="required_attendance_pct">85.0</field>
36+
<field name="required_attendance_percentage">85.0</field>
3737
<field name="track_topics">True</field>
3838
</record>
3939

@@ -43,7 +43,7 @@
4343
<field name="description">Specialized workshops on various topics</field>
4444
<field name="frequency">one_time</field>
4545
<field name="duration_hours">4.0</field>
46-
<field name="required_attendance_pct">90.0</field>
46+
<field name="required_attendance_percentage">90.0</field>
4747
<field name="track_topics">True</field>
4848
</record>
4949

spp_session_tracking/models/session.py

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
22

3+
import logging
4+
35
from odoo import api, fields, models
6+
from odoo.exceptions import UserError, ValidationError
7+
from odoo.tools.translate import _
8+
9+
_logger = logging.getLogger(__name__)
410

511

612
class Session(models.Model):
@@ -10,14 +16,26 @@ class Session(models.Model):
1016
_order = "date desc"
1117

1218
name = fields.Char(required=True, tracking=True)
13-
session_type_id = fields.Many2one("spp.session.type", required=True, string="Session Type", tracking=True)
19+
session_type_id = fields.Many2one(
20+
"spp.session.type",
21+
required=True,
22+
ondelete="restrict",
23+
string="Session Type",
24+
tracking=True,
25+
)
1426

1527
date = fields.Date(required=True, default=fields.Date.today, tracking=True)
1628
start_time = fields.Float()
1729
end_time = fields.Float()
1830
duration_hours = fields.Float(compute="_compute_duration", store=True, string="Duration (Hours)")
1931

20-
facilitator_id = fields.Many2one("res.users", required=True, string="Facilitator", tracking=True)
32+
facilitator_id = fields.Many2one(
33+
"res.users",
34+
required=True,
35+
ondelete="restrict",
36+
string="Facilitator",
37+
tracking=True,
38+
)
2139
co_facilitator_ids = fields.Many2many(
2240
"res.users",
2341
"session_co_facilitator_rel",
@@ -65,16 +83,22 @@ class Session(models.Model):
6583
)
6684

6785
notes = fields.Text()
68-
company_id = fields.Many2one("res.company", default=lambda self: self.env.company)
86+
company_id = fields.Many2one("res.company", default=lambda self: self.env.company, ondelete="restrict")
6987

7088
@api.depends("start_time", "end_time")
7189
def _compute_duration(self):
7290
for rec in self:
7391
if rec.start_time and rec.end_time:
74-
rec.duration_hours = rec.end_time - rec.start_time
92+
rec.duration_hours = max(0.0, rec.end_time - rec.start_time)
7593
else:
7694
rec.duration_hours = 0.0
7795

96+
@api.constrains("start_time", "end_time")
97+
def _check_time_range(self):
98+
for rec in self:
99+
if rec.start_time and rec.end_time and rec.end_time < rec.start_time:
100+
raise ValidationError(_("End time must be after start time."))
101+
78102
@api.depends("attendance_ids", "attendance_ids.is_attended", "expected_participant_ids")
79103
def _compute_attendance(self):
80104
for rec in self:
@@ -87,11 +111,45 @@ def _compute_attendance(self):
87111
else:
88112
rec.attendance_rate = 0.0
89113

114+
_VALID_TRANSITIONS = {
115+
"scheduled": {"in_progress", "cancelled"},
116+
"in_progress": {"completed", "cancelled"},
117+
"completed": set(),
118+
"cancelled": set(),
119+
}
120+
121+
def write(self, vals):
122+
if "state" in vals:
123+
new_state = vals["state"]
124+
for rec in self:
125+
valid = self._VALID_TRANSITIONS.get(rec.state, set())
126+
if new_state not in valid:
127+
raise UserError(
128+
_(
129+
"Cannot transition session from '%(current)s' to '%(target)s'.",
130+
current=rec.state,
131+
target=new_state,
132+
)
133+
)
134+
return super().write(vals)
135+
90136
def action_start(self):
91-
self.state = "in_progress"
137+
for rec in self:
138+
if rec.state != "scheduled":
139+
raise UserError(_("Only scheduled sessions can be started."))
140+
_logger.info("Session id=%s transitioning from scheduled to in_progress.", rec.id)
141+
rec.state = "in_progress"
92142

93143
def action_complete(self):
94-
self.state = "completed"
144+
for rec in self:
145+
if rec.state != "in_progress":
146+
raise UserError(_("Only sessions in progress can be completed."))
147+
_logger.info("Session id=%s transitioning from in_progress to completed.", rec.id)
148+
rec.state = "completed"
95149

96150
def action_cancel(self):
97-
self.state = "cancelled"
151+
for rec in self:
152+
if rec.state not in ("scheduled", "in_progress"):
153+
raise UserError(_("Only scheduled or in-progress sessions can be cancelled."))
154+
_logger.info("Session id=%s transitioning from %s to cancelled.", rec.id, rec.state)
155+
rec.state = "cancelled"

spp_session_tracking/models/session_attendance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ class SessionAttendance(models.Model):
99
_rec_name = "participant_id"
1010

1111
session_id = fields.Many2one("spp.session", required=True, ondelete="cascade", string="Session")
12-
participant_id = fields.Many2one("res.partner", required=True, string="Participant")
12+
participant_id = fields.Many2one("res.partner", required=True, ondelete="restrict", string="Participant")
1313

14-
is_attended = fields.Boolean(default=False)
14+
is_attended = fields.Boolean()
1515
attendance_time = fields.Datetime(string="Time of Attendance")
1616

17-
is_excused = fields.Boolean(default=False)
17+
is_excused = fields.Boolean()
1818
excuse_reason = fields.Char()
1919

2020
notes = fields.Text()

spp_session_tracking/models/session_topic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class SessionTopic(models.Model):
1111
name = fields.Char(required=True)
1212
code = fields.Char()
1313
description = fields.Text()
14-
session_type_id = fields.Many2one("spp.session.type", string="Session Type")
14+
session_type_id = fields.Many2one("spp.session.type", string="Session Type", ondelete="cascade")
1515
sequence = fields.Integer(default=10)
1616
active = fields.Boolean(default=True)

spp_session_tracking/models/session_type.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
22

33
from odoo import api, fields, models
4+
from odoo.tools.translate import _
45

56

67
class SessionType(models.Model):
@@ -23,7 +24,7 @@ class SessionType(models.Model):
2324
default="monthly",
2425
)
2526

26-
required_attendance_pct = fields.Float(
27+
required_attendance_percentage = fields.Float(
2728
default=80.0,
2829
help="Minimum attendance percentage required for compliance",
2930
)
@@ -37,21 +38,30 @@ class SessionType(models.Model):
3738

3839
session_count = fields.Integer(compute="_compute_counts", string="Number of Sessions")
3940

40-
company_id = fields.Many2one("res.company", default=lambda self: self.env.company)
41+
company_id = fields.Many2one("res.company", default=lambda self: self.env.company, ondelete="restrict")
4142

42-
@api.depends("topic_ids")
43+
_unique_code = models.Constraint(
44+
"UNIQUE(code)",
45+
"Session type code must be unique.",
46+
)
47+
48+
@api.depends()
4349
def _compute_counts(self):
50+
data = self.env["spp.session"].read_group(
51+
[("session_type_id", "in", self.ids)], ["session_type_id"], ["session_type_id"]
52+
)
53+
mapped = {d["session_type_id"][0]: d["session_type_id_count"] for d in data}
4454
for rec in self:
45-
rec.session_count = self.env["spp.session"].search_count([("session_type_id", "=", rec.id)])
55+
rec.session_count = mapped.get(rec.id, 0)
4656

4757
def action_view_sessions(self):
4858
"""Open view with sessions of this type."""
4959
self.ensure_one()
5060
return {
5161
"type": "ir.actions.act_window",
52-
"name": f"Sessions - {self.name}",
62+
"name": _("Sessions - %s", self.name),
5363
"res_model": "spp.session",
54-
"view_mode": "tree,form,calendar,kanban",
64+
"view_mode": "list,form,calendar,kanban",
5565
"domain": [("session_type_id", "=", self.id)],
5666
"context": {"default_session_type_id": self.id},
5767
}

spp_session_tracking/readme/DESCRIPTION.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ After installing:
2626

2727
1. Navigate to **Session Tracking > Configuration > Session Types**
2828
2. Review pre-configured session types (Training, Family Development Session, Group Meeting, Workshop)
29-
3. Add or modify session types and topics as needed
29+
3. Add or modify session types as needed; topics are managed within each session type form when topic tracking is enabled
3030
4. Adjust required attendance percentage per session type
3131

3232
Four session types are pre-configured with sample topics for Family Development Sessions and Training Sessions.
@@ -36,16 +36,16 @@ Four session types are pre-configured with sample topics for Family Development
3636
- **Menu**: Session Tracking > Sessions > All Sessions
3737
- **My Sessions**: Session Tracking > Sessions > My Sessions (filtered to current user as facilitator)
3838
- **Configuration**: Session Tracking > Configuration > Session Types (managers only)
39-
- **Views**: List, form, calendar (by date), kanban (grouped by state)
39+
- **Views**: List, form, calendar (by date), kanban (grouped by state), graph, pivot
4040

4141
### Security
4242

4343
| Group | Access |
4444
| ---------------------------------------------- | ------------------------------------- |
45-
| `spp_session_tracking.group_session_user` | Read all sessions and session types/topics; write own facilitated sessions; read/write/create attendance (no delete) |
45+
| `spp_session_tracking.group_session_user` | Read all sessions and session types/topics; create/write own facilitated or co-facilitated sessions; read/write/create attendance for own sessions (no delete) |
4646
| `spp_session_tracking.group_session_manager` | Full CRUD on all sessions, types, topics, and attendance |
4747

48-
The session user group can view all sessions but only edit sessions they facilitate (via record rule). The `spp_security.group_spp_admin` group implies manager access.
48+
The session user group can view all sessions but only edit sessions where they are the facilitator or co-facilitator (via record rules). Session creation requires manager access. The `spp_security.group_spp_admin` group implies manager access. Multi-company record rules ensure users only see sessions belonging to their company.
4949

5050
### Extension Points
5151

spp_session_tracking/security/ir.model.access.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ access_spp_session_type_user,access_spp_session_type_user,spp_session_tracking.m
33
access_spp_session_type_manager,access_spp_session_type_manager,spp_session_tracking.model_spp_session_type,spp_session_tracking.group_session_manager,1,1,1,1
44
access_spp_session_topic_user,access_spp_session_topic_user,spp_session_tracking.model_spp_session_topic,spp_session_tracking.group_session_user,1,0,0,0
55
access_spp_session_topic_manager,access_spp_session_topic_manager,spp_session_tracking.model_spp_session_topic,spp_session_tracking.group_session_manager,1,1,1,1
6-
access_spp_session_user,access_spp_session_user,spp_session_tracking.model_spp_session,spp_session_tracking.group_session_user,1,0,0,0
6+
access_spp_session_user,access_spp_session_user,spp_session_tracking.model_spp_session,spp_session_tracking.group_session_user,1,1,0,0
77
access_spp_session_manager,access_spp_session_manager,spp_session_tracking.model_spp_session,spp_session_tracking.group_session_manager,1,1,1,1
88
access_spp_session_attendance_user,access_spp_session_attendance_user,spp_session_tracking.model_spp_session_attendance,spp_session_tracking.group_session_user,1,1,1,0
99
access_spp_session_attendance_manager,access_spp_session_attendance_manager,spp_session_tracking.model_spp_session_attendance,spp_session_tracking.group_session_manager,1,1,1,1

0 commit comments

Comments
 (0)