15 |
15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
16 |
16 |
|
17 |
17 |
import psycopg2
|
|
18 |
import psycopg2.extensions
|
18 |
19 |
import datetime
|
19 |
20 |
import time
|
20 |
21 |
import re
|
... | ... | |
30 |
31 |
import wcs.tracking_code
|
31 |
32 |
import wcs.users
|
32 |
33 |
|
|
34 |
# enable psycogp2 unicode mode, this will fetch postgresql varchar/text columns
|
|
35 |
# as unicode objects
|
|
36 |
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
|
37 |
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
|
|
38 |
|
33 |
39 |
SQL_TYPE_MAPPING = {
|
34 |
40 |
'title': None,
|
35 |
41 |
'subtitle': None,
|
... | ... | |
234 |
240 |
return (where_clauses, parameters, None)
|
235 |
241 |
|
236 |
242 |
|
|
243 |
def str_encode(value):
|
|
244 |
if isinstance(value, list):
|
|
245 |
return [str_encode(x) for x in value]
|
|
246 |
if not isinstance(value, unicode):
|
|
247 |
return value
|
|
248 |
return value.encode(get_publisher().site_charset)
|
|
249 |
|
237 |
250 |
|
238 |
251 |
def get_connection(new=False):
|
239 |
252 |
if new:
|
... | ... | |
999 |
1012 |
if sql_type is None:
|
1000 |
1013 |
continue
|
1001 |
1014 |
value = row[i]
|
1002 |
|
if value:
|
|
1015 |
if value is not None:
|
|
1016 |
value = str_encode(value)
|
1003 |
1017 |
if field.key == 'ranked-items':
|
1004 |
1018 |
d = {}
|
1005 |
1019 |
for data, rank in value:
|
... | ... | |
1020 |
1034 |
obdata[field.id] = value
|
1021 |
1035 |
i += 1
|
1022 |
1036 |
if field.store_display_value:
|
1023 |
|
value = row[i]
|
|
1037 |
value = str_encode(row[i])
|
1024 |
1038 |
obdata['%s_display' % field.id] = value
|
1025 |
1039 |
i += 1
|
1026 |
1040 |
if field.store_structured_value:
|
... | ... | |
1124 |
1138 |
@classmethod
|
1125 |
1139 |
def _row2evo(cls, row):
|
1126 |
1140 |
o = wcs.formdata.Evolution()
|
1127 |
|
o._sql_id, o.who, o.status, o.time, o.comment = tuple(row[:5])
|
|
1141 |
o._sql_id, o.who, o.status, o.time, o.comment = [str_encode(x) for x in tuple(row[:5])]
|
1128 |
1142 |
if o.time:
|
1129 |
1143 |
o.time = o.time.timetuple()
|
1130 |
1144 |
if row[5]:
|
... | ... | |
1319 |
1333 |
o = cls()
|
1320 |
1334 |
for static_field, value in zip(cls._table_static_fields,
|
1321 |
1335 |
tuple(row[:len(cls._table_static_fields)])):
|
1322 |
|
setattr(o, static_field[0], value)
|
|
1336 |
setattr(o, static_field[0], str_encode(value))
|
1323 |
1337 |
if o.receipt_time:
|
1324 |
1338 |
o.receipt_time = o.receipt_time.timetuple()
|
1325 |
1339 |
if o.workflow_data:
|
... | ... | |
1559 |
1573 |
o = cls()
|
1560 |
1574 |
(o.id, o.name, o.email, o.roles, o.is_admin, o.anonymous,
|
1561 |
1575 |
o.name_identifiers, o.verified_fields, o.lasso_dump,
|
1562 |
|
o.last_seen, ascii_name) = tuple(row[:11])
|
|
1576 |
o.last_seen, ascii_name) = [str_encode(x) for x in tuple(row[:11])]
|
1563 |
1577 |
if o.last_seen:
|
1564 |
1578 |
o.last_seen = time.mktime(o.last_seen.timetuple())
|
1565 |
1579 |
if o.roles:
|
... | ... | |
1688 |
1702 |
sql_dict['id'] = self.get_new_id()
|
1689 |
1703 |
else:
|
1690 |
1704 |
break
|
1691 |
|
self.id = cur.fetchone()[0]
|
|
1705 |
self.id = str_encode(cur.fetchone()[0])
|
1692 |
1706 |
else:
|
1693 |
1707 |
column_names = sql_dict.keys()
|
1694 |
1708 |
sql_dict['id'] = self.id
|
... | ... | |
1705 |
1719 |
@classmethod
|
1706 |
1720 |
def _row2ob(cls, row):
|
1707 |
1721 |
o = cls()
|
1708 |
|
(o.id, o.formdef_id, o.formdata_id) = tuple(row[:3])
|
|
1722 |
(o.id, o.formdef_id, o.formdata_id) = [str_encode(x) for x in tuple(row[:3])]
|
1709 |
1723 |
return o
|
1710 |
1724 |
|
1711 |
1725 |
@classmethod
|
... | ... | |
1756 |
1770 |
o = formdef.data_class()()
|
1757 |
1771 |
for static_field, value in zip(cls._table_static_fields,
|
1758 |
1772 |
tuple(row[:len(cls._table_static_fields)])):
|
1759 |
|
setattr(o, static_field[0], value)
|
|
1773 |
setattr(o, static_field[0], str_encode(value))
|
1760 |
1774 |
# [CRITICALITY_2] transform criticality_level back to the expected
|
1761 |
1775 |
# range (see [CRITICALITY_1])
|
1762 |
1776 |
levels = len(formdef.workflow.criticality_levels or [0])
|
1763 |
|
-
|