Skip to content
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

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spp_base/models/__init__.py
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
73 changes: 73 additions & 0 deletions spp_base/models/spp_unique_id.py
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(
Copy link
Member

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)

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeremi done 😄

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()
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeremi done 😄

rec.registrant_id = "_".join([prefix, registrant_id])
Loading