Skip to content

Commit

Permalink
fix: 🐛 Create sessions when they don't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiabhi94 committed Jun 17, 2021
1 parent 3859aea commit 0766caa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hitcount/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def hit_count(request, hitcount):
"""
UpdateHitCountResponse = namedtuple(
'UpdateHitCountResponse', 'hit_counted hit_message')
# as of Django 1.8.4 empty sessions are not being saved
# https://code.djangoproject.com/ticket/25489
if not request.session.session_key:
request.session.create()

user = request.user

Expand Down
14 changes: 14 additions & 0 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.contrib.auth.models import Group
from django.contrib.auth.models import User
from django.contrib.sessions.backends.db import SessionStore
from django.utils import timezone

from hitcount.conf import settings
Expand All @@ -28,6 +29,19 @@ def test_when_use_ip_is_disabled(self):
hit = Hit.objects.last()
self.assertIsNone(hit.ip)

def test_session_key_is_not_present(self):
session = SessionStore(session_key=None)

self.request_post.session = session

response = HitCountViewMixin.hit_count(self.request_post, self.hit_count)

self.assertIs(response.hit_counted, True)
self.assertEqual(response.hit_message, 'Hit counted: session key')
# test database
hit = Hit.objects.last()
self.assertIsNotNone(hit.session)

def test_anonymous_user_hit(self):
response = HitCountViewMixin.hit_count(self.request_post, self.hit_count)

Expand Down

0 comments on commit 0766caa

Please sign in to comment.