Alive CMS
from django import forms from django.utils.html import escape from django.http import HttpResponse, HttpResponseRedirect from utils.object import Obj __all__ = ['HelloFoo'] class EditFooForm(forms.Form): name = forms.CharField() class HelloFoo(Obj): 'Content type with storage and a simple edit form' is_container = 0 # Set the fields for self.data, self.data is a Storage type # i.e. self.data.name works as well as self.data['name'] # Foo is the default data_spec = {'name': 'Foo'} def view_item(self): # Shorten the request request = self.context.request # The user is always stored on the request user = request.user # Make a link to edit. Usually you only want to show when # the user has 'edit' permissions, # has_permissions() is provided by Obj html = '' if self.has_permissions(user, 'edit_item'): html = '<a href="%s">Edit</><br />' # It's a very good idea to use self.absolute_url to make a link # this way URL mapping will actuallly work html %= self.absolute_url('edit_item') # do an escape on user provided data content = '%s<h2>Hello %s!</h2>' % (html, escape(self.data.name)) msg = 'The %s example:' % self.data.name return {'content': content, 'message': msg} def edit_item(self): request = self.context.request # make cancel go the view view_url = self.absolute_url('view_item') msg = '' if request.method == 'GET': # get the form guts to be rendered, including the initial value # this is a normal Django form form = EditFooForm(initial={'name': self.data.name}) else: form = EditFooForm(request.POST) if form.is_valid(): # if the form is valid, store the data self.data.name = form.cleaned_data['name'] self.save_data() # redirect to the view return HttpResponseRedirect(view_url) else: # Oops, show the message and go to the form again msg = 'There are errrs in the form' return {'form': form, 'message': msg, 'cancel_url' view_url}