Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Last active December 24, 2015 01:19
Show Gist options
  • Save inklesspen/6723256 to your computer and use it in GitHub Desktop.
Save inklesspen/6723256 to your computer and use it in GitHub Desktop.
# Baseview has crud methods set up to do crud, based on some class attributes which are intended
# to be overridden by subclasses. Needless to say I have the full set of CRUD, and they actually
# do stuff.
class BaseView(object):
def __init__(self, request):
self.request = request
def create(self):
return "New object"
def retrieve(self):
return "Existing object"
# make_view_class is a function which constructs a subclass of BaseView, setting class attributes
# such as the model type. it also takes two route names as arguments; the first route name is for
# the collection as a whole, while the second is for the instances. In this example,
# 'posts' maps to '/posts', while 'post' maps to '/posts/{post_id'.
# It will use these route names to set up view configs
PostView = make_view_class(models.Post, 'posts', 'post')
# the above one-liner is equivalent to:
@view_config(route_name='posts', attr='create', request_method='POST')
@view_config(route_name='post', attr='retrieve', request_method='GET')
class PostView(BaseView):
model_type = models.Post
# where I find myself not sure how best to proceed is if I want to augment the subclass.
@view_config(route_name='comment', attr='mark_as_spam', request_method='POST')
class CommentsView(make_view_class(models.Comment, 'comments', 'comment')):
def mark_as_spam(self):
return "Now it's spam"
# since the create and retrieve view configs will be defined on the unnnamed subclass
# which CommentsView then subclasses, even if I use @venusian.lift there will then be
# two classes with the same view configuration.
# My current best idea is to split out the view_config from make_view_class into its
# own decorator, so it would look like this:
@view_config(route_name='comment', attr='mark_as_spam', request_method='POST')
@crud_view_config('comments', 'comment')
class CommentsView(make_view_class(models.Comment)):
def mark_as_spam(self):
return "Now it's spam"
# But that has an extra line in there. Any ideas?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment