Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #22849 -- Added Session.__str__() #2952

Merged
merged 1 commit into from Jul 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions django/contrib/sessions/models.py
@@ -1,6 +1,7 @@
from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _


Expand All @@ -20,6 +21,7 @@ def save(self, session_key, session_dict, expire_date):
return s


@python_2_unicode_compatible
class Session(models.Model):
"""
Django provides full support for anonymous sessions. The session
Expand Down Expand Up @@ -48,6 +50,9 @@ class Meta:
verbose_name = _('session')
verbose_name_plural = _('sessions')

def __str__(self):
return self.session_key

def get_decoded(self):
return SessionStore().decode(self.session_data)

Expand Down
11 changes: 11 additions & 0 deletions django/contrib/sessions/tests.py
Expand Up @@ -24,6 +24,7 @@
from django.test.utils import patch_logger
from django.utils import six
from django.utils import timezone
from django.utils.encoding import force_text

from django.contrib.sessions.exceptions import InvalidSessionKey

Expand Down Expand Up @@ -310,6 +311,16 @@ class DatabaseSessionTests(SessionTestsMixin, TestCase):

backend = DatabaseSession

def test_session_str(self):
"Session repr should be the session key."
self.session['x'] = 1
self.session.save()

session_key = self.session.session_key
s = Session.objects.get(session_key=session_key)

self.assertEqual(force_text(s), session_key)

def test_session_get_decoded(self):
"""
Test we can use Session.get_decoded to retrieve data stored
Expand Down