from django.conf.urls import url
from django.shortcuts import HttpResponse, render
class ModelXadmin(object):
def __init__(self, model, site):
self.model = model
self.site = site
def list_view(self, request):
data_list = self.model.objects.all()
return render(request, ‘list_view.html‘, {‘data_list‘:data_list})
def add_view(self, request):
return HttpResponse(‘add_view‘)
def change_view(self, request, num):
return HttpResponse(‘change_view‘)
def delete_view(self, request, num):
return HttpResponse(‘delete_view‘)
def get_urls_operate(self):
temp = []
temp.append(url(r‘^$‘, self.list_view))
temp.append(url(r‘^add/$‘, self.add_view))
temp.append(url(r‘^(\d+)/delete‘, self.delete_view))
temp.append(url(r‘^(\d+)/change‘, self.change_view))
return temp
@property
def urls_operate(self):
return self.get_urls_operate(), None, None
class XadminSite(object):
def __init__(self):
self._registry = {}
def get_urls(self):
temp = []
for model, admin_class_obj in self._registry.items():
model_name = model._meta.model_name
app_name = model._meta.app_label
temp.append(url(r‘^{0}/{1}/‘.format(app_name, model_name), admin_class_obj.urls_operate))
return temp
@property
def urls(self):
return self.get_urls(), None, None
def register(self, model, admin_class=None, **options):
if not admin_class:
admin_class = ModelXadmin
self._registry[model] = admin_class(model, self)
site = XadminSite()