Exemplo n.º 1
0
def grab_shelf(shelf):
    name = re.split(r'\([0-9]+\)',
                    shelf.text)[0].replace("(", "").replace(")", "").strip()
    num_books = int(
        re.findall(r'\([0-9]+\)', shelf.text)[0].replace("(",
                                                         "").replace(")", ""))
    link = shelf.get_attribute("href")
    return Shelf(name, num_books, link)
Exemplo n.º 2
0
def add_to_shelf(reader_id, shelf_name, book_id):

    new_book_in_shelf = Shelf(id_reader=reader_id,
                              shelf_name=shelf_name,
                              id_book=book_id)

    new_book_in_shelf.add_book_to_shelf()

    return jsonify(new_book_in_shelf.serialize())
Exemplo n.º 3
0
def add_edit_shelf(request, shelf_id=None):
    if shelf_id:
        shelf = get_object_or_404(Shelf, pk=shelf_id)
    else:
        shelf = Shelf()
    if request.method == "GET":
        shelf_form = ShelfForm(instance=shelf)
    elif request.method == "POST":
        shelf_form = ShelfForm(request.POST, instance=shelf)
        if shelf_form.is_valid():
            shelf_form.save()
            return redirect(reverse("pamietacz.views.shelf_list"))
    return render(request, "add_edit_shelf.html", {
        "shelf_form": shelf_form,
        "action": request.get_full_path()
    })
Exemplo n.º 4
0
def load_data_as_xml(data_dump_as_xml):
    tree = etree.parse(data_dump_as_xml)
    docinfo = tree.docinfo
    if docinfo.encoding != "UTF-8":
        raise XMLDataDumpException("Not supported encoding: %s" %
                                   docinfo.encoding)
    root = tree.getroot()
    if root.tag != "data":
        raise XMLDataDumpException("%s: %s != 'data'" %
                                   (root.sourceline, root.tag))
    for shelf_xml in root:
        if shelf_xml.tag != "shelf":
            raise XMLDataDumpException("%s: %s != 'shelf'" %
                                       (shelf_xml.sourceline, shelf_xml.tag))
        shelf = Shelf()
        shelf.name = shelf_xml.get("name")
        try:
            shelf.save()
        except IntegrityError as e:
            raise XMLDataDumpException("%s: cannot add shelf: %s" %
                                       (shelf_xml.sourceline, str(e)))
        for deck_data in shelf_xml:
            if deck_data.tag != "deck":
                raise XMLDataDumpException(
                    "%s: %s != 'deck'" % (deck_data.sourceline, deck_data.tag))
            deck = Deck()
            deck.shelf = shelf
            deck.name = deck_data.get("name")
            deck.save()
            for card_data in deck_data:
                if card_data.tag != "card":
                    raise XMLDataDumpException(
                        "%s: %s != 'card'" %
                        (card_data.sourceline, card_data.tag))
                if card_data[0].tag != "question":
                    raise XMLDataDumpException(
                        "%s: %s != 'question'" %
                        (card_data[0].sourceline, card_data[0].tag))
                if card_data[1].tag != "answer":
                    raise XMLDataDumpException(
                        "%s: s%s != 'answer'" %
                        (card_data[1].sourceline, card_data[1].tag))
                card = Card()
                card.deck = deck
                card.question = card_data[0].text
                card.answer = card_data[1].text
                card.save()
Exemplo n.º 5
0
def main():
    output_books_metadata()

    json_reader = JSONBookReader(METADATA_JSON_PATH)

    shelf = Shelf([])

    for d in json_reader.read():
        shelf.add_book(Book(**d))

    if AIRTABLE_API_KEY and AIRTABLE_BOOK_CATALOG_API:
        airtable_reader = AirtableBookReader(AIRTABLE_API_KEY,
                                             AIRTABLE_BOOK_CATALOG_API)
        for d in airtable_reader.read():
            shelf.add_book(Book(**d), check_similarity=True)

    LOG.info("Output readme %s", README_PATH)
    with open(README_PATH, "w") as f:
        f.write(HeaderLine)
        f.write(BookMDRender.md_header())
        for book in shelf.books:
            f.write(BookMDRender(book).md_column())

        f.write(FooterLine)