ebook files activate and deactivate

when manager unselects 'add_ask', the system didn't revert to the files
without added ask. This fixes that. To test, toggle the add_ask
checkbox and save campaign.
pull/1/head
eric 2014-10-04 16:51:16 -04:00
parent 0ddbdbc944
commit ef6c4a659e
4 changed files with 48 additions and 19 deletions

View File

@ -948,7 +948,7 @@ class Campaign(models.Model):
done_formats.append('epub') done_formats.append('epub')
except Exception as e: except Exception as e:
logger.error("error making epub ask %s" % (e)) logger.error("error making epub ask %s" % (e))
self.work.make_ebooks_from_ebfs() self.work.make_ebooks_from_ebfs(add_ask=True)
@ -1239,25 +1239,37 @@ class Work(models.Model):
def pdffiles(self): def pdffiles(self):
return EbookFile.objects.filter(edition__work=self, format='pdf').exclude(file='').order_by('-created') return EbookFile.objects.filter(edition__work=self, format='pdf').exclude(file='').order_by('-created')
def make_ebooks_from_ebfs(self): def make_ebooks_from_ebfs(self, add_ask=True):
if self.last_campaign().type != THANKS: # just to make sure that ebf's can be unglued by mistake if self.last_campaign().type != THANKS: # just to make sure that ebf's can be unglued by mistake
return return
ebfs=EbookFile.objects.filter(edition__work=self).exclude(file='').order_by('-created') ebfs=EbookFile.objects.filter(edition__work=self).exclude(file='').order_by('-created')
done_formats= [] done_formats= []
for ebf in ebfs: for ebf in ebfs:
previous_ebooks=Ebook.objects.filter(url= ebf.file.url,)
try:
previous_ebook = previous_ebooks[0]
for eb in previous_ebooks[1:]: #housekeeping
eb.deactivate()
except IndexError:
previous_ebook = None
if ebf.format not in done_formats: if ebf.format not in done_formats:
ebook=Ebook.objects.get_or_create( if ebf.asking==add_ask or ebf.format=='mobi':
edition=ebf.edition, if previous_ebook:
format=ebf.format, previous_ebook.activate()
rights=self.last_campaign().license, else:
provider="Unglue.it", ebook=Ebook.objects.get_or_create(
url= ebf.file.url, edition=ebf.edition,
) format=ebf.format,
done_formats.append(ebf.format) rights=self.last_campaign().license,
else: provider="Unglue.it",
obsolete=Ebook.objects.filter(url= ebf.file.url,) url= ebf.file.url,
for eb in obsolete: )
eb.deactivate() done_formats.append(ebf.format)
elif previous_ebook:
previous_ebook.deactivate()
elif previous_ebook:
previous_ebook.deactivate()
return return
def remove_old_ebooks(self): def remove_old_ebooks(self):
@ -1667,6 +1679,13 @@ class EbookFile(models.Model):
return test_epub(self.file) return test_epub(self.file)
return None return None
@property
def active(self):
try:
return Ebook.objects.filter(url=self.file.url)[0].active
except:
return False
class Ebook(models.Model): class Ebook(models.Model):
FORMAT_CHOICES = settings.FORMATS FORMAT_CHOICES = settings.FORMATS
RIGHTS_CHOICES = cc.CHOICES RIGHTS_CHOICES = cc.CHOICES
@ -1727,6 +1746,10 @@ class Ebook(models.Model):
def deactivate(self): def deactivate(self):
self.active=False self.active=False
self.save() self.save()
def activate(self):
self.active=True
self.save()
class Wishlist(models.Model): class Wishlist(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)

View File

@ -133,7 +133,7 @@ def process_ebfs(campaign):
if campaign.use_add_ask: if campaign.use_add_ask:
campaign.add_ask_to_ebfs() campaign.add_ask_to_ebfs()
else: else:
campaign.work.make_ebooks_from_ebfs() campaign.work.make_ebooks_from_ebfs(add_ask=False)
campaign.work.remove_old_ebooks() campaign.work.remove_old_ebooks()
@task @task

View File

