Skip to content

Commit

Permalink
[ADD] Add absence concept in attendance server
Browse files Browse the repository at this point in the history
  • Loading branch information
reichie020212 committed Nov 21, 2024
1 parent a604428 commit e837792
Showing 4 changed files with 77 additions and 37 deletions.
90 changes: 58 additions & 32 deletions spp_attendance/controllers/controllers.py
Original file line number Diff line number Diff line change
@@ -205,6 +205,8 @@ def create_attendance_list(self, ignore_unique=None, **kwargs):
attendance_list_data = []
person_id_list = []

ALLOWED_CATEGORIES = req.env["spp.attendance.list"].ALLOWED_CATEGORIES

Check warning on line 208 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L208

Added line #L208 was not covered by tests

for person_data in data["records"]:
if missing_required_fields := self.check_required_fields(person_data, ["time_card", "person_id"]):
return self.error_wrapper(400, f"Missing required fields: {', '.join(missing_required_fields)}")
@@ -230,6 +232,13 @@ def create_attendance_list(self, ignore_unique=None, **kwargs):
attendance_date = str(attendance_datetime.date())
attendance_time = str(attendance_datetime.time())

category = time_card.get("attendance_category", "present")
category = category.lower()

Check warning on line 236 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L235-L236

Added lines #L235 - L236 were not covered by tests
if category not in ALLOWED_CATEGORIES:
return self.error_wrapper(

Check warning on line 238 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L238

Added line #L238 was not covered by tests
400, f"Invalid category. Allowed categories are {', '.join(ALLOWED_CATEGORIES)}."
)

attendance_type = time_card.get("attendance_type", False) or False
if result := self.validate_attendance_type(attendance_type):
return result
@@ -247,6 +256,7 @@ def create_attendance_list(self, ignore_unique=None, **kwargs):
"attendance_date": attendance_date,
"attendance_time": attendance_time,
"attendance_type_id": attendance_type,
"attendance_category": category,
"attendance_location_id": attendance_location,
"attendance_description": attendance_description,
"attendance_external_url": attendance_external_url,
@@ -273,6 +283,50 @@ def create_attendance_list(self, ignore_unique=None, **kwargs):
200, {"message": "Attendance list created successfully.", "person_ids": person_id_list}
)

def get_time_card_vals(self, time_card):
ALLOWED_CATEGORIES = request.env["spp.attendance.list"].ALLOWED_CATEGORIES
vals = {}

Check warning on line 288 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L287-L288

Added lines #L287 - L288 were not covered by tests
if "date_time" in time_card:
attendance_datetime = time_card.get("date_time")
attendance_datetime = datetime.strptime(attendance_datetime, "%Y-%m-%d %H:%M:%S")
attendance_date = str(attendance_datetime.date())
attendance_time = str(attendance_datetime.time())

Check warning on line 293 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L290-L293

Added lines #L290 - L293 were not covered by tests

vals["attendance_date"] = attendance_date
vals["attendance_time"] = attendance_time

Check warning on line 296 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L295-L296

Added lines #L295 - L296 were not covered by tests

if "attendance_type" in time_card:
attendance_type = time_card.get("attendance_type")

Check warning on line 299 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L299

Added line #L299 was not covered by tests
if result := self.validate_attendance_type(attendance_type):
return result
attendance_type = int(attendance_type)
vals["attendance_type_id"] = attendance_type

Check warning on line 303 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L301-L303

Added lines #L301 - L303 were not covered by tests

if "attendance_location" in time_card:
attendance_location = time_card.get("attendance_location")

Check warning on line 306 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L306

Added line #L306 was not covered by tests
if result := self.validate_attendance_location(attendance_location):
return result
attendance_location = int(attendance_location)
vals["attendance_location_id"] = attendance_location

Check warning on line 310 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L308-L310

Added lines #L308 - L310 were not covered by tests

if "attendance_description" in time_card:
attendance_description = time_card.get("attendance_description")
vals["attendance_description"] = attendance_description

Check warning on line 314 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L313-L314

Added lines #L313 - L314 were not covered by tests

if "attendance_external_url" in time_card:
attendance_external_url = time_card.get("attendance_external_url")
vals["attendance_external_url"] = attendance_external_url

Check warning on line 318 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L317-L318

Added lines #L317 - L318 were not covered by tests

if "attendance_category" in time_card:
category = time_card["attendance_category"]
category = category.lower()

Check warning on line 322 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L321-L322

Added lines #L321 - L322 were not covered by tests
if category not in ALLOWED_CATEGORIES:
return self.error_wrapper(

Check warning on line 324 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L324

Added line #L324 was not covered by tests
400, f"Invalid category. Allowed categories are {', '.join(ALLOWED_CATEGORIES)}."
)
vals["attendance_category"] = category
return vals

