Example #1
0
def index_site(request, autocommit=True):
    """Index all site contents in internal catalog"""
    application = site_factory(request)
    if application is not None:
        try:
            set_local_registry(application.getSiteManager())
            catalog = get_utility(ICatalog)
            catalog.reset()
            transaction.savepoint()
            intids = get_utility(IIntIds)
            for index, document in enumerate(find_objects_providing(application, Interface)):
                if INoAutoIndex.providedBy(document):
                    continue
                if IBroken.providedBy(document):
                    print("Skipping broken object: {0!r}".format(document))
                else:
                    print("Indexing: {0!r}".format(document))
                    catalog.reindex_doc(intids.register(document), document)
                    if not index % 100:
                        transaction.savepoint()
        finally:
            set_local_registry(None)
        if autocommit:
            transaction.commit()
    return application
Example #2
0
 def get_oid(self):
     """Get new document OID"""
     container = get_utility(IDocumentContainer)
     intids = get_utility(IIntIds)
     oid = '{}{}'.format(container.oid_prefix or '',
                         hex(intids.register(self))[2:])
     if container.get_document(oid) is not None:
         raise DocumentContainerError("Specified OID already exists!")
     self.oid = oid
Example #3
0
def get_connection_from_settings(settings=None):
    """Load connection matching registry settings"""
    if settings is None:
        settings = get_utility(ISettings)
    for name, uri in get_uris(settings):
        zdb = db_from_uri(uri, name, {})
        return zdb.open()
Example #4
0
def evolve(site):
    """Evolve 1: remove all images thumbnails to free blobs"""
    registry = get_local_registry()
    try:
        medias = set()
        set_local_registry(site.getSiteManager())
        LOGGER.warning(
            "Removing all thumbnails from database to free unused blobs...")
        intids = get_utility(IIntIds)
        for ref in list(intids.refs.keys()):
            obj = intids.queryObject(ref)
            if IMediaFile.providedBy(obj):
                LOGGER.debug(
                    ">>> removing thumbnails for image {!r}".format(obj))
                thumbnails = IThumbnails(obj, None)
                if thumbnails is not None:
                    medias.add(obj)
                    thumbnails.clear_thumbnails()
        LOGGER.warning(
            "Thumbnails cleanup is finished. Launch *zeopack* (for ZEO storage) "
            "or *zodbpack* (for Relstorage) command to remove all unused blobs."
        )
        LOGGER.warning("{} images updated".format(len(medias)))
    finally:
        set_local_registry(registry)
Example #5
0
 def get_document(oid, version=None, status=None):
     """Get existing document from it's OID"""
     if not oid:
         return None
     catalog = get_utility(ICatalog)
     params = Eq(catalog['zfile_oid'], oid)
     # check for version or state
     index = None
     if version:  # check for version number or status
         try:
             index = int(version)  # version number
             status = None
         except ValueError:  # status string
             index = None
             status = version
     if index:
         params = and_(params, Eq(catalog['workflow_version'], index))
     elif status:
         params = and_(params, Eq(catalog['workflow_state'], status))
     for result in ResultSet(
             CatalogQuery(catalog).query(params,
                                         sort_index='workflow_version',
                                         reverse=True)):
         if version or status:
             return result
         return IWorkflowVersions(result).get_last_versions()[0]
     return None
Example #6
0
 def traverse(self, name, furtherpath=None):  # pylint: disable=unused-argument
     """Profile traverser"""
     if not name:
         return IPublicProfile(self.request.principal)
     intids = get_utility(IIntIds)
     profile = intids.queryObject(int(name))
     return IPublicProfile(profile, None)
