Esempio n. 1
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())
Esempio n. 2
0
def get_all_shelves(reader_id, shelf_name):
    books_in_shelf = Shelf.read_by_reader_and_name(shelf_name, reader_id)

    books = []
    for book in books_in_shelf:
        books.append(Book.read_by_id(book['id_book']))
    return jsonify(books), 200
Esempio n. 3
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()
Esempio n. 4
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)
Esempio n. 5
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()
    })
Esempio n. 6
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)
Esempio n. 7
0
def get_shelves():
    all_shelves = Shelf.read_all_shelves()
    if all_shelves:
        return jsonify(all_shelves), 200
    else:
        return "Self not found", 400
Esempio n. 8
0
def delete_book_of_shelf(id_reader, shelf_name, id_book):
    delete_book = Shelf.delete_book_on_shelf(id_reader, shelf_name, id_book)
    if delete_book is None:
        return "Do not found book in this shelf", 400
    else:
        return delete_book.serialize(), 200
Esempio n. 9
0
def read_all_shelves():
    try:
        shelves = Shelf.read_all_shelves()
        return jsonify(shelves), 200
    except:
        return 'not found', 400