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
987546e3
Commit
987546e3
authored
Jul 17, 2024
by
Ilham Maulana
💻
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: custom validators for loans
parent
8efffb91
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
3 deletions
+26
-3
models.py
loans/models.py
+3
-3
validators.py
loans/validators.py
+23
-0
No files found.
loans/models.py
View file @
987546e3
from
django.db
import
models
from
django.utils
import
timezone
from
book.models
import
Book
from
users.models
import
Member
from
.validators
import
validate_due_date
,
validate_loan_date
class
BookLoan
(
models
.
Model
):
book
=
models
.
ForeignKey
(
Book
,
on_delete
=
models
.
CASCADE
)
member
=
models
.
ForeignKey
(
Member
,
on_delete
=
models
.
CASCADE
)
loan_date
=
models
.
DateTimeField
()
due_date
=
models
.
DateTimeField
()
loan_date
=
models
.
DateTimeField
(
validators
=
[
validate_loan_date
]
)
due_date
=
models
.
DateTimeField
(
validators
=
[
validate_due_date
]
)
return_date
=
models
.
DateTimeField
(
blank
=
True
,
null
=
True
)
created_at
=
models
.
DateTimeField
(
auto_now_add
=
True
)
updated_at
=
models
.
DateTimeField
(
auto_now
=
True
)
loans/validators.py
0 → 100644
View file @
987546e3
from
django.core.exceptions
import
ValidationError
from
django.utils.translation
import
gettext_lazy
as
_
from
django.utils
import
timezone
def
validate_loan_date
(
value
):
now
=
timezone
.
now
()
loan_date
=
value
if
loan_date
>
now
:
raise
ValidationError
(
_
(
"Loan date cannot be later than today"
),
params
=
{
"value"
:
value
},
)
def
validate_due_date
(
value
):
due_date
=
value
loan_date
=
timezone
.
now
()
if
due_date
<
loan_date
:
raise
ValidationError
(
_
(
"Due date cannot be less than loan date"
),
params
=
{
"value"
:
value
},
)
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