示例#1
0
文件: views.py 项目: fnp/redakcja
def preview_from_xml(request):
    from hashlib import sha1
    from PIL import Image
    from os import makedirs
    from lxml import etree
    from librarian.cover import make_cover
    from librarian.dcparser import BookInfo

    xml = request.POST['xml']
    try:
        info = BookInfo.from_bytes(xml.encode('utf-8'))
    except:
        return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
    coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
    cover = make_cover(info)

    cover_dir = 'cover/preview'
    try:
        makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
    except OSError:
        pass
    fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
    img = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
    img.save(os.path.join(settings.MEDIA_ROOT, fname))
    return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
示例#2
0
文件: views.py 项目: fnp/redakcja
def preview(request, book, chunk=None, rev=None):
    """Creates a cover image.

    If chunk and rev number are given, use version from given revision.
    If rev is not given, use publishable version.
    """
    from PIL import Image
    from librarian.cover import make_cover
    from librarian.dcparser import BookInfo

    chunk = Chunk.get(book, chunk)
    if rev is not None:
        try:
            revision = chunk.at_revision(rev)
        except Chunk.change_model.DoesNotExist:
            raise Http404
    else:
        revision = chunk.publishable()
        if revision is None:
            raise Http404
    xml = revision.materialize().encode('utf-8')

    try:
        info = BookInfo.from_bytes(xml)
    except:
        return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
    cover = make_cover(info)
    response = HttpResponse(content_type=cover.mime_type())
    img = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
    img.save(response, cover.format)
    return response
示例#3
0
文件: book.py 项目: fnp/redakcja
 def book_info(self, publishable=True):
     try:
         book_xml = self.materialize(publishable=publishable)
     except self.NoTextError:
         pass
     else:
         from librarian.dcparser import BookInfo
         from librarian import NoDublinCore, ParseError, ValidationError
         try:
             return BookInfo.from_bytes(book_xml.encode('utf-8'))
         except (self.NoTextError, ParseError, NoDublinCore, ValidationError):
             return None