Exemplo n.º 1
0
def xmlify(model, q):
    """Serialize a Python dict as generic XML using xml.dom.minidom.
    See: https://docs.python.org/2/library/xml.html
    """
    doc = xmldoc()
    _xml = doc.appendChild(doc.createElement(model + 's'))
    for obj in q:
        node = _xml.appendChild(doc.createElement(model))

        for key, value in obj.iteritems():
            nodesub = node.appendChild(doc.createElement(key))
            nodesub.appendChild(doc.createTextNode(unicode(value)))

    resp = app.response_class(
        doc.toprettyxml(indent='    ', encoding='utf-8'),
        mimetype='application/xml'
    )

    return resp
Exemplo n.º 2
0
def rssify(q):
    """Serialize a Python dict as XML using xml.dom.minidom,
    this time constructing the document according to the RSS
    (Rich Site Summary) format. The resulting document can be
    used by feed readers like Google's Feedburner.
    See: http://www.rssboard.org/rss-specification
    """
    doc = xmldoc()
    _rss = doc.appendChild(doc.createElement('rss'))
    _rss.setAttribute('version', '2.0')
    channel = _rss.appendChild(doc.createElement('channel'))
    title = channel.appendChild(doc.createElement('title'))
    title.appendChild(doc.createTextNode('Book List from Lending Library'))
    desc = channel.appendChild(doc.createElement('description'))
    desc.appendChild(
        doc.createTextNode('A list of books from the Lending Library.'))
    link = channel.appendChild(doc.createElement('link'))
    link.appendChild(
        doc.createTextNode(url_for('home.booksAPI', _external=True)))
    date_format = '%a, %d %b %Y %H:%M:%S +0000'
    update_date = dt.utcnow().strftime(date_format)
    pub_date = channel.appendChild(doc.createElement('pubDate'))
    pub_date.appendChild(doc.createTextNode(update_date))

    for obj in q:
        item = channel.appendChild(doc.createElement('item'))

        item_title = item.appendChild(doc.createElement('title'))
        item_title.appendChild(doc.createTextNode(obj['title']))

        eLink = url_for('home.bookInfo', book_id=obj['id'], _external=True)
        item_link = item.appendChild(doc.createElement('link'))
        item_link.appendChild(doc.createTextNode(eLink))

        item_date = obj['date_added'].strftime(date_format)
        item_updated = item.appendChild(doc.createElement('pubDate'))
        item_updated.appendChild(doc.createTextNode(item_date))

        item_summary = item.appendChild(doc.createElement('description'))
        item_summary.appendChild(doc.createTextNode(obj['synopsis']))

        author = item.appendChild(doc.createElement('author'))
        author.appendChild(doc.createTextNode(obj['author']))

        year_pub = item.appendChild(doc.createElement('yearpublished'))
        year_pub.appendChild(doc.createTextNode(str(obj['year_published'])))

        picture = item.appendChild(doc.createElement('picture'))
        picture.appendChild(doc.createTextNode(obj['picture']))

        avail = item.appendChild(doc.createElement('isavailable'))
        avail.appendChild(doc.createTextNode(str(obj['is_available'])))

        if (obj['due_date'] is not None):
            ts = time.strptime(obj['due_date'], '%m/%d/%Y')
            due_date = dt.fromtimestamp(time.mktime(ts))
            item_due_date = due_date.strftime(date_format)
            due_date = item.appendChild(doc.createElement('duedate'))
            due_date.appendChild(doc.createTextNode(item_due_date))

        lender = item.appendChild(doc.createElement('lender'))
        lender.appendChild(doc.createTextNode(obj['lender']))

        if (obj['borrower'] is not None):
            borrower_str = obj['borrower']['email'] + ' ('
            borrower_str += obj['borrower']['name'] + ')'
            borrower = item.appendChild(doc.createElement('borrower'))
            borrower.appendChild(doc.createTextNode(borrower_str))

    resp = app.response_class(
        doc.toprettyxml(indent='    ', encoding='utf-8'),
        mimetype='application/rss+xml'
    )

    return resp
