Пример #1
0
def webdav_lock(obj):
    """Returns the WebDAV LockItem"""
    lockable = ILockable(obj, None)
    if lockable is None:
        return

    lock_info = lockable.lock_info()
    if len(lock_info) > 0:
        token = lock_info[0]["token"]
        return obj.wl_getLock(token)
Пример #2
0
 def test_update_locked_object_with_token_succeeds(self):
     lockable = ILockable(self.doc)
     lockable.lock()
     transaction.commit()
     response = self.api_session.patch(
         '/',
         headers={'Lock-Token': lockable.lock_info()[0]['token']},
         json={'title': 'New Title'})
     transaction.commit()
     self.assertEqual(response.status_code, 204)
     self.assertEqual(self.doc.Title(), 'New Title')
Пример #3
0
 def test_update_locked_object_with_token_succeeds(self):
     lockable = ILockable(self.doc)
     lockable.lock()
     transaction.commit()
     response = self.api_session.patch(
         "/",
         headers={"Lock-Token": lockable.lock_info()[0]["token"]},
         json={"title": "New Title"},
     )
     transaction.commit()
     self.assertEqual(response.status_code, 204)
     self.assertEqual(self.doc.Title(), "New Title")
Пример #4
0
    def test_statictime_lockinfo(self):
        frozen_time = datetime(1950, 7, 31, 17, 30)
        statictime = StaticTime(modified=frozen_time)

        doc1 = self.create_document("doc1")
        alsoProvides(doc1, ITTWLockable)
        lockable = ILockable(doc1)
        lockable.lock()

        statictime.start()
        lock_infos = lockable.lock_info()
        self.assertEqual(1, len(lock_infos))
        self.assertEqual(-612858600.0, lock_infos[0]["time"])
        fake_datetimes = [lock_infos[0]["time"]]

        statictime.stop()
        lock_infos = lockable.lock_info()
        self.assertEqual(1, len(lock_infos))
        self.assert_roughly_now(lock_infos[0]["time"])
        real_datetimes = [lock_infos[0]["time"]]

        self.assert_of_same_type(fake_datetimes, real_datetimes)
Пример #5
0
    def test_protocol_document_is_locked_by_system_once_generated(self, browser):
        self.setup_generated_protocol(browser)

        browser.find('Protocol-My meeting').click()
        document = browser.context
        lockable = ILockable(document)

        self.assertTrue(lockable.locked())
        self.assertTrue(lockable.can_safely_unlock(SYS_LOCK))
        self.assertFalse(lockable.can_safely_unlock(STEALABLE_LOCK))

        lock_info = lockable.lock_info()[0]
        self.assertEqual(u'sys.lock', lock_info['type'].__name__)
Пример #6
0
def is_locked(obj, request):
    """Returns true if the object is locked and the request doesn't contain
    the lock token.
    """
    lockable = ILockable(obj, None)
    if lockable is None:
        return False
    if lockable.locked():
        token = request.getHeader("Lock-Token", "")
        lock_info = lockable.lock_info()
        if len(lock_info) > 0 and lock_info[0]["token"] == token:
            return False
        return True
    return False
Пример #7
0
    def test_protocol_document_is_locked_by_system_once_generated(
            self, browser):
        self.setup_generated_protocol(browser)

        browser.find('Protocol-My meeting').click()
        document = browser.context
        lockable = ILockable(document)

        self.assertTrue(lockable.locked())
        self.assertTrue(lockable.can_safely_unlock(SYS_LOCK))
        self.assertFalse(lockable.can_safely_unlock(STEALABLE_LOCK))

        lock_info = lockable.lock_info()[0]
        self.assertEqual(u'sys.lock', lock_info['type'].__name__)
Пример #8
0
def lock_info(obj):
    """Returns lock information about the given object."""
    lockable = ILockable(obj, None)
    if lockable is not None:
        info = {"locked": lockable.locked(), "stealable": lockable.stealable()}
        lock_info = lockable.lock_info()
        if len(lock_info) > 0:
            info["creator"] = lock_info[0]["creator"]
            info["time"] = lock_info[0]["time"]
            info["token"] = lock_info[0]["token"]
            lock_type = lock_info[0]["type"]
            if lock_type:
                info["name"] = lock_info[0]["type"].__name__
            lock_item = webdav_lock(obj)
            if lock_item:
                info["timeout"] = lock_item.getTimeout()
        return info
