Skip to content

Commit

Permalink
Part 19
Browse files Browse the repository at this point in the history
  • Loading branch information
zeliangyao committed Aug 14, 2021
1 parent a952b9e commit a4edee9
Show file tree
Hide file tree
Showing 28 changed files with 871 additions and 173 deletions.
2 changes: 1 addition & 1 deletion authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def login_view(request):
login(request, user)
return redirect("/")
else:
msg = 'Invalid credentials'
msg = 'Username or Password invalid'
else:
msg = 'Error validating the form'

Expand Down
25 changes: 25 additions & 0 deletions book/migrations/0016_auto_20210812_1039.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.2.10 on 2021-08-12 08:39

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('book', '0015_auto_20210811_2303'),
]

operations = [
migrations.RenameField(
model_name='borrowrecord',
old_name='status',
new_name='open_or_close',
),
migrations.AlterField(
model_name='borrowrecord',
name='end_day',
field=models.DateTimeField(default=datetime.datetime(2021, 8, 19, 8, 39, 11, 212961, tzinfo=utc)),
),
]
20 changes: 20 additions & 0 deletions book/migrations/0017_auto_20210812_1556.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.10 on 2021-08-12 13:56

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('book', '0016_auto_20210812_1039'),
]

operations = [
migrations.AlterField(
model_name='borrowrecord',
name='end_day',
field=models.DateTimeField(default=datetime.datetime(2021, 8, 19, 13, 56, 7, 491659, tzinfo=utc)),
),
]
20 changes: 20 additions & 0 deletions book/migrations/0018_auto_20210812_1556.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.10 on 2021-08-12 13:56

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('book', '0017_auto_20210812_1556'),
]

operations = [
migrations.AlterField(
model_name='borrowrecord',
name='end_day',
field=models.DateTimeField(default=datetime.datetime(2021, 8, 19, 13, 56, 29, 721011, tzinfo=utc)),
),
]
20 changes: 20 additions & 0 deletions book/migrations/0019_auto_20210812_1556.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.10 on 2021-08-12 13:56

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('book', '0018_auto_20210812_1556'),
]

operations = [
migrations.AlterField(
model_name='borrowrecord',
name='end_day',
field=models.DateTimeField(default=datetime.datetime(2021, 8, 19, 13, 56, 53, 396797, tzinfo=utc)),
),
]
20 changes: 20 additions & 0 deletions book/migrations/0020_auto_20210812_1558.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.10 on 2021-08-12 13:58

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('book', '0019_auto_20210812_1556'),
]

operations = [
migrations.AlterField(
model_name='borrowrecord',
name='end_day',
field=models.DateTimeField(default=datetime.datetime(2021, 8, 19, 13, 58, 4, 399124, tzinfo=utc)),
),
]
12 changes: 2 additions & 10 deletions book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@
(1,'Closed')
)

# RETURN_STATUS=(
# (True,'On Time'),
# (False,'Delayed')
# )

class Category(models.Model):

name = models.CharField(max_length=50, blank=True)
Expand Down Expand Up @@ -181,21 +176,18 @@ def get_absolute_url(self):

class BorrowRecord(models.Model):


borrower = models.CharField(blank=False,max_length=20)
borrower_card = models.CharField(max_length=8,blank=True)
borrower_email = models.EmailField(max_length=50,blank=True)
borrower_phone_number = models.CharField(max_length=30,blank=True)

book = models.CharField(blank=False,max_length=20)

quantity = models.PositiveIntegerField(default=1)

start_day = models.DateTimeField(default=timezone.now)
end_day = models.DateTimeField(default=timezone.now()+timedelta(days=7))
periode = models.PositiveIntegerField(default=0)

status = models.IntegerField(choices=BORROW_RECORD_STATUS,default=0)
open_or_close = models.IntegerField(choices=BORROW_RECORD_STATUS,default=0)
delay_days = models.IntegerField(default=0)

created_at= models.DateTimeField(default=timezone.now)
Expand Down Expand Up @@ -226,7 +218,7 @@ def __str__(self):

def save(self, *args, **kwargs):
# profile = super(Profile, self).save(*args, **kwargs)
self.periode =(self.end_day - self.start_day).days
self.periode =(self.end_day - self.start_day).days+1
return super(BorrowRecord, self).save(*args, **kwargs)


Expand Down
11 changes: 7 additions & 4 deletions book/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from django.conf.urls.static import static
from .views import BorrowRecordListView,BorrowRecordCreateView,BorrowRecordDeleteView,BorrowRecordDetailView,auto_member,auto_book,BorrowRecordClose
from .views import DataCenterView,download_data

# BorrowRecordCreateView

from .views import ChartView,global_serach

urlpatterns = [

Expand Down Expand Up @@ -63,11 +61,16 @@
path('record-delete/<int:pk>/',BorrowRecordDeleteView.as_view(),name="record_delete"),
path('record-close/<int:pk>/',BorrowRecordClose.as_view(),name="record_close"),


# Data center
path('data-center/',DataCenterView.as_view(),name="data_center"),
path('data-download/<str:model_name>/',download_data,name="data_download"),

# Chart
path('charts/',ChartView.as_view(),name="chart"),

# Global Search
path('global-search/',global_serach,name="global_search"),

]


Expand Down
82 changes: 73 additions & 9 deletions book/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pandas as pd
import json

from django.db.models.functions import ExtractMonth,ExtractWeek,TruncMonth,TruncWeek

