from Products.CMFCore.utils import getToolByName

def renameRichDocument(portal):
    portal_types = getToolByName(portal, 'portal_types')
    rich_document_fti = getattr(portal_types, 'RichDocument')
    rich_document_fti.title = "Page"

def disableDocument(portal):
    portal_types = getToolByName(portal, 'portal_types')
    document_fti = getattr(portal_types, 'Document')
    document_fti.global_allow = False

def addDocuments(portal):
    """
    Create Documents Folder.
    """
    folder_id = 'dokumentai'

    if folder_id not in portal.objectIds():
        portal.invokeFactory('Folder', folder_id, title = "Dokumentai", description = "Bendri mokyklos dokumentai")
    folder = portal._getOb(folder_id)
    folder.setConstrainTypesMode(1)
    folder.setLocallyAllowedTypes(["Folder", "RichDocument", "File"])
    folder.reindexObject() 

def changeName(portal):
    if 'forumai' in portal.objectIds():
        forum = portal._getOb('forumai')
        forum.setTitle('Geri forumai')
    #if 'news' in portal.objectsIds():
    #    news = portal._getOb('news')
    #    news.setTitle('Naujienos')
    news = portal._getOb('news')
    news.setTitle('Naujienos')
    news.setDescription('Mokyklos gyvenimo naujienos')
    news.aggregator.setTitle('Naujienos')
    news.aggregator.setDescription('Mokyklos gyvenimo naujienos')
    
    # Events
    events = portal._getOb('events')
    events.setTitle('Įvykiai')
    events.setDescription('Mokyklos įvykiai')
    events.aggregator.setTitle('Įvykiai')
    events.aggregator.setDescription('Mokyklos įvykiai.')
    events.aggregator.previous.setTitle('Praeities įvykiai')
    events.aggregator.previous.setDescription('Įvykiai kurie jau įvyko.')

def reindexObject(self, object, idxs=[], update_metadata=1, uid=None):
        """Update catalog after object data has changed.

        The optional idxs argument is a list of specific indexes
        to update (all of them by default).

        The update_metadata flag controls whether the object's
        metadata record is updated as well.

        If a non-None uid is passed, it will be used as the catalog uid
        for the object instead of its physical path.
        """
        if uid is None:
            uid = self.__url(object)
        if idxs != []:
            # Filter out invalid indexes.
            valid_indexes = self._catalog.indexes.keys()
            idxs = [i for i in idxs if i in valid_indexes]
        self.catalog_object(object, uid, idxs, update_metadata)

def importVarious(context):
    """Miscellanous steps import handle"""
    portal = context.getSite()
    disableDocument(portal)
    renameRichDocument(portal)
    addDocuments(portal)
    changeName(portal)
    
