| |
from OFS.DTMLDocument import DTMLDocument
from OFS.Folder import Folder
from Globals import HTML, DTMLFile
from OFS.content_types import guess_content_type
from urllib import quote
from DateTime import DateTime
import Comment
import string
import Globals, OFS
import Syntax
class Recipe(Folder, DTMLDocument):
meta_type='Code Recipe'
_properties = (
{'id':'description', 'type':'text', 'mode':'w'},
{'id':'explanation', 'type':'text', 'mode':'w'},
{'id':'code', 'type':'text', 'mode':'w'},
{'id':'title', 'type':'string', 'mode':'w'},
{'id':'category', 'type':'string', 'mode':'w'},
{'id':'rating', 'type':'float', 'mode':'w'},
{'id':'timesrated', 'type':'int', 'mode':'w'},
)
__ac_permissions__ = (
('Change Code Recipe',
('manage','manage_main','manage_editRecipe','add_rating')),
('Add Code Recipe',
('manage_addRecipe','manage_addRecipeForm',
'manage_userAddRecipeForm'))
)
recipeOptions = ({'label':'Edit Recipe', 'action':'manage_editForm'},)
manage_options=(
Folder.manage_options[:1]+
recipeOptions+
DTMLDocument.manage_options[1:2]+
Folder.manage_options[3:]
)
manage_editForm = DTMLFile('dtml/editRecipeForm', globals())
user_editForm = DTMLFile('dtml/userEditRecipeForm', globals())
addComment = DTMLFile('dtml/userAddCommentForm', globals())
index_html = DTMLFile('dtml/indexhtmlRecipe', globals())
def filtered_meta_types(self, user=None):
meta_types = []
meta_types.append(Globals.Dictionary(name=Comment.Comment.meta_type,
action='manage_addProduct/CookBook/addCommentForm'),)
return meta_types
def getOwnership(self):
owner=self.getOwner(1)
return owner[1]
def isOwner(self):
owner = self.getOwnership()
user = self.REQUEST.AUTHENTICATED_USER.getUserName()
if owner == user:
return 1
else:
return 0
def manage_editRecipe(self,code,description,explanation,category='',title='',
redir=None, file='', REQUEST=None):
"""
Replaces a Recipes contents with new information
"""
if type(file) is not type(''): file=file.read()
if not file:
self.code=code
else:
self.code=file
self.title=str(title)
self.category=str(category)
self.description=description
self.explanation=explanation
if REQUEST is not None:
try: u='%s' % (self.DestinationURL(),)
except: u='%s' % (REQUEST['URL1'],)
if redir is None:
u='%s/manage_main' %(u,)
REQUEST.RESPONSE.redirect(u)
return ''
def add_rating(self, rating, REQUEST={}, RESPONSE=None):
"""
Sets a new avg rating level on this recipe
Proper code contributed by Bernie Simon (bsimon at stsci dot edu)
"""
total = self.timesrated * self.rating + rating
self.timesrated = self.timesrated + 1
rating = '%.2f' % (total / self.timesrated)
self.rating = float(rating)
if REQUEST is not None:
try: u='%s' % (self.DestinationURL(),)
except: u='%s' % (REQUEST['URL1'],)
REQUEST.RESPONSE.redirect(u)
return ''
def txt_src(self, REQUEST=None, RESPONSE=None):
"""Return unprocessed code source."""
if RESPONSE is not None:
RESPONSE.setHeader('Content-Type', 'text/plain')
return self.code
def syntax_src(self, REQUEST={}, RESPONSE=None):
"""Returns syntax highlighted source"""
hilighted = Syntax.Parser(self.code)
hilighted.format()
if RESPONSE is not None:
RESPONSE.setHeader('Content-Type', 'text/html')
return hilighted.out
__call__ = index_html
Globals.default__class_init__(Recipe)
manage_addRecipeForm = DTMLFile('dtml/addRecipeForm', globals())
manage_userAddRecipeForm = DTMLFile('dtml/userAddRecipeForm', globals())
def manage_addRecipe(self, description, code, explanation,category='', title='', redir=None,
file='', REQUEST=None, submit=None):
"""Add a Recipe object with the contents of file. If
'file' is empty, then add what was in the Code textarea.
"""
if type(file) is not type(''): file=file.read()
if not file:
code=code
else:
code=file
id=str(int(DateTime()))
ob=Recipe(__name__=id)
ob.id=str(int(DateTime()))
ob.title=str(title)
ob.rating=0.0
ob.timesrated=0
ob.category=str(category)
ob.description=description
ob.explanation=explanation
ob.code=code
id=self._setObject(id, ob)
if REQUEST is not None:
try: u='%s' % (self.DestinationURL(),)
except: u='%s' % (REQUEST['URL1'],)
if redir is None:
u='%s/manage_main' % (u,)
if submit==" Add and Edit ":
if redir is None:
u="%s/%s/manage_main" % (REQUEST['URL1'],quote(id))
else:
u="%s/%s/%s" % (REQUEST['URL1'],quote(id),'user_editForm')
REQUEST.RESPONSE.redirect(u)
return ''
|