Example #7
0
 def run(self, report, **kwargs):  # pylint: disable=unused-argument
     """Run SQL query task"""
     try:
         session = get_user_session(self.session_name, join=False, twophase=False,
                                    use_zope_extension=False)
         try:
             report.write('SQL query output\n'
                          '================\n')
             report.write('SQL query: \n    {}\n\n'.format(
                 self.query.replace('\r', '').replace('\n', '\n    ')))
             results = session.execute(self.query)
             session.commit()
             converter = get_utility(IAlchemyConverter, name=self.output_format)
             result = converter.convert(results)
             report.write('SQL output ({} records):\n\n'.format(results.rowcount))
             report.write(result)
             return TASK_STATUS_OK, result
         except ResourceClosedError:
             report.write('SQL query returned no result.\n')
             return TASK_STATUS_EMPTY, None
     except SQLAlchemyError:
         session.rollback()
         etype, value, tb = sys.exc_info()  # pylint: disable=invalid-name
         report.write('\n\n'
                      'An SQL error occurred\n'
                      '=====================\n')
         report.write(''.join(traceback.format_exception(etype, value, tb)))
         return TASK_STATUS_ERROR, None
Example #8
0
def find_documents(request):
    """Find documents matching specified properties"""
    properties = request.params.copy(
    ) if TEST_MODE else request.validated.copy()
    fields = properties.pop('fields', None)
    container = get_utility(IDocumentContainer)
    return list(
        map(lambda x: x.to_json(fields), container.find_documents(properties)))
Example #9
0
 def public_profile_callback(profile):
     """Public profile creation callback"""
     request = get_current_request()
     if request is not None:
         root = request.root
         intids = get_utility(IIntIds)
         locate(profile, root)  # avoid NotYet exception
         locate(profile, root, '++profile++{0}'.format(intids.register(profile)))
Example #10
0
    def remove_blob_reference(self):
        """Remove reference to internal blob

        Blob is deleted if there is no more reference to it.
        """
        if self._blob is not None:
            references = get_utility(IBlobReferenceManager)
            references.drop_reference(self._blob, self)
            self._blob = None
Example #11
0
 def publication(self):
     """Publication label used to display workflow publication state"""
     request = check_request()
     translate = request.localizer.translate
     sm = get_utility(ISecurityManager)  # pylint: disable=invalid-name
     return translate(_('{date} by {principal}')).format(
         date=format_datetime(tztime(
             IWorkflowPublicationInfo(self).publication_date),
                              request=request),
         principal=translate(sm.get_principal(self.publisher).title))
Example #12
0
 def mutate(self, info, **properties):  # pylint: disable=no-self-use
     """Create new document"""
     request = info.context
     container = get_utility(IDocumentContainer)
     if not request.has_permission(CREATE_DOCUMENT_PERMISSION,
                                   context=container):
         raise HTTPForbidden()
     data = base64.b64decode(properties.pop('data', None))
     document = container.add_document(data, properties, request=request)
     return CreateDocument(document=document.to_json())
Example #13
0
def evolve(site):
    """Evolve 2: create reference for all files blobs"""
    registry = get_local_registry()
    try:
        files = set()
        set_local_registry(site.getSiteManager())
        LOGGER.warning("Creating references to all blobs...")
        intids = get_utility(IIntIds)
        references = get_utility(IBlobReferenceManager)
        for ref in list(intids.refs.keys()):
            obj = intids.queryObject(ref)
            if IFile.providedBy(obj):
                blob = getattr(obj, '_blob', None)
                if blob is not None:
                    references.add_reference(blob, obj)
                LOGGER.debug(">>> updated blob reference for file {!r}".format(obj))
        LOGGER.warning("{} files updated".format(len(files)))
    finally:
        set_local_registry(registry)
Example #14
0
 def password(self, value):
     """Encode password before storing new value"""
     if value:
         if value == '*****':
             return
         self._password_salt = urandom(4)
         manager = get_utility(IPasswordManager, name='SSHA')
         self._password = manager.encodePassword(value,
                                                 salt=self._password_salt)
     else:
         self._password = None
