<commit_before>from django.core.signing import BadSignature
from django.core.urlresolvers import reverse
from django.views.generic.base import View
from django.views.generic.detail import DetailView, SingleObjectMixin
from django.views.generic.edit import FormView
from django.http import Http404, HttpResponse
from django.utils.text import slugify

from OpenSSL import crypto
from darkknight.forms import GenerateForm
from darkknight.models import CertificateSigningRequest, pk_signer


class GenerateView(FormView):
    template_name = 'darkknight/generate.html'
    form_class = GenerateForm

    def get_success_url(self):
        assert hasattr(self, 'instance')
        return reverse(
            detail,
            kwargs=dict(signed_pk=self.instance.signed_pk),
        )

    def form_valid(self, form):
        self.instance = form.generate()
        return super(GenerateView, self).form_valid(form)


class CertificateSigingRequestMixin(object):
    model = CertificateSigningRequest

    def get_object(self, queryset=None):
        if hasattr(self, 'object'):
            return self.object

        assert 'signed_pk' in self.kwargs
        if queryset is None:
            queryset = self.get_queryset()
        try:
            pk = pk_signer.unsign(self.kwargs['signed_pk'])
        except BadSignature:
            raise Http404

        try:
            self.object = queryset.get(pk=pk)
        except self.model.DoesNotExist:
            raise Http404

        return self.object


class DetailView(CertificateSigingRequestMixin, DetailView):
    template_name = 'darkknight/detail.html'

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)

        assert 'signed_pk' in self.kwargs
        context['signed_pk'] = self.kwargs['signed_pk']

        obj = context['object']
        with open(obj.csr, 'r') as f:
            csr = f.read()

        req = crypto.load_certificate_request(crypto.FILETYPE_PEM, csr)
        context['csr'] = req.get_subject()

        return context



class DownloadView(CertificateSigingRequestMixin, SingleObjectMixin, View):
    http_method_names = ['get']

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()

        with open(self.object.csr, 'r') as f:
            csr = f.read()

        response = HttpResponse(csr, content_type='text/plain')

        if 'download' in request.GET:
            content_disposition = 'attachement; filename="{}"'.format(
                '%s.csr' % slugify(self.object.domain))
            response['Content-Disposition'] = content_disposition
        elif 'view' not in request.GET:
            raise Http404

        return response


generate = GenerateView.as_view()
detail = DetailView.as_view()
download = DownloadView.as_view()<commit_msg>I don't know what Siging is<commit_after>from django.core.signing import BadSignature
from django.core.urlresolvers import reverse
from django.views.generic.base import View
from django.views.generic.detail import DetailView, SingleObjectMixin
from django.views.generic.edit import FormView
from django.http import Http404, HttpResponse
from django.utils.text import slugify

from OpenSSL import crypto
from darkknight.forms import GenerateForm
from darkknight.models import CertificateSigningRequest, pk_signer


class GenerateView(FormView):
    template_name = 'darkknight/generate.html'
    form_class = GenerateForm

    def get_success_url(self):
        assert hasattr(self, 'instance')
        return reverse(
            detail,
            kwargs=dict(signed_pk=self.instance.signed_pk),
        )

    def form_valid(self, form):
        self.instance = form.generate()
        return super(GenerateView, self).form_valid(form)


class CertificateSigningRequestMixin(object):
    model = CertificateSigningRequest

    def get_object(self, queryset=None):
        if hasattr(self, 'object'):
            return self.object

        assert 'signed_pk' in self.kwargs
        if queryset is None:
            queryset = self.get_queryset()
        try:
            pk = pk_signer.unsign(self.kwargs['signed_pk'])
        except BadSignature:
            raise Http404

        try:
            self.object = queryset.get(pk=pk)
        except self.model.DoesNotExist:
            raise Http404

        return self.object


class DetailView(CertificateSigningRequestMixin, DetailView):
    template_name = 'darkknight/detail.html'

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)

        assert 'signed_pk' in self.kwargs
        context['signed_pk'] = self.kwargs['signed_pk']

        obj = context['object']
        with open(obj.csr, 'r') as f:
            csr = f.read()

        req = crypto.load_certificate_request(crypto.FILETYPE_PEM, csr)
        context['csr'] = req.get_subject()

        return context



class DownloadView(CertificateSigningRequestMixin, SingleObjectMixin, View):
    http_method_names = ['get']

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()

        with open(self.object.csr, 'r') as f:
            csr = f.read()

        response = HttpResponse(csr, content_type='text/plain')

        if 'download' in request.GET:
            content_disposition = 'attachement; filename="{}"'.format(
                '%s.csr' % slugify(self.object.domain))
            response['Content-Disposition'] = content_disposition
        elif 'view' not in request.GET:
            raise Http404

        return response


generate = GenerateView.as_view()
detail = DetailView.as_view()
download = DownloadView.as_view()
{'fraction_non_alphanumeric': 0.09526325057228385, 'fraction_numerical': 0.004226096143687269, 'mean_word_length': 2.8904109589041096, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3666329', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1601, 'n_words': 376}