本文整理汇总了Python中books.models.Book.validate_unique方法的典型用法代码示例。如果您正苦于以下问题:Python Book.validate_unique方法的具体用法?Python Book.validate_unique怎么用?Python Book.validate_unique使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类books.models.Book
的用法示例。
在下文中一共展示了Book.validate_unique方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _handle_csv
# 需要导入模块: from books.models import Book [as 别名]
# 或者: from books.models.Book import validate_unique [as 别名]
def _handle_csv(self, csvpath):
"""
Store books from a file in CSV format.
WARN: does not handle tags
"""
csvfile = open(csvpath)
# Sniffer fais to detect a CSV created with DictWriter with default Dialect (excel) !
# dialect = csv.Sniffer().sniff(csvfile.read(32000))
# csvfile.seek(0)
dialect = 'excel'
reader = csv.reader(csvfile) #, dialect)
# TODO: Figure out if this is a valid CSV file
status_published = Status.objects.get(status='Published')
for row in reader:
path = row[0]
title = row[1]
author = row[2]
summary = row[3]
if not os.path.exists(path):
# check if file is located in same directory as CSV
path = os.path.join(os.path.dirname(csvpath), path)
if os.path.exists(path):
f = open(path, 'rb')
a_author = Author.objects.get_or_create(a_author=author)[0]
book = Book(book_file=File(f), a_title=title, a_author=a_author,
a_summary=summary, a_status=status_published)
try:
book.validate_unique()
book.save()
except:
print("EXCEPTION SAVING FILE '%s': %s" % (
path, sys.exc_info()[0]))
else:
print("FILE NOT FOUND '%s'" % path)
示例2: handle
# 需要导入模块: from books.models import Book [as 别名]
# 或者: from books.models.Book import validate_unique [as 别名]
def handle(self, *args, **options):
dirpath = options.get('dirpath')
if not dirpath or not os.path.exists(dirpath):
raise CommandError("%r is not a valid path" % dirpath)
if os.path.isdir(dirpath):
names = get_epubs(dirpath)
for name in names:
info = None
try:
e = Epub(name)
info = e.get_info()
e.close()
except:
print("%s is not a valid epub file" % name)
continue
lang = Language.objects.filter(code=info.language)
if not lang:
for data in langs:
if data[0] == info.language:
lang = Language()
lang.label = data[1]
lang.save()
break
else:
lang = lang[0]
#XXX: Hacks below
if not info.title:
info.title = ''
if not info.summary:
info.summary = ''
if not info.creator:
info.creator = ''
if not info.rights:
info.rights = ''
if not info.date:
info.date = ''
if not info.identifier:
info.identifier = {}
if not info.identifier.get('value'):
info.identifier['value'] = ''
f = open(name, "rb")
sha = sha256_sum(open(name, "rb"))
pub_status = Status.objects.get(status='Published')
author = Author.objects.get_or_create(a_author=info.creator)[0]
book = Book(a_title = info.title,
a_author = author, a_summary = info.summary,
file_sha256sum=sha,
a_rights = info.rights, dc_identifier = info.identifier['value'].strip('urn:uuid:'),
dc_issued = info.date,
a_status = pub_status, mimetype="application/epub+zip")
try:
# Not sure why this errors, book_file.save exists
book.book_file.save(os.path.basename(name), File(f)) #pylint: disable=no-member
book.validate_unique()
book.save()
# FIXME: Find a better way to do this.
except IntegrityError as e:
if str(e) == "column file_sha256sum is not unique":
print("The book (", book.book_file, ") was not saved because the file already exsists in the database.")
else:
if options['ignore_error']:
print('Error adding file %s: %s' % (book.book_file, sys.exc_info()[1]))
continue
raise CommandError('Error adding file %s: %s' % (book.book_file, sys.exc_info()[1]))
except:
if options['ignore_error']:
print('Error adding file %s: %s' % (book.book_file, sys.exc_info()[1]))
continue
raise CommandError('Error adding file %s: %s' % (book.book_file, sys.exc_info()[1]))
示例3: _handle_json
# 需要导入模块: from books.models import Book [as 别名]
# 或者: from books.models.Book import validate_unique [as 别名]
def _handle_json(self, jsonpath):
"""
Store books from a file in JSON format.
"""
jsonfile = open(jsonpath)
data_list = json.loads(jsonfile.read())
status_published = Status.objects.get(status='Published')
stats = dict(total=0, errors=0, skipped=0, imported=0)
for d in data_list:
stats['total'] += 1
logger.debug('read item %s' % json.dumps(d))
# Skip unless there is book content
if 'book_path' not in d:
stats['skipped'] += 1
continue
# Skip unless there is book content
if not os.path.exists(d['book_path']):
stats['skipped'] += 1
continue
# Get a Django File from the given path:
f = open(d['book_path'], 'rb')
d['book_file'] = File(f)
del d['book_path']
if 'cover_path' in d:
f_cover = open(d['cover_path'], 'rb')
d['cover_img'] = File(f_cover)
del d['cover_path']
if 'a_status' in d:
d['a_status'] = Status.objects.get(status=d['a_status'])
else:
d['a_status'] = status_published
a_author = None
if 'a_author' in d:
d['a_author'] = Author.objects.get_or_create(a_author=d['a_author'])[0]
tags = d.get('tags', [])
if 'tags' in d:
del d['tags']
book = Book(**d)
try:
book.validate_unique() # Throws ValidationError if not unique
with transaction.atomic():
book.save() # must save item to generate Book.id before creating tags
[book.tags.add(tag) for tag in tags if tag]
book.save() # save again after tags are generated
stats['imported'] += 1
except ValidationError as e:
stats['skipped'] += 1
logger.info('Book already imported, skipping title="%s"' % book.a_title)
except Exception as e:
stats['errors'] += 1
# Likely a bug
logger.warn('Error adding book title="%s": %s' % (
book.a_title, e))
logger.info("addbooks complete total=%(total)d imported=%(imported)d skipped=%(skipped)d errors=%(errors)d" % stats)