On May 20, 6:33 am, Filip Gruszczyński <grusz...@gmail.com> wrote:
>
> I have recently created a class:
>
> class RequestService:
>
>         def __call__(self, request, *args, **kwargs):
>                 self.prepare(request, *args, **kwargs)
>                 if request.method == 'POST':
>                         value = self.servePost(request, *args, **kwargs)
>                         if value is not None:
>                                 return value
>                 else:
>                         value = self.serveGet(request, *args, **kwargs)
>                         if value is not None:
>                                 return value
>                 return self.serveBoth(request, *args, **kwargs)
>
> When I need another view, I inherit from it and create an instance,
> that is used instead of a function. I decided to use it, because my
> views functions began to grow big and I wanted to divide them into
> smaller chunks, and yet keep them logically connected.
>

I don't have major beefs with your solution, but I'll offer a few
alternatives that might accomplish the same thing, without creating
inheritance.  Inheritance feels a little heavy for what you're trying
to provide, which is basically just a common method to tie together
three other methods.  Seems like it might be more pythonic to do
simple duck typing, but that's just my two cents of course.

1) Just pass in the three functions--this gives you the ultimate
flexibility to reuse methods between views.

def request_service(request, serve_post, serve_get, serve_both, *args,
**kwargs)

def blog_view(request,...):
    return request_service(request, serve_blog_post, serve_blog_get,
serve_blog_both, ..)

OR

2) Replace inheritance with delegation.

def request_service(request, service_provider, *args, **kwargs)

class BlogServiceProvider:
    def serve_post(..)
    def serve_get(...)
    def serve_post(..)

def blog_view(request,...):
    return request_service(request, BlogServiceProvider(), ..)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to