-
-
Notifications
You must be signed in to change notification settings - Fork 130
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
Command for migrating existing sessions to the new session store #33
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0018b5f
Command for migrating existing sessions to the new session store
ivorbosloper 5d2efd9
Test Session migration functionality
ivorbosloper b745418
Small test improvement
ivorbosloper 77b1ee2
Cleanup
ivorbosloper 6372c4b
Add documentation
ivorbosloper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ | |
|
||
/docs/_build/ | ||
/GeoLite2-City.mmdb | ||
.idea/ | ||
*.pyc | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: UTF-8 -*- | ||
from django.core.management.base import BaseCommand | ||
from user_sessions.models import Session as UserSession | ||
from django.contrib.auth import get_user_model | ||
import logging | ||
import importlib | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_model_class(full_model_name): | ||
old_model_package, old_model_class_name = full_model_name.rsplit('.', 1) | ||
package = importlib.import_module(old_model_package) | ||
return getattr(package, old_model_class_name) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Convert existing (old) sessions to the new session store. | ||
""" | ||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--oldmodel', | ||
dest='oldmodel', | ||
default='django.contrib.sessions.models.Session', | ||
help='Existing session model to migrate to the new UserSessions database table' | ||
) | ||
|
||
def handle(self, *args, **options): | ||
User = get_user_model() | ||
old_sessions = get_model_class(options['oldmodel']).objects.all() | ||
logger.info("Processing %d session objects" % old_sessions.count()) | ||
conversion_count = 0 | ||
for old_session in old_sessions: | ||
if not UserSession.objects.filter(session_key=old_session.session_key).exists(): | ||
data = old_session.get_decoded() | ||
user = None | ||
if '_auth_user_id' in data: | ||
user = User.objects.filter(pk=data['_auth_user_id']).first() | ||
UserSession.objects.create( | ||
session_key=old_session.session_key, | ||
session_data=old_session.session_data, | ||
expire_date=old_session.expire_date, | ||
user=user, | ||
ip='127.0.0.1' | ||
) | ||
conversion_count += 1 | ||
|
||
logger.info("Created %d new session objects" % conversion_count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
# Django 1.9 and above | ||
from django.contrib.gis.geoip2 import HAS_GEOIP2 | ||
HAS_GEOIP = False | ||
except: | ||
except ImportError: | ||
# Django 1.8 | ||
from django.contrib.gis.geoip import HAS_GEOIP | ||
HAS_GEOIP2 = False | ||
|
@@ -98,7 +98,7 @@ def location(value): | |
""" | ||
try: | ||
location = geoip() and geoip().city(value) | ||
except: | ||
except Exception as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable isn't used, no need to assign it. |
||
try: | ||
location = geoip() and geoip().country(value) | ||
except Exception as e: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for adding these ignores, I prefer to have these in my global gitignore.