[MODULE] +constraints: a non-student can't be scholarship
authorLudovic CHEVALIER <ludovic@beurresarrasin.net>
Fri, 3 Nov 2017 17:01:02 +0000 (18:01 +0100)
committerLudovic CHEVALIER <ludovic@beurresarrasin.net>
Fri, 3 Nov 2017 17:01:02 +0000 (18:01 +0100)
eturecup.py
view/bikecoop.xml

index 14a3298..5f58510 100644 (file)
@@ -44,10 +44,12 @@ class Partner(orm.Model):
         partners = self.browse(cr, uid, ids)
         occupations = self.pool.get('bikecoop.partner.theme')
         for partner in partners:
-            v['is_student'] = False
             occupation = occupations.browse(cr, uid, occupation_id)
             if occupation.type == 'studies':
                 v['is_student'] = True
+            else:
+                v['is_student'] = False
+                v['is_scholarship'] = False
         return {'value': v}
 
     _columns = {
@@ -55,17 +57,32 @@ class Partner(orm.Model):
             'Do you want to receive our monthly newsletter?'),
         'is_student': fields.function(_get_bikecoop_theme_type,
                                       method=True,
-                                      string='Is this partner a student?',
+                                      string='Student?',
                                       type='boolean',
                                       store=True),
-        'scholarship': fields.boolean('Scholarship',
-                                      help='Is this student a scholarship?'),
+        'is_scholarship': fields.boolean('Scholarship',
+                                         help='Is this student a scholarship?'),
         'want_to_be_volunteer': fields.boolean(
             'Do you want to receive some informations about volunteer \
             activities?',
             help='… in company and its activities: bikecoop, events, …'),
     }
 
+    def _check_occupation_is_not_studies(self, cr, uid, ids, context=None):
+        """Check if partners are students. If not, they can't be
+        scholarships."""
+        partners = self.browse(cr, uid, ids, context=context)
+        for partner in partners:
+            if partner.occupation_id.type != 'studies':
+                if partner.is_scholarship:
+                    return False
+        return True
+
+    _constraints = [
+        (_check_occupation_is_not_studies, 'Error: This partner can\'t be a\
+         scholarship because s·he isn\'t a student.', ['is_scholarship']),
+    ]
+
 
 class Theme(orm.Model):
     _inherit = 'bikecoop.partner.theme'
index 558ba6b..7f53346 100644 (file)
@@ -40,7 +40,7 @@
                 <xpath expr="//field[@name='occupation_id']" position="replace">
                     <field name="occupation_id" on_change="onchange_occupation_id(occupation_id)" domain="[('domain','=', 'occupation')]" attrs="{'required': [('is_company','=', False)]}" widget="selection"/>
                     <field name="is_student"/>
-                    <field name="scholarship" attrs="{'invisible': [('is_student','=', False)]}"/>
+                    <field name="is_scholarship" attrs="{'invisible': [('is_student','=', False)]}"/>
                 </xpath>
             </field>
         </record>