<commit_before>"""

TODO: most of the  response mixins do a check for  ndim etc, we should
be able to refactor code to remove duplication..
"""
import xml.etree.ElementTree as etree
import json
import time
import hashlib
import datetime
import decimal
from urlparse import urlparse

from django.utils.functional import Promise
from django.shortcuts import render_to_response, redirect, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponse, StreamingHttpResponse, HttpResponseBadRequest
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied, MultipleObjectsReturned
from django import forms
from django.views.generic import View, ListView, DetailView, RedirectView
from django.utils.decorators import method_decorator
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic import TemplateView
from django.core.urlresolvers import resolve, reverse

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import YAMLRenderer
from rest_framework.renderers import XMLRenderer
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.compat import timezone, force_text

from h1ds.serializers import NodeSerializer, ShotSerializer, DeviceSerializer
from h1ds.models import UserSignal, UserSignalForm, Worksheet, Node, Shot, Device, UserSignalUpdateForm
from h1ds.utils import get_backend_shot_manager
from h1ds.base import get_filter_list


backend_shot_manager = get_backend_shot_manager()


def get_shot_stream_generator():
    shotman = backend_shot_manager()

    def new_shot_generator():
        latest_shot = shotman.get_latest_shot()
        while True:
            time.sleep(1)
            tmp = shotman.get_latest_shot()
            if tmp != latest_shot:
                latest_shot = tmp
                yield "{}\n".format(latest_shot)

    return new_shot_generator


new_shot_generator = get_shot_stream_generator()

### TEMP ###
#import h1ds.filters
from h1ds.base import get_all_filters
############
all_filters = get_all_filters()


def get_format(request, default='html'):
    """get format URI query key.

    Fall back to 'view' for backwards compatability.

    """
    format_ = request.GET.get('format', None)
    if not format_:
        format_ = request.GET.get('view', default)
    return format_


def homepage(request):
    """Return the H1DS homepage."""
    return render_to_response('h1ds/homepage.html',
                              context_instance=RequestContext(request))


def logout_view(request):
    """Log the user out of H1DS."""
    logout(request)
    return redirect('/')


class ChangeProfileForm(forms.Form):
    help_text = ("Please use CamelCase, with each word capitalised. "
                 "For example: MarkOliphant or LymanSpitzer")
    username = forms.CharField(max_length=30, help_text=help_text)
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    email = forms.EmailField()


class UserMainView(ListView):
    def get_queryset(self):
        return Worksheet.objects.filter(user=self.request.user)

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(UserMainView, self).dispatch(*args, **kwargs)


class WorksheetView(DetailView):
    def get_object(self):
        w = get_object_or_404(Worksheet,
                              user__username=self.kwargs['username'],
                              slug=self.kwargs['worksheet'])
        if w.is_public or w.user == self.request.user:
            return w
        else:<commit_msg>fixed method signature in WorksheetView.get_object<commit_after>"""

TODO: most of the  response mixins do a check for  ndim etc, we should
be able to refactor code to remove duplication..
"""
import xml.etree.ElementTree as etree
import json
import time
import hashlib
import datetime
import decimal
from urlparse import urlparse

from django.utils.functional import Promise
from django.shortcuts import render_to_response, redirect, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponse, StreamingHttpResponse, HttpResponseBadRequest
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied, MultipleObjectsReturned
from django import forms
from django.views.generic import View, ListView, DetailView, RedirectView
from django.utils.decorators import method_decorator
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic import TemplateView
from django.core.urlresolvers import resolve, reverse

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import YAMLRenderer
from rest_framework.renderers import XMLRenderer
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.compat import timezone, force_text

from h1ds.serializers import NodeSerializer, ShotSerializer, DeviceSerializer
from h1ds.models import UserSignal, UserSignalForm, Worksheet, Node, Shot, Device, UserSignalUpdateForm
from h1ds.utils import get_backend_shot_manager
from h1ds.base import get_filter_list


backend_shot_manager = get_backend_shot_manager()


def get_shot_stream_generator():
    shotman = backend_shot_manager()

    def new_shot_generator():
        latest_shot = shotman.get_latest_shot()
        while True:
            time.sleep(1)
            tmp = shotman.get_latest_shot()
            if tmp != latest_shot:
                latest_shot = tmp
                yield "{}\n".format(latest_shot)

    return new_shot_generator


new_shot_generator = get_shot_stream_generator()

### TEMP ###
#import h1ds.filters
from h1ds.base import get_all_filters
############
all_filters = get_all_filters()


def get_format(request, default='html'):
    """get format URI query key.

    Fall back to 'view' for backwards compatability.

    """
    format_ = request.GET.get('format', None)
    if not format_:
        format_ = request.GET.get('view', default)
    return format_


def homepage(request):
    """Return the H1DS homepage."""
    return render_to_response('h1ds/homepage.html',
                              context_instance=RequestContext(request))


def logout_view(request):
    """Log the user out of H1DS."""
    logout(request)
    return redirect('/')


class ChangeProfileForm(forms.Form):
    help_text = ("Please use CamelCase, with each word capitalised. "
                 "For example: MarkOliphant or LymanSpitzer")
    username = forms.CharField(max_length=30, help_text=help_text)
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    email = forms.EmailField()


class UserMainView(ListView):
    def get_queryset(self):
        return Worksheet.objects.filter(user=self.request.user)

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(UserMainView, self).dispatch(*args, **kwargs)


class WorksheetView(DetailView):
    def get_object(self, queryset=None):
        w = get_object_or_404(Worksheet,
                              user__username=self.kwargs['username'],
                              slug=self.kwargs['worksheet'])
        if w.is_public or w.user == self.request.user:
            return w
        else:
{'fraction_non_alphanumeric': 0.07871312309257375, 'fraction_numerical': 0.005595116988809766, 'mean_word_length': 4.057877813504823, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1156654', 'n_tokens_mistral': 2216, 'n_tokens_neox': 2147, 'n_words': 608}