From 56a66332fadcaee93e25368974efe48752359ead Mon Sep 17 00:00:00 2001 From: doubaniux Date: Mon, 23 Nov 2020 23:18:14 +0100 Subject: [PATCH] add backend support for multiple entity sources --- books/forms.py | 2 + books/models.py | 12 ++-- books/templates/books/create_update.html | 25 ++++++- books/templates/books/scrape.html | 2 +- books/views.py | 39 +++++++++-- common/models.py | 31 +++++---- common/scraper.py | 57 +++++++++++---- common/static/css/boofilsic.css | 4 ++ common/static/css/boofilsic.min.css | 2 +- common/static/sass/_Form.sass | 4 +- common/utils.py | 15 +--- common/views.py | 81 ++++++++++++++++++++-- movies/forms.py | 2 + movies/models.py | 10 ++- movies/templates/movies/create_update.html | 43 ++++++------ movies/templates/movies/scrape.html | 2 +- movies/views.py | 39 +++++++++-- 17 files changed, 275 insertions(+), 95 deletions(-) diff --git a/books/forms.py b/books/forms.py index b023028f..88c58c17 100644 --- a/books/forms.py +++ b/books/forms.py @@ -24,6 +24,8 @@ class BookForm(forms.ModelForm): fields = [ 'id', 'title', + 'source_site', + 'source_url', 'isbn', 'author', 'pub_house', diff --git a/books/models.py b/books/models.py index aa539fc8..9eecb479 100644 --- a/books/models.py +++ b/books/models.py @@ -3,7 +3,8 @@ import django.contrib.postgres.fields as postgres from django.utils.translation import ugettext_lazy as _ from django.db import models from django.core.serializers.json import DjangoJSONEncoder -from common.models import Resource, Mark, Review, Tag +from django.shortcuts import reverse +from common.models import Entity, Mark, Review, Tag from boofilsic.settings import BOOK_MEDIA_PATH_ROOT, DEFAULT_BOOK_IMAGE from django.utils import timezone @@ -19,7 +20,7 @@ def book_cover_path(instance, filename): return root + timezone.now().strftime('%Y/%m/%d') + f'{filename}' -class Book(Resource): +class Book(Entity): # widely recognized name, usually in Chinese title = models.CharField(_("title"), max_length=200) subtitle = models.CharField(_("subtitle"), blank=True, default='', max_length=200) @@ -46,7 +47,7 @@ class Book(Resource): # since data origin is not formatted and might be CNY USD or other currency, use char instead price = models.CharField(_("pricing"), blank=True, default='', max_length=50) pages = models.PositiveIntegerField(_("pages"), null=True, blank=True) - isbn = models.CharField(_("ISBN"), blank=True, null=True, max_length=20, unique=True, db_index=True) + isbn = models.CharField(_("ISBN"), blank=True, null=True, max_length=20, db_index=True) # to store previously scrapped data cover = models.ImageField(_("cover picture"), upload_to=book_cover_path, default=DEFAULT_BOOK_IMAGE, blank=True) contents = models.TextField(blank=True, default="") @@ -66,6 +67,9 @@ class Book(Resource): def __str__(self): return self.title + + def get_absolute_url(self): + return reverse("books:retrieve", args=[self.id]) def get_tags_manager(self): return self.book_tags @@ -76,8 +80,6 @@ class Book(Resource): class BookMark(Mark): - # maybe this is the better solution, for it has less complex index - # book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='book_marks', null=True, unique=True) book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='book_marks', null=True) class Meta: constraints = [ diff --git a/books/templates/books/create_update.html b/books/templates/books/create_update.html index 3f9406c3..a6c9427c 100644 --- a/books/templates/books/create_update.html +++ b/books/templates/books/create_update.html @@ -50,9 +50,30 @@ diff --git a/books/templates/books/scrape.html b/books/templates/books/scrape.html index cd00eb2b..8626855f 100644 --- a/books/templates/books/scrape.html +++ b/books/templates/books/scrape.html @@ -99,7 +99,7 @@ ISBN: 9787020104345 diff --git a/movies/templates/movies/scrape.html b/movies/templates/movies/scrape.html index 85212f15..c60a41aa 100644 --- a/movies/templates/movies/scrape.html +++ b/movies/templates/movies/scrape.html @@ -97,7 +97,7 @@