-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
library management system created #1
Open
rohan-saeed
wants to merge
3
commits into
master
Choose a base branch
from
library-management
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,21 @@ | ||
/venv | ||
# Created by https://www.toptal.com/developers/gitignore/api/django | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=django | ||
|
||
### Django ### | ||
*.log | ||
*.pot | ||
*.pyc | ||
__pycache__/ | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
media | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
title,author,publisher,available_quantity | ||
Book 1,Author 1,Publisher A,10 | ||
Book 2,Author 2,Publisher B,15 | ||
Book 3,Author 3,Publisher A,8 | ||
Book 4,Author 4,Publisher C,20 | ||
Book 5,Author 5,Publisher B,12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.contrib import admin | ||
|
||
from .models import Book, BookIssue, NewBookTicket | ||
# Register your models here. | ||
|
||
admin.site.register(Book) | ||
admin.site.register(BookIssue) | ||
admin.site.register(NewBookTicket) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class LibraryManagementConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'library_management' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.core.mail import send_mail | ||
from django.utils import timezone | ||
from .models import BookIssue | ||
|
||
|
||
def send_return_reminders(): | ||
actual_return_date = timezone.now() + timezone.timedelta(days=15) | ||
overdue_books = BookIssue.objects.filter( | ||
return_date__lt=actual_return_date, returned=True) | ||
for book in overdue_books: | ||
user = book.user | ||
subject = 'Return Reminder' | ||
message = f"Dear {user.username}, please remember to return the book '{book.name}' as soon as possible." | ||
from_email = '[email protected]' | ||
recipient_list = [user.email] | ||
|
||
send_mail(subject, message, from_email, | ||
recipient_list, fail_silently=False) |
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# myapp/management/commands/import_books_from_csv.py | ||
import csv | ||
from django.core.management.base import BaseCommand | ||
|
||
from library_management.models import Book | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Import books from a CSV file' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('file_path', type=str, help='Path to the CSV file') | ||
|
||
def handle(self, *args, **kwargs): | ||
file_path = kwargs['file_path'] | ||
try: | ||
with open(file_path, 'r') as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row in reader: | ||
title = row['title'] | ||
author = row['author'] | ||
publisher = row['publisher'] | ||
available_quantity = int(row['available_quantity']) | ||
|
||
book, created = Book.objects.get_or_create( | ||
name=title, | ||
author=author, | ||
publisher=publisher, | ||
defaults={'quantity': available_quantity} | ||
) | ||
|
||
if not created: | ||
book.quantity = available_quantity | ||
book.save() | ||
|
||
self.stdout.write(self.style.SUCCESS( | ||
f'Successfully imported book: {title}')) | ||
|
||
except FileNotFoundError: | ||
self.stdout.write(self.style.ERROR( | ||
'File not found. Please check the file path.')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Generated by Django 4.2.6 on 2023-11-06 05:45 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Book', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=100)), | ||
('author', models.CharField(max_length=100)), | ||
('publisher', models.CharField(max_length=100)), | ||
('image', models.ImageField(blank=True, null=True, upload_to='books/')), | ||
('quantity', models.PositiveIntegerField(default=0)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='NewBookTicket', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('status', models.CharField(choices=[('Pending', 'Pending'), ('Approved', 'Approved'), ('Rejected', 'Rejected')], default='Pending', max_length=20)), | ||
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='library_management.book')), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='BookIssue', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('issued_date', models.DateField(auto_now=True)), | ||
('return_date', models.DateField()), | ||
('returned', models.BooleanField(default=False)), | ||
('status', models.CharField(choices=[('Issued', 'Issued'), ('Pending', 'Pending'), ('Rejected', 'Rejected')], default='Pending', max_length=20)), | ||
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='library_management.book')), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
library_management/migrations/0002_alter_bookissue_status.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.6 on 2023-11-07 05:21 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('library_management', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='bookissue', | ||
name='status', | ||
field=models.CharField(choices=[('pending', 'Pending'), ('issued', 'Issued'), ('rejected', 'Rejected')], default='pending', max_length=9), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from django.conf import settings | ||
from django.db import models | ||
from users.models import User | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from django.core.mail import send_mail | ||
|
||
|
||
class Book(models.Model): | ||
name = models.CharField(max_length=100) | ||
author = models.CharField(max_length=100) | ||
publisher = models.CharField(max_length=100) | ||
image = models.ImageField(upload_to='books/', null=True, blank=True) | ||
quantity = models.PositiveIntegerField(default=0) | ||
|
||
|
||
class BookIssue(models.Model): | ||
# REQUEST_STATUS_CHOICES = ( | ||
# ('Issued', 'Issued'), | ||
# ('Pending', 'Pending'), | ||
# ('Rejected', 'Rejected'), | ||
# ) | ||
user = models.ForeignKey(User, on_delete=models.CASCADE) | ||
book = models.ForeignKey(Book, on_delete=models.CASCADE) | ||
issued_date = models.DateField(auto_now=True) | ||
return_date = models.DateField() | ||
returned = models.BooleanField(default=False) | ||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have a return_date here when we have a separate table for returned books. |
||
|
||
class Status(models.TextChoices): | ||
""" | ||
Status of each request, changed to depict the current status | ||
Utilized TextChoices here as I required some comparisons with keys using request data | ||
""" | ||
PENDING = "pending", "Pending" | ||
ISSUED = "issued", "Issued" | ||
REJECTED = "rejected", "Rejected" | ||
status = models.CharField(choices=Status.choices, | ||
max_length=9, default=Status.PENDING) | ||
|
||
# status = models.CharField( | ||
# max_length=20, choices=REQUEST_STATUS_CHOICES, default='Pending') | ||
|
||
|
||
class NewBookTicket(models.Model): | ||
BOOK_TICKET_STATUS_CHOICES = ( | ||
('Pending', 'Pending'), | ||
('Approved', 'Approved'), | ||
('Rejected', 'Rejected'), | ||
) | ||
user = models.ForeignKey(User, on_delete=models.CASCADE) | ||
book = models.ForeignKey(Book, on_delete=models.CASCADE) | ||
status = models.CharField( | ||
max_length=20, choices=BOOK_TICKET_STATUS_CHOICES, default='Pending') | ||
|
||
def __str__(self): | ||
return f'{self.user} - {self.book}' | ||
|
||
|
||
@receiver(post_save, sender=NewBookTicket) | ||
def call_car_api(sender, instance, **kwargs): | ||
subject = 'Ticket Received' | ||
message = f"Dear {instance.user.username}, we have got your request regarding '{instance.book.name}'. We will soon inform you about availability ." | ||
from_email = '[email protected]' | ||
recipient_list = [instance.user.email] | ||
|
||
send_mail(subject, message, from_email, | ||
recipient_list, fail_silently=False) | ||
|
||
print('Ticket object created') | ||
print(sender, instance, kwargs) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good work on using inbuilt permission checks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class IsStaffEditorPermission(permissions.DjangoModelPermissions): | ||
def has_permission(self, request, view): | ||
if request.method == 'GET': | ||
return request.user and request.user.is_authenticated | ||
return request.user and request.user.is_authenticated and (request.user.is_librarian or request.user.is_staff) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good use of choices but we can utilize classes for choices as well. For example like this:
class MyModel(models.Model): class Month(models.IntegerChoices): JAN = 1, "JANUARY" FEB = 2, "FEBRUARY" MAR = 3, "MAR" # (...) month = models.PositiveSmallIntegerField( choices=Month.choices, default=Month.JAN )