from django.shortcuts import render,get_object_or_404,redirect
from django.urls import reverse_lazy,reverse
from django.contrib.auth import get_user_model
Expand All @@ -18,7 +20,7 @@
from django.core.paginator import EmptyPage, PageNotAnInteger
from django.core import serializers


from django.db.models import Sum, Count
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
Expand Down Expand Up @@ -74,7 +76,7 @@ def get(self,request, *args, **kwargs):

books_return_thisweek = BorrowRecord.objects.filter(end_day__week=current_week)
number_books_return_thisweek = books_return_thisweek.count()
new_closed_records = BorrowRecord.objects.filter(status=1).order_by('-closed_at')[:5]
new_closed_records = BorrowRecord.objects.filter(open_or_close=1).order_by('-closed_at')[:5]

self.context['data_count']=data_count
self.context['recent_user_activities']=user_activities
Expand All @@ -89,6 +91,66 @@ def get(self,request, *args, **kwargs):

return render(request, self.template_name, self.context)

@login_required(login_url='login')
def global_serach(request):
search_value = request.POST.get('global_search')
if search_value =='':
return HttpResponseRedirect("/")

r_category = Category.objects.filter(Q(name__icontains=search_value))
r_publisher = Publisher.objects.filter(Q(name__icontains=search_value)|Q(contact__icontains=search_value))
r_book = Book.objects.filter(Q(author__icontains=search_value)|Q(title__icontains=search_value))
r_member = Member.objects.filter(Q(name__icontains=search_value)|Q(card_number__icontains=search_value)|Q(phone_number__icontains=search_value))
r_borrow = BorrowRecord.objects.filter(Q(borrower__icontains=search_value)|Q(borrower_card__icontains=search_value)|Q(book__icontains=search_value))


context={
'categories':r_category,
'publishers':r_publisher,
'books':r_book,
'members':r_member,
'records':r_borrow,
}

return render(request, 'book/global_search.html',context=context)


# Chart
class ChartView(LoginRequiredMixin,TemplateView):
template_name = "charts.html"
login_url = 'login'
context={}

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

top_5_book= Book.objects.order_by('-quantity')[:5].values_list('title','quantity')
top_5_book_titles = [b[0] for b in top_5_book ]
top_5_book__quantities = [b[1] for b in top_5_book ]
# print(top_5_book_titles,top_5_book__quantities)

top_borrow = Book.objects.order_by('-total_borrow_times')[:5].values_list('title','total_borrow_times')
top_borrow_titles = [b[0] for b in top_borrow ]
top_borrow_times = [b[1] for b in top_borrow ]

r_open = BorrowRecord.objects.filter(open_or_close=0).count()
r_close = BorrowRecord.objects.filter(open_or_close=1).count()

m = Member.objects.annotate(month=TruncMonth('created_at')).values('month').annotate(c=Count('id'))
months_member = [e['month'].strftime("%m/%Y") for e in m]
count_monthly_member= [e['c'] for e in m]


self.context['top_5_book_titles']=top_5_book_titles
self.context['top_5_book__quantities']=top_5_book__quantities
self.context['top_borrow_titles']=top_borrow_titles
self.context['top_borrow_times']=top_borrow_times
self.context['r_open']=r_open
self.context['r_close']=r_close
self.context['months_member']=months_member
self.context['count_monthly_member']=count_monthly_member


return render(request, self.template_name, self.context)

# Book

Expand Down Expand Up @@ -542,10 +604,13 @@ class MemberDetailView(LoginRequiredMixin,DetailView):
template_name = 'book/member_detail.html'
login_url = 'login'

# def get_context_data(self, **kwargs):
# context = super().get_context_data(**kwargs)
# context["card_number"] = str(self.get_object().card_id)[:8]
# return context
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
current_member_name = self.get_object().name
related_records = BorrowRecord.objects.filter(borrower=current_member_name)
context['related_records'] = related_records
context["card_number"] = str(self.get_object().card_id)[:8]
return context


# Profile View
Expand Down Expand Up @@ -594,12 +659,11 @@ def get_form(self):

def form_valid(self, form):
selected_member= get_object_or_404(Member,name = form.cleaned_data['borrower'] )

form.instance.borrower_card = selected_member.card_number
form.instance.borrower_email = selected_member.email
form.instance.borrower_phone_number = selected_member.phone_number
form.instance.created_by = self.request.user.username
# form.save()
form.save()
return super(BorrowRecordCreateView,self).form_valid(form)


Expand Down Expand Up @@ -718,7 +782,7 @@ class BorrowRecordClose(LoginRequiredMixin,View):
def get(self, request, *args, **kwargs):
close_record = BorrowRecord.objects.get(pk=self.kwargs['pk'])
close_record.closed_by = self.request.user.username
close_record.status = 1
close_record.open_or_close = 1
close_record.save()
model_name = close_record.__class__.__name__
UserActivity.objects.create(created_by=self.request.user.username,
Expand Down
4 changes: 1 addition & 3 deletions core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
"""
from django.conf import settings
from django.conf.urls.static import static
from ajax_select import urls as ajax_select_urls
from django.contrib import admin
from django.urls import path, include # add this
# from django.conf.urls import handler400, handler403, handler404, handler500

urlpatterns = [
path('ajax_select/', include(ajax_select_urls)),
path('admin/', admin.site.urls),
path("auth/", include("authentication.urls")), # Auth routes - login / register
path("", include("book.urls")),
# path("app/", include("app.urls")) ,
path("app/", include("app.urls")) ,

]

Expand Down
Binary file modified db.sqlite3
Binary file not shown.
Loading

0 comments on commit a4edee9

Please sign in to comment.