Back to index

plone3  3.1.7
content_edit_impl.py
Go to the documentation of this file.
00001 ## Script (Python) "content_edit_impl"
00002 ##title=Edit content
00003 ##bind container=container
00004 ##bind context=context
00005 ##bind namespace=
00006 ##bind script=script
00007 ##bind subpath=traverse_subpath
00008 ##parameters=state, id=''
00009 ##
00010 
00011 from Products.Archetypes import PloneMessageFactory as _
00012 from Products.Archetypes.utils import addStatusMessage
00013 from Products.CMFCore.utils import getToolByName
00014 
00015 REQUEST = context.REQUEST
00016 old_id = context.getId()
00017 
00018 try:
00019     new_context = context.portal_factory.doCreate(context, id)
00020 except AttributeError:
00021     # Fallback for AT + plain CMF where we don't have a portal_factory
00022     new_context = context
00023 new_context.processForm()
00024 
00025 # Get the current language and put it in request/LANGUAGE
00026 form = REQUEST.form
00027 if form.has_key('current_lang'):
00028     form['language'] = form.get('current_lang')
00029 
00030 portal_status_message = _(u'Changes saved.')
00031 
00032 # handle navigation for multi-page edit forms
00033 next = not REQUEST.get('form_next', None) is None
00034 previous = not REQUEST.get('form_previous', None) is None
00035 fieldset = REQUEST.get('fieldset', None)
00036 schemata = new_context.Schemata()
00037 
00038 if next or previous:
00039     s_names = [s for s in schemata.keys() if s != 'metadata']
00040 
00041     if previous:
00042         s_names.reverse()
00043 
00044     next_schemata = None
00045     try:
00046         index = s_names.index(fieldset)
00047     except ValueError:
00048         raise 'Non-existing fieldset: %s' % fieldset
00049     else:
00050         index += 1
00051         if index < len(s_names):
00052             next_schemata = s_names[index]
00053             addStatusMessage(REQUEST, portal_status_message)
00054             return state.set(status='next_schemata',
00055                              context=new_context,
00056                              fieldset=next_schemata)
00057 
00058     if next_schemata != None:
00059         addStatusMessage(REQUEST, portal_status_message)
00060         return state.set(status='next_schemata',
00061                  context=new_context,
00062                  fieldset=next_schemata)
00063     else:
00064         raise 'Unable to find next field set after %s' % fieldset
00065 
00066 env = state.kwargs
00067 reference_source_url = env.get('reference_source_url')
00068 if reference_source_url is not None:
00069     reference_source_url = env['reference_source_url'].pop()
00070     reference_source_field = env['reference_source_field'].pop()
00071     reference_source_fieldset = env['reference_source_fieldset'].pop()
00072     portal = context.portal_url.getPortalObject()
00073     reference_obj = portal.restrictedTraverse(reference_source_url)
00074     portal_status_message = _(u'message_reference_added',
00075                               default=u'Reference added.')
00076 
00077     edited_reference_message = _(u'message_reference_edited',
00078                                  default=u'Reference edited.')
00079 
00080     # Avoid implicitly creating a session if one doesn't exists
00081     session = None
00082     sdm = getToolByName(context, 'session_data_manager', None)
00083     if sdm is not None:
00084         session = sdm.getSessionData(create=0)
00085 
00086     # update session saved data, if session exists.
00087     uid = new_context.UID()
00088     if session is not None:
00089         saved_dic = session.get(reference_obj.getId(), None)
00090         if saved_dic:
00091             saved_value = saved_dic.get(reference_source_field, None)
00092             if same_type(saved_value, []):
00093                 # reference_source_field is a multiValued field, right!?
00094                 if uid in saved_value:
00095                     portal_status_message = edited_reference_message
00096                 else:
00097                     saved_value.append(uid)
00098             else:
00099                 if uid == saved_value:
00100                     portal_status_message = edited_reference_message
00101                 else:
00102                     saved_value = uid
00103             saved_dic[reference_source_field] = saved_value
00104             session.set(reference_obj.getId(), saved_dic)
00105 
00106     kwargs = {
00107         'status':'success_add_reference',
00108         'context':reference_obj,
00109         'fieldset':reference_source_fieldset,
00110         'field':reference_source_field,
00111         'reference_focus':reference_source_field,
00112         }
00113     addStatusMessage(REQUEST, portal_status_message)
00114     return state.set(**kwargs)
00115 
00116 if state.errors:
00117     errors = state.errors
00118     s_items = [(s, schemata[s].keys()) for s in schemata.keys()]
00119     fields = []
00120     for s, f_names in s_items:
00121         for f_name in f_names:
00122             fields.append((s, f_name))
00123     for s_name, f_name in fields:
00124         if errors.has_key(f_name):
00125             REQUEST.set('fieldset', s_name)
00126             addStatusMessage(REQUEST, portal_status_message)
00127             return state.set(
00128                 status='failure',
00129                 context=new_context)
00130 
00131 if not state.errors:
00132     from Products.Archetypes import transaction_note
00133     transaction_note('Edited %s %s at %s' % (new_context.meta_type,
00134                                              new_context.title_or_id(),
00135                                              new_context.absolute_url()))
00136 
00137 addStatusMessage(REQUEST, portal_status_message)
00138 return state.set(status='success',
00139                  context=new_context)