on success, add a cc license, watermark (and generate mobi), and save

pull/1/head
eric 2013-11-14 23:16:55 -05:00
parent c6edd83884
commit efea7d0198
5 changed files with 70 additions and 2 deletions

View File

@ -17,3 +17,11 @@ class Boox(models.Model):
def expired(self):
return self.created+timedelta(days=self.expirydays) < datetime.now()
def download_link(self, format):
if format == 'epub':
return self.download_link_epub
elif format == 'mobi':
return self.download_link_mobi
else:
return None

View File

@ -14,4 +14,15 @@ def personalize(epub_file, acq):
output.addmetadata('rights','%s after %s'%(acq.work.last_campaign().license_url,acq.work.last_campaign().cc_date))
output.close()
#output.writetodisk('testfile2.epub')
return output
def ungluify(epub_file, campaign):
output = EPUB(epub_file, "a")
context={'campaign':campaign}
part = StringIO(unicode(render_to_string('epub/cc_license.xhtml', context)))
output.addpart(part, "cc_license.xhtml", "application/xhtml+xml", 1) #after title, we hope
output.addmetadata('rights', campaign.license_url)
output.close()
#output.writetodisk('testfile3.epub')
return output

View File

@ -7,6 +7,7 @@ import hashlib
import re
import random
import urllib
import urllib2
from ckeditor.fields import RichTextField
from datetime import timedelta, datetime
@ -21,6 +22,7 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.core.files.base import ContentFile
from django.db import models
from django.db.models import F, Q, get_model
from django.db.models.signals import post_save
@ -31,7 +33,7 @@ regluit imports
'''
import regluit
import regluit.core.isbn
from regluit.core.epub import personalize
from regluit.core.epub import personalize, ungluify
from regluit.core.signals import (
successful_campaign,
@ -862,6 +864,36 @@ class Campaign(models.Model):
@classmethod
def latest_ending(cls):
return (timedelta(days=int(settings.UNGLUEIT_LONGEST_DEADLINE)) + now())
def make_unglued_ebf(self, format, watermarked):
ebf=EbookFile.objects.create(edition=self.work.preferred_edition, format=format)
r=urllib2.urlopen(watermarked.download_link(format))
ebf.file.save(path_for_file(ebf,None),ContentFile(r.read()))
ebf.file.close()
ebf.save()
def watermark_success(self):
if self.status == 'SUCCESSFUL' and self.type == BUY2UNGLUE:
params={
'customeremailaddress': self.license,
'customername': 'The Public',
'languagecode':'1033',
'expirydays': 1,
'downloadlimit': 7,
'exlibris':0,
'chapterfooter':0,
'disclaimer':0,
'referenceid': '%s:%s:%s' % (self.work.id, self.id, self.license),
'kf8mobi': True,
'epub': True,
}
ungluified = ungluify(self.work.ebookfiles()[0].file, self)
ungluified.filename.seek(0)
watermarked = watermarker.platform(epubfile= ungluified.filename, **params)
self.make_unglued_ebf('epub', watermarked)
self.make_unglued_ebf('mobi', watermarked)
return True
return False
class Identifier(models.Model):
# olib, ltwk, goog, gdrd, thng, isbn, oclc, olwk, olib, gute, glue

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>License Information</title>
</head>
<body>
<div class="booksection">
<p class="copyk">
<p>&#x00A0;</p>
<p class="copyk">After {{ campaign.cc_date }}, this book is be released under a {{ campaign.license }} license at which time the any previous restricted license terms shall be replaced and superseded by the terms of the applicable Creative Commons license <a href="{{ campaign.license_url }}">{{ acq.work.last_campaign.license_url }}</a></p>
<p class="copy"></p>
</div>
</body>
</html>

View File

@ -2679,7 +2679,7 @@ def download_campaign(request, work_id, format):
if campaign is None or (campaign.status != 'SUCCESSFUL'):
raise Http404
if campaign.type is BUY2UNGLUE:
ebfs= models.EbookFile.objects.filter(edition__work=campaign.work, format=format).order_by('-created')
ebfs= models.EbookFile.objects.filter(edition__work=campaign.work, format=format).exclude(file='').order_by('-created')
logger.info(ebfs.count())
for ebf in ebfs:
logger.info(ebf.file.url)