set user on ebooks loaded from webpage

pull/43/head
eric 2017-08-08 12:38:54 -04:00
parent 0ebbb21d47
commit 8de43cfda8
2 changed files with 8 additions and 7 deletions

View File

@ -897,7 +897,7 @@ class BasePandataLoader(object):
edition.save() edition.save()
return edition return edition
def load_ebooks(self, metadata, edition, test_mode=False): def load_ebooks(self, metadata, edition, test_mode=False, user=None):
default_edition = edition default_edition = edition
for key in ['epub', 'pdf', 'mobi']: for key in ['epub', 'pdf', 'mobi']:
url = metadata.metadata.get('download_url_{}'.format(key), None) url = metadata.metadata.get('download_url_{}'.format(key), None)
@ -917,7 +917,7 @@ class BasePandataLoader(object):
) )
ebf.file.save(contentfile_name, contentfile) ebf.file.save(contentfile_name, contentfile)
ebf.file.close() ebf.file.close()
(ebook, created) = models.Ebook.objects.get_or_create( ebook = models.Ebook.objects.create(
url=ebf.file.url, url=ebf.file.url,
provider='Unglue.it', provider='Unglue.it',
rights=license, rights=license,
@ -925,6 +925,7 @@ class BasePandataLoader(object):
edition=edition, edition=edition,
filesize=contentfile.size, filesize=contentfile.size,
active=False, active=False,
user=user,
) )
ebf.ebook = ebook ebf.ebook = ebook
ebf.save() ebf.save()
@ -1018,7 +1019,7 @@ def ebooks_in_github_release(repo_owner, repo_name, tag, token=None):
for asset in release.iter_assets() for asset in release.iter_assets()
if EBOOK_FORMATS.get(asset.content_type) is not None] if EBOOK_FORMATS.get(asset.content_type) is not None]
def add_by_webpage(url, work=None): def add_by_webpage(url, work=None, user=None):
edition = None edition = None
scraper = BaseScraper(url) scraper = BaseScraper(url)
loader = BasePandataLoader(url) loader = BasePandataLoader(url)
@ -1027,7 +1028,7 @@ def add_by_webpage(url, work=None):
for metadata in pandata.get_edition_list(): for metadata in pandata.get_edition_list():
edition = loader.load_from_pandata(metadata, work) edition = loader.load_from_pandata(metadata, work)
work = edition.work work = edition.work
loader.load_ebooks(pandata, edition) loader.load_ebooks(pandata, edition, user=user)
return edition if edition else None return edition if edition else None

View File

@ -68,7 +68,7 @@ def get_edition(edition_id):
except models.Edition.DoesNotExist: except models.Edition.DoesNotExist:
raise Http404 (duplicate-code) raise Http404 (duplicate-code)
def get_edition_for_id(id_type, id_value): def get_edition_for_id(id_type, id_value, user=None):
''' the identifier is assumed to not be in database ''' ''' the identifier is assumed to not be in database '''
identifiers = {id_type: id_value} identifiers = {id_type: id_value}
if id_type == 'http': if id_type == 'http':
@ -107,7 +107,7 @@ def get_edition_for_id(id_type, id_value):
pass pass
if identifiers.has_key('http'): if identifiers.has_key('http'):
edition = add_by_webpage(identifiers['http']) edition = add_by_webpage(identifiers['http'], user=user)
return edition return edition
@ -149,7 +149,7 @@ def new_edition(request, by=None):
work = ident.work work = ident.work
edition = ident.edition if ident.edition else work.preferred_edition edition = ident.edition if ident.edition else work.preferred_edition
else: else:
edition = get_edition_for_id(id_type, id_value) edition = get_edition_for_id(id_type, id_value, user=request.user)
return HttpResponseRedirect( return HttpResponseRedirect(
reverse('new_edition', kwargs={ reverse('new_edition', kwargs={