Skip to content

Commit

Permalink
Restyled by yapf
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Apr 1, 2020
1 parent 57ba4f6 commit acaa368
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 100 deletions.
4 changes: 3 additions & 1 deletion book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class Transaction(models.Model):
issue_by = models.ForeignKey(User, on_delete=models.CASCADE)
# when librarian accept book request
issue_date = models.DateField(null=True, blank=True)
return_date = models.DateField(null=True, blank=True) # when user return book
return_date = models.DateField(null=True,
blank=True) # when user return book

# status =models.PositiveIntegerField(default=0,choices=STATUS)

def __str__(self):
Expand Down
28 changes: 15 additions & 13 deletions book/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
urlpatterns = [
path("", views.index, name="index"),
path("books", views.BooksCBView.as_view(), name="books"),
path(
"books/issue/<int:book_id>", views.IssueBookCBView.as_view(), name="issuebook"
),
path("books/issue/<int:book_id>",
views.IssueBookCBView.as_view(),
name="issuebook"),
path("mybooks", views.MybookCBV.as_view(), name="mybooks"),
path(
"mybooks/cancel/<int:book_id>", views.MybookcancelCBV.as_view(), name="cancel"
),
path(
"mybooks/return/<int:book_id>", views.MybookreturnCBV.as_view(), name="return"
),
path("mybooks/returned", views.PastTransactionCBV.as_view(), name="returned"),
path("mybooks/cancel/<int:book_id>",
views.MybookcancelCBV.as_view(),
name="cancel"),
path("mybooks/return/<int:book_id>",
views.MybookreturnCBV.as_view(),
name="return"),
path("mybooks/returned",
views.PastTransactionCBV.as_view(),
name="returned"),
path("mybooks/queue", views.MybookWaitingCBView.as_view(), name="queue"),
path(
"mybooks/queue/<int:book_id>", views.UserWaitingList.as_view(), name="userqueue"
),
path("mybooks/queue/<int:book_id>",
views.UserWaitingList.as_view(),
name="userqueue"),
path(
"mybooks/queue/cancel/<int:book_id>",
views.UserWaitingCancel.as_view(),
Expand Down
135 changes: 65 additions & 70 deletions book/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,75 +33,88 @@ def get(self, request, *args, **kwargs):
class IssueBookCBView(View):
def get(self, request, *args, **kwargs):
# check user book does not morethen five
bookavailable = Books.objects.filter(id=kwargs["book_id"], quantity__gt=0)
bookavailable = Books.objects.filter(id=kwargs["book_id"],
quantity__gt=0)
try:
if bookavailable.count():
return render(request, "book_issue.html", {"book": bookavailable.get()})
return render(request, "book_issue.html",
{"book": bookavailable.get()})
else:
return render(
request,
"book_issue.html",
{
"book": bookavailable.get(),
"message": "Book not Available at moment! Can you wait for book?",
"book":
bookavailable.get(),
"message":
"Book not Available at moment! Can you wait for book?",
},
)
except Exception as e:
return render(
request,
"book_issue.html",
{"message": "Book not Available at moment.! can you wait for book?"},
{
"message":
"Book not Available at moment.! can you wait for book?"
},
)

def post(self, request, *args, **kwargs):
bookavailable = Books.objects.filter(id=kwargs["book_id"], quantity__gt=0)
bookavailable = Books.objects.filter(id=kwargs["book_id"],
quantity__gt=0)
book = request.POST.get("bookid", "")
try:
if bookavailable.count():

bookexist = Transaction.objects.filter(
issue_by=request.user, return_date=None, book=bookavailable.get()
)
issue_by=request.user,
return_date=None,
book=bookavailable.get())
if bookexist:
messages.warning(
request,
"book already issue by you. one book cannot issue multiple at a times ",
)
return redirect("books")
bookavailable = bookavailable.get()
Transaction.objects.create(book=bookavailable, issue_by=request.user)
Transaction.objects.create(book=bookavailable,
issue_by=request.user)
bookavailable.quantity -= 1
bookavailable.save()
messages.success(request, "book has been issue done")
return redirect("books")
else:
book1 = Books.objects.filter(id=kwargs["book_id"]).get()
transexist = WaitingTransaction.objects.filter(
book=book1, issue_by=request.user
)
book=book1, issue_by=request.user)
if transexist.exists():
messages.error(request, "opps you are already in waiting queue")
messages.error(request,
"opps you are already in waiting queue")
return redirect("mybooks")
WaitingTransaction.objects.create(book=book1, issue_by=request.user)
WaitingTransaction.objects.create(book=book1,
issue_by=request.user)
messages.success(request, "Your request added in queue.")
return redirect("mybooks")
except Exception as e:
book1 = Books.objects.filter(id=kwargs["book_id"]).get()
transexist = WaitingTransaction.objects.filter(
book=book1, issue_by=request.user
)
book=book1, issue_by=request.user)
if transexist.exists():
messages.error(request, "opps you are already in waiting queue")
messages.error(request,
"opps you are already in waiting queue")
return redirect("mybooks")
WaitingTransaction.objects.create(book=book1, issue_by=request.user)
WaitingTransaction.objects.create(book=book1,
issue_by=request.user)
messages.success(request, "Your request added in queue.")
return redirect("mybooks")


