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

Added created field to Session model #75

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def test_save(self):
self.assertEqual(session.user_agent, 'Python/2.7')
self.assertEqual(session.ip, '127.0.0.1')
self.assertEqual(session.user_id, 1)
self.assertAlmostEqual(now(), session.created,
delta=timedelta(seconds=5))
self.assertAlmostEqual(now(), session.last_activity,
delta=timedelta(seconds=5))

Expand Down
1 change: 1 addition & 0 deletions user_sessions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SessionAdmin(admin.ModelAdmin):
list_filter = ExpiredFilter, OwnerFilter
raw_id_fields = 'user',
exclude = 'session_key',
readonly_fields = 'created', 'last_activity',

def get_search_fields(self, request):
User = get_user_model()
Expand Down
28 changes: 28 additions & 0 deletions user_sessions/migrations/0004_session_created.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import datetime

def set_created(apps, schema_editor):
Session = apps.get_model('user_sessions', 'Session')

for obj in Session.objects.all():
obj.created = obj.last_activity
obj.save()

class Migration(migrations.Migration):

dependencies = [
('user_sessions', '0003_auto_20161205_1516'),
]

operations = [
migrations.AddField(
model_name='session',
name='created',
field=models.DateTimeField(default=datetime.datetime(2017, 4, 18, 12, 11, 0, 286186), auto_now_add=True),
preserve_default=False,
),
migrations.RunPython(set_created, reverse_code=migrations.RunPython.noop),
]
2 changes: 1 addition & 1 deletion user_sessions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_decoded(self):
user_agent = models.CharField(null=True, blank=True, max_length=200)
last_activity = models.DateTimeField(auto_now=True)
ip = models.GenericIPAddressField(null=True, blank=True, verbose_name='IP')

created = models.DateTimeField(auto_now_add=True)

# At bottom to avoid circular import
from .backends.db import SessionStore # noqa: E402 isort:skip