<commit_before>from django.http import HttpResponse
from django.views.decorators.http import require_http_methods
from django.shortcuts import render, redirect
from django.core.context_processors import csrf
from django.forms import widgets

from harvardcards.apps.flash.models import Collection, Deck, Card, Decks_Cards, Users_Collections
from harvardcards.apps.flash.forms import DeckImportForm
from harvardcards.apps.flash.decorators import check_role
from harvardcards.apps.flash.lti_service import LTIService
from harvardcards.apps.flash import services, queries, analytics

import logging
log = logging.getLogger(__name__)

def deck_view_helper(request, current_collection, deck_cards):

    role_bucket = services.get_or_update_role_bucket(request)
    canvas_course_collections = LTIService(request).getCourseCollections()
    collection_list = queries.getCollectionList(role_bucket, collection_ids=canvas_course_collections)

    is_quiz_mode = request.GET.get('mode') == 'quiz'
    is_deck_admin = current_collection.id in role_bucket['ADMINISTRATOR']
    card_id = request.GET.get('card_id', '')
    cards = []
    for dcard in deck_cards:
        card_fields = {'show':[],'reveal':[]}
        for cfield in dcard.card.cards_fields_set.all():
            if cfield.field.display:
                bucket = 'show'
            else:
                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,
            'color': dcard.card.color,
            'fields': card_fields
            })
    return [cards, collection_list, is_quiz_mode, is_deck_admin, card_id]


def index(request, deck_id=None):
    """Displays the deck of cards for review/quiz."""

    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 = deck.collection
    [cards, collection_list, is_quiz_mode, is_deck_admin, card_id] = deck_view_helper(request, current_collection, deck_cards)

    context = {  
        "collection": current_collection,
        "nav_collections": collection_list,
        "deck": deck,
        "cards": cards,
        "is_quiz_mode": is_quiz_mode,
        "is_deck_admin": is_deck_admin,
        "card_id": card_id,
    }

    analytics.track(
        actor=request.user,
        verb=analytics.VERBS.viewed,
        object=analytics.OBJECTS.deck,
        context={"deck_id": deck_id},
    )
    return render(request, "deck_view.html", context)

def all_cards(request, collection_id):
    collection_id = int(collection_id)
    decks = queries.getDecksByCollection(collection_ids = [collection_id])
    decks = decks[collection_id]
    current_collection = Collection.objects.get(id=collection_id)

    deck_cards = []
    for deck in decks:
        deck_cards += Decks_Cards.objects.filter(deck=deck).order_by('sort_order').prefetch_related('card__cards_fields_set__field')
    [cards, collection_list, is_quiz_mode, is_deck_admin, card_id] = deck_view_helper(request, current_collection, deck_cards)
    context = {
        "collection": current_collection,
        "nav_collections": collection_list,
        "deck": {'id': -collection_id, 'title': 'All Cards'},
        "cards": cards,
        "is_quiz_mode": is_quiz_mode,
        "is_deck_admin": is_deck_admin,
        "card_id": card_id,
    }

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

@check_role([Users_Collections.ADMINISTRATOR, Users_Collections.INSTRUCTOR, Users_Collections.TEACHING_ASSISTANT, Users_Collections.CONTENT_DEVELOPER], 'deck')
def delete(request, deck_id=None):
    """Deletes a deck."""
    d = {'user': request.user}

    collection_id = queries.getDeckCollectionId(deck_id)
    success = services.delete_deck(deck_id)
    if success:<commit_msg>add analytics to all cards deck function<commit_after>from django.http import HttpResponse
from django.views.decorators.http import require_http_methods
from django.shortcuts import render, redirect
from django.core.context_processors import csrf
from django.forms import widgets

from harvardcards.apps.flash.models import Collection, Deck, Card, Decks_Cards, Users_Collections
from harvardcards.apps.flash.forms import DeckImportForm
from harvardcards.apps.flash.decorators import check_role
from harvardcards.apps.flash.lti_service import LTIService
from harvardcards.apps.flash import services, queries, analytics

import logging
log = logging.getLogger(__name__)

def deck_view_helper(request, current_collection, deck_cards):

    role_bucket = services.get_or_update_role_bucket(request)
    canvas_course_collections = LTIService(request).getCourseCollections()
    collection_list = queries.getCollectionList(role_bucket, collection_ids=canvas_course_collections)

    is_quiz_mode = request.GET.get('mode') == 'quiz'
    is_deck_admin = current_collection.id in role_bucket['ADMINISTRATOR']
    card_id = request.GET.get('card_id', '')
    cards = []
    for dcard in deck_cards:
        card_fields = {'show':[],'reveal':[]}
        for cfield in dcard.card.cards_fields_set.all():
            if cfield.field.display:
                bucket = 'show'
            else:
                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,
            'color': dcard.card.color,
            'fields': card_fields
            })
    return [cards, collection_list, is_quiz_mode, is_deck_admin, card_id]


def index(request, deck_id=None):
    """Displays the deck of cards for review/quiz."""

    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 = deck.collection
    [cards, collection_list, is_quiz_mode, is_deck_admin, card_id] = deck_view_helper(request, current_collection, deck_cards)

    context = {  
        "collection": current_collection,
        "nav_collections": collection_list,
        "deck": deck,
        "cards": cards,
        "is_quiz_mode": is_quiz_mode,
        "is_deck_admin": is_deck_admin,
        "card_id": card_id,
    }

    analytics.track(
        actor=request.user,
        verb=analytics.VERBS.viewed,
        object=analytics.OBJECTS.deck,
        context={"deck_id": deck_id},
    )
    return render(request, "deck_view.html", context)

def all_cards(request, collection_id):
    collection_id = int(collection_id)
    decks = queries.getDecksByCollection(collection_ids = [collection_id])
    decks = decks[collection_id]
    current_collection = Collection.objects.get(id=collection_id)

    deck_cards = []
    for deck in decks:
        deck_cards += Decks_Cards.objects.filter(deck=deck).order_by('sort_order').prefetch_related('card__cards_fields_set__field')
    [cards, collection_list, is_quiz_mode, is_deck_admin, card_id] = deck_view_helper(request, current_collection, deck_cards)
    context = {
        "collection": current_collection,
        "nav_collections": collection_list,
        "deck": {'id': -collection_id, 'title': 'All Cards'},
        "cards": cards,
        "is_quiz_mode": is_quiz_mode,
        "is_deck_admin": is_deck_admin,
        "card_id": card_id,
    }
    analytics.track(
        actor=request.user,
        verb=analytics.VERBS.viewed,
        object=analytics.OBJECTS.deck,
        context={"collection_id": collection_id, 'type': 'All Cards Deck'},
    )
    return render(request, "deck_view.html", context)

@check_role([Users_Collections.ADMINISTRATOR, Users_Collections.INSTRUCTOR, Users_Collections.TEACHING_ASSISTANT, Users_Collections.CONTENT_DEVELOPER], 'deck')
def delete(request, deck_id=None):
    """Deletes a deck."""
    d = {'user': request.user}

    collection_id = queries.getDeckCollectionId(deck_id)
    success = services.delete_deck(deck_id)
    if success:
{'fraction_non_alphanumeric': 0.10610238580505935, 'fraction_numerical': 0.0, 'mean_word_length': 3.6012134583563156, 'pattern_counts': {'":': 31, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2540557', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2559, 'n_words': 475}