GanttCalendarPlugin: refactored admin.py and use Trac db api since 0.12
@@ -41,87 +41,106 @@ | ||
41 | 41 | |
42 | 42 | def get_admin_panels(self, req): |
43 | 43 | if 'TRAC_ADMIN' in req.perm: |
44 | - yield ('ganttcalendar', u'Ganttcalendar', 'holiday', _('Holiday Setting')) | |
44 | + yield ('ganttcalendar', u'Ganttcalendar', | |
45 | + 'holiday', _('Holiday Setting')) | |
45 | 46 | |
46 | 47 | def render_admin_panel(self, req, cat, page, path_info): |
48 | + if req.method == 'POST': | |
49 | + fn = None | |
50 | + if req.args.get('add'): | |
51 | + fn = self._process_add | |
52 | + elif req.args.get('remove'): | |
53 | + fn = self._process_remove | |
54 | + elif req.args.get('create_table'): | |
55 | + fn = self._process_create_table | |
56 | + elif req.args.get('drop_table'): | |
57 | + fn = self._process_drop_table | |
58 | + if fn: | |
59 | + fn(req, cat, page, path_info) | |
60 | + req.redirect(req.href.admin(cat, page)) | |
61 | + | |
62 | + return self._process_list(req, cat, page, path_info) | |
63 | + | |
64 | + def _process_list(self, req, cat, page, path_info): | |
65 | + db = self.env.get_read_db() | |
66 | + cursor = db.cursor() | |
47 | 67 | tbl_chk = True |
48 | - db = self.env.get_db_cnx() | |
49 | - cursor = db.cursor(); | |
50 | - sql = "SELECT count(*) from holiday" | |
51 | 68 | try: |
52 | - cursor.execute(sql) | |
69 | + cursor.execute("SELECT COUNT(*) FROM holiday") | |
53 | 70 | except: |
71 | + holidays = [] | |
54 | 72 | tbl_chk = False |
73 | + else: | |
74 | + cursor.execute("SELECT date,description FROM holiday " | |
75 | + "ORDER BY date") | |
76 | + columns = ('date', 'description') | |
77 | + holidays = [dict(zip(columns, row)) for row in cursor] | |
78 | + tbl_chk = True | |
55 | 79 | |
56 | - if req.method == 'POST': | |
57 | - if req.args.get('add'): | |
58 | - keydate = req.args.getfirst('date') | |
59 | - keydate = user_time(req, parse_date, keydate) | |
60 | - keydate = user_time(req, format_date, keydate, | |
61 | - format='iso8601') | |
62 | - cursor.execute("SELECT COUNT(*) FROM holiday WHERE date=%s", | |
63 | - (keydate,)) | |
64 | - for cnt, in cursor: | |
65 | - dup_chk = cnt | |
66 | - if dup_chk != 0: | |
67 | - raise TracError(_('Holiday %(date)s already exists.', | |
68 | - date=keydate)) | |
69 | - cursor.execute("INSERT INTO holiday VALUES(%s,%s)", | |
70 | - (keydate, req.args.get('description'))) | |
71 | - db.commit() | |
72 | - req.redirect(req.href.admin(cat, page)) | |
80 | + data = {'_': _, 'holidays': holidays, 'tbl_chk': tbl_chk} | |
81 | + return 'ganttcalendar_admin_holiday.html', data | |
73 | 82 | |
74 | - elif req.args.get('remove'): | |
75 | - sel = req.args.getlist('sel') | |
76 | - if not sel: | |
77 | - raise TracError(_('No holiday selected')) | |
78 | - cursor.executemany("DELETE FROM holiday WHERE date=%s", | |
79 | - [(val,) for val in sel]) | |
80 | - db.commit() | |
81 | - req.redirect(req.href.admin(cat, page)) | |
83 | + def _process_add(self, req, cat, page, path_info): | |
84 | + keydate = req.args.getfirst('date') | |
85 | + keydate = user_time(req, parse_date, keydate) | |
86 | + keydate = user_time(req, format_date, keydate, format='iso8601') | |
82 | 87 | |
83 | - elif req.args.get('create_table'): | |
84 | - loc, enc = locale.getdefaultlocale() | |
88 | + db = self.env.get_read_db() | |
89 | + cursor = db.cursor() | |
90 | + cursor.execute("SELECT COUNT(*) FROM holiday WHERE date=%s", | |
91 | + (keydate,)) | |
92 | + row = cursor.fetchone() | |
93 | + if row[0] != 0: | |
94 | + raise TracError(_('Holiday %(date)s already exists.', | |
95 | + date=keydate)) | |
85 | 96 | |
86 | - self.log.debug("loc: %r", loc) | |
87 | - loc = (loc or '').lower() | |
88 | - if loc.startswith('ko_') or loc.startswith('korean_'): | |
89 | - import holiday_ko | |
90 | - holidays_tbl = holiday_ko.holidays_tbl | |
91 | - self.log.debug("import holiday_ko") | |
92 | - elif loc.startswith('ja_') or loc.startswith('japanese_'): | |
93 | - import holiday_ja | |
94 | - holidays_tbl = holiday_ja.holidays_tbl | |
95 | - self.log.debug("import holiday_ja") | |
96 | - else: | |
97 | - holidays_tbl = {} | |
98 | - self.log.debug('create empty holiday table') | |
97 | + @self.env.with_transaction() | |
98 | + def fn(db): | |
99 | + description = req.args.getfirst('description') | |
100 | + cursor = db.cursor() | |
101 | + cursor.execute("INSERT INTO holiday VALUES(%s,%s)", | |
102 | + (keydate, description)) | |
99 | 103 | |
100 | - sql = "CREATE TABLE holiday (date TEXT, description TEXT)" | |
101 | - cursor.execute(sql) | |
102 | - coltype = 'date' | |
103 | - if self.config.get('trac', 'database').startswith('mysql:'): | |
104 | - coltype = 'date(10)' | |
105 | - sql = 'CREATE UNIQUE INDEX idx_holiday ON holiday (%s)' \ | |
106 | - % coltype | |
107 | - cursor.execute(sql) | |
108 | - cursor.executemany('INSERT INTO holiday VALUES(%s,%s)', | |
109 | - list(holidays_tbl.iteritems())) | |
110 | - db.commit() | |
111 | - req.redirect(req.href.admin(cat, page)) | |
104 | + def _process_remove(self, req, cat, page, path_info): | |
105 | + sel = req.args.getlist('sel') | |
106 | + if not sel: | |
107 | + raise TracError(_('No holiday selected')) | |
112 | 108 | |
113 | - elif req.args.get('drop_table'): | |
114 | - sql = "DROP TABLE holiday" | |
115 | - cursor.execute(sql) | |
116 | - db.commit() | |
117 | - req.redirect(req.href.admin(cat, page)) | |
109 | + @self.env.with_transaction() | |
110 | + def fn(db): | |
111 | + cursor = db.cursor() | |
112 | + cursor.executemany("DELETE FROM holiday WHERE date=%s", | |
113 | + [(val,) for val in sel]) | |
118 | 114 | |
119 | - #list | |
120 | - holidays = [] | |
121 | - if tbl_chk: | |
122 | - sql = "SELECT date,description FROM holiday ORDER BY date" | |
123 | - cursor.execute(sql) | |
124 | - for hol_date,hol_desc in cursor: | |
125 | - holidays.append( { 'date': hol_date, 'description': hol_desc}) | |
126 | - data = {'_': _, 'holidays': holidays, 'tbl_chk': tbl_chk} | |
127 | - return 'ganttcalendar_admin_holiday.html', data | |
115 | + def _process_create_table(self, req, cat, page, path_info): | |
116 | + loc, enc = locale.getdefaultlocale() | |
117 | + loc = (loc or '').lower() | |
118 | + | |
119 | + mod = None | |
120 | + if loc.startswith('ko_') or loc.startswith('korean_'): | |
121 | + import holiday_ko as mod | |
122 | + elif loc.startswith('ja_') or loc.startswith('japanese_'): | |
123 | + import holiday_ja as mod | |
124 | + if mod: | |
125 | + holidays_tbl = mod.holidays_tbl | |
126 | + else: | |
127 | + holidays_tbl = {} | |
128 | + | |
129 | + @self.env.with_transaction() | |
130 | + def fn(db): | |
131 | + cursor = db.cursor() | |
132 | + cursor.execute( | |
133 | + "CREATE TABLE holiday (date TEXT, description TEXT)") | |
134 | + coltype = 'date' | |
135 | + if self.config.get('trac', 'database').startswith('mysql:'): | |
136 | + coltype = 'date(10)' | |
137 | + cursor.execute("CREATE UNIQUE INDEX idx_holiday ON holiday (%s)" | |
138 | + % coltype) | |
139 | + cursor.executemany("INSERT INTO holiday VALUES(%s,%s)", | |
140 | + list(holidays_tbl.iteritems())) | |
141 | + | |
142 | + def _process_drop_table(self, req, cat, page, path_info): | |
143 | + @self.env.with_transaction() | |
144 | + def fn(db): | |
145 | + cursor = db.cursor() | |
146 | + cursor.execute("DROP TABLE holiday") |