Esempio n. 1
0
    def test_showTime_duration(self):
        """ test somewhat arbitrary duration results """
        seconds_expected = ((25, ('seconds', 25)),
                            (89, ('seconds', 89)),
                            (91, ('minutes', 2)),
                            (5399, ('minutes', 90)),
                            (5401, ('hours', 2)),
                            (128999, ('hours', 36)),
                            (864000, ('weeks', 1)),
                            (4838399, ('weeks', 8)),
                            (4838401, ('months', 2)),
                            (63071999, ('months', 24)),
                            (126144000, ('years', 4)),
                            )

        for seconds, expected in seconds_expected:
            result = show_time.duration(seconds)
            assert result == expected
Esempio n. 2
0
    def lock_item(self):
        """
        Return True, None if lock policy is None. Return True, msg if item locked successfully,
        else return False, 'lock failed' message.
        """
        msg = None
        if app.cfg.edit_locking_policy == LOCK:
            locked = self.get_lock_status()
            if locked:
                # somebody has lock, may be current user or other user; lock may have timed out
                i_id, i_name, u_name, timeout = locked
                if u_name == self.user_name:
                    # it is locked by current user, we do not care if lock timed out
                    self.update_editlock()
                    return LOCKED, None
                wait_time = timeout - int(time.time())
                interval, number = show_time.duration(wait_time)
                if wait_time < 0.0:
                    # some other user's lock has timed out, give one-time alert user about potential future conflict,
                    msg = L_(
                        """Edit lock for %(user_name)s timed out %(number)s %(interval)s ago, click Cancel to yield more time,
                             clicking Save may require %(user_name)s to resolve conflicting edits.""",
                        user_name=u_name,
                        number=number,
                        interval=interval)
                    self.update_editlock()
                    self.put_draft(None)
                    return LOCKED, msg
                else:
                    # item is locked by somebody else, make current user wait
                    msg = L_(
                        "Item '%(item_name)s' is locked by %(user_name)s. Try again in %(number)s %(interval)s.",
                        item_name=i_name,
                        user_name=u_name,
                        number=number,
                        interval=interval,
                    )
                    return NO_LOCK, msg

            else:
                # item is not locked
                timeout = int(time.time()) + app.cfg.edit_lock_time * 60
                draft, data = self.get_draft()
                if draft:
                    u_name, i_id, i_name, rev_number, save_time, rev_id = draft
                    if self.rev_number > rev_number:
                        # current user timed out, then other user updated and saved
                        msg = L_(
                            """Someone else updated '%(item_name)s' after your edit lock timed out. If you click 'Save',
                                 conflicting changes must be manually merged. Click 'Cancel' to discard changes.""",
                            item_name=self.item_name)
                    self.cursor.execute(
                        '''INSERT INTO editlock(item_id, item_name, user_name, timeout)
                                      VALUES(?,?,?,?)''',
                        (self.item_id, self.item_name, self.user_name,
                         timeout))
                    self.conn.commit()
                    return LOCKED, msg
                # if no draft, preserve starting rev_number by creating entry without rev_id
                self.put_draft(None)
                self.cursor.execute(
                    '''INSERT INTO editlock(item_id, item_name, user_name, timeout)
                                  VALUES(?,?,?,?)''',
                    (self.item_id, self.item_name, self.user_name, timeout))
                self.conn.commit()
        return LOCKED, msg