Sep 16 - Optimize code, part 2
This commit is contained in:
+23
-12
@@ -126,11 +126,19 @@ def parse_employee_id_for_work_type(employee_id: str) -> Tuple[str, str]:
|
||||
"""
|
||||
Parse employee ID to extract base ID and work type (supports SP, PW, PT, C)
|
||||
|
||||
Handles multiple formats:
|
||||
- Suffix with space: "1234 SP", "1234 PW", "1234 PT", "1234 C"
|
||||
- Suffix without space: "1234SP", "1234PW", "1234PT", "1234C"
|
||||
- Prefix with space: "SP 1234", "PW 1234", "PT 1234", "C 1234"
|
||||
- Prefix without space: "SP1234", "PW1234", "PT1234", "C1234"
|
||||
Handles multiple formats — the separator may be anything that is not a letter
|
||||
or a digit (space, dot, dash, underscore, slash, or nothing at all), because
|
||||
imported IDs come straight from customer Excel files:
|
||||
- Suffix: "1234SP", "1234 SP", "1234.PW", "1234-PT", "1234 . C"
|
||||
- Prefix: "SP1234", "SP 1234", "PW.1234", "PT-1234"
|
||||
|
||||
This mirrors build_employee_id_regex() in utils/helpers.py (SQL filters) and
|
||||
parseEmployeeIdWorkType() in static/js/attendance_report.js — keep the three
|
||||
in sync, or the report, the filters and the exports disagree about who worked
|
||||
those hours (§13).
|
||||
|
||||
Not a work type: "1234.5" (no code) and "1234SPX" (code runs into a word) —
|
||||
both come back as regular with the ID unchanged.
|
||||
|
||||
Args:
|
||||
employee_id: Employee ID string in any of the above formats
|
||||
@@ -147,16 +155,19 @@ def parse_employee_id_for_work_type(employee_id: str) -> Tuple[str, str]:
|
||||
# Define work type codes
|
||||
work_type_codes = ['SP', 'PW', 'PT', 'C']
|
||||
|
||||
# Any run of non-alphanumeric characters may separate the ID from the code
|
||||
# (or nothing at all): "1234SP", "1234 SP", "1234.PW", "1234 - PT".
|
||||
# The class excludes letters, so "1234SPX" never matches.
|
||||
separator = r'[^0-9A-Z]*'
|
||||
|
||||
for work_type in work_type_codes:
|
||||
# Pattern 1: Suffix with optional space - "1234 SP" or "1234SP"
|
||||
suffix_pattern = rf'^(\d+)\s*{work_type}$'
|
||||
suffix_match = re.match(suffix_pattern, employee_id_clean)
|
||||
# Pattern 1: Suffix - "1234SP", "1234 SP", "1234.SP"
|
||||
suffix_match = re.match(rf'^(\d+){separator}{work_type}$', employee_id_clean)
|
||||
if suffix_match:
|
||||
return suffix_match.group(1), work_type
|
||||
|
||||
# Pattern 2: Prefix with optional space - "SP 1234" or "SP1234"
|
||||
prefix_pattern = rf'^{work_type}\s*(\d+)$'
|
||||
prefix_match = re.match(prefix_pattern, employee_id_clean)
|
||||
|
||||
# Pattern 2: Prefix - "SP1234", "SP 1234", "SP.1234"
|
||||
prefix_match = re.match(rf'^{work_type}{separator}(\d+)$', employee_id_clean)
|
||||
if prefix_match:
|
||||
return prefix_match.group(1), work_type
|
||||
|
||||
|
||||
Reference in New Issue
Block a user