view scripts/roundup-reminder @ 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 55f09ca366c4
children e124d76311e0
line wrap: on
line source

#! /usr/bin/env python
# Copyright (c) 2002 ekit.com Inc (http://www.ekit-inc.com/)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

'''
Simple script that emails all users of a tracker with the issues that
are currently assigned to them.

TODO: introduce some structure ;)
TODO: possibly make this more general and configurable...
'''

from __future__ import print_function
import sys, MimeWriter, smtplib
from roundup import instance, date
from roundup.mailer import SMTPConnection
from roundup.anypy.strings import StringIO

# open the instance
if len(sys.argv) != 2:
    print('You need to specify an instance home dir')
instance_home = sys.argv[1]
instance = instance.open(instance_home)
db = instance.open('admin')

resolved_id = db.status.lookup('resolved')

def listKey(x):
    "key for tuples such that order is positive on [0] and negative on [1]"
    return (x[0], -x[1])
    return 0

# loop through all the users
for user_id in db.user.list():
    # make sure we care aboue this user
    name = db.user.get(user_id, 'realname')
    if name is None:
        name = db.user.get(user_id, 'username')
    address = db.user.get(user_id, 'address')
    if address is None:
        continue

    # extract this user's issues
    l = []
    for issue_id in db.issue.find(assignedto=user_id):
        if db.issue.get(issue_id, 'status') == resolved_id:
            continue
        order = db.priority.get(db.issue.get(issue_id, 'priority'), 'order')
        l.append((order, db.issue.get(issue_id, 'activity'),
            db.issue.get(issue_id, 'creation'), issue_id))

    # sort the issues by timeliness and creation date
    l.sort(key=listKey)
    if not l:
        continue

    # generate the email message
    message = StringIO()
    writer = MimeWriter.MimeWriter(message)
    writer.addheader('Subject', 'Your active %s issues'%db.config.TRACKER_NAME)
    writer.addheader('To', address)
    writer.addheader('From', '%s <%s>'%(db.config.TRACKER_NAME,
        db.config.ADMIN_EMAIL))
    writer.addheader('Reply-To', '%s <%s>'%(db.config.TRACKER_NAME,
        db.config.ADMIN_EMAIL))
    writer.addheader('MIME-Version', '1.0')
    writer.addheader('X-Roundup-Name', db.config.TRACKER_NAME)

    # start the multipart
    part = writer.startmultipartbody('alternative')
    part = writer.nextpart()
    body = part.startbody('text/plain')
    
    # do the plain text bit
    print('Created     ID   Activity  Title', file=body)
    print('='*75, file=body)
    #             '2 months    213  immediate cc_daemon barfage
    old_priority = None
    for priority_order, activity_date, creation_date, issue_id in l:
        priority = db.issue.get(issue_id, 'priority')
        if (priority != old_priority):
            old_priority = priority
            print('    ', db.priority.get(priority,'name'), file=body)
        # pretty creation
        creation = (creation_date - date.Date('.')).pretty()
        activity = (activity_date - date.Date('.')).pretty()
        title = db.issue.get(issue_id, 'title')
        if len(title) > 42:
            title = title[:38] + ' ...'
        print('%-11s %-4s %-9s %-42s'%(creation, issue_id,
            activity, title), file=body)

    # some help to finish off
    print('''
To view or respond to any of the issues listed above, visit the URL

   %s

and click on "My Issues". Do NOT respond to this message.
'''%db.config.TRACKER_WEB, file=body)


    # now the HTML one
    part = writer.nextpart()
    body = part.startbody('text/html')
    colours = {
        'immediate': ' bgcolor="#ffcdcd"',
        'day': ' bgcolor="#ffdecd"',
        'week': ' bgcolor="#ffeecd"',
        'month': ' bgcolor="#ffffcd"',
        'whenever': ' bgcolor="#ffffff"',
    }
    print('''<table border>
<tr><th>Created</th> <th>ID</th> <th>Activity</th> <th>Title</th></tr>
''', file=body)
    old_priority = None
    for priority_order, activity_date, creation_date, issue_id in l:
        priority = db.issue.get(issue_id,'priority')
        if (priority != old_priority):
           old_priority = priority
           print('<tr><td>-></td><td>-></td><td>-></td><td><b>%s</b></td></tr>'%db.priority.get(priority,'name'), file=body)
        creation = (creation_date - date.Date('.')).pretty()
        title = db.issue.get(issue_id, 'title')
        issue_id = '<a href="%sissue%s">%s</a>'%(db.config.TRACKER_WEB,
            issue_id, issue_id)
        activity = (activity_date - date.Date('.')).pretty()
        print('''<tr><td>%s</td><td>%s</td><td>%s</td>
    <td>%s</td></tr>'''%(creation, issue_id, activity, title), file=body)
    print('</table>', file=body)

    print('''<p>To view or respond to any of the issues listed
        above, simply click on the issue ID. Do <b>not</b> respond to
        this message.</p>''', file=body)

    # finish of the multipart
    writer.lastpart()

    # all done, send!
    smtp = SMTPConnection(db.config)
    smtp.sendmail(db.config.ADMIN_EMAIL, address, message.getvalue())

# vim: set filetype=python ts=4 sw=4 et si

Roundup Issue Tracker: http://roundup-tracker.org/