Skip to content

Commit

Permalink
use constrains
Browse files Browse the repository at this point in the history
  • Loading branch information
emjay0921 committed Sep 12, 2023
1 parent 7594090 commit 8ec4dc4
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions spp_base/models/reg_id.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
import logging
import textwrap

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
Expand All @@ -12,27 +13,8 @@ class OpenSPPRegistrantID(models.Model):

card_uid = fields.Char("Card UID")

@api.model
def create(self, vals):
if "id_type" in vals:
if vals["id_type"] == self.env.ref("spp_base.id_top_up_card").id:
if (
"card_uid" in vals
and vals["card_uid"]
and len(vals["card_uid"]) != 10
):
raise ValidationError(
_("Top-up Card UID should have 10 characters")
)

return super().create(vals)

def write(self, vals):
id_type = vals.get("id_type", self.id_type.id)
card_uid = vals.get("card_uid", self.value)

if id_type == self.env.ref("spp_base.id_top_up_card").id:
if card_uid and len(card_uid) != 10:
@api.constrains("card_uid")
def _check_card_uid(self):
for rec in self:
if rec.id_type.id == self.env.ref("spp_base.id_top_up_card").id and len(str(rec.card_uid)) != 10:
raise ValidationError(_("Top-up Card UID should have 10 characters"))

return super().write(vals)

0 comments on commit 8ec4dc4

Please sign in to comment.