add distro

pull/1/head
eric 2016-05-10 17:33:51 -04:00
parent 50930ce2b1
commit c92a823a89
4 changed files with 60 additions and 0 deletions

0
distro/__init__.py Normal file
View File

45
distro/models.py Normal file
View File

@ -0,0 +1,45 @@
from ftplib import FTP, FTP_TLS
from django.db import models
class Target(models.Model):
name = models.CharField(max_length=30)
host = models.CharField(max_length=60)
pw = models.CharField(max_length=30)
user = models.CharField(max_length=30)
protocol = models.CharField(max_length=10, default='ftp')
formats = models.ManyToManyField(Format, related_name='targets')
def __unicode__(self):
return self.name
_ftp = None
def get_ftp(self):
if not self._ftp:
if self.protocol == 'secure':
self._ftp = FTP_TLS(self.host, self.user, self.pw)
self._ftp.prot_p()
else:
self._ftp = FTP(self.host, self.user, self.pw)
return self._ftp
def push(self, book):
for ebook in book.ebooks():
isbn = ebook.edition.isbn_13 if ebook.edition.isbn_13 else ebook.edition.work.first_isbn_13()
if isbn and self.formats.filter(name=ebook.format).exists():
ebfile = ebook.get_archive()
self.get_ftp().storbinary(u'STOR {}.{}'.format(isbn,ebook.format), ebfile)
ebfile.close()
Depost.objects.create(target=self, isbn=isbn, format=ebook.format)
class Deposit(models.Model):
target = models.ForeignKey(Target, related_name="deposits")
isbn = models.CharField(max_length=13)
format = models.CharField(max_length=30)
updated = models.DateTimeField(auto_now_add=True)
class Format(
name = models.CharField(max_length=4)
def __unicode__(self):
return self.name

0
distro/push.py Normal file
View File

15
distro/tasks.py Normal file
View File

@ -0,0 +1,15 @@
import logging
from celery.task import task
from .models import Target
logger = logging.getLogger(__name__)
def push_books_to_target(books, target):
"""given a list of books this task will push the books, metadata and covers to the target
"""
for book in books:
target.push_file(book)
target.push_cover(book)
target.push_onix(books)