示例#1
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()
示例#2
0
def add_edit_deck(request, shelf_id=None, deck_id=None):
    if deck_id:
        deck = get_object_or_404(Deck, pk=deck_id)
    else:
        deck = Deck()
    if request.method == "GET":
        deck_form = DeckForm(instance=deck)
    elif request.method == "POST":
        deck_form = DeckForm(request.POST, instance=deck)
        if deck_form.is_valid():
            deck = deck_form.save(commit=False)
            if not hasattr(deck, "shelf"):
                shelf = get_object_or_404(Shelf, pk=shelf_id)
                deck.shelf = shelf
            else:
                shelf_id = deck.shelf.id
            deck.save()
            return redirect(reverse("pamietacz.views.show_shelf",
                                    args=(shelf_id,)))
    return render(request,
                  "add_edit_deck.html",
                  {"deck_form": deck_form,
                   "action": request.get_full_path()})
示例#3
0
def add_edit_deck(request, shelf_id=None, deck_id=None):
    if deck_id:
        deck = get_object_or_404(Deck, pk=deck_id)
    else:
        deck = Deck()
    if request.method == "GET":
        deck_form = DeckForm(instance=deck)
    elif request.method == "POST":
        deck_form = DeckForm(request.POST, instance=deck)
        if deck_form.is_valid():
            deck = deck_form.save(commit=False)
            if not hasattr(deck, "shelf"):
                shelf = get_object_or_404(Shelf, pk=shelf_id)
                deck.shelf = shelf
            else:
                shelf_id = deck.shelf.id
            deck.save()
            return redirect(
                reverse("pamietacz.views.show_shelf", args=(shelf_id, )))
    return render(request, "add_edit_deck.html", {
        "deck_form": deck_form,
        "action": request.get_full_path()
    })