| 1 | |
|---|
| 2 | from pkg_resources import resource_filename |
|---|
| 3 | import re |
|---|
| 4 | import time |
|---|
| 5 | import datetime |
|---|
| 6 | import dbhelper |
|---|
| 7 | from usermanual import * |
|---|
| 8 | from trac.core import * |
|---|
| 9 | from trac.web import IRequestHandler |
|---|
| 10 | from trac.util import Markup |
|---|
| 11 | from trac.web.chrome import add_stylesheet, add_script, \ |
|---|
| 12 | INavigationContributor, ITemplateProvider |
|---|
| 13 | from trac.web.href import Href |
|---|
| 14 | from reportmanager import CustomReportManager |
|---|
| 15 | from statuses import get_statuses |
|---|
| 16 | import trac.util.datefmt |
|---|
| 17 | import reports |
|---|
| 18 | |
|---|
| 19 | def strptime(date_string, format): |
|---|
| 20 | return datetime.datetime(*(time.strptime(date_string, format)[0:6])) |
|---|
| 21 | |
|---|
| 22 | #get_statuses = api.get_statuses |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | validTimeFormats=[ |
|---|
| 26 | '%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %I:%M:%S.%f %p', |
|---|
| 27 | '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %I:%M:%S %p', |
|---|
| 28 | '%Y-%m-%d %H:%M', '%Y-%m-%d %I:%M %p', |
|---|
| 29 | '%Y-%m-%d %H', '%Y-%m-%d %I %p', |
|---|
| 30 | '%Y-%m-%d', |
|---|
| 31 | |
|---|
| 32 | '%Y/%m/%d %H:%M:%S.%f', '%Y/%m/%d %I:%M:%S.%f %p', |
|---|
| 33 | '%Y/%m/%d %H:%M:%S', '%Y/%m/%d %I:%M:%S %p', |
|---|
| 34 | '%Y/%m/%d %H:%M', '%Y/%m/%d %I:%M %p', |
|---|
| 35 | '%Y/%m/%d %H', '%Y/%m/%d %H %p', |
|---|
| 36 | '%Y/%m/%d', |
|---|
| 37 | |
|---|
| 38 | '%Y.%m.%d %H:%M:%S.%f', '%Y.%m.%d %I:%M:%S.%f %p', |
|---|
| 39 | '%Y.%m.%d %H:%M:%S', '%Y.%m.%d %I:%M:%S %p', |
|---|
| 40 | '%Y.%m.%d %H:%M', '%Y.%m.%d %I:%M %p', |
|---|
| 41 | '%Y.%m.%d %H', '%Y.%m.%d %I %p', |
|---|
| 42 | '%Y.%m.%d', |
|---|
| 43 | ] |
|---|
| 44 | |
|---|
| 45 | def parsetime(val, tzinfo=trac.util.datefmt.to_datetime(None).tzinfo): |
|---|
| 46 | if not val: return None |
|---|
| 47 | val = val.strip() |
|---|
| 48 | if not val: return None |
|---|
| 49 | it = None |
|---|
| 50 | for f in validTimeFormats: |
|---|
| 51 | #print f, datetime.datetime.strptime(val, f) |
|---|
| 52 | try: return strptime(val, f).replace(tzinfo=tzinfo) |
|---|
| 53 | except ValueError: pass |
|---|
| 54 | raise TracError('Unable to convert bill date %s to a time, please provide a date in yyyy-mm-dd hh:mm:ss format' % val) |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | class TimingEstimationAndBillingPage(Component): |
|---|
| 58 | implements(INavigationContributor, IRequestHandler, ITemplateProvider) |
|---|
| 59 | |
|---|
| 60 | def __init__(self): |
|---|
| 61 | self.BILLING_PERMISSION = self.env.config.get('timingandestimation', 'billing_permission') or 'REPORT_VIEW' |
|---|
| 62 | self.log.debug('TimingAndEstimation billing_permission: %s' % self.BILLING_PERMISSION) |
|---|
| 63 | |
|---|
| 64 | def set_bill_date(self, username="Timing and Estimation Plugin", when=None): |
|---|
| 65 | now = trac.util.datefmt.to_datetime(None)#get now |
|---|
| 66 | if isinstance(when, str) or isinstance(when, unicode): |
|---|
| 67 | when = parsetime(when, now.tzinfo) |
|---|
| 68 | if not when: when = now |
|---|
| 69 | |
|---|
| 70 | strwhen = "%#04d-%#02d-%#02d %#02d:%#02d:%#02d" % \ |
|---|
| 71 | (when.year, when.month, when.day, when.hour,when.minute, when.second) |
|---|
| 72 | sql = """ |
|---|
| 73 | INSERT INTO bill_date (time, set_when, str_value) |
|---|
| 74 | VALUES (%s, %s, %s) |
|---|
| 75 | """ |
|---|
| 76 | dbhelper.execute_non_query(self.env, sql, trac.util.datefmt.to_timestamp(when), |
|---|
| 77 | trac.util.datefmt.to_timestamp(now), strwhen) |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | # INavigationContributor methods |
|---|
| 81 | def get_active_navigation_item(self, req): |
|---|
| 82 | val = re.search('/billing$', req.path_info) |
|---|
| 83 | if val and val.start() == 0: |
|---|
| 84 | return "billing" |
|---|
| 85 | else: |
|---|
| 86 | return "" |
|---|
| 87 | |
|---|
| 88 | def get_navigation_items(self, req): |
|---|
| 89 | url = req.href.billing() |
|---|
| 90 | if req.perm.has_permission(self.BILLING_PERMISSION): |
|---|
| 91 | yield 'mainnav', "billing", \ |
|---|
| 92 | Markup('<a href="%s">%s</a>' % \ |
|---|
| 93 | (url , "Management")) |
|---|
| 94 | |
|---|
| 95 | # IRequestHandler methods |
|---|
| 96 | def set_request_billing_dates(self, data): |
|---|
| 97 | billing_dates = [] |
|---|
| 98 | billing_time_sql = """ |
|---|
| 99 | SELECT DISTINCT time as value, str_value as text |
|---|
| 100 | FROM bill_date |
|---|
| 101 | ORDER BY time DESC |
|---|
| 102 | """ |
|---|
| 103 | rs = dbhelper.get_result_set(self.env, billing_time_sql) |
|---|
| 104 | if rs: |
|---|
| 105 | for (value, text) in rs.rows: |
|---|
| 106 | billing_info = {'text':text , 'value':value*1000*1000} |
|---|
| 107 | billing_dates.extend([billing_info]) |
|---|
| 108 | #self.log.debug("bill-dates: %s"%billing_dates) |
|---|
| 109 | data['billing_info']["billdates"] = billing_dates |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | def match_request(self, req): |
|---|
| 113 | matches = re.search('^/billing$', req.path_info) |
|---|
| 114 | self.log.debug('T&E matched: %s %s' % (req.path_info, matches)) |
|---|
| 115 | #if matches: req.perm.require(self.BILLING_PERMISSION) |
|---|
| 116 | return matches |
|---|
| 117 | |
|---|
| 118 | def process_request(self, req): |
|---|
| 119 | req.perm.require(self.BILLING_PERMISSION) |
|---|
| 120 | messages = [] |
|---|
| 121 | |
|---|
| 122 | def addMessage(s): |
|---|
| 123 | messages.extend([s]); |
|---|
| 124 | |
|---|
| 125 | if req.method == 'POST': |
|---|
| 126 | if req.args.has_key('setbillingtime'): |
|---|
| 127 | self.set_bill_date(req.authname, req.args.get('newbilltime')) |
|---|
| 128 | addMessage("All tickets last bill date updated") |
|---|
| 129 | |
|---|
| 130 | mgr = CustomReportManager(self.env, self.log) |
|---|
| 131 | data = {}; |
|---|
| 132 | data["statuses"] = get_statuses(self.env) |
|---|
| 133 | data["reports"] = mgr.get_reports_by_group(CustomReportManager.TimingAndEstimationKey); |
|---|
| 134 | # Handle pulling in report_descriptions |
|---|
| 135 | # Could be added to custom report stuff, but that requires |
|---|
| 136 | # coordinating with too many people for me to care right now |
|---|
| 137 | report_descriptions = {} |
|---|
| 138 | for h in reports.all_reports: |
|---|
| 139 | report_descriptions[h["title"]] = h["description"] |
|---|
| 140 | for key in data["reports"]: |
|---|
| 141 | if report_descriptions.has_key(key): |
|---|
| 142 | data["reports"][key]["description"] = report_descriptions[key] |
|---|
| 143 | #self.log.debug("DEBUG got %s, %s" % (data["reports"], type(data["reports"]))); |
|---|
| 144 | data["billing_info"] = {"messages": messages, |
|---|
| 145 | "href": req.href.billing(), |
|---|
| 146 | "report_base_href": req.href.report(), |
|---|
| 147 | "usermanual_href": req.href.wiki(user_manual_wiki_title), |
|---|
| 148 | "usermanual_title": user_manual_title } |
|---|
| 149 | |
|---|
| 150 | self.set_request_billing_dates(data) |
|---|
| 151 | |
|---|
| 152 | add_stylesheet(req, "billing/billingplugin.css") |
|---|
| 153 | add_script(req, "billing/date.js") |
|---|
| 154 | add_script(req, "billing/linkifyer.js") |
|---|
| 155 | return 'billing.html', data, None |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | # ITemplateProvider |
|---|
| 159 | def get_htdocs_dirs(self): |
|---|
| 160 | """Return the absolute path of a directory containing additional |
|---|
| 161 | static resources (such as images, style sheets, etc). |
|---|
| 162 | """ |
|---|
| 163 | return [('billing', resource_filename(__name__, 'htdocs'))] |
|---|
| 164 | |
|---|
| 165 | def get_templates_dirs(self): |
|---|
| 166 | """Return the absolute path of the directory containing the provided |
|---|
| 167 | genshi templates. |
|---|
| 168 | """ |
|---|
| 169 | rtn = [resource_filename(__name__, 'templates')] |
|---|
| 170 | return rtn |
|---|
| 171 | |
|---|