# Copyright 2014 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # =*= License: GPL-3+ =*= import email import pgpwordlist import distixlib NYBBLES_FOR_WORDS = 6 class TicketAlreadyHasId(distixlib.StructuredError): msg = 'Ticket {ticket_id} already has an id' class Ticket(object): def __init__(self): self._metadata = distixlib.Metadata() self._messages = [] self._dirty = False self._dirty_messages = [] def is_dirty(self): return self._dirty def make_clean(self): self._dirty = False self._dirty_messages = [] def get_metadata_keys(self): return list(self._metadata.keys()) def add_metadata_key_value_pair(self, key, value): self._metadata.add(key, value) self._dirty = True def get_ticket_metadata(self): return self._metadata def set_ticket_metadata(self, new_metadata): self._metadata = new_metadata self._dirty = True def get_ticket_id(self): return self._metadata.get_first_value('ticket-id') def get_ticket_id_as_words(self): ticket_id = self.get_ticket_id() if ticket_id is not None: return pgpwordlist.hex_to_words(ticket_id[:NYBBLES_FOR_WORDS]) return None def set_ticket_id(self, ticket_id): if 'ticket-id' in self._metadata: raise TicketAlreadyHasId(ticket_id=self.get_ticket_id()) self._metadata.add('ticket-id', ticket_id) self._dirty = True def get_title(self): return self._metadata.get_first_value('title') def set_title(self, new_title): if 'title' in self._metadata: self._metadata.remove_all_values('title') self._metadata.add('title', new_title) self._dirty = True def get_messages(self): return self._messages def add_message(self, message): self._messages.append(message) self._dirty_messages.append(message) self._dirty = True def get_dirty_messages(self): return self._dirty_messages def get_newest_message_timestamp(self): messages = self.get_messages() ticket_timestamp = None for message in messages: message_timestamp = self._get_message_timestamp(message) ticket_timestamp = max(ticket_timestamp, message_timestamp) return ticket_timestamp def _get_message_timestamp(self, message): date = message.get('Date', 'Thu, 01 Jan 1970 00:00:00 +0000') return email.utils.mktime_tz(email.utils.parsedate_tz(date))