def index(request, collection_id=None):
    collections = Collection.objects.all()
    user_collection_role = Users_Collections.get_role_buckets(request.user, collections)
    decks = Deck.objects.all().prefetch_related('collection', 'cards')

    decks_by_collection = {}
    for deck in decks:
        if deck.collection.id not in decks_by_collection:
            decks_by_collection[deck.collection.id] = []
        decks_by_collection[deck.collection.id].append(deck)


    collection_list = []
    for collection in collections:
        collection_decks = []
        for deck in decks_by_collection[collection.id]:
            collection_decks.append({
                'id': deck.id,
                'title': deck.title,
                'num_cards': deck.cards.count()
            })
        collection_list.append({
            'id': collection.id,
            'title':collection.title,
            'decks': collection_decks
        })

    context = {
        "collections": collection_list,
        "user_collection_role": user_collection_role
    }

    if collection_id:
        current_collection = collections.get(id=collection_id)
        curr_collection = filter(lambda x: x['id']==current_collection.id, collection_list)
        context = {
        "collections": collection_list,
        "user_collection_role": user_collection_role,
        "collection": curr_collection[0]
        }
        return render(request, 'current_collection_view.html', context)
    else:
        return render(request, 'collection_index.html', context)
Beispiel #2
0
def index(request, deck_id=None):
    collections = Collection.objects.all().prefetch_related('deck_set')
    deck = Deck.objects.get(id=deck_id)
    deck_cards = Decks_Cards.objects.filter(deck=deck).order_by('sort_order').prefetch_related('card__cards_fields_set__field')
    current_collection = Collection.objects.get(id=deck.collection.id)
    user_collection_role = Users_Collections.get_role_buckets(request.user, collections)
    is_quiz_mode = request.GET.get('mode') == 'quiz'

    cards = []
    for dcard in deck_cards:
        card_fields = {'show':[],'reveal':[]}
        for cfield in dcard.card.cards_fields_set.all():
            bucket = 'show'
            if cfield.field.display:
                bucket = 'reveal'
            card_fields[bucket].append({
                'type': cfield.field.field_type,
                'label': cfield.field.label,
                'show_label': cfield.field.show_label,
                'value': cfield.value,
            })
        cards.append({
            'card_id': dcard.card.id,
            'fields': card_fields
        })

    context = {
        "user_collection_role": user_collection_role,
        "collections": collections,
        "deck": deck,
        "cards": cards,
        "collection": current_collection,
        "is_quiz_mode": is_quiz_mode
    }

    return render(request, "deck_view.html", context)