| 1 | import re |
|---|
| 2 | import dbhelper |
|---|
| 3 | import time |
|---|
| 4 | from tande_filters import * |
|---|
| 5 | from reports_filter import * |
|---|
| 6 | from blackmagic import * |
|---|
| 7 | from ticket_daemon import * |
|---|
| 8 | from ticket_webui import * |
|---|
| 9 | from usermanual import * |
|---|
| 10 | from ticket_policy import * |
|---|
| 11 | from trac.ticket import ITicketChangeListener, Ticket |
|---|
| 12 | from trac.core import * |
|---|
| 13 | from trac.env import IEnvironmentSetupParticipant |
|---|
| 14 | from trac.perm import IPermissionRequestor, PermissionSystem |
|---|
| 15 | from webui import * |
|---|
| 16 | from query_webui import * |
|---|
| 17 | from reportmanager import CustomReportManager |
|---|
| 18 | from statuses import * |
|---|
| 19 | from reports import all_reports |
|---|
| 20 | from stopwatch import * |
|---|
| 21 | from hours_layout_changer import * |
|---|
| 22 | |
|---|
| 23 | ## report columns |
|---|
| 24 | ## id|author|title|query|description |
|---|
| 25 | |
|---|
| 26 | class TimeTrackingSetupParticipant(Component): |
|---|
| 27 | """ This is the config that must be there for this plugin to work: |
|---|
| 28 | |
|---|
| 29 | [ticket-custom] |
|---|
| 30 | totalhours = text |
|---|
| 31 | totalhours.value = 0 |
|---|
| 32 | totalhours.label = Total Hours |
|---|
| 33 | |
|---|
| 34 | billable = checkbox |
|---|
| 35 | billable.value = 1 |
|---|
| 36 | billable.label = Is this billable? |
|---|
| 37 | |
|---|
| 38 | hours = text |
|---|
| 39 | hours.value = 0 |
|---|
| 40 | hours.label = Hours to Add |
|---|
| 41 | |
|---|
| 42 | estimatedhours = text |
|---|
| 43 | estimatedhours.value = 0 |
|---|
| 44 | estimatedhours.label = Estimated Hours? |
|---|
| 45 | |
|---|
| 46 | internal = checkbox |
|---|
| 47 | internal.value = 0 |
|---|
| 48 | internal.label = Internal? |
|---|
| 49 | |
|---|
| 50 | """ |
|---|
| 51 | implements(IEnvironmentSetupParticipant) |
|---|
| 52 | db_version_key = None |
|---|
| 53 | db_version = None |
|---|
| 54 | db_installed_version = None |
|---|
| 55 | |
|---|
| 56 | """Extension point interface for components that need to participate in the |
|---|
| 57 | creation and upgrading of Trac environments, for example to create |
|---|
| 58 | additional database tables.""" |
|---|
| 59 | def __init__(self): |
|---|
| 60 | # Setup logging |
|---|
| 61 | self.statuses_key = 'T&E-statuses' |
|---|
| 62 | self.db_version_key = 'TimingAndEstimationPlugin_Db_Version' |
|---|
| 63 | self.db_version = 8 |
|---|
| 64 | # Initialise database schema version tracking. |
|---|
| 65 | self.db_installed_version = dbhelper.get_system_value(self, \ |
|---|
| 66 | self.db_version_key) or 0 |
|---|
| 67 | |
|---|
| 68 | def environment_created(self): |
|---|
| 69 | """Called when a new Trac environment is created.""" |
|---|
| 70 | if self.environment_needs_upgrade(None): |
|---|
| 71 | self.upgrade_environment(None) |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | def system_needs_upgrade(self): |
|---|
| 75 | return self.db_installed_version < self.db_version |
|---|
| 76 | |
|---|
| 77 | def do_db_upgrade(self): |
|---|
| 78 | if self.db_installed_version < 1: |
|---|
| 79 | print "Creating bill_date table" |
|---|
| 80 | sql = """ |
|---|
| 81 | CREATE TABLE bill_date ( |
|---|
| 82 | time integer, |
|---|
| 83 | set_when integer, |
|---|
| 84 | str_value text |
|---|
| 85 | ); |
|---|
| 86 | """ |
|---|
| 87 | dbhelper.execute_non_query(self, sql) |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | if self.db_installed_version < 5: |
|---|
| 91 | if dbhelper.db_table_exists(self, 'report_version'): |
|---|
| 92 | print "Dropping report_version table" |
|---|
| 93 | sql = "DELETE FROM report " \ |
|---|
| 94 | "WHERE author=%s AND id IN (SELECT report FROM report_version)" |
|---|
| 95 | dbhelper.execute_non_query(self, sql, 'Timing and Estimation Plugin') |
|---|
| 96 | |
|---|
| 97 | sql = "DROP TABLE report_version" |
|---|
| 98 | dbhelper.execute_non_query(self, sql) |
|---|
| 99 | |
|---|
| 100 | #version 6 upgraded reports |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | if self.db_installed_version < 7: |
|---|
| 104 | field_settings = "field settings" |
|---|
| 105 | self.config.set( field_settings, "fields", "billable, totalhours, hours, estimatedhours, internal" ) |
|---|
| 106 | self.config.set( field_settings, "billable.permission", "TIME_VIEW:hide, TIME_RECORD:disable" ) |
|---|
| 107 | self.config.set( field_settings, "hours.permission", "TIME_VIEW:remove, TIME_RECORD:disable" ) |
|---|
| 108 | self.config.set( field_settings, "estimatedhours.permission", "TIME_RECORD:disable" ) |
|---|
| 109 | self.config.set( field_settings, "internal.permission", "TIME_RECORD:hide") |
|---|
| 110 | |
|---|
| 111 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
|---|
| 112 | # This statement block always goes at the end this method |
|---|
| 113 | dbhelper.set_system_value(self, self.db_version_key, self.db_version) |
|---|
| 114 | self.db_installed_version = self.db_version |
|---|
| 115 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | def reports_need_upgrade(self): |
|---|
| 120 | mgr = CustomReportManager(self.env, self.log) |
|---|
| 121 | db_reports = mgr.get_version_hash_by_group(CustomReportManager.TimingAndEstimationKey) |
|---|
| 122 | py_reports = {} |
|---|
| 123 | for report_group in all_reports: |
|---|
| 124 | for report in report_group['reports']: |
|---|
| 125 | py_reports[report['uuid']]= report['version'] |
|---|
| 126 | |
|---|
| 127 | diff = [(uuid, version) for (uuid, version) in py_reports.items() |
|---|
| 128 | if not db_reports.has_key(uuid) or int(db_reports[uuid]) < int(version)] |
|---|
| 129 | |
|---|
| 130 | if len(diff) > 0: |
|---|
| 131 | self.log.debug ("T&E needs upgrades for the following reports: %s" % |
|---|
| 132 | (diff, )) |
|---|
| 133 | return len(diff) > 0 |
|---|
| 134 | |
|---|
| 135 | def do_reports_upgrade(self, force=False): |
|---|
| 136 | self.log.debug( "Beginning Reports Upgrade"); |
|---|
| 137 | mgr = CustomReportManager(self.env, self.log) |
|---|
| 138 | statuses = get_statuses(self) |
|---|
| 139 | stat_vars = status_variables(statuses) |
|---|
| 140 | |
|---|
| 141 | for report_group in all_reports: |
|---|
| 142 | rlist = report_group["reports"] |
|---|
| 143 | group_title = report_group["title"] |
|---|
| 144 | for report in rlist: |
|---|
| 145 | title = report["title"] |
|---|
| 146 | new_version = report["version"] |
|---|
| 147 | |
|---|
| 148 | sql = report["sql"].replace('#STATUSES#', stat_vars) |
|---|
| 149 | mgr.add_report(report["title"], "Timing and Estimation Plugin", \ |
|---|
| 150 | "Reports Must Be Accessed From the Management Screen", |
|---|
| 151 | sql, report["uuid"], report["version"], |
|---|
| 152 | CustomReportManager.TimingAndEstimationKey, |
|---|
| 153 | group_title, force) |
|---|
| 154 | |
|---|
| 155 | def ticket_fields_need_upgrade(self): |
|---|
| 156 | ticket_custom = "ticket-custom" |
|---|
| 157 | return not ( self.config.get( ticket_custom, "totalhours" ) and \ |
|---|
| 158 | self.config.get( ticket_custom, "hours" ) and \ |
|---|
| 159 | self.config.get( ticket_custom, "totalhours.order") and \ |
|---|
| 160 | self.config.get( ticket_custom, "hours.order") and \ |
|---|
| 161 | self.config.get( ticket_custom, "estimatedhours.order") and \ |
|---|
| 162 | self.config.get( ticket_custom, "estimatedhours") and \ |
|---|
| 163 | self.config.get( ticket_custom, "internal") and \ |
|---|
| 164 | "InternalTicketsPolicy" in self.config.getlist("trac", "permission_policies")) |
|---|
| 165 | |
|---|
| 166 | def do_ticket_field_upgrade(self): |
|---|
| 167 | ticket_custom = "ticket-custom" |
|---|
| 168 | |
|---|
| 169 | if not self.config.get(ticket_custom,"totalhours"): |
|---|
| 170 | self.config.set(ticket_custom,"totalhours", "text") |
|---|
| 171 | self.config.set(ticket_custom,"totalhours.order", "4") |
|---|
| 172 | self.config.set(ticket_custom,"totalhours.value", "0") |
|---|
| 173 | self.config.set(ticket_custom,"totalhours.label", "Total Hours") |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | if not self.config.get(ticket_custom,"billable"): |
|---|
| 177 | self.config.set(ticket_custom,"billable", "checkbox") |
|---|
| 178 | self.config.set(ticket_custom,"billable.value", "1") |
|---|
| 179 | self.config.set(ticket_custom,"billable.order", "3") |
|---|
| 180 | self.config.set(ticket_custom,"billable.label", "Billable?") |
|---|
| 181 | |
|---|
| 182 | if not self.config.get(ticket_custom,"hours"): |
|---|
| 183 | self.config.set(ticket_custom,"hours", "text") |
|---|
| 184 | self.config.set(ticket_custom,"hours.value", "0") |
|---|
| 185 | self.config.set(ticket_custom,"hours.order", "2") |
|---|
| 186 | self.config.set(ticket_custom,"hours.label", "Add Hours to Ticket") |
|---|
| 187 | |
|---|
| 188 | if not self.config.get(ticket_custom,"estimatedhours"): |
|---|
| 189 | self.config.set(ticket_custom,"estimatedhours", "text") |
|---|
| 190 | self.config.set(ticket_custom,"estimatedhours.value", "0") |
|---|
| 191 | self.config.set(ticket_custom,"estimatedhours.order", "1") |
|---|
| 192 | self.config.set(ticket_custom,"estimatedhours.label", "Estimated Number of Hours") |
|---|
| 193 | |
|---|
| 194 | if not self.config.get( ticket_custom, "internal"): |
|---|
| 195 | self.config.set(ticket_custom, "internal", "checkbox") |
|---|
| 196 | self.config.set(ticket_custom, "internal.value", "0") |
|---|
| 197 | self.config.set(ticket_custom, "internal.label", "Internal?") |
|---|
| 198 | self.config.set(ticket_custom,"internal.order", "5") |
|---|
| 199 | |
|---|
| 200 | if "InternalTicketsPolicy" not in self.config.getlist("trac", "permission_policies"): |
|---|
| 201 | perms = ["InternalTicketsPolicy"] |
|---|
| 202 | other_policies = self.config.getlist("trac", "permission_policies") |
|---|
| 203 | if "DefaultPermissionPolicy" not in other_policies: |
|---|
| 204 | perms.append("DefaultPermissionPolicy") |
|---|
| 205 | perms.extend( other_policies ) |
|---|
| 206 | self.config.set("trac", "permission_policies", ', '.join(perms)) |
|---|
| 207 | |
|---|
| 208 | self.config.save(); |
|---|
| 209 | |
|---|
| 210 | def needs_user_man(self): |
|---|
| 211 | maxversion = dbhelper.get_scalar(self, "SELECT MAX(version) FROM wiki WHERE name like %s", 0, |
|---|
| 212 | user_manual_wiki_title) |
|---|
| 213 | if (not maxversion) or maxversion < user_manual_version: |
|---|
| 214 | return True |
|---|
| 215 | return False |
|---|
| 216 | |
|---|
| 217 | def do_user_man_update(self): |
|---|
| 218 | |
|---|
| 219 | when = int(time.time()) |
|---|
| 220 | sql = """ |
|---|
| 221 | INSERT INTO wiki (name,version,time,author,ipnr,text,comment,readonly) |
|---|
| 222 | VALUES ( %s, %s, %s, 'Timing and Estimation Plugin', '127.0.0.1', %s,'',0) |
|---|
| 223 | """ |
|---|
| 224 | dbhelper.execute_non_query(self, sql, |
|---|
| 225 | user_manual_wiki_title, |
|---|
| 226 | user_manual_version, |
|---|
| 227 | when, |
|---|
| 228 | user_manual_content) |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | def environment_needs_upgrade(self, db): |
|---|
| 232 | """Called when Trac checks whether the environment needs to be upgraded. |
|---|
| 233 | |
|---|
| 234 | Should return `True` if this participant needs an upgrade to be |
|---|
| 235 | performed, `False` otherwise. |
|---|
| 236 | |
|---|
| 237 | """ |
|---|
| 238 | self.log.debug("NEEDS UP?: sys:%s, rep:%s, stats:%s, fields:%s, man:%s" % \ |
|---|
| 239 | ((self.system_needs_upgrade()), |
|---|
| 240 | (self.reports_need_upgrade()), |
|---|
| 241 | (self.have_statuses_changed()), |
|---|
| 242 | (self.ticket_fields_need_upgrade()), |
|---|
| 243 | (self.needs_user_man()))) |
|---|
| 244 | return (self.system_needs_upgrade()) or \ |
|---|
| 245 | (self.reports_need_upgrade()) or \ |
|---|
| 246 | (self.have_statuses_changed()) or \ |
|---|
| 247 | (self.ticket_fields_need_upgrade()) or \ |
|---|
| 248 | (self.needs_user_man()) |
|---|
| 249 | |
|---|
| 250 | def upgrade_environment(self, db): |
|---|
| 251 | """Actually perform an environment upgrade. |
|---|
| 252 | |
|---|
| 253 | Implementations of this method should not commit any database |
|---|
| 254 | transactions. This is done implicitly after all participants have |
|---|
| 255 | performed the upgrades they need without an error being raised. |
|---|
| 256 | """ |
|---|
| 257 | def p(s): |
|---|
| 258 | print s |
|---|
| 259 | return True |
|---|
| 260 | print "Timing and Estimation needs an upgrade" |
|---|
| 261 | p("Upgrading Database") |
|---|
| 262 | self.do_db_upgrade() |
|---|
| 263 | p("Upgrading reports") |
|---|
| 264 | self.do_reports_upgrade(force=self.have_statuses_changed()) |
|---|
| 265 | |
|---|
| 266 | #make sure we upgrade the statuses string so that we dont need to always rebuild the |
|---|
| 267 | # reports |
|---|
| 268 | stats = get_statuses(self) |
|---|
| 269 | val = ','.join(list(stats)) |
|---|
| 270 | dbhelper.set_system_value(self, self.statuses_key, val) |
|---|
| 271 | |
|---|
| 272 | if self.ticket_fields_need_upgrade(): |
|---|
| 273 | p("Upgrading fields") |
|---|
| 274 | self.do_ticket_field_upgrade() |
|---|
| 275 | if self.needs_user_man(): |
|---|
| 276 | p("Upgrading usermanual") |
|---|
| 277 | self.do_user_man_update() |
|---|
| 278 | print "Done Upgrading" |
|---|
| 279 | |
|---|
| 280 | def have_statuses_changed(self): |
|---|
| 281 | """get the statuses from the last time we saved them, |
|---|
| 282 | compare them to the ones we have now (ignoring '' and None), |
|---|
| 283 | if we have different ones, throw return true |
|---|
| 284 | """ |
|---|
| 285 | s = dbhelper.get_system_value(self, self.statuses_key) |
|---|
| 286 | if not s: |
|---|
| 287 | return True |
|---|
| 288 | sys_stats = get_statuses(self) |
|---|
| 289 | s = s.split(',') |
|---|
| 290 | sys_stats.symmetric_difference_update(s) |
|---|
| 291 | sys_stats.difference_update(['', None]) |
|---|
| 292 | return len(sys_stats) > 0 |
|---|