Changeset 1177

Show
Ignore:
Timestamp:
07/25/08 05:13:06
Author:
piv
Message:

refactored acton deletion, some minor fixes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • qPloneTabs/branches/quintagroup.plonetabs/trunk/quintagroup/plonetabs/browser/kssactions.zcml

    r1087 r1177  
    2929    <browser:page 
    3030        for="plone.app.kss.interfaces.IPortalObject" 
    31         attribute="deleteAction" 
     31        attribute="kss_deleteAction" 
    3232        class=".plonetabs.PloneTabsControlPanel" 
    3333        name="plonetabs-deleteAction" 
  • qPloneTabs/branches/quintagroup.plonetabs/trunk/quintagroup/plonetabs/browser/plonetabs.py

    r1174 r1177  
    3030from interfaces import IPloneTabsControlPanel 
    3131 
    32 ACTION_ATTRS = ["id", "visible", "url_expr", "title", "available_expr"] 
     32ACTION_ATTRS = ["id", "title", "url_expr", "available_expr", "visible"] 
    3333 
    3434class PloneTabsControlPanel(PloneKSSView): 
     
    115115     
    116116    def manage_addAction(self, form, errs): 
    117         """ Add a new action to given category, if category doesn't exist, create it """ 
     117        """ Manage method to add a new action to given category, 
     118            if category doesn't exist, create it """ 
    118119        cat_name = form['category'] 
    119120         
    120         # before checking for existence category we will validate all input data 
    121         # and issue errors if needed 
    122         errors = self.validateActionFields(form) 
     121        # validate posted data 
     122        errors = self.validateActionFields(cat_name, form) 
    123123         
    124124        # if not errors find (or create) category and set action to it 
     
    134134     
    135135    def manage_editAction(self, form, errs): 
    136         """ Process after edit form post """ 
    137         portal_actions = getToolByName(self.context, "portal_actions") 
    138          
    139         # extract data from request's form 
    140         orig_id = form.get("orig_id") 
    141         new_id = form.get("id_%s" % orig_id) 
    142         category = form.get("category") 
    143          
    144         # before updating action we validate action id/category 
    145         if category not in portal_actions.objectIds(): 
    146             IStatusMessage(self.request).addStatusMessage(_(u"Unexistent root portal actions category '%s'" % category), type="error") 
    147         else: 
    148             cat_container = portal_actions[category] 
    149             if orig_id not in map(lambda x: x.id, filter(lambda x: IAction.providedBy(x), cat_container.objectValues())): 
    150                 IStatusMessage(self.request).addStatusMessage(_(u"'%s' action does not exist in '%s' category" % (orig_id, category)), type="error") 
    151             else: 
    152                 # validate input 
    153                 # id is required 
    154                 if not new_id: 
    155                     errors["id_%s" % orig_id] = _(u"Id field is required") 
    156                  
    157                 # title is required 
    158                 if not form.get("title_%s" % orig_id): 
    159                     errors["title_%s" % orig_id] = _(u"Title field is required") 
    160                  
    161                 # check action for valid tal expression 
    162                 try: 
    163                     Expression(form.get("url_expr_%s" % orig_id)) 
    164                 except Exception, e: 
    165                     errors["url_expr_%s" % orig_id] = "%s" % str(e) 
    166                  
    167                 # check condition for valid tal expression 
    168                 try: 
    169                     Expression(form.get("available_expr_%s" % orig_id)) 
    170                 except Exception, e: 
    171                     errors["available_expr_%s" % orig_id] = "%s" % str(e) 
    172                  
    173                 # check visible for integer 
    174                 try: 
    175                     int(form.get("visible_%s" % orig_id, '0')) 
    176                 except Exception, e: 
    177                     errors["visible_%s" % orig_id] = "%s" % str(e) 
    178                  
    179                 # rename action if needed 
    180                 if not errors: 
    181                     if new_id != orig_id: 
    182                         try: 
    183                             cat_container.manage_renameObject(orig_id, new_id) 
    184                         except CopyError, e: 
    185                             errors["id_%s" % orig_id] = _(u"Invalid id") 
    186                         except: 
    187                             exctype, v = sys.exc_info()[:2] 
    188                             errors["id_%s" % orig_id] = "%s" % str(v) 
    189                  
    190                 if not errors: 
    191                     action = cat_container[new_id] 
    192                      
    193                     for prop in ACTION_ATTRS: 
    194                         if prop != "id": 
    195                             if prop == "visible": 
    196                                 value = int(form.get("%s_%s" % (prop, orig_id), '0')) 
    197                             else: 
    198                                 value = form.get("%s_%s" % (prop, orig_id)) 
    199                             action._setPropValue(prop, value) 
    200                      
    201                     IStatusMessage(self.request).addStatusMessage(_(u"'%s' action saved." % new_id), type="info") 
    202                     self.redirect(search="category=%s" % category) 
    203                      
    204                     return False 
    205          
    206         IStatusMessage(self.request).addStatusMessage(_(u"Please correct the indicated errors."), type="error") 
    207         return True 
    208      
    209     def manage_deleteAction(self, form, errors): 
    210         """ Delete action with given id/category """ 
    211         portal_actions = getToolByName(self.context, "portal_actions") 
    212  
    213         id = form.get("orig_id", "") 
    214         category = form.get("category", "") 
    215          
    216         if category not in portal_actions.objectIds(): 
    217             IStatusMessage(self.request).addStatusMessage(_(u"Unexistent root portal actions category '%s'" % category), type="error") 
    218         else: 
    219             cat_container = portal_actions[category] 
    220             if id not in map(lambda x: x.id, filter(lambda x: IAction.providedBy(x), cat_container.objectValues())): 
    221                 IStatusMessage(self.request).addStatusMessage(_(u"'%s' action does not exist in '%s' category" % (id, category)), type="error") 
    222             else: 
    223                 cat_container.manage_delObjects(ids=[id,]) 
    224                 IStatusMessage(self.request).addStatusMessage(_(u"'%s' action in '%s' category successfully deleted" % (id, category)), type="info") 
    225                  
    226                 # redirect to form after successfull deletion 
    227                 self.redirect(search="category=%s" % category) 
    228                 return False 
    229          
    230         # return form with errors 
    231         return True 
    232  
     136        """ Manage Method to update action """ 
     137        # extract posted data 
     138        id, cat_name, data = self.parseForm(form) 
     139         
     140        # get category and action to edit 
     141        category = self.getActionCategory(cat_name) 
     142        action = category[id] 
     143         
     144        # validate posted data 
     145        errors = self.validateActionFields(cat_name, data, allow_dup=True) 
     146         
     147        if not errors: 
     148            action = self.updateAction(id, cat_name, data) 
     149            IStatusMessage(self.request).addStatusMessage(_(u"'%s' action saved." % action.id), type="info") 
     150            self.redirect(search="category=%s" % cat_name) 
     151            return False 
     152        else: 
     153            errs.update(self.processErrors(errors, sufix='_%s' % id)) # add edit form sufix to error ids 
     154            IStatusMessage(self.request).addStatusMessage(_(u"Please correct the indicated errors."), type="error") 
     155            return True 
     156     
     157    def manage_deleteAction(self, form, errs): 
     158        """ Manage Method to delete action """ 
     159        # extract posted data 
     160        id, cat_name, data = self.parseForm(form) 
     161         
     162        # get category and action to delete 
     163        category = self.getActionCategory(cat_name) 
     164        if id in category.objectIds(): 
     165            self.deleteAction(id, cat_name) 
     166            IStatusMessage(self.request).addStatusMessage(_(u"'%s' action in '%s' category deleted." % (id, cat_name)), type="info") 
     167            self.redirect(search="category=%s" % cat_name) 
     168            return False 
     169        else: 
     170            IStatusMessage(self.request).addStatusMessage(_(u"No '%s' action in '%s' category." % (id, cat_name)), type="error") 
     171            return True 
    233172     
    234173    def manage_moveUpAction(self, form, errors): 
     
    295234        self.request.response.redirect("%s%s%s" % (url, search, hash)) 
    296235     
    297      
    298236    ################################### 
    299     ##  Methods - providers for templates 
     237    # 
     238    #  Methods - providers for templates 
     239    # 
    300240    ################################### 
    301241     
     
    419359        return portal_actions.objectIds() 
    420360     
     361    # 
     362    # Methods to make this class looks like global sections viewlet 
     363    # 
     364     
    421365    def test(self, condition, ifTrue, ifFalse): 
    422366        """ See interface """ 
     
    444388     
    445389    ########################## 
    446     # kss server actions 
     390    # 
     391    # KSS Server Actions 
     392    # 
    447393    ########################## 
    448394     
     
    531477     
    532478    @kssaction 
    533     def deleteAction(self, id, category): 
     479    def kss_deleteAction(self, id, category): 
    534480        """ Delete portal action with given id & category """ 
    535481        portal_actions = getToolByName(self.context, "portal_actions") 
     
    584530        ksscore.replaceHTML(ksscore.getHtmlIdSelector(replace_id), content) 
    585531     
     532    # 
    586533    # Utility Methods 
     534    # 
    587535     
    588536    def copyAction(self, action): 
     
    593541        return action_info 
    594542     
    595      
    596      
    597         if not id: 
    598             errors["id"] = _(u"Id field is required") 
    599         if not form.get("title"): 
    600             errors["title"] = _(u"Title field is required") 
    601  
    602         action = Action(id) 
    603         for prop in ACTION_ATTRS: 
    604             if prop != "id": 
    605                 try: 
    606                     if prop == "visible": 
    607                         value = int(form.get(prop, '0')) 
    608                     else: 
    609                         value = form.get(prop) 
    610                      
    611                     action._setPropValue(prop, value) 
    612                 except Exception, e: 
    613                     errors[prop] = "%s" % str(e) 
    614      
    615     def validateActionFields(self, data): 
     543    def validateActionFields(self, cat_name, data, allow_dup=False): 
    616544        """ Check action fields on validity """ 
    617545        errors = {} 
    618546         
    619         # get or create (if necessary) actions category 
    620         cat_name = data['category'] 
    621         category = self.getOrCreateCategory(cat_name) 
     547        if allow_dup: 
     548            category = ActionCategory(cat_name)           # create dummy category to avoid id duplication during action update 
     549        else: 
     550            category = self.getOrCreateCategory(cat_name) # get or create (if necessary) actions category 
    622551         
    623552        # validate action id 
     
    630559        # validate action name 
    631560        if not data['title'].strip(): 
    632             errors['title'] = 'Empty or invalid id specified' 
     561            errors['title'] = 'Empty or invalid title specified' 
    633562         
    634563        # validate condition expression 
     
    648577        return errors 
    649578     
     579    def processErrors(self, errors, prefix='', sufix=''): 
     580        """ Add prefixes, sufixes to error ids  
     581            This is necessary during edit form validation, 
     582            because every edit form on the page has it's own sufix (id) """ 
     583        if not (prefix or sufix): 
     584            return errors 
     585         
     586        result = {} 
     587        for key, value in errors.items(): 
     588            result['%s%s%s' % (prefix, key, sufix)] = value 
     589         
     590        return result 
     591     
     592    def parseForm(self, form): 
     593        """ Extract all needed fields """ 
     594        # get original id and category 
     595        info = {} 
     596        id = form['orig_id'] 
     597        category = form['category'] 
     598         
     599        # preprocess 'visible' field (checkbox needs special checking) 
     600        if form.has_key('visible_%s' % id): 
     601            form['visible_%s' % id] = True 
     602        else: 
     603            form['visible_%s' % id] = False 
     604         
     605        # collect all action fields 
     606        for attr in ACTION_ATTRS: 
     607            info[attr] = form['%s_%s' % (attr, id)] 
     608         
     609        return (id, category, info) 
     610     
     611    def getActionCategory(self, name): 
     612        portal_actions = getToolByName(self.context, 'portal_actions') 
     613        return portal_actions[name] 
     614     
    650615    def getOrCreateCategory(self, name): 
     616        """ Get or create (if necessary) category """ 
    651617        portal_actions = getToolByName(self.context, 'portal_actions') 
    652618        if name not in map(lambda x: x.id, filter(lambda x: IActionCategory.providedBy(x), portal_actions.objectValues())): 
    653619            portal_actions._setObject(name, ActionCategory(name)) 
    654             category = portal_actions[name] 
    655         else: 
    656             category = portal_actions[name] 
    657         return category 
     620        return self.getActionCategory(name) 
    658621     
    659622    def addAction(self, cat_name, data): 
    660623        """ Create and add new action to category with given name """ 
    661          
    662         # get or create category if it's necessary 
    663624        id = data.pop('id') 
    664625        category = self.getOrCreateCategory(cat_name) 
    665          
    666         # add action 
    667626        action = Action(id, **data) 
    668627        category._setObject(id, action) 
    669628        return action 
    670629     
     630    def updateAction(self, id, cat_name, data): 
     631        """ Update action with given id and category """ 
     632        new_id = data.pop('id') 
     633        category = self.getActionCategory(cat_name) 
     634         
     635        # rename action if necessary 
     636        if id != new_id: 
     637            category.manage_renameObject(id, new_id) 
     638         
     639        # get action 
     640        action = category[new_id] 
     641         
     642        # update action properties 
     643        for attr in ACTION_ATTRS: 
     644            if data.has_key(attr): 
     645                action._setPropValue(attr, data[attr]) 
     646         
     647        return action 
     648     
     649    def deleteAction(self, id, cat_name): 
     650        """ Delete action with given id from given category """ 
     651        category = self.getActionCategory(cat_name) 
     652        category.manage_delObjects(ids=[id,]) 
     653        return True 
     654     
     655    # 
    671656    # KSS Methods that are used to update different parts of the page 
    672657    # according to category 
     658    # 
    673659     
    674660    def updatePage(self, category): 
     
    705691 
    706692 
    707  
    708  
    709  
    710  
    711  
    712  
    713  
    714  
    715  
  • qPloneTabs/branches/quintagroup.plonetabs/trunk/quintagroup/plonetabs/browser/templates/actionslist.pt

    r1174 r1177  
    3535      <input type="checkbox" class="visibility" value="1" name="visible" title="visibility" 
    3636             tal:define="name string:${attrs/name}_${id}; 
    37                          submitted request/form.submitted|nothing; 
    38                          checked python:test(submitted, test(request.get(name, []) != [], 'checked', None), test(visible, 'checked', None))" 
    39              tal:attributes="checked checked; 
    40                              name name" /> 
     37                         submitted python:test(request.get('form.submitted','') and request.get('orig_id','')==id, True, False)" 
     38             tal:attributes="name name; 
     39                             checked python:test(submitted, test(request.form.get(name, []) != [], 'checked', None), test(visible, 'checked', None))"/> 
    4140    </div> 
    4241