Ticket #6087: ticket_6087__rev_6812.diff

File ticket_6087__rev_6812.diff, 1.1 KB (added by Ben Slavin, 17 years ago)

Add setdefult, includes tests.

  • django/contrib/sessions/tests.py

     
    6666>>> s.accessed, s.modified
    6767(True, False)
    6868
     69>>> s.setdefault('foo', 'bar')
     70'bar'
     71>>> s.setdefault('foo', 'baz')
     72'bar'
     73
    6974>>> s.accessed = False  # Reset the accessed flag
    7075
    7176>>> s.pop('some key')
  • django/contrib/sessions/backends/base.py

     
    5151        self.modified = self.modified or key in self._session
    5252        return self._session.pop(key, *args)
    5353
     54    def setdefault(self, key, value):
     55        if key in self._session:
     56            return self._session[key]
     57        else:
     58            self.modified = True
     59            self._session[key] = value
     60            return value
     61
    5462    def set_test_cookie(self):
    5563        self[self.TEST_COOKIE_NAME] = self.TEST_COOKIE_VALUE
    5664
Back to Top