Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
library-app-django
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ilham Maulana
library-app-django
Commits
877e25ed
Commit
877e25ed
authored
Jul 31, 2024
by
Ilham Maulana
💻
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: member loan filter by near outstanding and overdue api
parent
6e18cf16
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
2 deletions
+37
-2
serializers.py
api/loans/serializers.py
+16
-2
views.py
api/loans/views.py
+21
-0
No files found.
api/loans/serializers.py
View file @
877e25ed
...
...
@@ -13,8 +13,22 @@ class BookLoanSerializer(serializers.ModelSerializer):
def
to_representation
(
self
,
instance
):
data
=
super
()
.
to_representation
(
instance
)
remaining_loan_time
=
timezone
.
now
()
.
day
-
instance
.
due_date
.
day
data
[
"remaining_loan_time"
]
=
str
(
abs
(
remaining_loan_time
))
+
" days left"
now
=
timezone
.
now
()
remaining_loan_time
=
instance
.
due_date
-
now
days
=
remaining_loan_time
.
days
hours
,
remainder
=
divmod
(
remaining_loan_time
.
seconds
,
3600
)
minutes
=
remainder
//
60
time_string
=
""
if
days
>
0
:
time_string
+=
f
"{days} days"
if
hours
>
0
:
time_string
+=
f
" {hours} hrs"
if
minutes
>
0
:
time_string
+=
f
" {minutes} mins"
data
[
"remaining_loan_time"
]
=
time_string
+
" days left"
return
data
class
Meta
:
...
...
api/loans/views.py
View file @
877e25ed
...
...
@@ -57,4 +57,25 @@ class MemberLoanViewSet(BookLoanViewSet):
def
get_queryset
(
self
):
member_id
=
self
.
kwargs
.
get
(
"member_id"
)
now
=
timezone
.
now
()
due_date_treshold
=
now
+
timezone
.
timedelta
(
days
=
3
)
near_outstanding
=
self
.
request
.
query_params
.
get
(
"near_outstanding"
)
overdue
=
self
.
request
.
query_params
.
get
(
"overdue"
)
if
near_outstanding
:
return
(
BookLoan
.
objects
.
filter
(
member
=
member_id
)
.
filter
(
due_date__lte
=
due_date_treshold
,
return_date
=
None
)
.
filter
(
due_date__gte
=
now
)
.
order_by
(
"loan_date"
)
)
if
overdue
:
return
(
BookLoan
.
objects
.
filter
(
member
=
member_id
)
.
filter
(
due_date__lte
=
now
,
return_date
=
None
)
.
order_by
(
"loan_date"
)
)
return
BookLoan
.
objects
.
filter
(
member
=
member_id
)
.
order_by
(
"loan_date"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment