class UrlBuilder(BaseBuilder): def __init__(self, *args, **kwargs): super(UrlBuilder, self).__init__(*args, **kwargs) self.viewbuilder = ViewBuilder(*args, **kwargs) self.urls = self.generate_urls() def generate_urls(self): urls = [] pluralized = text.plural(self.model) list_view = self.viewbuilder.generate_list_view() update_view = self.viewbuilder.generate_update_view() detail_view = self.viewbuilder.generate_detail_view() create_view = self.viewbuilder.generate_create_view() delete_view = self.viewbuilder.generate_delete_view() entries = [ (r'^{}/{}/$', list_view.as_view(), '{}-{}-list'), (r'^{}/{}/(?P<pk>\d+)/$', detail_view.as_view(), '{}-{}-detail'), (r'^{}/{}/create/$', create_view.as_view(), '{}-{}-create'), (r'^{}/{}/(?P<pk>\d+)/update/$', update_view.as_view(), '{}-{}-update'), (r'^{}/{}/(?P<pk>\d+)/delete/$', delete_view.as_view(), '{}-{}-delete'), ] for entry in entries: address = entry[0].format(self.app, pluralized) url_name = entry[2].format(self.app, self.model) urls.append( url(address, entry[1], name=url_name), ) return urls
class ViewBuilderTestCase(TestCase): def setUp(self): self.builder = ViewBuilder('tests', 'testmodel') def test_get_model_class(self): model_class = self.builder.get_model_class self.assertEqual(model_class.__name__, "TestModel") def test_list_view(self): list_view = self.builder.generate_list_view() self.assertEqual(list_view.__name__, "TestmodelListView") self.assertEqual(list_view.table_pagination, self.builder.tables2_pagination) def test_create_view(self): create_view = self.builder.generate_create_view() self.assertEqual(create_view.__name__, 'TestmodelCreateView') def test_detail_view(self): detail_view = self.builder.generate_detail_view() self.assertEqual(detail_view.__name__, 'TestmodelDetailView') def test_update_view(self): update_view = self.builder.generate_update_view() self.assertEqual(update_view.__name__, 'TestmodelUpdateView') def test_table_pagination(self): tables2_pagination = self.builder.tables2_pagination self.assertEqual(tables2_pagination, 20) # test the default pagination self.builder.tables2_pagination = None list_view = self.builder.generate_list_view() self.assertEqual(list_view.table_pagination, 10) def test_tables2_feilds(self): tables2_feilds = self.builder.tables2_fields self.assertEqual(tables2_feilds, ('name', 'email'))
from django.conf.urls import url from django.db import connection from crudbuilder.registry import registry from crudbuilder.views import ViewBuilder from crudbuilder import helpers helpers.auto_discover() urlpatterns = [] tables = connection.introspection.table_names() if tables: for app_model, base_crud in registry.items(): app, model = app_model.split('-') viewbuilder = ViewBuilder(app, model, base_crud) urls = [] pluralized = helpers.plural(model) list_view = viewbuilder.generate_list_view() update_view = viewbuilder.generate_update_view() detail_view = viewbuilder.generate_detail_view() create_view = viewbuilder.generate_create_view() delete_view = viewbuilder.generate_delete_view() entries = [ (r'^{}/{}/$', list_view.as_view(), '{}-{}-list'), (r'^{}/{}/(?P<pk>\d+)/$', detail_view.as_view(), '{}-{}-detail'), (r'^{}/{}/create/$', create_view.as_view(), '{}-{}-create'), (r'^{}/{}/(?P<pk>\d+)/update/$',
def setUp(self): self.builder = ViewBuilder('tests', 'testmodel', TestModelCrud)
class ViewBuilderTestCase(TestCase): def setUp(self): self.builder = ViewBuilder('tests', 'testmodel', TestModelCrud) def test_get_model_class(self): model_class = self.builder.get_model_class self.assertEqual(model_class.__name__, "TestModel") def test_list_view(self): list_view = self.builder.generate_list_view() self.assertEqual(list_view.__name__, "TestmodelListView") self.assertEqual(list_view.table_pagination, {'per_page': self.builder.tables2_pagination}) def test_create_view(self): create_view = self.builder.generate_create_view() self.assertEqual(create_view.__name__, 'TestmodelCreateView') def test_detail_view(self): detail_view = self.builder.generate_detail_view() self.assertEqual(detail_view.__name__, 'TestmodelDetailView') def test_update_view(self): update_view = self.builder.generate_update_view() self.assertEqual(update_view.__name__, 'TestmodelUpdateView') def test_table_pagination(self): tables2_pagination = self.builder.tables2_pagination self.assertEqual(tables2_pagination, 20) # test the default pagination self.builder.tables2_pagination = None list_view = self.builder.generate_list_view() self.assertEqual(list_view.table_pagination, {'per_page': 10}) def test_tables2_feilds(self): tables2_feilds = self.builder.tables2_fields self.assertEqual(tables2_feilds, ('name', 'email')) def test_custom_list_view_permission(self): # test custom permission self.builder.permissions = dict(list='mycustom_list_perm') self.assertEqual( self.builder.view_permission('list'), 'mycustom_list_perm') def test_default_list_view_permission(self): # test auto generated (default) permission self.assertEqual( self.builder.view_permission('list'), 'tests.testmodel_list') def test_check_crud_views(self): self.builder.generate_crud() generated_views = [ view_type for view_type in self.builder.classes.keys()] expected_views = [ 'TestmodelCreateView', 'TestmodelListView', 'TestmodelDetailView', 'TestmodelUpdateView', 'TestmodelDeleteView' ] self.assertEqual(sorted(generated_views), sorted(expected_views))
from django.http import HttpResponseRedirect from crudbuilder.views import ViewBuilder from crudbuilder.helpers import reverse from example.crud import PersonCrud builder = ViewBuilder('example', 'person', PersonCrud) builder.generate_crud() PersonListView = builder.classes['PersonListView'] PersonCreateView = builder.classes['PersonCreateView'] PersonUpdateView = builder.classes['PersonUpdateView'] PersonDetailView = builder.classes['PersonDetailView'] class MyCustomPersonListView(PersonListView): def get_context_data(self, **kwargs): context = super(MyCustomPersonListView, self).get_context_data(**kwargs) context['your_template_variable'] = 'Your new template variable' return context def get_queryset(self): # return super(MyCustomPersonListView, self).get_queryset() return self.model.objects.none() class MyCustomPersonDetailView(PersonDetailView): def get_context_data(self, **kwargs): context = super(MyCustomPersonDetailView, self).get_context_data(**kwargs) # context['form'] = YourAnotherForm return context
from crudbuilder.registry import registry from crudbuilder.views import ViewBuilder, crudlist_view from crudbuilder import helpers helpers.auto_discover() urlpatterns = [ url(r'^$', crudlist_view, name='crud-index'), ] tables = connection.introspection.table_names() if tables: for app_model, base_crud in registry.items(): app, model, postfix_url = app_model.split('-', 2) viewbuilder = ViewBuilder(app, model, base_crud) urls = [] list_view = viewbuilder.generate_list_view() update_view = viewbuilder.generate_update_view() detail_view = viewbuilder.generate_detail_view() create_view = viewbuilder.generate_create_view() delete_view = viewbuilder.generate_delete_view() entries = [ (r'^{}/{}/$', list_view.as_view(), '{}-{}-list'), (r'^{}/{}/(?P<pk>\d+)/$', detail_view.as_view(), '{}-{}-detail'), (r'^{}/{}/create/$', create_view.as_view(), '{}-{}-create'), (r'^{}/{}/(?P<pk>\d+)/update/$', update_view.as_view(), '{}-{}-update'),
""" Definition of views. """ from django.shortcuts import render from django.http import HttpRequest from django.template import RequestContext from datetime import datetime from crudbuilder.views import ViewBuilder from app.crud import ModuleCrud builder = ViewBuilder('app', 'module', ModuleCrud) builder.generate_crud() def home(request): """Renders the home page.""" assert isinstance(request, HttpRequest) return render(request, 'app/index.html', { 'title': 'Home Page', 'year': datetime.now().year, }) def contact(request): """Renders the contact page.""" assert isinstance(request, HttpRequest) return render( request, 'app/contact.html', { 'title': 'Contact',
from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from crudbuilder.views import ViewBuilder from example.crud import PersonCrud builder = ViewBuilder('example', 'person', PersonCrud) builder.generate_crud() PersonListView = builder.classes['PersonListView'] PersonCreateView = builder.classes['PersonCreateView'] PersonUpdateView = builder.classes['PersonUpdateView'] PersonDetailView = builder.classes['PersonDetailView'] class MyCustomPersonListView(PersonListView): def get_context_data(self, **kwargs): context = super(MyCustomPersonListView, self).get_context_data(**kwargs) context['your_template_variable'] = 'Your new template variable' return context def get_queryset(self): # return super(MyCustomPersonListView, self).get_queryset() return self.model.objects.none() class MyCustomPersonDetailView(PersonDetailView): def get_context_data(self, **kwargs): context = super(MyCustomPersonDetailView, self).get_context_data(**kwargs)
from crudbuilder.views import ViewBuilder from productos.cruds import CategoriaCrud, ProductoCrud from django.db.utils import OperationalError from reportlab.lib import colors from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.enums import TA_CENTER, TA_LEFT from productos.models import Producto from io import BytesIO from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import A4, cm from django.http import HttpResponse from datetime import datetime try: builder = ViewBuilder('productos', 'producto', ProductoCrud) builder.generate_crud() builder = ViewBuilder('productos', 'categoria', CategoriaCrud) builder.generate_crud() except OperationalError: pass # happens when db doesn't exist yet, views.py should be # importable without this side effect def report(request): response = HttpResponse(content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename=reporte_productos.pdf' buffer = BytesIO() c = canvas.Canvas(buffer, pagesize=A4)
def __init__(self, *args, **kwargs): super(UrlBuilder, self).__init__(*args, **kwargs) self.viewbuilder = ViewBuilder(*args, **kwargs) self.urls = self.generate_urls()
class employeeList(APIView): def get(self,request): employees=Employee.objects.all() serializer=employeesSerializers(employees,many=True) return Response(serializer.data) def post(self): pass from django.http import HttpResponseRedirect from crudbuilder.views import ViewBuilder from crudbuilder.helpers import reverse from skills.crud import EmpSkillsCrud builder = ViewBuilder('skills', 'empSkills', EmpSkillsCrud) builder.generate_crud() EmpSkillsListView = builder.classes['EmpSkillsListView'] EmpSkillsCreateView = builder.classes['EmpSkillsCreateView'] EmpSkillsUpdateView = builder.classes['EmpSkillsUpdateView'] EmpSkillsDetailView = builder.classes['EmpSkillsDetailView'] class MyCustomEmpSkillsListView(EmpSkillsListView): def get_context_data(self, **kwargs): context = super(MyCustomEmpSkillsListView, self).get_context_data(**kwargs) context['your_template_variable'] = 'Your new template variable' return context def get_queryset(self):
def setUp(self): self.builder = ViewBuilder('tests', 'testmodel')