<commit_before>import csv

from django.shortcuts import render
from django.core.urlresolvers import reverse, reverse_lazy
from django.core.exceptions import PermissionDenied
from django.views import generic
from rest_framework.generics import RetrieveAPIView
from .models import Dataset, DatasetDocument
from .serializers import DatasetSerializer
from .forms import DatasetUploadForm, DatasetForm
from django.http import HttpResponse


class DatasetManagement(generic.ListView):
    template_name="datasets/dataset_management.html"
    model = Dataset
    paginate_by = 10

    def get_queryset(self):
        return Dataset.objects.filter(owner_id=self.request.user.pk).order_by('-modified_at')


class DatasetDisplay(generic.DetailView):
    model = Dataset

    def get_context_data(self, **kwargs):
        context = super(DatasetDisplay, self).get_context_data(**kwargs)
        context['form'] = DatasetUploadForm()
        return context

    def get_object(self, **kwargs):
        dataset = super(DatasetDisplay, self).get_object(**kwargs)
        if dataset.owner != self.request.user:<commit_msg>Added prefetch_related and reduced the number of queries from 212 to ~8.<commit_after>import csv

from django.shortcuts import render
from django.core.urlresolvers import reverse, reverse_lazy
from django.core.exceptions import PermissionDenied
from django.views import generic
from rest_framework.generics import RetrieveAPIView
from .models import Dataset, DatasetDocument
from .serializers import DatasetSerializer
from .forms import DatasetUploadForm, DatasetForm
from django.http import HttpResponse


class DatasetManagement(generic.ListView):
    template_name="datasets/dataset_management.html"
    model = Dataset
    paginate_by = 10

    def get_queryset(self):
        return Dataset.objects.filter(owner_id=self.request.user.pk).order_by('-modified_at')


class DatasetDisplay(generic.DetailView):
    model = Dataset
    queryset = Dataset.objects.prefetch_related('records__cartogram_entity')

    def get_context_data(self, **kwargs):
        context = super(DatasetDisplay, self).get_context_data(**kwargs)
        context['form'] = DatasetUploadForm()
        return context

    def get_object(self, **kwargs):
        dataset = super(DatasetDisplay, self).get_object(**kwargs)
        if dataset.owner != self.request.user:
{'fraction_non_alphanumeric': 0.08297872340425531, 'fraction_numerical': 0.003404255319148936, 'mean_word_length': 4.79064039408867, 'pattern_counts': {'":': 0, '<': 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': '7620994', 'n_tokens_mistral': 665, 'n_tokens_neox': 639, 'n_words': 162}