@method_decorator(login_required, name="dispatch")
class MybookCBV(View):
def get(self, request):
allbook = Transaction.objects.filter(issue_by=request.user, return_date=None)
allbook = Transaction.objects.filter(issue_by=request.user,
return_date=None)
return render(request, "book_user_all.html", {"mybook": allbook})

def post(self, request):
Expand All @@ -112,34 +125,29 @@ def post(self, request):
class MybookcancelCBV(View):
def get(self, request, *args, **kwargs):
# check transection exist or not
transation_exist = Transaction.objects.filter(
issue_by=request.user, issue_date=None, id=kwargs["book_id"]
)
transation_exist = Transaction.objects.filter(issue_by=request.user,
issue_date=None,
id=kwargs["book_id"])
if transation_exist.count():
return render(
request, "book_cancel.html", {"trans": transation_exist.get()}
)
return render(request, "book_cancel.html",
{"trans": transation_exist.get()})
else:
messages.error(request, "book's transaction does not exist.!!")
return redirect("mybooks")

def post(self, request, *args, **kwargs):

transation_exist = Transaction.objects.filter(
issue_by=request.user, issue_date=None, id=kwargs["book_id"]
)
transation_exist = Transaction.objects.filter(issue_by=request.user,
issue_date=None,
id=kwargs["book_id"])
if transation_exist.count():
transation_exist = transation_exist.get()
book1 = Books.objects.filter(id=transation_exist.book.id).get()
waitinguser = (
WaitingTransaction.objects.filter(book=book1)
.order_by("issue_date")
.first()
)
waitinguser = (WaitingTransaction.objects.filter(
book=book1).order_by("issue_date").first())
if waitinguser:
trans = Transaction.objects.create(
book=book1, issue_by=waitinguser.issue_by
)
book=book1, issue_by=waitinguser.issue_by)
else:
book1.quantity += 1
transation_exist.delete()
Expand All @@ -153,35 +161,30 @@ def post(self, request, *args, **kwargs):
@method_decorator(login_required, name="dispatch")
class MybookreturnCBV(View):
def get(self, request, *args, **kwargs):
transation_exist = Transaction.objects.filter(
issue_by=request.user, return_date=None, id=kwargs["book_id"]
)
transation_exist = Transaction.objects.filter(issue_by=request.user,
return_date=None,
id=kwargs["book_id"])
if transation_exist.count():
return render(
request, "book_return.html", {"trans": transation_exist.get()}
)
return render(request, "book_return.html",
{"trans": transation_exist.get()})
else:
messages.error(request, "book's transaction does not exist.!!")
return redirect("mybooks")

