From 7318610ccd0f8d50468ee52641081ebf4ff69451 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:02:32 +0800 Subject: [PATCH 01/12] 1 --- library_app/selfFunc.py | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 library_app/selfFunc.py diff --git a/library_app/selfFunc.py b/library_app/selfFunc.py new file mode 100644 index 0000000..d308ca5 --- /dev/null +++ b/library_app/selfFunc.py @@ -0,0 +1,85 @@ +#return the Book queryset object +def getBookObject(bookClass): + return bookClass.queryset + +##return the author queryset object +def getAuthorObject(authorClass): + return authorClass.queryset + +#return the category queryset object +def getCategoryObject(categoryClass): + return categoryClass.queryset + +#use the build-in function to count the number of return object directly +def countBook(bookObject): + """ + input: the book queryset object + return: number of book + """ + return bookObject.count() + +def totalValue(bookObject): + """ + input: the book queryset object + output: the total value of book + """ + #get the book's price list + valueList = bookObject.values('price') + #initialize + value = 0 + #traverse the price list and sum up + for i in range(len(valueList)): + value += valueList[i]['price'] + return value + +#get the authod whose number of book is the most +def authorMostbook(bookObject, authorObject): + """ + input: book queryset object, author queryset object + output: author name and number of book + """ + #use a dict to store the infor, key is author id, value is the count of frequency of author id + countDict = {} + #the author list of book + bookList = bookObject.values('author') + #traverse the list + #if the author is not in dict, add it into dict and set the value as 1 + #if the author is already in dict, get the value and add 1 + for i in range(len(bookList)): + if(bookList[i]['author'] not in countDict): + countDict[bookList[i]['author']] = 1 + else: + count = countDict[bookList[i]['author']] + countDict[bookList[i]['author']] = count + 1 + #zip the keys list and values list + #use max() to get the biggest value and the corresponding author id + key, value = max(zip(countDict.keys(),countDict.values())) + #get the author name according to the id + authorName = authorObject.values().filter(id=key)[0]['name'] + return authorName, value + +def categoryMostBook(bookObject, categoryObject): + """ + input: book queryset object, catrgory queryset object + output: category title and the number of book in that category + """ + #use dict to store the infor + countDict = {} + #get the list of category id from book queryset object + bookList = bookObject.values('category_id') + #traverse the list + #if the category id is not in dict, add it into dict and set the value as 1 + #if the category id is already in dict, get the value and add 1 + for i in range(len(bookList)): + if(bookList[i]['category_id'] not in countDict): + countDict[bookList[i]['category_id']] = 1 + else: + count = countDict[bookList[i]['category_id']] + countDict[bookList[i]['category_id']] = count + 1 + #zip the keys list and values list + #use max() to get the biggest value and the corresponding category id + key, value = max(zip(countDict.keys(),countDict.values())) + #get the category name + category = categoryObject.values().filter(id=key)[0]['title'] + + return category, value \ No newline at end of file -- Gitee From 973cf3e8628f8d1174c36a371bcddb9037e044db Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:02:57 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20li?= =?UTF-8?q?brary=5Fapp/views.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library_app/views.py | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 library_app/views.py diff --git a/library_app/views.py b/library_app/views.py deleted file mode 100644 index da56ab7..0000000 --- a/library_app/views.py +++ /dev/null @@ -1,23 +0,0 @@ -from rest_framework import viewsets - -from library_app.models import Category, Author, Book -from library_app.serializers import CategorySerializer, AuthorSerializer, BookReadSerializer, BookWriteSerializer - - -class BookViewSet(viewsets.ModelViewSet): - queryset = Book.objects.all() - - def get_serializer_class(self): - if self.request.method in ['GET', 'HEAD']: - return BookReadSerializer - else: - return BookWriteSerializer - -class CategoryViewSet(viewsets.ModelViewSet): - queryset = Category.objects.all() - serializer_class = CategorySerializer - - -class AuthorViewSet(viewsets.ModelViewSet): - queryset = Author.objects.all() - serializer_class = AuthorSerializer -- Gitee From 78957797bcd44516af792bb6c7caf19feb176bd4 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:03:21 +0800 Subject: [PATCH 03/12] 2 --- library_app/urls.py | 7 ++++++ library_app/views.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 library_app/urls.py create mode 100644 library_app/views.py diff --git a/library_app/urls.py b/library_app/urls.py new file mode 100644 index 0000000..3ef24d9 --- /dev/null +++ b/library_app/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path('', views.index, name='index'), +] \ No newline at end of file diff --git a/library_app/views.py b/library_app/views.py new file mode 100644 index 0000000..f6d634c --- /dev/null +++ b/library_app/views.py @@ -0,0 +1,55 @@ +from rest_framework import viewsets + +from library_app.models import Category, Author, Book +from library_app.serializers import CategorySerializer, AuthorSerializer, BookReadSerializer, BookWriteSerializer +from django.http import HttpResponse +from django.shortcuts import render + +from .selfFunc import * + +class BookViewSet(viewsets.ModelViewSet): + queryset = Book.objects.all() + def get_serializer_class(self): + if self.request.method in ['GET', 'HEAD']: + return BookReadSerializer + else: + return BookWriteSerializer + +class CategoryViewSet(viewsets.ModelViewSet): + queryset = Category.objects.all() + serializer_class = CategorySerializer + + +class AuthorViewSet(viewsets.ModelViewSet): + queryset = Author.objects.all() + serializer_class = AuthorSerializer + +def index(request): + """ + go to the site: http://localhost:8000/library_app/ + ALL the function are implemented in the doc selfFunc.py + """ + book = BookViewSet() + bookObject = getBookObject(book) + + author = AuthorViewSet() + authorObject = getAuthorObject(author) + + category = CategoryViewSet + categoryObject = getCategoryObject(category) + + #number of book + numBook = countBook(bookObject) + #value: + value = totalValue(bookObject) + #author with most book + author, count = authorMostbook(bookObject, authorObject) + #category with most book + category, countCategory = categoryMostBook(bookObject, categoryObject) + + + + + result = {'numBook': numBook, 'value': value, 'author': author, 'count': count, + 'category': category, "countCategory": countCategory} + return render(request,'library_app/index.html', result) \ No newline at end of file -- Gitee From 6854f32d239d1b00d1d7f8943795cc76b9d94357 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:03:39 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20templates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library_app/templates/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 library_app/templates/.keep diff --git a/library_app/templates/.keep b/library_app/templates/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From bcda1c37c55e03903bcca49255958a8ba2d04e44 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:04:13 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20library=5Fapp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library_app/templates/library_app/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 library_app/templates/library_app/.keep diff --git a/library_app/templates/library_app/.keep b/library_app/templates/library_app/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From ac42b0eae335c482df3c2ff3fa48946b61f09613 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:04:30 +0800 Subject: [PATCH 06/12] 3 --- library_app/templates/library_app/index.html | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 library_app/templates/library_app/index.html diff --git a/library_app/templates/library_app/index.html b/library_app/templates/library_app/index.html new file mode 100644 index 0000000..9061f5a --- /dev/null +++ b/library_app/templates/library_app/index.html @@ -0,0 +1,4 @@ +

