Skip to content

Commit

Permalink
[refactor] Moved download crl-view to mixin #116
Browse files Browse the repository at this point in the history
Closes #116
  • Loading branch information
ManishShah120 authored Jun 2, 2021
1 parent 43d810f commit eca0b79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
17 changes: 3 additions & 14 deletions django_x509/base/admin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from django import forms
from django.conf.urls import url
from django.contrib.admin import ModelAdmin
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.shortcuts import render
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import ngettext
from django.utils.translation import ugettext_lazy as _

from django_x509 import settings as app_settings
from .mixin import CrlDownloadMixin


class X509Form(forms.ModelForm):
Expand Down Expand Up @@ -97,7 +96,7 @@ def get_context(self, data, ca_count=0, cert_count=0):
return context


class AbstractCaAdmin(BaseAdmin):
class AbstractCaAdmin(BaseAdmin, CrlDownloadMixin):
list_filter = ['key_length', 'digest', 'created']
fields = [
'operation_type',
Expand Down Expand Up @@ -132,16 +131,6 @@ def get_urls(self):
url(r'^x509/ca/(?P<pk>[^/]+).crl$', self.crl_view, name='crl')
] + super().get_urls()

def crl_view(self, request, pk):
authenticated = request.user.is_authenticated
authenticated = authenticated() if callable(authenticated) else authenticated
if app_settings.CRL_PROTECTED and not authenticated:
return HttpResponse(_('Forbidden'), status=403, content_type='text/plain')
instance = get_object_or_404(self.model, pk=pk)
return HttpResponse(
instance.crl, status=200, content_type='application/x-pem-file'
)

def renew_ca(self, request, queryset):
if request.POST.get('post'):
renewed_rows = 0
Expand Down
17 changes: 17 additions & 0 deletions django_x509/base/mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _

from django_x509 import settings as app_settings


class CrlDownloadMixin:
def crl_view(self, request, pk):
authenticated = request.user.is_authenticated
authenticated = authenticated() if callable(authenticated) else authenticated
if app_settings.CRL_PROTECTED and not authenticated:
return HttpResponse(_('Forbidden'), status=403, content_type='text/plain')
instance = get_object_or_404(self.model, pk=pk)
return HttpResponse(
instance.crl, status=200, content_type='application/x-pem-file'
)

0 comments on commit eca0b79

Please sign in to comment.