Skip to content

Commit 6ad574d

Browse files
committed
[FIX] academic: add constrains for groups
closes ingadhoc#177 X-original-commit: fb5c995 Signed-off-by: Juan José Scarafía <[email protected]> Signed-off-by: Franco Leyes <[email protected]>
1 parent 6422aaf commit 6ad574d

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

academic/i18n/es.po

+11
Original file line numberDiff line numberDiff line change
@@ -1053,3 +1053,14 @@ msgstr "asignatura"
10531053
#: model:ir.model.fields,help:academic.field_res_users__disabled_person
10541054
msgid "¿Alumno/a con Dificultades de aprendizaje?"
10551055
msgstr ""
1056+
1057+
#. module: academic
1058+
#. odoo-python
1059+
#: code:addons/academic/models/res_partner.py:0
1060+
#, python-format
1061+
msgid ""
1062+
"The partner '%s' cannot belong to multiple groups without a subject in the "
1063+
"same year. Conflicting year(s): %s."
1064+
msgstr ""
1065+
"El contacto '%s' no puede pertenecer a varios grupos sin una materia en el "
1066+
"mismo año. Año(s) en conflicto: %s."

academic/models/academic_group.py

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class AcademicGroup(models.Model):
2929
year = fields.Integer(
3030
required=True,
3131
default=date.today().year,
32+
index=True
3233
)
3334
division_id = fields.Many2one(
3435
'academic.division',
@@ -51,6 +52,7 @@ class AcademicGroup(models.Model):
5152
'academic.subject',
5253
string='Subject',
5354
required=False,
55+
index=True
5456
)
5557
teacher_id = fields.Many2one(
5658
'res.partner',

academic/models/res_partner.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,37 @@ def _compute_same_dni_partner_id(self):
197197
partner.same_dni_partner_id = Partner.search(domain, limit=1)
198198
(self - filtered_partners).same_dni_partner_id = False
199199

200+
@api.constrains('current_main_group_id')
201+
def _check_unique_main_group_per_year(self):
202+
"""La validación se realiza sobre el campo `current_main_group_id` en lugar de `student_group_ids`
203+
porque, al usar un campo many2many, la validación no se activaba al agregar un estudiante
204+
directamente desde un grupo.
205+
"""
206+
for partner in self:
207+
domain = [
208+
('student_ids', '=', partner.id),
209+
('subject_id', '=', False)
210+
]
211+
grouped_data = self.env['academic.group'].read_group(
212+
domain,
213+
['year'],
214+
['year']
215+
)
216+
duplicate_years = [group['year'] for group in grouped_data if group['year_count'] > 1]
217+
if duplicate_years:
218+
raise ValidationError(_(
219+
"The partner '%s' cannot belong to multiple groups "
220+
"without a subject in the same year. Conflicting year(s): %s."
221+
) % (
222+
partner.name,
223+
', '.join(map(str, duplicate_years))
224+
))
225+
200226
@api.depends('student_group_ids')
201227
def _compute_current_main_group(self):
202228
for rec in self:
203229
student_group = rec.student_group_ids.filtered(lambda g: g.year == date.today().year and not g.subject_id)
204-
if len(student_group) > 1:
205-
raise ValidationError("There shouldn't be two groups in the same year without a subject for partner %s." % rec.name)
206-
rec.current_main_group_id = student_group
230+
rec.current_main_group_id = student_group[:1]
207231

208232
@api.model
209233
def get_payment_responsible(self):

0 commit comments

Comments
 (0)