Number of book: {{ numBook|safe }}

+

Total cost of all Book: {{ value|safe }}

+

The author {{author|safe}} has the most number of book of {{count|safe}}

+

The category {{category|safe}} has the most number of book of {{countCategory|safe}}

-- Gitee From c280542ca7d2f724512898894857f056b5088386 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:05:35 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20dj?= =?UTF-8?q?ango=5Frest=5Fsample/urls.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- django_rest_sample/urls.py | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 django_rest_sample/urls.py diff --git a/django_rest_sample/urls.py b/django_rest_sample/urls.py deleted file mode 100644 index 05fb841..0000000 --- a/django_rest_sample/urls.py +++ /dev/null @@ -1,30 +0,0 @@ -"""django_rest_sample URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/2.0/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" -from django.contrib import admin -from django.urls import path -from rest_framework import routers - -from library_app.views import BookViewSet, CategoryViewSet, AuthorViewSet - -router = routers.DefaultRouter() -router.register(r'books', BookViewSet) -router.register(r'categories', CategoryViewSet) -router.register(r'authors', AuthorViewSet) -urlpatterns = router.urls - -urlpatterns += [ - path('admin/', admin.site.urls), -] -- Gitee From 86c343c7297a71a807c3c3a4a64881d457954c86 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:05:44 +0800 Subject: [PATCH 08/12] 4 --- django_rest_sample/urls.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 django_rest_sample/urls.py diff --git a/django_rest_sample/urls.py b/django_rest_sample/urls.py new file mode 100644 index 0000000..606b351 --- /dev/null +++ b/django_rest_sample/urls.py @@ -0,0 +1,32 @@ +"""django_rest_sample URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path +from rest_framework import routers +from django.urls import include, path + +from library_app.views import BookViewSet, CategoryViewSet, AuthorViewSet + +router = routers.DefaultRouter() +router.register(r'books', BookViewSet) +router.register(r'categories', CategoryViewSet) +router.register(r'authors', AuthorViewSet) +urlpatterns = router.urls + +urlpatterns += [ + path('library_app/', include('library_app.urls')), + path('admin/', admin.site.urls), +] -- Gitee From 915ed8d08e4a9a52bc91b81a53005d0ad312dd3e Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:07:36 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20li?= =?UTF-8?q?brary=5Fapp/serializers.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library_app/serializers.py | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 library_app/serializers.py diff --git a/library_app/serializers.py b/library_app/serializers.py deleted file mode 100644 index 849aea1..0000000 --- a/library_app/serializers.py +++ /dev/null @@ -1,28 +0,0 @@ -from rest_framework import serializers - -from library_app.models import Category, Book, Author - - - -class AuthorSerializer(serializers.ModelSerializer): - class Meta: - model = Author - fields = "__all__" - -class BookReadSerializer(serializers.ModelSerializer): - category = CategorySerializer() - author = AuthorSerializer() - - class Meta: - model = Book - fields = '__all__' - -class BookWriteSerializer(serializers.ModelSerializer): - class Meta: - model = Book - fields = "__all__" - -class CategorySerializer(serializers.ModelSerializer): - class Meta: - model = Category - fields = "__all__" -- Gitee From 43fec7b92db6f74c3e3087e363f4c69aa7e6f4a0 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 19:07:51 +0800 Subject: [PATCH 10/12] 5 --- library_app/serializers.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 library_app/serializers.py diff --git a/library_app/serializers.py b/library_app/serializers.py new file mode 100644 index 0000000..9b79f72 --- /dev/null +++ b/library_app/serializers.py @@ -0,0 +1,30 @@ +from rest_framework import serializers + +from library_app.models import Category, Book, Author + + + +class AuthorSerializer(serializers.ModelSerializer): + class Meta: + model = Author + fields = "__all__" + +class CategorySerializer(serializers.ModelSerializer): + class Meta: + model = Category + fields = "__all__" + + +class BookReadSerializer(serializers.ModelSerializer): + category = CategorySerializer() + author = AuthorSerializer() + + class Meta: + model = Book + fields = '__all__' + +class BookWriteSerializer(serializers.ModelSerializer): + class Meta: + model = Book + fields = "__all__" + -- Gitee From b1bf93178f3e4f97083014d1e134e9693ae8e2e6 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 20:38:46 +0800 Subject: [PATCH 11/12] =?UTF-8?q?add=20=E5=BC=80=E5=8F=91=E7=AC=94?= =?UTF-8?q?=E8=AE=B0.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\345\274\200\345\217\221\347\254\224\350\256\260.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\345\274\200\345\217\221\347\254\224\350\256\260.md" diff --git "a/\345\274\200\345\217\221\347\254\224\350\256\260.md" "b/\345\274\200\345\217\221\347\254\224\350\256\260.md" new file mode 100644 index 0000000..e69de29 -- Gitee From 538754019a2e673125603893fdcae475da899d86 Mon Sep 17 00:00:00 2001 From: caihaocheng <8535954+caihaocheng@user.noreply.gitee.com> Date: Tue, 5 Jan 2021 21:01:23 +0800 Subject: [PATCH 12/12] =?UTF-8?q?update=20=E5=BC=80=E5=8F=91=E7=AC=94?= =?UTF-8?q?=E8=AE=B0.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\217\221\347\254\224\350\256\260.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/\345\274\200\345\217\221\347\254\224\350\256\260.md" "b/\345\274\200\345\217\221\347\254\224\350\256\260.md" index e69de29..acfd112 100644 --- "a/\345\274\200\345\217\221\347\254\224\350\256\260.md" +++ "b/\345\274\200\345\217\221\347\254\224\350\256\260.md" @@ -0,0 +1,52 @@ +所用方法都在文件selfFunc.py 中实现 + 1. Total number of books owned. +首先获取Book object, + +``` +book = BookViewSet() +bookObject = getBookObject(book) +``` +然后对bookObject调用function countBook(bookObject)以获取总的书本数量;其实现方法为对bookObject调用内置函数count(),直接输出书的数量。 + + 2. Total cost of all books. +获取bookObject中说有书的价格 + +``` +valueList = bookObject.values('price') +``` +然后对于valueList,用for循环将所有价格相加,然后输出。 + +``` +value = 0 +#traverse the price list and sum up +for i in range(len(valueList)): + value += valueList[i]['price'] +``` + 3. Author with most books. +思想:利用dict储存信息,遍历library中的所有书,如果遇到一个新的作者(作者id not in dict),建一个新的key,并将value设为1;如果作者已经在dict里(作者id in dict), 获取在字典中该作者的书的数量,并更新(dict[author id])= value+1 + +``` +for i in range(len(bookList)): + if(bookList[i]['author'] not in countDict): + countDict[bookList[i]['author']] = 1 + else: + count = countDict[bookList[i]['author']] + countDict[bookList[i]['author']] = count + 1 +``` +然后获取字典中value最大的值和其对应的id +获取id后,可以在authorObject中,通过filter(ID=id)获取作者名字 + + 4. Category with most books. +思想:同 3. 相似。利用dict储存信息,遍历library中的所有书,如果遇到一个新的类别(category id not in dict),建一个新的key,并将value设为1;如果类别已经在dict里(category id in dict), 获取在字典中该类别的书的数量,并更新(dict[category id])= value+1 + +``` + for i in range(len(bookList)): + if(bookList[i]['category_id'] not in countDict): + countDict[bookList[i]['category_id']] = 1 + else: + count = countDict[bookList[i]['category_id']] + countDict[bookList[i]['category_id']] = count + 1 +``` +然后获取字典中value最大的值和其对应的id +获取id后,可以在categoryObject中,通过filter(ID=id)获取作者名字 + -- Gitee