diff --git a/django_rest_sample/urls.py b/django_rest_sample/urls.py index 05fb841da371ccc55cbe81f3dacc6687bba81312..606b35101976ef18a829f6d03d35f81876f293f5 100644 --- a/django_rest_sample/urls.py +++ b/django_rest_sample/urls.py @@ -16,6 +16,7 @@ Including another URLconf 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 @@ -26,5 +27,6 @@ router.register(r'authors', AuthorViewSet) urlpatterns = router.urls urlpatterns += [ + path('library_app/', include('library_app.urls')), path('admin/', admin.site.urls), ] diff --git a/library_app/selfFunc.py b/library_app/selfFunc.py new file mode 100644 index 0000000000000000000000000000000000000000..d308ca515d448e2a9b0f5c843c3187f819f23b11 --- /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 diff --git a/library_app/serializers.py b/library_app/serializers.py index 849aea1f7cfdf51d36afe7743319ac57b0688bc5..9b79f72ac7c920cfd472ab25c220ac30ec49e303 100644 --- a/library_app/serializers.py +++ b/library_app/serializers.py @@ -9,6 +9,12 @@ class AuthorSerializer(serializers.ModelSerializer): model = Author fields = "__all__" +class CategorySerializer(serializers.ModelSerializer): + class Meta: + model = Category + fields = "__all__" + + class BookReadSerializer(serializers.ModelSerializer): category = CategorySerializer() author = AuthorSerializer() @@ -22,7 +28,3 @@ class BookWriteSerializer(serializers.ModelSerializer): model = Book fields = "__all__" -class CategorySerializer(serializers.ModelSerializer): - class Meta: - model = Category - fields = "__all__" diff --git a/library_app/templates/.keep b/library_app/templates/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/library_app/templates/library_app/.keep b/library_app/templates/library_app/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/library_app/templates/library_app/index.html b/library_app/templates/library_app/index.html new file mode 100644 index 0000000000000000000000000000000000000000..9061f5ae26bfff9d4d7dc1177944fdc4a542ec78 --- /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}}

diff --git a/library_app/urls.py b/library_app/urls.py new file mode 100644 index 0000000000000000000000000000000000000000..3ef24d9717f6abdd8d356990d19b5c92b2ed0261 --- /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 index da56ab7fe376dc3d778ee7e3d280b4971270631d..f6d634c1c8615aeb8c202599f2f642558b3f0a5f 100644 --- a/library_app/views.py +++ b/library_app/views.py @@ -2,11 +2,13 @@ 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 @@ -21,3 +23,33 @@ class CategoryViewSet(viewsets.ModelViewSet): 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 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 0000000000000000000000000000000000000000..acfd112c0443244253dfaf6ecfa00f1f7d63c7e9 --- /dev/null +++ "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)获取作者名字 +