def post(self, request, *args, **kwargs):
transation_exist = Transaction.objects.filter(
issue_by=request.user, return_date=None, id=kwargs["book_id"]
)
transation_exist = Transaction.objects.filter(issue_by=request.user,
return_date=None,
id=kwargs["book_id"])
if transation_exist.count():
transation_exist = transation_exist.get()
transation_exist.return_date = timezone.now()

book1 = Books.objects.filter(id=transation_exist.book.id).get()
waitinguser = (
WaitingTransaction.objects.filter(book=book1)
.order_by("issue_date")
.first()
)
waitinguser = (WaitingTransaction.objects.filter(
book=book1).order_by("issue_date").first())
if waitinguser:
trans = Transaction.objects.create(
book=book1, issue_by=waitinguser.issue_by
)
book=book1, issue_by=waitinguser.issue_by)
waitinguser.delete()
else:
book1.quantity += 1
Expand All @@ -198,12 +201,10 @@ def post(self, request, *args, **kwargs):
class PastTransactionCBV(View):
def get(self, request, *args, **kwargs):
transation_exist = Transaction.objects.filter(
issue_by=request.user, return_date__isnull=False
)
issue_by=request.user, return_date__isnull=False)
if transation_exist:
return render(
request, "book_return_transaction.html", {"tran": transation_exist}
)
return render(request, "book_return_transaction.html",
{"tran": transation_exist})
return render(request, "book_return_transaction.html")


Expand All @@ -225,16 +226,13 @@ def post(self, request, *args, **kwargs):
@method_decorator(login_required, name="dispatch")
class UserWaitingList(View):
def get(self, request, *args, **kwargs):
book1 = WaitingTransaction.objects.filter(
id=kwargs["book_id"], issue_by=request.user
)
book1 = WaitingTransaction.objects.filter(id=kwargs["book_id"],
issue_by=request.user)
if book1.exists():
waitinguser = WaitingTransaction.objects.filter(
book=book1.get().book
).order_by("issue_date")
return render(
request, "book_user_waiting_show.html", {"books": waitinguser}
)
book=book1.get().book).order_by("issue_date")
return render(request, "book_user_waiting_show.html",
{"books": waitinguser})
messages.error(request, "opps. data does'nt exist")
return redirect("mybooks")

Expand All @@ -243,19 +241,16 @@ def get(self, request, *args, **kwargs):
class UserWaitingCancel(View):
def get(self, request, *args, **kwargs):
userinwaiting = WaitingTransaction.objects.filter(
id=kwargs["book_id"], issue_by=request.user
)
id=kwargs["book_id"], issue_by=request.user)
if userinwaiting.exists():
return render(
request, "book_waiting_cancel.html", {"trans": userinwaiting.get()}
)
return render(request, "book_waiting_cancel.html",
{"trans": userinwaiting.get()})
messages.error(request, "book's transaction does not exist.!!")
return redirect("queue")

def post(self, request, *args, **kwargs):
userinwaiting = WaitingTransaction.objects.filter(
id=kwargs["book_id"], issue_by=request.user
)
id=kwargs["book_id"], issue_by=request.user)
if userinwaiting.exists():
userinwaiting.delete()
messages.success(request, "waiting book deleted")
Expand Down
14 changes: 10 additions & 4 deletions librarian/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@

class add_book(forms.ModelForm):
class Meta:
category = forms.ChoiceField(choices=Category.objects.all(), required=True,)
category = forms.ChoiceField(
choices=Category.objects.all(),
required=True,
)
model = Books
fields = ["title", "author", "quantity", "category"]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].widget.attrs.update(
{"class": "form-control", "placeholder": self.fields[field].label}
)
self.fields[field].widget.attrs.update({
"class":
"form-control",
"placeholder":
self.fields[field].label
})
Loading

0 comments on commit acaa368

Please sign in to comment.