summaryrefslogtreecommitdiff
path: root/distixlib/ticket.py
blob: 9ac6c83ec1a50b66cddc767ef52cb7dca0bf979c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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 <http://www.gnu.org/licenses/>.
#
# =*= 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))