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 1 commit
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 @@ -201,6 +201,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 @@ -53,6 +53,7 @@ class SessionAdmin(admin.ModelAdmin):
list_filter = ExpiredFilter, OwnerFilter
raw_id_fields = 'user',
exclude = 'session_key',
readonly_fields = 'created', 'last_activity',

def __init__(self, *args, **kwargs):
super(SessionAdmin, self).__init__(*args, **kwargs)
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),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrations should be reversible, so please also provide the reverse_code argument here. It doesn't need to do anything; see also this snippet from the Django docs:

Pass the RunPython.noop method to code or reverse_code when you want the operation not to do anything in the given direction. This is especially useful in making the operation reversible.

]
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