Ticket #5789: session_language_migration.py

File session_language_migration.py, 1.3 KB (added by jdunck, 7 years ago)

django_language -> _language migration script for sessions.

Line 
1import datetime
2
3from django.contrib.sessions.models import Session
4from django.core.paginator import ObjectPaginator
5
6#using paginator to work around QS._get_data sucking in whole result set:  self._result_cache = list(self.iterator())
7SESSION_GROUP_SIZE = 1000
8
9paginator = ObjectPaginator(Session.objects.filter(expire_date__gte=datetime.datetime.now()).order_by('session_key'), 
10                            SESSION_GROUP_SIZE)
11
12
13j = 0
14page_count = paginator.pages
15for page in (paginator.get_page(i) for i in range(paginator.pages)): #generator over page qs to avoid holding onto more than one group of sessions at a time.
16    print "page %s of %s" % (j, page_count)
17    j+=1
18    for s in page:
19        mutated = False
20        bag = s.get_decoded()
21        try:
22            if not '_language' in bag: #skip existing usage of _language-- possibly from another version of django, or from a prior partial upgrade.
23                bag['_language'] = bag['django_language']
24                mutated = True
25        except KeyError: #no old django_language to port over.
26            continue
27        if 'django_language' in bag: #remove old key, making way for app usage.
28            del bag['django_language']
29            mutated = True
30        if mutated:
31            Session.objects.save(s.session_key, bag, s.expire_date)
Back to Top