Exemplo n.º 3
0
def atomify(q):
    """Serialize a Python dict as XML using xml.dom.minidom,
    this time constructing the document according to the Atom
    Syndication Format. The resulting document can be used by
    feed readers like Google's Feedburner.
    See: https://tools.ietf.org/html/rfc4287
    """
    doc = xmldoc()
    _xml = doc.appendChild(doc.createElement('feed'))
    _xml.setAttribute('xmlns', 'http://www.w3.org/2005/Atom')
    title = _xml.appendChild(doc.createElement('title'))
    title.appendChild(doc.createTextNode('Book List from Lending Library'))
    link = _xml.appendChild(doc.createElement('link'))
    link.setAttribute('href', url_for('home.booksAPI', _external=True))
    link.setAttribute('rel', 'self')
    feed_id = _xml.appendChild(doc.createElement('id'))
    feed_id.appendChild(
        doc.createTextNode(url_for('home.catalogHome', _external=True)))
    update_date = dt.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
    updated = _xml.appendChild(doc.createElement('updated'))
    updated.appendChild(doc.createTextNode(update_date))

    for obj in q:
        entry = _xml.appendChild(doc.createElement('entry'))

        entry_title = entry.appendChild(doc.createElement('title'))
        entry_title.appendChild(doc.createTextNode(obj['title']))

        eLink = url_for('home.bookInfo', book_id=obj['id'], _external=True)
        entry_link = entry.appendChild(doc.createElement('link'))
        entry_link.setAttribute('href', eLink)

        host = re.search(r'//(.+?)/(.+)/', eLink)
        entry_tag = 'tag:' + host.group(1) + ','
        entry_tag += obj['date_added'].strftime('%Y-%m-%d')
        entry_tag += ':/' + host.group(2)
        entry_id = entry.appendChild(doc.createElement('id'))
        entry_id.appendChild(doc.createTextNode(entry_tag))

        entry_date = obj['date_added'].strftime('%Y-%m-%dT%H:%M:%SZ')
        entry_updated = entry.appendChild(doc.createElement('updated'))
        entry_updated.appendChild(doc.createTextNode(entry_date))

        entry_summary = entry.appendChild(doc.createElement('summary'))
        entry_summary.appendChild(doc.createTextNode(obj['synopsis']))

        author = entry.appendChild(doc.createElement('author'))
        author_name = author.appendChild(doc.createElement('name'))
        author_name.appendChild(doc.createTextNode(obj['author']))

        year_pub = entry.appendChild(doc.createElement('yearpublished'))
        year_pub.appendChild(doc.createTextNode(str(obj['year_published'])))

        picture = entry.appendChild(doc.createElement('picture'))
        picture.appendChild(doc.createTextNode(obj['picture']))

        avail = entry.appendChild(doc.createElement('isavailable'))
        avail.appendChild(doc.createTextNode(str(obj['is_available'])))

        if (obj['due_date'] is not None):
            ts = time.strptime(obj['due_date'], '%m/%d/%Y')
            due_date = dt.fromtimestamp(time.mktime(ts))
            entry_due_date = due_date.strftime('%Y-%m-%dT%H:%M:%SZ')
            due_date = entry.appendChild(doc.createElement('duedate'))
            due_date.appendChild(doc.createTextNode(entry_due_date))

        lender = entry.appendChild(doc.createElement('lender'))
        lender_email = lender.appendChild(doc.createElement('email'))
        lender_email.appendChild(doc.createTextNode(obj['lender']))

        if (obj['borrower'] is not None):
            borrower = entry.appendChild(doc.createElement('borrower'))
            borrower_name = borrower.appendChild(doc.createElement('name'))
            borrower_name.appendChild(
                doc.createTextNode(obj['borrower']['name']))
            borrower_email = borrower.appendChild(doc.createElement('email'))
            borrower_email.appendChild(
                doc.createTextNode(obj['borrower']['email']))

    resp = app.response_class(
        doc.toprettyxml(indent='    ', encoding='utf-8'),
        mimetype='application/atom+xml'
    )

    return resp