-
Notifications
You must be signed in to change notification settings - Fork 66
/
migrate.py
235 lines (194 loc) · 5.81 KB
/
migrate.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/python
from django.utils.encoding import force_unicode
import MySQLdb
from django.contrib.auth.models import User
from snipts.models import Favorite, Snipt
from taggit.models import Tag
from tastypie.models import ApiKey
conn = MySQLdb.connect(host='localhost', user='root', passwd='', db='sniptold')
cursor = conn.cursor()
def i():
users()
create_api_keys()
snipts()
favs()
def users():
print "Deleting existing users"
users = User.objects.all()
for u in users:
u.delete()
cursor.execute("SELECT * FROM auth_user")
rows = cursor.fetchall()
print "Adding users"
for row in rows:
user_id = row[0]
username = row[1]
first_name = row[2]
last_name = row[3]
email = row[4]
password = row[5]
is_staff = row[6]
is_active = row[7]
is_superuser = row[8]
last_login = row[9]
date_joined = row[10]
user = User(
id=user_id,
username=username,
first_name=first_name,
last_name=last_name,
email=email,
password=password,
is_staff=is_staff,
is_active=is_active,
is_superuser=is_superuser,
last_login=last_login,
date_joined=date_joined,
)
print 'Saving user ' + user.username
user.save()
print "Done with users"
def snipts():
print "Deleting existing snipts"
snipts = Snipt.objects.all()
for s in snipts:
s.delete()
print "Deleting existing tags"
existing_tags = Tag.objects.all()
existing_tagged_items = Tag.objects.all()
for t in existing_tags:
t.delete()
for t in existing_tagged_items:
t.delete()
cursor.execute("SELECT * FROM snippet_snippet")
rows = cursor.fetchall()
print "Adding snipts"
for row in rows:
snipt_id = row[0]
code = row[1]
title = row[2]
created = row[3]
user_id = row[4]
tags = row[5]
lexer = row[6]
public = row[7]
key = row[8]
slug = row[9]
title = title[:255]
snipt = Snipt(
id=snipt_id,
code=code,
title=title,
slug=slug,
lexer=lexer,
key=key,
user=User.objects.get(id=user_id),
public=public,
created=created,
modified=created,
)
for t in parse_tag_input(tags):
snipt.tags.add(t)
print 'Saving snipt ' + snipt.title
snipt.save()
print 'Done with snipts'
def favs():
print "Deleting existing favorites"
favs = Favorite.objects.all()
for f in favs:
f.delete()
cursor.execute("SELECT * FROM favsnipt_favsnipt")
rows = cursor.fetchall()
print "Adding favorites"
for row in rows:
fav_id = row[0]
snipt_id = row[1]
user_id = row[2]
created = row[3]
fav = Favorite(
id=fav_id,
snipt_id=snipt_id,
user_id=user_id,
created=created,
modified=created,
)
print 'Saving favorite ' + str(fav.id)
fav.save()
print 'Done with favorites'
def parse_tag_input(input):
"""
Parses tag input, with multiple word input being activated and
delineated by commas and double quotes. Quotes take precedence, so
they may contain commas.
Returns a sorted list of unique tag names.
"""
if not input:
return []
input = force_unicode(input)
# Special case - if there are no commas or double quotes in the
# input, we don't *do* a recall... I mean, we know we only need to
# split on spaces.
if u',' not in input and u'"' not in input:
words = list(set(split_strip(input, u' ')))
words.sort()
return words
words = []
buffer = []
# Defer splitting of non-quoted sections until we know if there are
# any unquoted commas.
to_be_split = []
saw_loose_comma = False
open_quote = False
i = iter(input)
try:
while 1:
c = i.next()
if c == u'"':
if buffer:
to_be_split.append(u''.join(buffer))
buffer = []
# Find the matching quote
open_quote = True
c = i.next()
while c != u'"':
buffer.append(c)
c = i.next()
if buffer:
word = u''.join(buffer).strip()
if word:
words.append(word)
buffer = []
open_quote = False
else:
if not saw_loose_comma and c == u',':
saw_loose_comma = True
buffer.append(c)
except StopIteration:
# If we were parsing an open quote which was never closed treat
# the buffer as unquoted.
if buffer:
if open_quote and u',' in buffer:
saw_loose_comma = True
to_be_split.append(u''.join(buffer))
if to_be_split:
if saw_loose_comma:
delimiter = u','
else:
delimiter = u' '
for chunk in to_be_split:
words.extend(split_strip(chunk, delimiter))
words = list(set(words))
words.sort()
return words
def split_strip(input, delimiter=u','):
"""
Splits ``input`` on ``delimiter``, stripping each resulting string
and returning a list of non-empty strings.
"""
if not input:
return []
words = [w.strip() for w in input.split(delimiter)]
return [w for w in words if w]
def create_api_keys():
for user in User.objects.all():
ApiKey.objects.create(user=user)