@ -24,7 +24,7 @@
<ul> <ul>
{% for ebook_file in edition.ebook_files.all %} {% for ebook_file in edition.ebook_files.all %}
{% if ebook_file.file %} {% if ebook_file.file %}
<li><a href="{{ebook_file.file.url}}">{{ebook_file.file}}</a> created {{ebook_file.created}} {% if ebook_file.asking %}(This file has had the campaign 'ask' added.){% endif %}</li> <li>{% if ebook_file.active %}<span class="yikes">ACTIVE</span> {% endif %}<a href="{{ebook_file.file.url}}">{{ebook_file.file}}</a> created {{ebook_file.created}} {% if ebook_file.asking %}(This file has had the campaign 'ask' added.){% endif %}</li>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</ul> </ul>

View File

@ -157,7 +157,9 @@ Please fix the following before launching your campaign:
<h3>Uploaded Files</h3> <h3>Uploaded Files</h3>
{% endif %} {% endif %}
{% if campaign.work.epubfiles.0 %} {% if campaign.work.epubfiles.0 %}
<p>Active EPUB file: <a href="{{campaign.work.epubfiles.0.file.url}}">{{campaign.work.epubfiles.0.file}}</a> <br />created {{campaign.work.epubfiles.0.created}} for edition <a href="#edition_{{campaign.work.epubfiles.0.edition.id}}">{{campaign.work.epubfiles.0.edition.id}}</a> {% if campaign.work.epubfiles.0.asking %}(This file has had the campaign 'ask' added.){% endif %}</p> {% for ebf in campaign.work.epubfiles %}
<p>{% if ebf.active %}<span class="yikes">ACTIVE</span> {% endif %}EPUB file: <a href="{{ebf.file.url}}">{{ebf.file}}</a> <br />created {{ebf.created}} for edition <a href="#edition_{{ebf.edition.id}}">{{ebf.edition.id}}</a> {% if ebf.asking %}(This file has had the campaign 'ask' added.){% endif %}</p>
{% endfor %}
{% if campaign.work.test_acqs.0 %} {% if campaign.work.test_acqs.0 %}
<ul> <ul>
<li><a href="{{campaign.work.test_acqs.0.watermarked.download_link_epub}}">Processed epub for testing</a></li> <li><a href="{{campaign.work.test_acqs.0.watermarked.download_link_epub}}">Processed epub for testing</a></li>
@ -166,10 +168,14 @@ Please fix the following before launching your campaign:
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if campaign.work.mobifiles.0 %} {% if campaign.work.mobifiles.0 %}
<p>Active MOBI file: <a href="{{campaign.work.mobifiles.0.file.url}}">{{campaign.work.mobifiles.0.file}}</a> <br />created {{campaign.work.mobifiles.0.created}} for edition <a href="#edition_{{campaign.work.mobifiles.0.edition.id}}">{{campaign.work.mobifiles.0.edition.id}}</a> {% if campaign.work.mobifiles.0.asking %}(This file has had the campaign 'ask' added.){% endif %}</p> {% for ebf in campaign.work.mobifiles %}
<p>{% if ebf.active %}<span class="yikes">ACTIVE</span> {% endif %}MOBI file: <a href="{{ebf.file.url}}">{{ebf.file}}</a> <br />created {{ebf.created}} for edition <a href="#edition_{{ebf.edition.id}}">{{ebf.edition.id}}</a> {% if ebf.asking %}(This file has had the campaign 'ask' added.){% endif %}</p>
{% endfor %}
{% endif %} {% endif %}
{% if campaign.work.pdffiles.0 %} {% if campaign.work.pdffiles.0 %}
<p>Active PDF file: <a href="{{campaign.work.pdffiles.0.file.url}}">{{campaign.work.pdffiles.0.file}}</a> <br />created {{campaign.work.pdffiles.0.created}} for edition <a href="#edition_{{campaign.work.pdffiles.0.edition.id}}">{{campaign.work.pdffiles.0.edition.id}}</a> {% if campaign.work.pdffiles.0.asking %}(This file has had the campaign 'ask' added.){% endif %}</p> {% for ebf in campaign.work.pdffiles %}
<p>{% if ebf.active %}<span class="yikes">ACTIVE</span> {% endif %}PDF file: <a href="{{ebf.file.url}}">{{ebf.file}}</a> <br />created {{ebf.created}} for edition <a href="#edition_{{ebf.edition.id}}">{{ebf.edition.id}}</a> {% if ebf.asking %}(This file has had the campaign 'ask' added.){% endif %}</p>
{% endfor %}
{% endif %} {% endif %}
{% ifnotequal campaign_status 'ACTIVE' %} {% ifnotequal campaign_status 'ACTIVE' %}
{% ifnotequal campaign.type 3 %} {% ifnotequal campaign.type 3 %}