-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADD] spp unique id abstract model #43
Changes from 1 commit
dd546c3
5e96b6e
d714f48
cb3a14c
6d44559
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
|
||
from . import reg_id | ||
from . import spp_unique_id |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import random | ||
import re | ||
import string | ||
|
||
from odoo import _, api, fields, models | ||
from odoo.exceptions import ValidationError | ||
|
||
|
||
def _generate_unique_id(): | ||
# Adjust the desired length of the unique identifier | ||
length = 8 | ||
# Define the characters allowed in the unique identifier | ||
characters = string.digits + string.ascii_uppercase | ||
# Exclude characters that can be confused | ||
excluded_characters = ["0", "O", "1", "I"] | ||
# Filter the characters to exclude | ||
allowed_characters = [c for c in characters if c not in excluded_characters] | ||
# Generate the unique identifier by randomly selecting characters | ||
unique_id = "".join(random.choice(allowed_characters) for _ in range(length)) | ||
|
||
return unique_id | ||
|
||
|
||
class SppUniqueId(models.AbstractModel): | ||
_name = "spp.unique.id" | ||
|
||
registrant_id = fields.Char( | ||
string="Unique ID", | ||
compute="_compute_registrant_id", | ||
store=True, | ||
readonly=False, | ||
index=True, | ||
) | ||
|
||
_sql_constraints = [ | ||
( | ||
"registrant_id_uniq", | ||
"UNIQUE(registrant_id)", | ||
"registrant_id is an unique identifier!", | ||
) | ||
] | ||
|
||
def _get_registrant_id_prefix(self): | ||
raise NotImplementedError() | ||
|
||
def _get_match_registrant_id_pattern(self): | ||
raise NotImplementedError() | ||
|
||
@api.constrains("registrant_id") | ||
def _check_registrant_id(self): | ||
not_correct_format = _("Unique ID is not following correct format!") | ||
for rec in self: | ||
match_pattern = rec._get_match_registrant_id_pattern() | ||
if not match_pattern: | ||
continue | ||
if not re.match(match_pattern, rec.registrant_id): | ||
raise ValidationError(not_correct_format) | ||
if any( | ||
[ | ||
char in rec.registrant_id.split("_")[-1] | ||
for char in ("0", "O", "1", "I") | ||
] | ||
): | ||
raise ValidationError(not_correct_format) | ||
|
||
def _compute_registrant_id(self): | ||
for rec in self: | ||
prefix = rec._get_registrant_id_prefix() | ||
if not prefix: | ||
rec.registrant_id = False | ||
continue | ||
registrant_id = _generate_unique_id() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we double check it has not been used by querying the DB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, I had sql constraints to guard the duplication, but if you want to, sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jeremi done 😄 |
||
rec.registrant_id = "_".join([prefix, registrant_id]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we rename it spp_id? (and the related methods as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will change all the adaptor as well, since we are using this field name through out all the operations, but yes, we can
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeremi done 😄