summaryrefslogtreecommitdiff
path: root/src/mailman/model/requests.py
blob: 9d9692b3014d307843f0f0c50e785d6fcbb89992 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright (C) 2007-2014 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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.
#
# GNU Mailman 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
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Implementations of the pending requests interfaces."""

__all__ = [
    'DataPendable',
    'ListRequests',
    ]


import six

from datetime import timedelta
from mailman.database.model import Model
from mailman.database.transaction import dbconnection
from mailman.database.types import Enum
from mailman.interfaces.pending import IPendable, IPendings
from mailman.interfaces.requests import IListRequests, RequestType
from six.moves.cPickle import dumps, loads
from sqlalchemy import Column, ForeignKey, Integer, Unicode
from sqlalchemy.orm import relationship
from zope.component import getUtility
from zope.interface import implementer



@implementer(IPendable)
class DataPendable(dict):
    """See `IPendable`."""

    def update(self, mapping):
        # Keys and values must be strings (unicodes, but bytes values are
        # accepted for now).  Any other types for keys are a programming
        # error.  If we find a non-Unicode value, pickle it and encode it in
        # such a way that it will be properly reconstituted when unpended.
        clean_mapping = {}
        for key, value in mapping.items():
            assert isinstance(key, six.string_types)
            if not isinstance(value, six.text_type):
                key = '_pck_' + key
                value = dumps(value).decode('raw-unicode-escape')
            clean_mapping[key] = value
        super(DataPendable, self).update(clean_mapping)



@implementer(IListRequests)
class ListRequests:
    """See `IListRequests`."""

    def __init__(self, mailing_list):
        self.mailing_list = mailing_list

    @property
    @dbconnection
    def count(self, store):
        return store.query(_Request).filter_by(
            mailing_list=self.mailing_list).count()

    @dbconnection
    def count_of(self, store, request_type):
        return store.query(_Request).filter_by(
            mailing_list=self.mailing_list, request_type=request_type).count()

    @property
    @dbconnection
    def held_requests(self, store):
        results = store.query(_Request).filter_by(
            mailing_list=self.mailing_list)
        for request in results:
            yield request

    @dbconnection
    def of_type(self, store, request_type):
        results = store.query(_Request).filter_by(
            mailing_list=self.mailing_list, request_type=request_type)
        for request in results:
            yield request

    @dbconnection
    def hold_request(self, store, request_type, key, data=None):
        if request_type not in RequestType:
            raise TypeError(request_type)
        if data is None:
            data_hash = None
        else:
            pendable = DataPendable()
            pendable.update(data)
            token = getUtility(IPendings).add(pendable, timedelta(days=5000))
            data_hash = token
        request = _Request(key, request_type, self.mailing_list, data_hash)
        store.add(request)
        # XXX The caller needs a valid id immediately, so flush the changes
        # now to the SA transaction context.  Otherwise .id would not be
        # valid.  Hopefully this has no unintended side-effects.
        store.flush()
        return request.id

    @dbconnection
    def get_request(self, store, request_id, request_type=None):
        result = store.query(_Request).get(request_id)
        if result is None:
            return None
        if request_type is not None and result.request_type != request_type:
            return None
        if result.data_hash is None:
            return result.key, None
        pendable = getUtility(IPendings).confirm(
            result.data_hash, expunge=False)
        if pendable is None:
            return None
        data = dict()
        # Unpickle any non-Unicode values.
        for key, value in pendable.items():
            if key.startswith('_pck_'):
                data[key[5:]] = loads(value.encode('raw-unicode-escape'))
            else:
                data[key] = value
        # Some APIs need the request type.
        data['_request_type'] = result.request_type.name
        return result.key, data

    @dbconnection
    def delete_request(self, store, request_id):
        request = store.query(_Request).get(request_id)
        if request is None:
            raise KeyError(request_id)
        # Throw away the pended data.
        getUtility(IPendings).confirm(request.data_hash)
        store.delete(request)



class _Request(Model):
    """Table for mailing list hold requests."""

    __tablename__ = '_request'

    id = Column(Integer, primary_key=True)
    key = Column(Unicode)
    request_type = Column(Enum(RequestType))
    data_hash = Column(Unicode)

    mailing_list_id = Column(Integer, ForeignKey('mailinglist.id'), index=True)
    mailing_list = relationship('MailingList')

    def __init__(self, key, request_type, mailing_list, data_hash):
        super(_Request, self).__init__()
        self.key = key
        self.request_type = request_type
        self.mailing_list = mailing_list
        self.data_hash = data_hash