oscarmlage / django-cruds-adminlte

django-cruds is simple drop-in django app that creates CRUD for faster prototyping
BSD 3-Clause "New" or "Revised" License
425 stars 82 forks source link

New crud system #9

Closed luisza closed 7 years ago

luisza commented 7 years ago

Hi, I create new CRUD system that works like your CRUD but with different approach.

Sorry if this PR change a lot of things I try to maintain all your features without change but including a lot of new features like permission system, namespace based urls, decorator per view system, UserCRUDView for saving user in model field user, possibility to overwrite generic view easy.

I also update urls function adding new parameters

crud_for_model(model, urlprefix=None, namespace=None,
               login_required=False, check_perms=False,
               add_form=None,
               update_form=None)
crud_for_app(app_label, urlprefix=None, namespace=None,
             login_required=False, check_perms=False, modelforms={}) 

CRUDView is a generic way to provide create, list, detail, update, delete views in one class, you can inherit for it and manage login_required, model perms, pagination, update and add forms how to use:

In views

from testapp.models import Customer
from cruds_adminlte.crud import CRUDView
class Myclass(CRUDView):
    model = Customer

In urls.py

myview = Myclass()
urlpatterns = [
    url('path', include(myview.get_urls()))  # also support namespace 
]

The default behavior is check_login = True and check_perms=True but you can turn off with

from testapp.models import Customer
from cruds_adminlte.crud import CRUDView

class Myclass(CRUDView):
    model = Customer
    check_login = False
    check_perms = False

You also can defined extra perms with

class Myclass(CRUDView):
    model = Customer
    perms = { 'create': ['applabel.mycustom_perm'],
              'list': [],
              'delete': [],
              'update': [],
              'detail': []
            }

If checkperms = True we will add default django model perms (.[add|change|delete|view])

You can also overwrite add and update forms

class Myclass(CRUDView):
    model = Customer
    add_form = MyFormClass
    update_form = MyFormClass  

And of course overwrite base template name

class Myclass(CRUDView):
    model = Customer
    template_name_base = "mybase"   

Remember basename is generated like app_label/modelname if template_name_base is set as None and 'cruds' by default so template loader search this structure

basename + '/create.html' basename + '/detail.html' basename + '/update.html' basename + '/list.html' basename + '/delete.html'

Using namespace

In views

from testapp.models import Customer
from cruds_adminlte.crud import CRUDView
class Myclass(CRUDView):
    model = Customer
    namespace = "mynamespace"

In urls.py

myview = Myclass()
urlpatterns = [
    url('path', include(myview.get_urls(), namespace="mynamespace"))  
]

To see in action watch a demo brach in my fork. And sorry there are a lot of test that needs to be done.

oscarmlage commented 7 years ago

Hey @luisza

First of all, thank you for the PR and for your time. It's nice to find someone proposing and implementing solutions.

I've read the patch and it looks ok to me, but I've a couple of comments/questions:

captura de pantalla 2017-04-10 a las 11 03 49

I think this PR would be perfectly accepted if we're able to fix first two points. What do you think?.

Thanks again for all the efforts!

oscarmlage commented 7 years ago

Ok, got it. The form is ok, in the demo app you included both examples, with namespace (no modelforms) and with modelforms (no namespace):

ns = crud_for_app('testapp', check_perms=True, namespace="ns")
urlpatterns += crud_for_app('testapp', check_perms=True, modelforms=customerforms)

So that point is ok with me.

luisza commented 7 years ago

Is crud.py now doing all the job instead of views.py or does we need views.py for something?. I mean, imho it's better to remove or merge them in only one.

Yes, I think curd.py do all the job, I got all I need of views.py (CRUDMixin) and I think you can remove safely views.py code.

it gave me a 403 when I was not logged

Yea, but you will redirect to login view if set login_required=True, this is because 403 is an error when you haven't permissions to access a view (as django admin permission system).

urlpatterns += crud_for_app('testapp',  login_required=True,
                        check_perms=True, modelforms=customerforms)

My mind thinks in a crud system for users, groups and perms too but probably it's too much.

I don't understand what you want, do you want crud for users and groups, something like

urlpatterns += crud_for_app('auth')

I also update demo branch.

oscarmlage commented 7 years ago
urlpatterns += crud_for_app('auth')

That was just what I was looking for. Really nice. Do you want to be a collaborator and write changes directly in this repository? I'm sure it would be faster.

oscarmlage commented 7 years ago

@luisza OTOH - if it's ok with you - would be nice to keep in touch via email or similar to talk about the goals of the project, features, priorities...

luisza commented 7 years ago

Do you want to be a collaborator and write changes directly in this repository?

Yes, and No, thats because I think PR are good approach, and provide second review.

Would be nice to keep in touch via email or similar to talk about the goals of the project, features, priorities.

Yea, I will send you a private email.