Пример #9
0
    def lock_info(self):
        """Get information about the current lock, a dict containing:

        creator - the id of the user who created the lock
        fullname - the full name of the lock creator
        author_page - a link to the home page of the author
        time - the creation time of the lock
        time_difference - a string representing the time since the lock was
        acquired.
        """

        portal_membership = getToolByName(self.context, 'portal_membership')
        portal_url = getToolByName(self.context, 'portal_url')
        lockable = ILockable(aq_inner(self.context))
        url = portal_url()
        for info in lockable.lock_info():
            creator = info['creator']
            time = info['time']
            token = info['token']
            lock_type = info['type']
            # Get the fullname, but remember that the creator may not
            # be a member, but only Authenticated or even anonymous.
            # Same for the author_page
            fullname = ''
            author_page = ''
            member = portal_membership.getMemberById(creator)
            if member:
                fullname = member.getProperty('fullname', '')
                author_page = "%s/author/%s" % (url, creator)
            if fullname == '':
                fullname = creator or _('label_an_anonymous_user',
                                        u'an anonymous user')
            time_difference = self._getNiceTimeDifference(time)

            return {
                'creator': creator,
                'fullname': fullname,
                'author_page': author_page,
                'time': time,
                'time_difference': time_difference,
                'token': token,
                'type': lock_type,
            }
Пример #10
0
    def lock_info(self):
        """Get information about the current lock, a dict containing:

        creator - the id of the user who created the lock
        fullname - the full name of the lock creator
        author_page - a link to the home page of the author
        time - the creation time of the lock
        time_difference - a string representing the time since the lock was
        acquired.
        """

        portal_membership = getToolByName(self.context, 'portal_membership')
        portal_url = getToolByName(self.context, 'portal_url')
        lockable = ILockable(aq_inner(self.context))
        url = portal_url()
        for info in lockable.lock_info():
            creator = info['creator']
            time = info['time']
            token = info['token']
            lock_type = info['type']
            # Get the fullname, but remember that the creator may not
            # be a member, but only Authenticated or even anonymous.
            # Same for the author_page
            fullname = ''
            author_page = ''
            member = portal_membership.getMemberById(creator)
            if member:
                fullname = member.getProperty('fullname', '')
                author_page = "%s/author/%s" % (url, creator)
            if fullname == '':
                fullname = creator or _('label_an_anonymous_user',
                                        u'an anonymous user')
            time_difference = self._getNiceTimeDifference(time)

            return {
                'creator': creator,
                'fullname': fullname,
                'author_page': author_page,
                'time': time,
                'time_difference': time_difference,
                'token': token,
                'type': lock_type,
            }
Пример #11
0
def move_project_advisory(obj, event=None):
    """Move advisory to other theme if project changes its theme.

    This event is called everytime an object with a theme is updated.

    After the initial migration to the new theme folders, some projects
    will need to be linked to a different theme.  When an advisory is
    linked to this project, the theme of the advisory must be updated as
    well.  This means we need to move the advisory to a different theme
    folder.
    """
    # Be defensive in case we are called on an object that is not a Project.
    advisory_getter = getattr(obj, 'get_public_advisory', None)
    if advisory_getter is None:
        return
    advisory = advisory_getter()
    if advisory is None:
        return
    project_theme = obj.getThemeTitle()
    advisory_theme = advisory.getThemeTitle()
    if project_theme == advisory_theme:
        return
    target = obj.getThemeObject()
    lockable = ILockable(advisory)
    if lockable.locked():
        # During migration, we always want to unlock.  During daily use, we
        # want to be a bit more careful.
        if event is not None:
            lock_info = lockable.lock_info()[0]
            lock_age = time.time() - lock_info.get('time', 0)
            if lock_age < (5 * 60):
                IStatusMessage(obj.REQUEST).addStatusMessage(
                    u'Gelinkt advies kon niet verplaatst worden naar nieuw '
                    u'thema: het wordt nu bewerkt door %s.' %
                    lock_info.get('creator'),
                    type='warning')
                return
        lockable.unlock()
        logger.info("Unlocked advisory %s", advisory.title)
    logger.info("Moving advisory %s from %r to %r",
                advisory.title, advisory_theme, project_theme)
    api.content.move(source=advisory, target=target)
Пример #12
0
def lock_info(obj):
    """Returns lock information about the given object."""
    lockable = ILockable(obj, None)
    if lockable is not None:
        info = {
            'locked': lockable.locked(),
            'stealable': lockable.stealable(),
        }
        lock_info = lockable.lock_info()
        if len(lock_info) > 0:
            info['creator'] = lock_info[0]['creator']
            info['time'] = lock_info[0]['time']
            info['token'] = lock_info[0]['token']
            lock_type = lock_info[0]['type']
            if lock_type:
                info['name'] = lock_info[0]['type'].__name__
            lock_item = webdav_lock(obj)
            if lock_item:
                info['timeout'] = lock_item.getTimeout()
        return info
Пример #13
0
 def get_lock_creator_user_name(self):
     lockable = ILockable(self.context)
     creator = lockable.lock_info()[0]['creator']
     return Actor.lookup(creator).get_label()
Пример #14
0
 def get_lock_creator_user_name(self):
     lockable = ILockable(self.context)
     creator = lockable.lock_info()[0]['creator']
     return Actor.lookup(creator).get_label()