<commit_before>"""
This is an experiment in splitting up the monolithic model view class into
smaller, more reusable parts, and using class-based views at the same time
(not the generic class based views, though)
"""

import json

from django import forms
from django.contrib import messages
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.forms.models import modelform_factory, model_to_dict
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateView

from towel.forms import BatchForm, towel_formfield_callback
from towel.paginator import Paginator, EmptyPage, InvalidPage
from towel.utils import changed_regions, safe_queryset_and, tryreverse


class ModelResourceView(TemplateView):
    base_template = 'base.html'
    model = None
    queryset = None
    template_name_suffix = None

    def url(self, item, *args, **kwargs):
        if getattr(self, 'object', None):
            return self.object.urls.url(item, *args, **kwargs)
        return self.model().urls.url(item, *args, **kwargs)

    def get_context_data(self, **kwargs):
        opts = self.model._meta
        context = {
            'base_template': self.base_template,
            'verbose_name': opts.verbose_name,
            'verbose_name_plural': opts.verbose_name_plural,
            'view': self,

            'add_url': self.url('add'),
            'list_url': self.url('list'),
            }<commit_msg>These are not fatal errors<commit_after>"""
This is an experiment in splitting up the monolithic model view class into
smaller, more reusable parts, and using class-based views at the same time
(not the generic class based views, though)
"""

import json

from django import forms
from django.contrib import messages
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.core.urlresolvers import NoReverseMatch
from django.forms.models import modelform_factory, model_to_dict
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateView

from towel.forms import BatchForm, towel_formfield_callback
from towel.paginator import Paginator, EmptyPage, InvalidPage
from towel.utils import changed_regions, safe_queryset_and, tryreverse


class ModelResourceView(TemplateView):
    base_template = 'base.html'
    model = None
    queryset = None
    template_name_suffix = None

    def url(self, item, *args, **kwargs):
        fail_silently = kwargs.pop('fail_silently', False)

        try:
            if getattr(self, 'object', None):
                return self.object.urls.url(item, *args, **kwargs)
            return self.model().urls.url(item, *args, **kwargs)
        except NoReverseMatch:
            if not fail_silently:
                raise
            return None

    def get_context_data(self, **kwargs):
        opts = self.model._meta
        context = {
            'base_template': self.base_template,
            'verbose_name': opts.verbose_name,
            'verbose_name_plural': opts.verbose_name_plural,
            'view': self,

            'add_url': self.url('add', fail_silently=True),
            'list_url': self.url('list', fail_silently=True),
            }
{'fraction_non_alphanumeric': 0.08389163996504514, 'fraction_numerical': 0.003495484998543548, 'mean_word_length': 3.4597402597402596, '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': '2533205', 'n_tokens_mistral': 977, 'n_tokens_neox': 943, 'n_words': 290}