Check warning on line 328 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L327-L328

Added lines #L327 - L328 were not covered by tests

@route(
"/attendances",
type="http",
@@ -317,38 +371,10 @@ def update_attendance_list(self, **kwargs):
if not isinstance(time_card, dict):
return self.error_wrapper(400, "time_card must be an object.")

vals = {}

if "date_time" in time_card:
attendance_datetime = time_card.get("date_time")
attendance_datetime = datetime.strptime(attendance_datetime, "%Y-%m-%d %H:%M:%S")
attendance_date = str(attendance_datetime.date())
attendance_time = str(attendance_datetime.time())

vals["attendance_date"] = attendance_date
vals["attendance_time"] = attendance_time

if "attendance_type" in time_card:
attendance_type = time_card.get("attendance_type")
if result := self.validate_attendance_type(attendance_type):
return result
attendance_type = int(attendance_type)
vals["attendance_type_id"] = attendance_type

if "attendance_location" in time_card:
attendance_location = time_card.get("attendance_location")
if result := self.validate_attendance_location(attendance_location):
return result
attendance_location = int(attendance_location)
vals["attendance_location_id"] = attendance_location

if "attendance_description" in time_card:
attendance_description = time_card.get("attendance_description")
vals["attendance_description"] = attendance_description

if "attendance_external_url" in time_card:
attendance_external_url = time_card.get("attendance_external_url")
vals["attendance_external_url"] = attendance_external_url
vals = self.get_time_card_vals(time_card)

Check warning on line 374 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L374

Added line #L374 was not covered by tests
if not isinstance(vals, dict):
# it returns an error if it is not a dict: return the vals
return vals

Check warning on line 377 in spp_attendance/controllers/controllers.py

Codecov / codecov/patch

spp_attendance/controllers/controllers.py#L377

Added line #L377 was not covered by tests

if vals:
vals["submitted_by"] = submitted_by
12 changes: 11 additions & 1 deletion spp_attendance/models/attendance_list.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from odoo import fields, models
from odoo import _, fields, models


class Attendance(models.Model):
_name = "spp.attendance.list"
_description = "Attendance List"

PRESENT = "present"
ABSENT = "absent"
ALLOWED_CATEGORIES = [PRESENT, ABSENT]

subscriber_id = fields.Many2one("spp.attendance.subscriber", required=True, readonly=True)
attendance_date = fields.Date(required=True, default=lambda self: fields.Date.today(), string="Date")
attendance_time = fields.Char(required=True, string="Time", default="00:00:00")
@@ -19,6 +23,12 @@ class Attendance(models.Model):
required=True, default=lambda self: fields.Datetime.now().strftime("%Y-%m-%d %H:%M:%S")
)
submission_source = fields.Char()
attendance_category = fields.Selection(
selection=[(PRESENT, _("Present")), (ABSENT, _("Absent"))],
string="Category",
default=PRESENT,
required=True,
)

def get_unique_domain(self):
self.ensure_one()
5 changes: 4 additions & 1 deletion spp_attendance/models/attendance_subscriber.py
Original file line number Diff line number Diff line change
@@ -136,8 +136,10 @@ def get_attendance_list(
domain, offset=offset, limit=limit, order="attendance_date desc, attendance_time desc"
)
total_attendances = self.env["spp.attendance.list"].sudo().search_count(domain)

present_domain = domain + [("attendance_category", "=", "present")]

Check warning on line 140 in spp_attendance/models/attendance_subscriber.py

Codecov / codecov/patch

spp_attendance/models/attendance_subscriber.py#L140

Added line #L140 was not covered by tests
number_of_days_present = list(
set(self.env["spp.attendance.list"].sudo().search(domain).mapped("attendance_date"))
set(self.env["spp.attendance.list"].sudo().search(present_domain).mapped("attendance_date"))
)

return total_attendances, {
@@ -168,6 +170,7 @@ def get_attendance_list(
"submitted_by": attendance.submitted_by,
"submitted_datetime": attendance.submitted_datetime,
"submission_source": attendance.submission_source or "",
"attendance_category": attendance.attendance_category,
}
for attendance in attendance_list_ids
],
7 changes: 4 additions & 3 deletions spp_attendance/views/attendance_views.xml
Original file line number Diff line number Diff line change
@@ -35,9 +35,10 @@
<field name="attendance_date" />
<field name="attendance_time" />
<field name="attendance_type_id" />
<field name="attendance_location_id" />
<field name="attendance_description" />
<field name="attendance_external_url" />
<field name="attendance_category" />
<field name="attendance_location_id" optional="hide" />
<field name="attendance_description" optional="hide" />
<field name="attendance_external_url" optional="hide" />
<field name="submission_source" />
<field name="submitted_by" />
<field name="submitted_datetime" />

0 comments on commit e837792

Please sign in to comment.