Index: Accreditation/Accreditation.py
===================================================================
--- Accreditation/Accreditation.py	(revision 16725)
+++ Accreditation/Accreditation.py	(working copy)
@@ -6,78 +6,73 @@
 # This software is licensed as described in the file COPYING, which
 # you should have received as part of this distribution.
 
-
 '''
-Created on 2014-03-15
+Created on 2014-03-19
 
 @author: cauly
 '''
 
-import re
+from genshi.filters.transform import Transformer
 
-from trac.core import Component, implements, TracError
-from trac.ticket.model import Ticket
-from trac.web.api import ITemplateStreamFilter, IRequestHandler, Request
+from trac.core import Component, TracError, implements
 from trac.env import IEnvironmentSetupParticipant
-from genshi.filters.transform import Transformer, StreamBuffer
-from genshi.builder import tag
-from trac.ticket.notification import TicketNotifyEmail
+from trac.ticket.model import Ticket
+from trac.util import to_list
+from trac.util.html import html as tag
+from trac.web.api import IRequestHandler
+from trac.web.chrome import ITemplateStreamFilter
+
 
 class Accreditation(Component):
 
-    implements(ITemplateStreamFilter, IEnvironmentSetupParticipant, IRequestHandler)
+    implements(IEnvironmentSetupParticipant, IRequestHandler,
+               ITemplateStreamFilter)
 
     def environment_created(self):
+        self.upgrade_environment()
 
-        with self.env.db_transaction as db:
-            self.upgrade_environment(db)
-
-    def environment_needs_upgrade(self, db):
-
-        try:
-
-            cursor = db.cursor()
-
-            cursor.execute('SELECT * FROM accreditation;')
-
-            return False
-
-        except:
-
-            return True
-
-    def upgrade_environment(self, db):
+    def environment_needs_upgrade(self, db=None):
 
+        dburi = self.config.get('trac', 'database')
         cursor = db.cursor()
+        tables = self._get_tables(dburi, cursor)
+        if 'accreditation' in tables:
+                return False
+        else:
+            return True
 
-        cursor.execute("""
-                            CREATE TABLE IF NOT EXISTS accreditation (
-                            ticket INT,
-                            topic VARCHAR(100),
-                            conclusion VARCHAR(50),
-                            comment TEXT,
-                            author TEXT);
-                       """)
-
-        cursor.execute('CREATE INDEX accreditation_ticket_index ON accreditation (ticket);')
+    def upgrade_environment(self, db=None):
+        with self.env.db_transaction as db:
+            db("""
+                CREATE TABLE IF NOT EXISTS accreditation (
+                    ticket INT,
+                    topic VARCHAR(100),
+                    conclusion VARCHAR(50),
+                    comment TEXT,
+                    author TEXT)
+                """)
+
+            db("""
+                CREATE INDEX accreditation_ticket_index
+                ON accreditation (ticket)
+                """)
 
     def match_request(self, req):
-
-        return req.path_info.startswith('/accreditation/new') or req.path_info.startswith('/accreditation/comment')
+        return (req.path_info.startswith('/accreditation/new') or
+                req.path_info.startswith('/accreditation/comment'))
 
     def process_request(self, req):
-        """
-
-        :type req: Request
-        :return:
-        """
-
         if req.path_info.startswith('/accreditation/new'):
 
             id = int(req.args['ticket'])
             topic = req.args['topic']
-            # Richard@Lyders.com: strip out spaces surrounding commas in the list
-            participants = [x.strip() for x in req.args['participants'].strip().split(',')]
+            participants = to_list(req.args.get('participants'))
+
+            with self.env.db_transaction as db:
+                for item in participants:
+                    db("""
+                        INSERT INTO accreditation VALUES(%s, %s, '', '', %s)
+                        """, (id, topic, item))
 
             for item in participants:
                 with self.env.db_transaction as db:
@@ -87,36 +82,42 @@
 
             ticket['cc'] += ',' + ','.join(participants)
 
-            ticket.save_changes(req.authname, "Launched New Accreditation '''%s'''" % topic)
+            ticket.save_changes(req.authname,
+                                "Launched New Accreditation '''%s'''" % topic)
 
             req.redirect(req.href('ticket', id))
 
         elif req.path_info.startswith('/accreditation/comment'):
 
             id = int(req.args['ticket'])
-            topic = req.args['topic']
+            topic = req.args.get('topic')
             author = req.authname
