On Wed, Aug 24, 2022 at 12:48:26AM -0700, Aakash Bhaikatti wrote:
> As a User
>
> - I can signup either as LIBRARIAN and MEMBER using username and password
> - I can login using username/password and get JWT access token
>
> As a Librarian
>
> - I can add, update, and remove Books from the system
> - I can add, update, view, and remove Member from the system
>
> As a Member
>
> - I can view, borrow, and return available Books
> - Once a book is borrowed, its status will change to BORROWED
> - Once a book is returned, its status will change to AVAILABLE
> - I can delete my own account
>
> As i'm a newbie. Can anyone Please help in how can I create the models of
> these. Thank you
You can use the Django User model[1] since it already has username and
password. To distinguish between librarian and member you can create
librarian and member groups using Django's Group model[2].
Then for the book, you can do something like:
class Book(models.Model):
STATUS_CHOICES = [
('BORROWED', 'Borrowed'),
('AVAILABLE', 'Available'),
]
title = models.CharField(max_length=200)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='AVAILABLE')
borrower = models.ForeignKey(User, null=True, blank=True)
class Meta:
permissions = [
('borrow_book', 'Can borrow a book'),
('return_book', 'Can return a book'),
]
def __str__(self):
return self.title
You'll add the following permissions[3] to the member group assuming
your Django app is called "library":
* library.view_book
* library.borrow_book
* library.return_book
You can create these permissions[4] for member:
* library.view_member
* library.add_member
* library.update_member
* library.delete_member
You can then add permissions to the librarian group:
* library.view_member
* library.add_member
* library.update_member
* library.delete_member
* library.add_book
* library.update_book
* library.delete_book
You can use the permission_required[5] decorator around your view
functions to control access.
[1]
https://docs.djangoproject.com/en/4.1/ref/contrib/auth/#user-model
[2]
https://docs.djangoproject.com/en/4.1/ref/contrib/auth/#group-model
[3]
https://docs.djangoproject.com/en/4.1/topics/auth/default/#default-permissions
[4]
https://docs.djangoproject.com/en/4.1/topics/auth/default/#programmatically-creating-permissions
[5]
https://docs.djangoproject.com/en/4.1/topics/auth/default/#the-permission-required-decorator