1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#!/usr/bin/env python
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'scannerjammer.settings'
from db import db as DB
from pprint import pprint
import django
from datetime import datetime
from django.conf import settings
from django.contrib.auth.models import User
from backend.models import SJLike
from backend.models import SJRoom
from backend.models import SJContent
from backend.models import SJSearch
from backend.models import SJUserProfile
class MigrateDB(DB):
def __init__(self, *args, **kwargs):
super(MigrateDB, self).__init__(*args, **kwargs)
def get_table(self, name):
self.execute('SELECT * FROM %s' % name)
fields = [d[0] for d in self.cursor.description]
rows = self.cursor.fetchall()
for row in rows:
yield dict(zip(fields, row))
def get_radio_chat_table(self):
return self.get_table('radio_chat')
def get_search_log_table(self):
return self.get_table('search_log')
def get_sj_chat_table(self):
return self.get_table('sj_chat')
def get_sj_likes_table(self):
return self.get_table('sj_likes')
def get_sj_likes_tmp_table(self):
return self.get_table('sj_likes_tmp')
def get_sj_room_table(self):
return self.get_table('sj_room')
def get_sj_search_log_table(self):
return self.get_table('sj_search_log')
def get_sj_session_table(self):
return self.get_table('sj_sesson')
def get_sj_url_table(self):
return self.get_table('sj_url')
def get_sj_url_tmp_table(self):
return self.get_table('sj_url_tmp')
def get_sj_user_table(self):
return self.get_table('sj_user')
def get_sj_video_table(self):
return self.get_table('sj_video')
if __name__ == '__main__':
db = MigrateDB()
db.connect()
# migrate sj_user table
for row in db.get_sj_user_table():
try:
user = User.objects.get(username=row['username'])
except User.DoesNotExist:
user = User.objects.create_user(
row['username'],
'%s@example.com' % row['username'],
row['password']
)
try:
user_profile = user.get_profile()
except django.core.exceptions.ObjectDoesNotExist:
user_profile = SJUserProfile(user=user)
user_profile.lastseen_chat = datetime.fromtimestamp(0)
user_profile.lastseen_webcam = datetime.fromtimestamp(0)
user_profile.save()
user.date_joined = datetime.fromtimestamp(row['joindate'])
user.last_login = datetime.fromtimestamp(row['seendate'])
user.save()
user_profile.nickname = row['nickname'] or ''
user_profile.score = row['score']
user_profile.access = row['access']
user_profile.bio = row['bio'] or ''
user_profile.lastseen_chat = datetime.fromtimestamp(row['seendate'])
user_profile.save()
#for key, value in row.iteritems():
# print key, ', ', value, ', ', type(value)
#print
|