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
|
#!/usr/bin/env python
# -*- coding: utf8 -*-
# sugar-lint: disable
# Copyright (C) 2013 Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from optparse import OptionParser
import MySQLdb as mdb
from sugar_network import db
from sugar_network.node import data_root
from sugar_network.resources.volume import Volume
from sugar_network.toolkit import Option
ASLO_GUID = 'd26cef70447160f31a7497cc0320f23a4e383cc3'
UNKNOWN_USERS = {
41: u'Mario César Señoranis Ayala',
4195: u'Andrés Aguirre',
35: u' Ridderman',
6842: u'Juan Ignacio Rodríguez Rodríguez',
9462: u'Cristian García Rea',
9823: u'Nicolás Furquez',
5808: u'Enzo Ludueña',
199: u'Tony Anderson',
60: u'Jameson Quinn',
52: u'Oneyda Ortega',
76: u'milan zimmermann',
}
mysql_server = Option(
'MySQL server',
default='localhost', name='mysql_server')
mysql_database = Option(
'MySQL database',
default='activities', name='mysql_database')
mysql_user = Option(
'MySQL user',
default='root', name='mysql_user')
mysql_password = Option(
'MySQL password',
name='mysql_password')
Option.seek('aslo', [mysql_server, mysql_user, mysql_password, mysql_database])
Option.seek('node', [data_root])
Option.parse_args(OptionParser(), config_files=['~/.config/sweets/config'])
db.index_write_queue.value = 1024 * 10
db.index_flush_threshold.value = 0
db.index_flush_timeout.value = 0
my_connection = mdb.connect('localhost', 'activities', 'Sugar8ME', 'activities')
my_connection = mdb.connect(mysql_server.value, mysql_user.value,
mysql_password.value, mysql_database.value)
def sqlexec(text):
cursor = my_connection.cursor()
cursor.execute(text)
return cursor.fetchall()
def new_authors(doc):
result = {}
for user, props in doc['author'].items():
if user == ASLO_GUID:
result[ASLO_GUID] = {
'role': 1,
'order': 1,
'name': 'Activity Library',
}
continue
if user in users_by_nickname:
nickname = user
fullname = users_by_nickname[nickname]
else:
fullname = props.get('name') or user
if fullname in users_by_fullname:
nickname = users_by_fullname[fullname] or fullname
else:
print '-- No %r/%r user' % (user, fullname)
nickname = user
result[nickname] = {'role': 2, 'order': 2, 'name': fullname}
return result
users_by_fullname = {}
users_by_nickname = {}
for uid, nickname, fullname in sqlexec("""
SELECT
id,
nickname,
IF(firstname!="",
CONCAT_WS(' ', firstname, lastname),
nickname)
FROM users
"""):
if uid in UNKNOWN_USERS:
users_by_fullname[UNKNOWN_USERS[uid]] = nickname
users_by_fullname[fullname] = nickname
users_by_nickname[nickname] = fullname
volume = Volume(data_root.value)
volume.populate()
try:
for document in ('context', 'review'):
documents, __ = volume[document].find(limit=db.MAX_LIMIT)
for doc in documents:
authors = new_authors(doc)
if authors:
volume[document].update(doc.guid, author=authors)
finally:
volume.close()
|