def publisher_register(request): #模板中发生提交事件,在下面代码中处理 if request.method == 'POST': #一旦发生post事件,在这里处理 username = request.POST['username'] password = request.POST['password'] nickname = request.POST['nickname'] sex = request.POST['sex'] signature = request.POST['signature'] url = request.FILES['headerimg'] #处理前端数据 #建模 pub = Publisher(username=username, password=password, nickname=nickname, sex=sex, signature=signature, url=url) pub.save() return HttpResponse( "<script type = 'text/javascript'>alert('注册成功');window.history.go(-2);</script>" ) return render(request, "register.html") pass
def new_item(request, *args, **kwargs): errors = [] type = kwargs.pop('type') if type == 'author': form = AuthorForm(request.POST) if form.is_valid(): cd = form.cleaned_data author = Author(first_name=cd.get('first_name'), last_name=cd['last_name'], email=cd['email']) author.save() elif type == 'publisher': form = PubForm(request.POST) if form.is_valid(): cd = form.cleaned_data print cd pub = Publisher(name=cd['name'], address=cd['address'], city=cd['city'], state_province=cd['state_province'], country=cd['country'], website=cd['website']) pub.save() elif type == 'book': form = BookForm(request.POST) if form.is_valid(): cd = form.cleaned_data print cd names = cd['authors'].split(',') pub_name = cd['publisher'] pub = '' try: pub = Publisher.objects.get(name=pub_name.strip()) except Exception: print 'Publisher[%s] not exist.' % pub_name book = Book(title=cd['title'], publisher=pub, publication_date=cd['pub_date']) book.save() for name in names: try: author = Author.objects.get(first_name=name.strip()) print author book.authors.add(author) except Exception as e: print e print 'Author[%s] not exist.' % name # errors.append('%s not exist.' %name) # book = Book(title=cd['title'], ) return form
def new_item(request, *args, **kwargs): errors=[] type = kwargs.pop('type') if type == 'author': form = AuthorForm(request.POST) if form.is_valid(): cd = form.cleaned_data author = Author(first_name=cd.get('first_name'), last_name=cd['last_name'], email=cd['email']) author.save() elif type == 'publisher': form = PubForm(request.POST) if form.is_valid(): cd = form.cleaned_data print cd pub = Publisher(name=cd['name'], address=cd['address'], city=cd['city'], state_province=cd['state_province'], country=cd['country'], website=cd['website']) pub.save() elif type == 'book': form = BookForm(request.POST) if form.is_valid(): cd = form.cleaned_data print cd names = cd['authors'].split(',') pub_name = cd['publisher'] pub = '' try: pub = Publisher.objects.get(name=pub_name.strip()) except Exception: print 'Publisher[%s] not exist.' %pub_name book = Book(title=cd['title'], publisher=pub, publication_date=cd['pub_date']) book.save() for name in names: try: author = Author.objects.get(first_name=name.strip()) print author book.authors.add(author) except Exception as e: print e print 'Author[%s] not exist.' %name # errors.append('%s not exist.' %name) # book = Book(title=cd['title'], ) return form
def hello(request): # return HttpResponse(datetime.datetime.strptime(('%Y-%m-%d %H:%M:%S',datetime.datetime.time()))) now = datetime.datetime.now() # html = "<html><body>It is now %s.</body></html>" % now t = get_template("mytemplete.html") html = t.render(Context({"person_name": now})) p = Publisher(name='Apress', address='2855 Telegraph Ave.', city='Berkeley', state_province='CA', country='U.S.A.', website='http://www.apress.com/') p.save() item_list = Publisher.objects.all() html = t.render(Context({"item_list": item_list})) return HttpResponse(html)
def hello(request): # return HttpResponse(datetime.datetime.strptime(('%Y-%m-%d %H:%M:%S',datetime.datetime.time()))) now = datetime.datetime.now() # html = "<html><body>It is now %s.</body></html>" % now t=get_template("mytemplete.html") html=t.render(Context({"person_name":now})) p=Publisher(name='Apress', address='2855 Telegraph Ave.', city='Berkeley', state_province='CA', country='U.S.A.', website='http://www.apress.com/') p.save() item_list=Publisher.objects.all() html=t.render(Context({"item_list":item_list})) return HttpResponse(html)
def publisher_register(request): # 模板中发生提交事件以后要在下面的代码中处理 if request.method == 'POST': # 一旦发生POST事件,则在这里处理 # 接收前端提出的数据 username = request.POST['username'] password = request.POST['password'] nickname = request.POST['nickname'] sex = request.POST['sex'] signature = request.POST['signature'] # 处理前端提交的数据 # 建模 pub = Publisher(username=username, password=password, nickname=nickname, sex=sex, signature=signature) # 存入数据库 pub.save() return HttpResponse("<script type='text/javascript>" "alert('注册成功');" "window.history.back();" "</script>") # 安装模板 return render(request, "publisher/register.html")
def acquisition(request): record_data = {} record = None if request.GET.get('isbn'): isbn = request.GET.get('isbn') if isbnpy.isValid(isbn): url = 'http://openlibrary.org/api/volumes/brief/json/isbn:' + isbn response = urllib2.urlopen(url) # response = urllib2.urlopen('http://127.0.0.1/json/3.json') data = json.load(response) if isbnpy.isI10(isbn): isbn = isbnpy.convert(isbn) if data == {}: record_data['isbn13'] = isbn record_form = RecordForm(instance=record) return render(request, 'acquisition.html', { 'data': record_data, 'form': record_form }) data = data.itervalues().next()['records'].itervalues().next() try: record = Record.objects.get(isbn13=isbn) new_record = False except Record.DoesNotExist: record = Record(isbn13=isbn) new_record = True # pp(data) if record.book_id: book = record.book else: book = Book() book.title = data['data']['title'] if data['details']['details'].has_key('subtitle'): book.subtitle = data['details']['details']['subtitle'] book.save() if data['details']['details'].has_key('pagination'): record.pagination = data['data']['pagination'] elif data['details']['details'].has_key('number_of_pages'): record.pagination = str( data['data']['number_of_pages']) + ' p.' if data['details']['details'].has_key('physical_format'): record.format = data['details']['details']['physical_format'] if record.format.startswith('electronic'): record.format = 'eBook' # record.openlibrary_url = data['data']['url'] if data['details']['details'].has_key('weight'): record.weight = data['details']['details'].get('weight') if data['details']['details'].has_key('physical_dimensions'): record.dimensions = data['details']['details'].get( 'physical_dimensions') if data['data'].has_key('classifications'): if data['data']['classifications'].has_key( 'dewey_decimal_class'): record.ddc = data['data']['classifications'].get( 'dewey_decimal_class')[0] if data['data']['classifications'].has_key( 'lc_classifications'): record.lcc = data['data']['classifications'].get( 'lc_classifications')[0] try: record.date_of_publication = datetime.strptime( data['data']['publish_date'], '%B %d, %Y').date() record.publication_has_month = True record.publication_has_day = True except ValueError: try: record.date_of_publication = datetime.strptime( data['data']['publish_date'], '%Y').date() record.publication_has_month = False record.publication_has_day = False except ValueError: try: record.date_of_publication = datetime.strptime( data['data']['publish_date'], '%B %Y').date() record.publication_has_day = False record.publication_has_month = True except ValueError: record.date_of_publication = datetime.strptime( data['data']['publish_date'], '%m/%d/%Y').date() record.publication_has_day = True record.publication_has_month = True if data['data'].has_key('identifiers'): if data['data']['identifiers'].has_key('openlibrary'): record.openlibrary_id = data['data']['identifiers'][ 'openlibrary'][0] if data['data']['identifiers'].has_key('goodreads'): record.goodreads_id = data['data']['identifiers'][ 'goodreads'][0] if data['data']['identifiers'].has_key('librarything'): record.librarything_id = data['data']['identifiers'][ 'librarything'][0] if data['data']['identifiers'].has_key('oclc'): record.oclc_id = data['data']['identifiers']['oclc'][0] if data['data']['identifiers'].has_key('lccn'): record.lccn_id = data['data']['identifiers']['lccn'][0] if data['data'].has_key('by_statement'): record.by_statement = data['data'].get('by_statement') if data['data'].has_key('notes'): record.notes = data['data'].get('notes') if data['data'].has_key('excerpts'): record.excerpt = data['data'].get('excerpts')[0].get('text') record.book = book setting = LibrarySetting.get() record.type = setting.default_type if new_record: record.date_added = datetime.today() record.save() if data['details']['details'].has_key('languages'): record.languages.clear() for lang in data['details']['details']['languages']: lang_key = lang['key'].replace('/languages/', '') try: book_lang = Language.objects.get(code=lang_key) except Language.DoesNotExist: try: book_lang = Language.objects.get( code=lang_key[:-1]) except Language.DoesNotExist: raise Exception( "Please add a language with code " + lang_key + " or " + lang_key[:-1] + " first!") record.languages.add(book_lang) if data['data'].has_key('publish_places'): record.published_places.clear() for place in data['data']['publish_places']: try: published_place = Place.objects.get(name=place['name']) except Place.DoesNotExist: published_place = Place(name=place['name']) published_place.save() record.published_places.add(published_place) record.authors.clear() if data['details']['details'].has_key('authors'): for author in data['details']['details']['authors']: author_key = author['key'].replace('/authors/', '') try: book_author = Author.objects.get(identifier=author_key) except Author.DoesNotExist: book_author = Author(identifier=author_key) book_author.name = author['name'] book_author.save() record.authors.add(book_author) elif data['data'].has_key('authors'): for author in data['data']['authors']: author_key = author['url'].replace( 'http://openlibrary.org/authors/', '') try: book_author = Author.objects.get(identifier=author_key) except Author.DoesNotExist: book_author = Author(identifier=author_key) book_author.name = author['name'] book_author.save() record.authors.add(book_author) if data['data'].has_key('ebooks'): if data['data']['ebooks'][0].has_key('formats'): formats = data['data']['ebooks'][0]['formats'] for book_format in formats: ebooks = record.ebooks(book_format) for ebook in ebooks: ebook.delete() if formats[book_format].has_key('url'): url = formats[book_format].get('url') result = urllib.urlretrieve(url) book_file = BookFile(record=record) book_file.file.save(os.path.basename(url), File(open(result[0]))) book_file.save() subjects = None if data['details']['details'].has_key('subjects'): subjects = data['details']['details']['subjects'] elif data['data'].has_key('subjects'): subjects = data['data']['subjects'] if subjects: record.book.subjects.clear() for subject in subjects: if type(subject) == dict: subject = title_case(subject['name']) try: book_subject = Subject.objects.get(name=subject) except Subject.DoesNotExist: book_subject = Subject(name=subject) book_subject.save() record.book.subjects.add(book_subject) # record.publishers.clear() # for publisher in data['details']['details']['publishers']: # try: # book_publisher = Publisher.objects.get(name=publisher['name']) # except Publisher.DoesNotExist: # book_publisher = Publisher(name=publisher['name']) # book_publisher.save() # record.publishers.add(book_publisher) try: book_publisher = Publisher.objects.get( name=data['details']['details']['publishers'][0]) except Publisher.DoesNotExist: book_publisher = Publisher( name=data['details']['details']['publishers'][0]) book_publisher.save() record.publisher = book_publisher if data['data'].has_key('cover'): if data['data']['cover'].has_key('large'): cover_url = data['data']['cover']['large'] result = urllib.urlretrieve(cover_url) record.large_cover.save(os.path.basename(cover_url), File(open(result[0]))) if data['data']['cover'].has_key('medium'): cover_url = data['data']['cover']['medium'] result = urllib.urlretrieve(cover_url) record.medium_cover.save(os.path.basename(cover_url), File(open(result[0]))) if data['data']['cover'].has_key('small'): cover_url = data['data']['cover']['small'] result = urllib.urlretrieve(cover_url) record.small_cover.save(os.path.basename(cover_url), File(open(result[0]))) # thumbnail_url = data['details']['thumbnail_url'] # result = urllib.urlretrieve(thumbnail_url) # record.thumbnail.save( # os.path.basename(thumbnail_url), # File(open(result[0])) # ) # import pdb # # pdb.set_trace() record_data = RecordSerializer(record).data record_form = RecordForm(instance=record) return render(request, 'acquisition.html', { 'data': record_data, 'form': record_form })
def save_acquisition(request): if request.POST.get('book').isnumeric(): book = Book.objects.get(id=request.POST.get('book')) new_book = False else: book = Book(title=request.POST.get('book')) new_book = True book.subtitle = request.POST.get('subtitle') book.save() if request.POST.get('isbn'): isbn = request.POST.get('isbn') if isbnpy.isValid(isbn): if isbnpy.isI10(isbn): isbn = isbnpy.convert(isbn) try: record = Record.objects.get(isbn13=isbn) new_record = False except Record.DoesNotExist: record = Record(isbn13=isbn) new_record = True else: if not new_book: try: record = Record.objects.get(book=book, edition=request.POST.get('book')) new_record = False except Record.DoesNotExist: record = Record(book=book) new_record = True else: record = Record(book=book) new_record = True record.book = book record.format = request.POST.get('format') if record.format != 'ebook': if new_record: record.quantity = request.POST.get('quantity') else: record.quantity += int(request.POST.get('quantity')) record.excerpt = request.POST.get('excerpt') record.edition = request.POST.get('edition') record.notes = request.POST.get('notes') record.ddc = request.POST.get('ddc') record.lcc = request.POST.get('lcc') record.pagination = request.POST.get('pagination') record.format = request.POST.get('format') record.type = request.POST.get('type') if record.format != 'eBook': record.quantity = request.POST.get('quantity') record.publication_has_month = False record.publication_has_day = False if request.POST.get('year'): dt = datetime(int(request.POST.get('year')), 1, 1) if request.POST.get('month'): record.publication_has_month = True dt = dt.replace(month=int(request.POST.get('month'))) if request.POST.get('day'): record.publication_has_day = True dt = dt.replace(day=int(request.POST.get('day'))) record.date_of_publication = dt else: record.date_of_publication = None if request.FILES.get('small_cover'): record.small_cover = request.FILES.get('small_cover') if request.FILES.get('medium_cover'): record.medium_cover = request.FILES.get('medium_cover') if request.FILES.get('large_cover'): record.large_cover = request.FILES.get('large_cover') if not record.date_added: record.date_added = datetime.today() record.save() if request.FILES.get('ebook'): ebooks = request.FILES.getlist('ebook') for ebook in ebooks: ebook_file = BookFile(record=record, file=ebook) existing_files = record.ebooks(ebook_file.format) for existing_file in existing_files: existing_file.delete() ebook_file.save() book.subjects.clear() for subject in request.POST.getlist('subjects'): if subject.isnumeric(): book.subjects.add(Subject.objects.get(id=subject)) else: new_subject = Subject(name=subject) new_subject.save() book.subjects.add(new_subject) record.authors.clear() for author in request.POST.getlist('authors'): if author.isnumeric(): record.authors.add(Author.objects.get(id=author)) else: new_author = Author(name=author) new_author.save() record.authors.add(new_author) record.languages.clear() for language in request.POST.getlist('languages'): record.languages.add(Language.objects.get(id=language)) publisher = request.POST.get('publisher') if publisher: if publisher.isnumeric(): record.publisher_id = publisher else: new_publisher = Publisher(name=publisher) new_publisher.save() record.publisher = new_publisher record.save() return redirect(reverse_lazy('view_record', kwargs={'pk': record.id}))
def acquisition(request): record_data = {} record = None if request.GET.get('isbn'): isbn = request.GET.get('isbn') if isbnpy.isValid(isbn): url = 'http://openlibrary.org/api/volumes/brief/json/isbn:' + isbn response = urllib2.urlopen(url) # response = urllib2.urlopen('http://127.0.0.1/json/3.json') data = json.load(response) if isbnpy.isI10(isbn): isbn = isbnpy.convert(isbn) if data == {}: record_data['isbn13'] = isbn record_form = RecordForm(instance=record) return render(request, 'acquisition.html', {'data': record_data, 'form': record_form}) data = data.itervalues().next()['records'].itervalues().next() try: record = Record.objects.get(isbn13=isbn) new_record = False except Record.DoesNotExist: record = Record(isbn13=isbn) new_record = True # pp(data) if record.book_id: book = record.book else: book = Book() book.title = data['data']['title'] if data['details']['details'].has_key('subtitle'): book.subtitle = data['details']['details']['subtitle'] book.save() if data['details']['details'].has_key('pagination'): record.pagination = data['data']['pagination'] elif data['details']['details'].has_key('number_of_pages'): record.pagination = str(data['data']['number_of_pages']) + ' p.' if data['details']['details'].has_key('physical_format'): record.format = data['details']['details']['physical_format'] if record.format.startswith('electronic'): record.format = 'eBook' # record.openlibrary_url = data['data']['url'] if data['details']['details'].has_key('weight'): record.weight = data['details']['details'].get('weight') if data['details']['details'].has_key('physical_dimensions'): record.dimensions = data['details']['details'].get('physical_dimensions') if data['data'].has_key('classifications'): if data['data']['classifications'].has_key('dewey_decimal_class'): record.ddc = data['data']['classifications'].get('dewey_decimal_class')[0] if data['data']['classifications'].has_key('lc_classifications'): record.lcc = data['data']['classifications'].get('lc_classifications')[0] try: record.date_of_publication = datetime.strptime(data['data']['publish_date'], '%B %d, %Y').date() record.publication_has_month = True record.publication_has_day = True except ValueError: try: record.date_of_publication = datetime.strptime(data['data']['publish_date'], '%Y').date() record.publication_has_month = False record.publication_has_day = False except ValueError: try: record.date_of_publication = datetime.strptime(data['data']['publish_date'], '%B %Y').date() record.publication_has_day = False record.publication_has_month = True except ValueError: record.date_of_publication = datetime.strptime(data['data']['publish_date'], '%m/%d/%Y').date() record.publication_has_day = True record.publication_has_month = True if data['data'].has_key('identifiers'): if data['data']['identifiers'].has_key('openlibrary'): record.openlibrary_id = data['data']['identifiers']['openlibrary'][0] if data['data']['identifiers'].has_key('goodreads'): record.goodreads_id = data['data']['identifiers']['goodreads'][0] if data['data']['identifiers'].has_key('librarything'): record.librarything_id = data['data']['identifiers']['librarything'][0] if data['data']['identifiers'].has_key('oclc'): record.oclc_id = data['data']['identifiers']['oclc'][0] if data['data']['identifiers'].has_key('lccn'): record.lccn_id = data['data']['identifiers']['lccn'][0] if data['data'].has_key('by_statement'): record.by_statement = data['data'].get('by_statement') if data['data'].has_key('notes'): record.notes = data['data'].get('notes') if data['data'].has_key('excerpts'): record.excerpt = data['data'].get('excerpts')[0].get('text') record.book = book setting = LibrarySetting.get() record.type = setting.default_type if new_record: record.date_added = datetime.today() record.save() if data['details']['details'].has_key('languages'): record.languages.clear() for lang in data['details']['details']['languages']: lang_key = lang['key'].replace('/languages/', '') try: book_lang = Language.objects.get(code=lang_key) except Language.DoesNotExist: try: book_lang = Language.objects.get(code=lang_key[:-1]) except Language.DoesNotExist: raise Exception( "Please add a language with code " + lang_key + " or " + lang_key[:-1] + " first!") record.languages.add(book_lang) if data['data'].has_key('publish_places'): record.published_places.clear() for place in data['data']['publish_places']: try: published_place = Place.objects.get(name=place['name']) except Place.DoesNotExist: published_place = Place(name=place['name']) published_place.save() record.published_places.add(published_place) record.authors.clear() if data['details']['details'].has_key('authors'): for author in data['details']['details']['authors']: author_key = author['key'].replace('/authors/', '') try: book_author = Author.objects.get(identifier=author_key) except Author.DoesNotExist: book_author = Author(identifier=author_key) book_author.name = author['name'] book_author.save() record.authors.add(book_author) elif data['data'].has_key('authors'): for author in data['data']['authors']: author_key = author['url'].replace('http://openlibrary.org/authors/', '') try: book_author = Author.objects.get(identifier=author_key) except Author.DoesNotExist: book_author = Author(identifier=author_key) book_author.name = author['name'] book_author.save() record.authors.add(book_author) if data['data'].has_key('ebooks'): if data['data']['ebooks'][0].has_key('formats'): formats = data['data']['ebooks'][0]['formats'] for book_format in formats: ebooks = record.ebooks(book_format) for ebook in ebooks: ebook.delete() if formats[book_format].has_key('url'): url = formats[book_format].get('url') result = urllib.urlretrieve(url) book_file = BookFile(record=record) book_file.file.save( os.path.basename(url), File(open(result[0])) ) book_file.save() subjects = None if data['details']['details'].has_key('subjects'): subjects = data['details']['details']['subjects'] elif data['data'].has_key('subjects'): subjects = data['data']['subjects'] if subjects: record.book.subjects.clear() for subject in subjects: if type(subject) == dict: subject = title_case(subject['name']) try: book_subject = Subject.objects.get(name=subject) except Subject.DoesNotExist: book_subject = Subject(name=subject) book_subject.save() record.book.subjects.add(book_subject) # record.publishers.clear() # for publisher in data['details']['details']['publishers']: # try: # book_publisher = Publisher.objects.get(name=publisher['name']) # except Publisher.DoesNotExist: # book_publisher = Publisher(name=publisher['name']) # book_publisher.save() # record.publishers.add(book_publisher) try: book_publisher = Publisher.objects.get(name=data['details']['details']['publishers'][0]) except Publisher.DoesNotExist: book_publisher = Publisher(name=data['details']['details']['publishers'][0]) book_publisher.save() record.publisher = book_publisher if data['data'].has_key('cover'): if data['data']['cover'].has_key('large'): cover_url = data['data']['cover']['large'] result = urllib.urlretrieve(cover_url) record.large_cover.save( os.path.basename(cover_url), File(open(result[0])) ) if data['data']['cover'].has_key('medium'): cover_url = data['data']['cover']['medium'] result = urllib.urlretrieve(cover_url) record.medium_cover.save( os.path.basename(cover_url), File(open(result[0])) ) if data['data']['cover'].has_key('small'): cover_url = data['data']['cover']['small'] result = urllib.urlretrieve(cover_url) record.small_cover.save( os.path.basename(cover_url), File(open(result[0])) ) # thumbnail_url = data['details']['thumbnail_url'] # result = urllib.urlretrieve(thumbnail_url) # record.thumbnail.save( # os.path.basename(thumbnail_url), # File(open(result[0])) # ) # import pdb # # pdb.set_trace() record_data = RecordSerializer(record).data record_form = RecordForm(instance=record) return render(request, 'acquisition.html', {'data': record_data, 'form': record_form})
def save_acquisition(request): if request.POST.get('book').isnumeric(): book = Book.objects.get(id=request.POST.get('book')) new_book = False else: book = Book(title=request.POST.get('book')) new_book = True book.subtitle = request.POST.get('subtitle') book.save() if request.POST.get('isbn'): isbn = request.POST.get('isbn') if isbnpy.isValid(isbn): if isbnpy.isI10(isbn): isbn = isbnpy.convert(isbn) try: record = Record.objects.get(isbn13=isbn) new_record = False except Record.DoesNotExist: record = Record(isbn13=isbn) new_record = True else: if not new_book: try: record = Record.objects.get(book=book, edition=request.POST.get('book')) new_record = False except Record.DoesNotExist: record = Record(book=book) new_record = True else: record = Record(book=book) new_record = True record.book = book record.format = request.POST.get('format') if record.format != 'ebook': if new_record: record.quantity = request.POST.get('quantity') else: record.quantity += int(request.POST.get('quantity')) record.excerpt = request.POST.get('excerpt') record.edition = request.POST.get('edition') record.notes = request.POST.get('notes') record.ddc = request.POST.get('ddc') record.lcc = request.POST.get('lcc') record.pagination = request.POST.get('pagination') record.format = request.POST.get('format') record.type = request.POST.get('type') if record.format != 'eBook': record.quantity = request.POST.get('quantity') record.publication_has_month = False record.publication_has_day = False if request.POST.get('year'): dt = datetime(int(request.POST.get('year')), 1, 1) if request.POST.get('month'): record.publication_has_month = True dt = dt.replace(month=int(request.POST.get('month'))) if request.POST.get('day'): record.publication_has_day = True dt = dt.replace(day=int(request.POST.get('day'))) record.date_of_publication = dt else: record.date_of_publication = None if request.FILES.get('small_cover'): record.small_cover = request.FILES.get('small_cover') if request.FILES.get('medium_cover'): record.medium_cover = request.FILES.get('medium_cover') if request.FILES.get('large_cover'): record.large_cover = request.FILES.get('large_cover') if not record.date_added: record.date_added = datetime.today() record.save() if request.FILES.get('ebook'): ebooks = request.FILES.getlist('ebook') for ebook in ebooks: ebook_file = BookFile(record=record, file=ebook) existing_files = record.ebooks(ebook_file.format) for existing_file in existing_files: existing_file.delete() ebook_file.save() book.subjects.clear() for subject in request.POST.getlist('subjects'): if subject.isnumeric(): book.subjects.add(Subject.objects.get(id=subject)) else: new_subject = Subject(name=subject) new_subject.save() book.subjects.add(new_subject) record.authors.clear() for author in request.POST.getlist('authors'): if author.isnumeric(): record.authors.add(Author.objects.get(id=author)) else: new_author = Author(name=author) new_author.save() record.authors.add(new_author) record.languages.clear() for language in request.POST.getlist('languages'): record.languages.add(Language.objects.get(id=language)) publisher = request.POST.get('publisher') if publisher: if publisher.isnumeric(): record.publisher_id = publisher else: new_publisher = Publisher(name=publisher) new_publisher.save() record.publisher = new_publisher record.save() return redirect(reverse_lazy('view_record', kwargs={'pk': record.id}))