def apply_action_side_effects(self, req, ticket, action): config = self.config['ticket-workflow'] if config.get(action + '.earn_value', '') != '': value = 0 time = to_utimestamp(datetime_now(FixedOffset(0, 'UTC'))) try: evdef = config.get(action + '.earn_value', '').strip() if evdef.isdigit(): value = float(evdef) elif evdef.endswith('%') and 'activity_earn_value' in ticket: value = float(ticket['activity_earn_value']) * float(evdef[:-1]) / 100 except Exception as e: self.log.warning(e) with self.env.db_transaction as db: cursor = db.cursor() cursor.execute(""" INSERT INTO earn_value VALUES (%s, %s, %s, %s, %s, %s, %s); """, (ticket.id, 'workflow', action, value, req.authname, time, 0)) if config.get(action + '.update_time', '') != '': field = config.get(action + '.update_time').strip() if field in ticket: ticket[field] = datetime_now(FixedOffset(0, 'UTC')) ticket.save_changes()
def _parse_user_time(s): """parse author/committer attribute lines and return (user,timestamp)""" (user,time,tz_str) = s.rsplit(None, 2) tz = FixedOffset((int(tz_str)*6)/10, tz_str) time = datetime.fromtimestamp(float(time), tz) return (user,time)
def test_can_search_time_with_utc_tzinfo(self): time = datetime(2012, 12, 13, 11, 8, 34, 711957, tzinfo=FixedOffset(0, 'UTC')) self.whoosh_backend.add_doc(dict(id="1", type="ticket", time=time)) result = self.whoosh_backend.query(query.Every()) self.print_result(result) self.assertEqual(time, result.docs[0]["time"])
def _parse_user_time(s): """Parse author or committer attribute lines and return corresponding ``(user, timestamp)`` pair. """ user, time, tz_str = s.rsplit(None, 2) tz = FixedOffset((int(tz_str) * 6) / 10, tz_str) time = datetime.fromtimestamp(float(time), tz) return user, time
def query(self, req, start, end, tzoffset): username = req.authname tz = FixedOffset(tzoffset, 'Browser offset') events = [ event_as_dict(event) for event in Event.select(self.env, username, daterange=[start, end]) ] return events
def save(self, req, id, calendar, name, allday, start, end, description, ticket, timetrack, auto, time, tzoffset=None): username = req.authname id = id and str(id) or None tz = tzoffset and FixedOffset(-1*int(tzoffset), 'Browser offset') or utc c = Calendar(self.env, calendar) req.perm.require('CALENDAR_VIEW', c.resource) e = self.save_event(req, id, c, name, allday, start, end, description, ticket, tz) self.save_timetrack(req, e, timetrack, auto, time) self.env.log.debug('tt %s' % (e.time_track)) return event_as_dict(e)
def test_can_search_time_with_non_utc_tzinfo(self): hours = 8 tz_diff = 1 time = datetime(2012, 12, 13, 11, hours, 34, 711957, tzinfo=FixedOffset(tz_diff, "just_one_timezone")) self.whoosh_backend.add_doc(dict(id="1", type="ticket", time=time)) result = self.whoosh_backend.query(query.Every()) self.print_result(result) self.assertEqual(datetime(2012, 12, 13, 11, hours-tz_diff, 34, 711957, tzinfo=utc), result.docs[0]["time"])
def test_can_parse_datetime_str_with_tz(self): self.assert_equals( datetime(2009, 9, 10, 14, 55, 11, tzinfo=utc), datetime_str_to_datetime('2009-09-10 14:55:11+00:00')) berlin = get_timezone('GMT +2:00') self.assert_equals( datetime(2009, 9, 10, 14, 55, 11, tzinfo=berlin), datetime_str_to_datetime('2009-09-10 14:55:11+02:00')) teheran = FixedOffset(3 * 60 + 30, 'GMT +3:30') self.assert_equals( datetime(2009, 9, 10, 14, 55, 11, tzinfo=teheran), datetime_str_to_datetime('2009-09-10 14:55:11+03:30'))
def test_can_return_user_time(self): #arrange ticket = self.insert_ticket("bla") ticket_time = ticket.time_changed #act tzinfo = FixedOffset(60, 'GMT +1:00') self.req.tz = tzinfo self.req.args[RequestParameters.QUERY] = "*:*" data = self.process_request() result_items = data["results"].items #asset self.assertEqual(1, len(result_items)) expected_datetime = format_datetime(ticket_time, tzinfo=tzinfo) result_datetime = result_items[0]["date"] self.env.log.debug( "Ticket time: %s, Formatted time: %s ,Returned time: %s", ticket_time, expected_datetime, result_datetime) self.assertEqual(expected_datetime, result_datetime)
def testPODateTimeIsSavedAsUTC(self): # Actually we don't have to implement any code - trac stores datetime # internally always UTC as already. class MyDatetimePO(PersistentObject): class Meta(object): id = Field(primary_key=True, type='integer') start = Field(type='datetime') self.pom.create_table(MyDatetimePO) pdt = FixedOffset(-7 * 60, 'PDT') start = datetime(2008, 10, 4, 12, 42, tzinfo=pdt) # utc_timestamp = 1223149320 po = MyDatetimePO(self.env, id=1, start=start) po.save() another_po = MyDatetimePO(self.env, id=1) self.assert_equals(start.astimezone(utc), another_po.start)
def datetime_str_to_datetime(value): """Parses the output of str(datetime) and returns a (timezone-aware) datetime instance.""" match = re.search('^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})'\ + '(?:\.(\d{6}))?'\ + '(?:\+(\d{2}):(\d{2}))?$', value) assert match datetime_string = match.group(1) microsecond = match.group(2) tz_hours = match.group(3) tz_minutes = match.group(4) naive_datetime = datetime( *strptime(datetime_string, '%Y-%m-%d %H:%M:%S')[0:6]) if microsecond: naive_datetime = naive_datetime.replace(microsecond=int(microsecond)) if tz_hours is None: return naive_datetime tz_label = 'GMT +%s:%s' % (tz_hours, tz_minutes) tz = FixedOffset(int(tz_hours) * 60 + int(tz_minutes), tz_label) return naive_datetime.replace(tzinfo=tz)
# # Copyright (C) Cauly Kan, mail: [email protected] # All rights reserved. # # This software is licensed as described in the file COPYING, which # you should have received as part of this distribution. from datetime import datetime from trac.core import Component, implements from trac.util.datefmt import to_utimestamp, FixedOffset from tracrpc.api import IXMLRPCHandler from ticketrelation.api import Relation, TicketRelationSystem from ticketrelation.schedule import TicketScheduleSystem from ticketrelation.earn_value import EarnValueSystem utc = FixedOffset(0, 'UTC') class TicketRelationRPC(Component): """ Interface to ticket relation definitions. """ implements(IXMLRPCHandler) def __init__(self): pass def xmlrpc_namespace(self): return 'ticketrelation' def xmlrpc_methods(self):