class RenameFormAdapter(object):
    interface.implements(IRenameForm)
    adapts(IBaseObject)

    def __init__(self, context):
        self.context = context
        self.languages = getToolByName(context, 'portal_languages').getSupportedLanguages()
        self.handler = IMultilanguageURLHandler(aq_parent(aq_inner(context)), None)

    def __setattr__(self, name, value):
        if name in ('languages', 'context', 'handler',):
            object.__setattr__(self, name, value)
        elif name == 'id':
            self.context.setId(value)
        elif name == 'title':
            self.context.setTitle(value)
        elif name in self.languages:
            self.handler.set_translated_id(self.id, value, name)
        else:
            object.__setattr__(self, name, value)

    def __getattr__(self, name):
        if name in ('languages', 'context', 'handler',):
            return object.__getattr__(self, name)
        if name == 'id':
            return self.context.getId()
        if name == 'title':
            return self.context.Title()
        if name in self.languages:
            return self.handler.get_translated_id(self.id, name)
        return object.__getattr__(self, name)
class MultilanguageTraverse(object):
    implements(IPublishTraverse)
    adapts(IAnnotatable, IHTTPRequest)

    def __init__(self,context,request):
        self.context = context
        self.request = request
        self.default_traverse = DefaultPublishTraverse(context, request)
        self.handler = IMultilanguageURLHandler(context, None)

    def publishTraverse(self, request, name):
        if self.handler is not None:
            ob = self.handler.get_object(name)
            if ob is not None:
                langs = self.handler.get_langs(name)
                if len(langs) == 1:
                    try:
                        portal_languages = getToolByName(self.context, 'portal_languages')
                        portal_languages.REQUEST.set_lazy('set_language', lambda: langs[0])
                        portal_languages.setLanguageBindings()
                    except:
                        pass
                return ob
        try:
            return self.default_traverse.publishTraverse(request, name)
        except NotFound:
            pass
        except KeyError:
            pass
        except AttributeError:
            pass
        raise NotFound(self.context, name, self.request)
 def __init__(self, context, request):
     super(RenameForm, self).__init__(context, request)
     for name, value in self.request.form.items():
         self.request.form[name] = safe_unicode(value)
     self.form_fields = FormFields(IRenameForm)
     self.form_name = '%s (%s)' % (self.context.Title(), self.context.getId())
     mtool = getToolByName(self.context, 'portal_membership')
     title = self.context.Schema()['title']
     self.id = self.context.getId()
     self.languages = {}
     if (IMultilanguageField.providedBy(title) or
         not mtool.checkPermission(DeleteObjects, self.context) or
         not mtool.checkPermission(copy_or_move, self.context)):
         self.form_fields = self.form_fields.omit('id')
     if (IMultilanguageField.providedBy(title) or
         not mtool.checkPermission(ModifyPortalContent, self.context)):
         self.form_fields = self.form_fields.omit('title')
     if IMultilanguageField.providedBy(title):
         self.handler = IMultilanguageURLHandler(aq_parent(aq_inner(self.context)), None)
         for lang in title.getAvailableLanguages(self.context):
             self.languages[lang['name']] = lang['title']
             self.form_fields = self.form_fields + FormFields(
                 schema.TextLine(
                     __name__=lang['name'],
                     title=lang['title'],
                     default=self.handler.get_translated_id(self.id, lang['name']),
                     required=True
                 )
             )
             self.form_fields[lang['name']].interface = IRenameForm
def update_translated_ids(obj, event):
    if IBaseObject.providedBy(obj) and obj.isTemporary():
        return
    handler = IMultilanguageURLHandler(event.newParent, None)
    old_handler = IMultilanguageURLHandler(event.oldParent, None)
    if handler is not None and old_handler is not None:
        for lang, id in old_handler.get_translated_ids(event.oldName):
            handler.set_translated_id(event.newName, id, lang)
    if not event.newParent is event.oldParent and old_handler is not None:
        old_handler.remove_translated_ids(event.oldName, False)
class RenameForm(EditForm):

    label = _p(u'heading_rename_item', default=u'Rename item')
    description = _p(u'description_rename_item',
                    default=u'Each item has a Short Name and a Title, which you can change '
                             'by entering the new details below.')

    def __init__(self, context, request):
        super(RenameForm, self).__init__(context, request)
        for name, value in self.request.form.items():
            self.request.form[name] = safe_unicode(value)
        self.form_fields = FormFields(IRenameForm)
        self.form_name = '%s (%s)' % (self.context.Title(), self.context.getId())
        mtool = getToolByName(self.context, 'portal_membership')
        title = self.context.Schema()['title']
        self.id = self.context.getId()
        self.languages = {}
        if (IMultilanguageField.providedBy(title) or
            not mtool.checkPermission(DeleteObjects, self.context) or
            not mtool.checkPermission(copy_or_move, self.context)):
            self.form_fields = self.form_fields.omit('id')
        if (IMultilanguageField.providedBy(title) or
            not mtool.checkPermission(ModifyPortalContent, self.context)):
            self.form_fields = self.form_fields.omit('title')
        if IMultilanguageField.providedBy(title):
            self.handler = IMultilanguageURLHandler(aq_parent(aq_inner(self.context)), None)
            for lang in title.getAvailableLanguages(self.context):
                self.languages[lang['name']] = lang['title']
                self.form_fields = self.form_fields + FormFields(
                    schema.TextLine(
                        __name__=lang['name'],
                        title=lang['title'],
                        default=self.handler.get_translated_id(self.id, lang['name']),
                        required=True
                    )
                )
                self.form_fields[lang['name']].interface = IRenameForm

    def validate(self, action, data):
        errors = super(RenameForm, self).validate(action, data)
        for field in self.form_fields:
            name = field.field.getName()
            if name == 'title':
                continue
            value = data.get(name, u'')
            check_id = getattr(self.context, 'check_id', None)
            if ((name == 'id' and not value == self.id) or
                (name in self.languages.keys() and not value == self.handler.get_translated_id(self.id, name) and
                 not value == self.id and not self.id == self.handler.get_actual_id(value)) and
                check_id is not None):
                error = check_id(str(value), required=1)
                if error:
                    widget = self.widgets[name]
                    widget._error = WidgetInputError(widget.name, widget.label, InvalidIDError(error))
                    errors.append(widget._error)
        return errors

    @action(_p(u'label_rename_all', default=u'Rename All'), name=u'rename')
    def handle_rename_action(self, action, data):
        context = aq_inner(self.context)
        oldid = context.getId()
        oldtranslatedids = self.handler.get_translated_ids(oldid)
        descriptions = applyData(context, self.form_fields, data, self.adapters)
        statusmessage = IStatusMessage(self.request)
        if descriptions:
            statusmessage.addStatusMessage(_p(u'Changes saved.'), u'success');
        else:
            statusmessage.addStatusMessage(_p(u'No changes made.'));
        self.request.RESPONSE.redirect(context.absolute_url())
 def __init__(self, context):
     self.context = context
     self.languages = getToolByName(context, 'portal_languages').getSupportedLanguages()
     self.handler = IMultilanguageURLHandler(aq_parent(aq_inner(context)), None)
 def __init__(self,context,request):
     self.context = context
     self.request = request
     self.default_traverse = DefaultPublishTraverse(context, request)
     self.handler = IMultilanguageURLHandler(context, None)