Revision 890c2065 snf-pithos-backend/pithos/backends/lib/sqlalchemy/alembic/versions/165ba3fbfe53_update_path_account.py
b/snf-pithos-backend/pithos/backends/lib/sqlalchemy/alembic/versions/165ba3fbfe53_update_path_account.py | ||
---|---|---|
13 | 13 |
from alembic import op |
14 | 14 |
from sqlalchemy.sql import table, column, literal, and_ |
15 | 15 |
|
16 |
from synnefo.lib.astakos import get_user_uuid, get_username as get_user_username
|
|
16 |
from synnefo.lib.astakos import get_user_uuid, get_displayname as get_user_displayname
|
|
17 | 17 |
from pithos.api.settings import ( |
18 |
SERVICE_TOKEN, USER_INFO_URL, AUTHENTICATION_USERS)
|
|
18 |
SERVICE_TOKEN, USER_CATALOG_URL, AUTHENTICATION_USERS)
|
|
19 | 19 |
|
20 | 20 |
import sqlalchemy as sa |
21 | 21 |
|
... | ... | |
27 | 27 |
return uuid |
28 | 28 |
try: |
29 | 29 |
uuid = get_user_uuid( |
30 |
SERVICE_TOKEN, account, USER_INFO_URL, AUTHENTICATION_USERS)
|
|
30 |
SERVICE_TOKEN, account, USER_CATALOG_URL, AUTHENTICATION_USERS)
|
|
31 | 31 |
except Exception, e: |
32 | 32 |
print 'Unable to retrieve uuid for %s: %s' % (account, e) |
33 | 33 |
return |
... | ... | |
35 | 35 |
if uuid: |
36 | 36 |
catalog[account] = uuid |
37 | 37 |
return uuid |
38 |
|
|
38 |
|
|
39 | 39 |
inverse_catalog = {} |
40 |
def get_username(account):
|
|
40 |
def get_displayname(account):
|
|
41 | 41 |
global inverse_catalog |
42 |
username = inverse_catalog.get(account)
|
|
43 |
if username:
|
|
44 |
return username
|
|
42 |
displayname = inverse_catalog.get(account)
|
|
43 |
if displayname:
|
|
44 |
return displayname
|
|
45 | 45 |
try: |
46 |
username = get_user_username(
|
|
47 |
SERVICE_TOKEN, account, USER_INFO_URL, AUTHENTICATION_USERS)
|
|
46 |
displayname = get_user_displayname(
|
|
47 |
SERVICE_TOKEN, account, USER_CATALOG_URL, AUTHENTICATION_USERS)
|
|
48 | 48 |
except Exception, e: |
49 |
print 'Unable to retrieve username for %s: %s' % (account, e)
|
|
49 |
print 'Unable to retrieve displayname for %s: %s' % (account, e)
|
|
50 | 50 |
return |
51 | 51 |
else: |
52 |
if username:
|
|
53 |
catalog[account] = username
|
|
54 |
return username
|
|
52 |
if displayname:
|
|
53 |
catalog[account] = displayname
|
|
54 |
return displayname
|
|
55 | 55 |
|
56 | 56 |
n = table( |
57 | 57 |
'nodes', |
... | ... | |
159 | 159 |
|
160 | 160 |
def downgrade(): |
161 | 161 |
connection = op.get_bind() |
162 |
|
|
162 |
|
|
163 | 163 |
s = sa.select([n.c.node, n.c.path]) |
164 | 164 |
nodes = connection.execute(s).fetchall() |
165 | 165 |
for node, path in nodes: |
166 | 166 |
account, sep, rest = path.partition('/') |
167 |
username = get_username(account)
|
|
168 |
if not username:
|
|
167 |
displayname = get_displayname(account)
|
|
168 |
if not displayname:
|
|
169 | 169 |
continue |
170 |
path = sep.join([username, rest])
|
|
170 |
path = sep.join([displayname, rest])
|
|
171 | 171 |
u = n.update().where(n.c.node == node).values({'path':path}) |
172 | 172 |
connection.execute(u) |
173 |
|
|
173 |
|
|
174 | 174 |
s = sa.select([p.c.public_id, p.c.path]) |
175 | 175 |
public = connection.execute(s).fetchall() |
176 | 176 |
for id, path in public: |
177 | 177 |
account, sep, rest = path.partition('/') |
178 |
username = get_username(account)
|
|
179 |
if not username:
|
|
178 |
displayname = get_displayname(account)
|
|
179 |
if not displayname:
|
|
180 | 180 |
continue |
181 |
path = sep.join([username, rest])
|
|
181 |
path = sep.join([displayname, rest])
|
|
182 | 182 |
u = p.update().where(p.c.public_id == id).values({'path':path}) |
183 | 183 |
connection.execute(u) |
184 |
|
|
184 |
|
|
185 | 185 |
s = sa.select([x.c.feature_id, x.c.path]) |
186 | 186 |
xfeatures = connection.execute(s).fetchall() |
187 | 187 |
for id, path in xfeatures: |
188 | 188 |
account, sep, rest = path.partition('/') |
189 |
username = get_username(account)
|
|
190 |
if not username:
|
|
189 |
displayname = get_displayname(account)
|
|
190 |
if not displayname:
|
|
191 | 191 |
continue |
192 |
path = sep.join([username, rest])
|
|
192 |
path = sep.join([displayname, rest])
|
|
193 | 193 |
u = x.update().where(x.c.feature_id == id).values({'path':path}) |
194 | 194 |
connection.execute(u) |
195 | 195 |
|
... | ... | |
198 | 198 |
xfeaturevals = connection.execute(s).fetchall() |
199 | 199 |
for feature_id, key, value in xfeaturevals: |
200 | 200 |
account, sep, group = value.partition(':') |
201 |
username = get_username(account)
|
|
202 |
if not username:
|
|
201 |
displayname = get_displayname(account)
|
|
202 |
if not displayname:
|
|
203 | 203 |
continue |
204 |
new_value = sep.join([username, group])
|
|
204 |
new_value = sep.join([displayname, group])
|
|
205 | 205 |
u = xvals.update() |
206 | 206 |
u = u.where(and_( |
207 | 207 |
xvals.c.feature_id == feature_id, |
... | ... | |
213 | 213 |
s = sa.select([g.c.owner, g.c.name, g.c.member]) |
214 | 214 |
groups = connection.execute(s).fetchall() |
215 | 215 |
for owner, name, member in groups: |
216 |
owner_username = get_username(owner)
|
|
217 |
member_username = get_username(member)
|
|
218 |
if owner_username or member_username:
|
|
216 |
owner_displayname = get_displayname(owner)
|
|
217 |
member_displayname = get_displayname(member)
|
|
218 |
if owner_displayname or member_displayname:
|
|
219 | 219 |
u = g.update() |
220 | 220 |
u = u.where(and_( |
221 | 221 |
g.c.owner == owner, |
222 | 222 |
g.c.name == name, |
223 | 223 |
g.c.member == member)) |
224 | 224 |
values = {} |
225 |
if owner_username:
|
|
226 |
values['owner'] = owner_username
|
|
227 |
if member_username:
|
|
228 |
values['member'] = member_username
|
|
225 |
if owner_displayname:
|
|
226 |
values['owner'] = owner_displayname
|
|
227 |
if member_displayname:
|
|
228 |
values['member'] = member_displayname
|
|
229 | 229 |
u = u.values(values) |
230 | 230 |
connection.execute(u) |
Also available in: Unified diff