Example #15
0
 def resolve_data(self, info, oid, version=None, status=None):  # pylint: disable=no-self-use
     """Resolve document data"""
     container = get_utility(IDocumentContainer)
     document = container.get_document(oid, version, status)
     if document is None:
         raise HTTPNotFound()
     request = info.context
     if not request.has_permission(READ_DOCUMENT_PERMISSION,
                                   context=document):
         raise HTTPForbidden()
     return base64.b64encode(document.data.data).decode()
Example #16
0
 def resolve_document(self, info, oid, version=None, status=None):  # pylint: disable=no-self-use
     """Resolve document properties"""
     container = get_utility(IDocumentContainer)
     document = container.get_document(oid, version, status)
     if document is None:
         raise HTTPNotFound()
     request = info.context
     if not request.has_permission(READ_DOCUMENT_PERMISSION,
                                   context=document):
         raise HTTPForbidden()
     return document.to_json(request=request)
Example #17
0
def set_file_properties(request, oid, properties, version=None):
    """Set document properties"""
    container = get_utility(IDocumentContainer)
    document = container.update_document(oid,
                                         version,
                                         properties=properties,
                                         request=request)
    if document is None:
        return None
    state = IWorkflowState(document)
    return {'version': state.version_id}
Example #18
0
def synchronize(request, imported=None, deleted=None):
    """Synchronize documents to remote container"""
    container = get_utility(IDocumentContainer)
    synchronizer = IDocumentSynchronizer(container)
    if not synchronizer.target:
        raise HTTPServiceUnavailable()
    result = {}
    for oid in (imported or ()):
        result[oid] = synchronizer.synchronize(oid, IMPORT_MODE, request)  # pylint: disable=assignment-from-no-return
    for oid in (deleted or ()):
        result[oid] = synchronizer.synchronize(oid, DELETE_MODE, request)  # pylint: disable=assignment-from-no-return
    return result
Example #19
0
def import_file(request, oid, data, properties):
    """Import document from outer ZFiles database through RPC"""
    container = get_utility(IDocumentContainer)
    if not request.has_permission(CREATE_DOCUMENT_WITH_OWNER_PERMISSION,
                                  context=container):
        raise HTTPForbidden()
    if isinstance(data, Binary):
        data = data.data
    else:
        data = base64.b64decode(data)
    document = container.import_document(oid, data, properties, request)
    return document.oid
Example #20
0
def upload_file(request, data, properties):
    """Create new document through RPC"""
    container = get_utility(IDocumentContainer)
    if not request.has_permission(CREATE_DOCUMENT_PERMISSION,
                                  context=container):
        raise HTTPForbidden()
    if isinstance(data, Binary):
        data = data.data
    else:
        data = base64.b64decode(data)
    document = container.add_document(data, properties, request)
    return document.oid
Example #21
0
 def authenticate(self, credentials, request):  # pylint: disable=unused-argument
     """Try to authenticate principal using given credentials"""
     if not (self.enabled and self.password):
         return None
     attrs = credentials.attributes
     login = attrs.get('login')
     password = attrs.get('password')
     manager = get_utility(IPasswordManager, name='SSHA')
     if login == self.login and manager.checkPassword(
             self.password, password):
         return "{0}:{1}".format(self.prefix, login)
     return None
Example #22
0
def set_file_data(request, oid, data, properties=None, version=None):
    """Set document data"""
    container = get_utility(IDocumentContainer)
    if isinstance(data, Binary):
        data = data.data
    else:
        data = base64.b64decode(data)
    document = container.update_document(oid, version, data, properties,
                                         request)
    if document is None:
        return None
    state = IWorkflowState(document)
    return {'version': state.version_id}
Example #23
0
 def password(self, value):
     """Encode and set user password"""
     if value:
         if value == '*****':
             return
         self._password_salt = urandom(SALT_SIZE.get(self.password_manager, 4))
         manager = get_utility(IPasswordManager, name=self.password_manager)
         if self.password_manager == 'Plain Text':
             self._password = manager.encodePassword(value)
         else:
             self._password = manager.encodePassword(value, salt=self._password_salt)
     else:
         self._password = None
