Mercurial > p > roundup > code
annotate roundup/actions.py @ 5525:bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
The roundup-admin export and import commands are used for migrating
between different database backends. It is desirable that they should
be usable also for migrations between Python 2 and Python 3, and in
some cases (e.g. with the anydbm backend) this may be required.
To be usable for such migrations, the format of the generated CSV
files needs to be stable, meaning the same as currently used with
Python 2. The export process uses repr() to produce the fields in the
CSV files and eval() to convert them back to Python data structures.
repr() of strings with non-ASCII characters produces different results
for Python 2 and Python 3.
This patch adds repr_export and eval_import functions to
roundup/anypy/strings.py which provide the required operations that
are just repr() and eval() in Python 2, but are more complicated in
Python 3 to use data representations compatible with Python 2. These
functions are then used in the required places for export and import.
repr() and eval() are also used in storing the dict of changed values
in the journal for the RDBMS backends. It is similarly desirable that
the database be compatible between Python 2 and Python 3, so that
export and import do not need to be used for a migration between
Python versions for non-anydbm back ends. Thus, this patch changes
rdbms_common.py in the places involved in storing journals in the
database, not just in those involved in import/export.
Given this patch, import/export with non-ASCII characters appear based
on some limited testing to work across Python versions, and an
instance using the sqlite backend appears to be compatible between
Python versions without needing import/export, *if* the sessions/otks
databases (which use anydbm) are deleted when changing Python version.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Sun, 02 Sep 2018 23:48:04 +0000 |
| parents | a7541077cf12 |
| children | ed02a1e0aa5d |
| rev | line source |
|---|---|
| 4083 | 1 # |
| 2 # Copyright (C) 2009 Stefan Seefeld | |
| 3 # All rights reserved. | |
| 4 # For license terms see the file COPYING.txt. | |
| 5 # | |
| 6 | |
|
5071
a7541077cf12
Remove 'import *' statement from actions.py
John Kristensen <john@jerrykan.com>
parents:
4357
diff
changeset
|
7 from roundup.exceptions import Unauthorised |
| 4083 | 8 from roundup import hyperdb |
| 9 from roundup.i18n import _ | |
| 10 | |
| 11 class Action: | |
| 12 def __init__(self, db, translator): | |
| 13 self.db = db | |
| 14 self.translator = translator | |
| 15 | |
| 16 def handle(self, *args): | |
| 17 """Action handler procedure""" | |
| 18 raise NotImplementedError | |
| 19 | |
| 20 def execute(self, *args): | |
| 21 """Execute the action specified by this object.""" | |
| 22 | |
| 23 self.permission(*args) | |
| 24 return self.handle(*args) | |
| 25 | |
| 26 | |
| 27 def permission(self, *args): | |
| 28 """Check whether the user has permission to execute this action. | |
| 29 | |
| 30 If not, raise Unauthorised.""" | |
| 31 | |
| 32 pass | |
| 33 | |
| 34 | |
| 35 def gettext(self, msgid): | |
| 36 """Return the localized translation of msgid""" | |
| 37 return self.translator.gettext(msgid) | |
| 38 | |
| 39 | |
| 40 _ = gettext | |
| 41 | |
| 42 | |
| 43 class Retire(Action): | |
| 44 | |
| 45 def handle(self, designator): | |
| 46 | |
| 47 classname, itemid = hyperdb.splitDesignator(designator) | |
| 48 | |
| 49 # make sure we don't try to retire admin or anonymous | |
| 50 if (classname == 'user' and | |
| 51 self.db.user.get(itemid, 'username') in ('admin', 'anonymous')): | |
|
4357
13b3155869e0
Beginnings of a big code cleanup / modernisation to make 2to3 happy
Richard Jones <richard@users.sourceforge.net>
parents:
4125
diff
changeset
|
52 raise ValueError(self._( |
|
13b3155869e0
Beginnings of a big code cleanup / modernisation to make 2to3 happy
Richard Jones <richard@users.sourceforge.net>
parents:
4125
diff
changeset
|
53 'You may not retire the admin or anonymous user')) |
| 4083 | 54 |
| 55 # do the retire | |
| 56 self.db.getclass(classname).retire(itemid) | |
| 57 self.db.commit() | |
| 58 | |
| 59 | |
| 60 def permission(self, designator): | |
| 61 | |
| 62 classname, itemid = hyperdb.splitDesignator(designator) | |
| 63 | |
| 64 if not self.db.security.hasPermission('Edit', self.db.getuid(), | |
| 65 classname=classname, itemid=itemid): | |
| 66 raise Unauthorised(self._('You do not have permission to ' | |
| 4125 | 67 'retire the %(classname)s class.')%classname) |
| 4083 | 68 |