-            conclusion = req.args['conclusion']
-            comment = req.args['comment']
-
-            with self.env.db_transaction as db:
-                db('UPDATE accreditation SET conclusion = %s, comment = %s WHERE ticket = %s AND topic = %s AND author = %s;',
-                (conclusion, comment, id, topic, author))
-
-            ticket = Ticket(self.env, id)
+            conclusion = req.args.get('conclusion')
+            comment = req.args.get('comment')
+            if conclusion and comment:
+                ticket_comment = "Accredited '''%s''' for \"''%s''\".\n\n%s" \
+                                 % (topic, conclusion, comment)
 
-            ticket.save_changes(req.authname, "Accredited '''%s''' for \"''%s''\".\n\n%s" % (topic, conclusion, comment))
+                with self.env.db_transaction as db:
+                    db("""
+                        UPDATE accreditation SET conclusion = %s, comment = %s
+                        WHERE ticket = %s AND topic = %s AND author = %s
+                        """, (conclusion, comment, id, topic, author))
+                    ticket = Ticket(self.env, id)
+                    ticket.save_changes(req.authname, ticket_comment)
 
             req.redirect(req.href('ticket', id))
 
     def filter_stream(self, req, method, filename, stream, data):
 
-        if not req.path_info.startswith('/ticket') or not 'ticket' in data:
+        if not req.path_info.startswith('/ticket') or 'ticket' not in data:
             return stream
 
         ticket = data['ticket']
 
-        accdivheader = tag.h3(tag.a('Accreditation', href='#accreditation_wrapper'), class_='foldable')
+        accdivheader = tag.h3(
+            tag.a('Accreditation', href='#accreditation_wrapper'),
+            class_='foldable')
 
         createacc = tag.div(
             tag.form(
@@ -157,7 +158,10 @@
             , class_='trac-content', style='background: none repeat scroll 0 0 #FFFFDD; border:1px solid #DDDD99;'
         )
 
-        accdiv = tag.div(accdivheader, tag.div(createacc, acclist, id='accreditation'), id='accreditation_wrapper')
+        accdiv = tag.div(
+            accdivheader,
+            tag.div(createacc, acclist, id='accreditation'),
+            id='accreditation_wrapper')
 
         stream |= Transformer('//div[@id="ticket"]').after(accdiv)
         return stream
@@ -165,11 +169,31 @@
     def _get_accreditations(self, id):
 
         result = {}
-
         for ticket, topic, conclusion, comment, author in self.env.db_query("""
             SELECT * FROM accreditation WHERE ticket = %s
             """, (id,) ):
-
             result.setdefault(topic, []).append((author, conclusion, comment))
-
         return result
+
+    def _get_tables(self, dburi, cursor):
+        """Code from TracMigratePlugin by Jun Omae (see tracmigrate.admin)."""
+        if dburi.startswith('sqlite:'):
+            sql = """
+                SELECT name
+                  FROM sqlite_master
+                 WHERE type='table'
+                   AND NOT name='sqlite_sequence'
+            """
+        elif dburi.startswith('postgres:'):
+            sql = """
+                SELECT tablename
+                  FROM pg_tables
+                 WHERE schemaname = ANY (current_schemas(false))
+            """
+        elif dburi.startswith('mysql:'):
+            sql = "SHOW TABLES"
+        else:
+            raise TracError('Unsupported database type "%s"'
+                            % dburi.split(':')[0])
+        cursor.execute(sql)
+        return sorted([row[0] for row in cursor])
Index: Accreditation/__init__.py
===================================================================
--- Accreditation/__init__.py	(revision 16725)
+++ Accreditation/__init__.py	(working copy)
@@ -6,11 +6,11 @@
 # This software is licensed as described in the file COPYING, which
 # you should have received as part of this distribution.
 
-
 '''
-Created on 2014-03-19 
+Created on 2014-03-19
 
 @author: cauly
 '''
 
-from Accreditation import *
\ No newline at end of file
+import pkg_resources
+pkg_resources.require('Trac >= 1.0')
Index: setup.py
===================================================================
--- setup.py	(revision 16725)
+++ setup.py	(working copy)
@@ -8,21 +8,23 @@
 
 
 '''
-Created on 2014-03-12 
+Created on 2014-03-12
 
 @author: cauly
 '''
 from setuptools import find_packages, setup
 
 setup(
-    name='TracAccreditation', version='1.1',
+    name='TracAccreditation',
+    version='1.2',
     packages=find_packages(exclude=['*.tests*']),
-    license = "BSD 3-Clause",
+    license="BSD 3-Clause",
     author_email='cauliflower.kan@gmail.com',
     author='Cauly Kan',
-    entry_points = {
+    entry_points={
         'trac.plugins': [
-            'tracaccreditation = Accreditation',
+            'tracaccreditation = Accreditation.Accreditation',
         ],
     },
+    install_requires=['Trac'],
 )