Example #24
0
def get_document(request,
                 oid,
                 version=None,
                 status=None,
                 permission=READ_DOCUMENT_PERMISSION):
    """Get document matching given OID and version"""
    container = get_utility(IDocumentContainer)
    document = container.get_document(oid, version, status)
    if document is None:
        raise HTTPNotFound()
    if permission and not request.has_permission(permission, context=document):
        raise HTTPForbidden()
    return document
Example #25
0
 def schedule_mode(self, value):
     """Scheduler mode setter"""
     if self._schedule_mode is not None:
         mode = query_utility(ITaskSchedulingMode, name=self._schedule_mode)
         if (mode is not None) and mode.marker_interface.providedBy(self):
             noLongerProvides(self, mode.marker_interface)
     self._schedule_mode = value
     if value:
         mode = get_utility(ITaskSchedulingMode, name=value)
         alsoProvides(self, mode.marker_interface)
         mode.schema(self).active = False
         if self.__parent__ is not None:
             self.reset()
Example #26
0
def patch_document(request):
    """Update existing document properties, excluding file data"""
    oid, version = get_ids(request)
    container = get_utility(IDocumentContainer)
    properties = request.params.copy(
    ) if TEST_MODE else request.validated.copy()
    document = container.update_document(oid,
                                         version,
                                         properties=properties,
                                         request=request)
    if document is None:
        return {'oid': oid, 'status': 'deleted'}
    return document.to_json()
Example #27
0
def get_document(request):
    """Retrieve existing document information"""
    container = get_utility(IDocumentContainer)
    document = container.get_document(*get_ids(request))
    if document is None:
        raise HTTPNotFound()
    if not request.has_permission(READ_DOCUMENT_PERMISSION, context=document):
        raise HTTPForbidden()
    fields = request.params.get(
        'fields') if TEST_MODE else request.validated.get('fields')
    if isinstance(fields, str):
        fields = set(fields.split(';'))
    return document.to_json(fields)
Example #28
0
 def _get_viewlets(self):
     viewlets = []
     content = get_parent(self.context, IWorkflowManagedContent)
     wf = get_utility(IWorkflow, name=content.workflow_name)  # pylint: disable=invalid-name
     if wf is None:
         return viewlets
     info = IWorkflowInfo(self.context)
     for transition_id in info.get_manual_transition_ids():
         transition = wf.get_transition_by_id(transition_id)
         menu = TransitionMenuItem(self.context, self.request, self.view,
                                   self, transition)
         viewlets.append((transition_id, menu))
     return sorted(viewlets, key=lambda x: x[1].weight)
Example #29
0
 def get_target(self):
     """Chat message targets getter"""
     principals = {ADMIN_USER_ID}
     root = self.context.request.root
     protection = IProtectedObject(root, None)
     if protection is not None:
         principals |= protection.get_principals(SYSTEM_ADMIN_ROLE)
     scheduler = get_utility(IScheduler)
     protection = IProtectedObject(scheduler, None)
     if protection is not None:
         principals |= protection.get_principals(SCHEDULER_MANAGER_ROLE)
         principals |= protection.get_principals(TASKS_MANAGER_ROLE)
     return {
         'principals': tuple(principals)
     }
Example #30
0
def put_document(request):
    """Update existing document content"""
    oid, version = get_ids(request)
    container = get_utility(IDocumentContainer)
    properties = request.params.copy(
    ) if TEST_MODE else request.validated.copy()
    if request.headers.get('Content-Type').startswith('multipart/form-data'):
        properties['data'] = request.params.get('data')
    else:
        properties['data'] = base64.b64decode(request.json.get('data'))
    data = properties.pop('data')
    document = container.update_document(oid, version, data, properties,
                                         request)
    if document is None:
        return {'oid': oid, 'status': 'deleted'}
    return document.to_json()