~mailmanweb-django/mailmanweb/systers_django

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
# Copyright (C) 1998-2010 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/>.

from django.db import models
from mailman_django.mailman_rest_client import MailmanRESTClient
from mailman_django.mailman_rest_client import MailmanRESTClientError
from django.conf import settings
from django.core.urlresolvers import reverse

class MailingList(models.Model):
    """
    The models for a Systers mailing list, first setting the values for
    Systers than creating the appropriate Mailman list.

    Testing the creation of a list.
    ===============================

    Start by importing the test client from Django

        >>> from django.test.client import Client

    Continue by instantiating a test client.

        >>> c = Client()

    Go to the page to create a new list and post the data needed to
    for creation.

        >>> response = c.post('/systers_django/create/',
        ...                   {'listname': 'test1@example.com',
        ...                   'list_owner': 'list@owner.com',
        ...                   'list_type': 'closed_discussion', 
        ...                   'languages': 'English (USA)', 
        ...                   'dlist_enabled': True, 
        ...                   'essay_req': True, 
        ...                   'questions_essay': 'Some questions...', 
        ...                   'fullname_req': False, 
        ...                   'picture_req': False})

    The status code for the response is that of a redirect so check
    that we got 302 as expected.

        >>> response.status_code
        302

    Now go to the page of the redirect to see that it got created.

        >>> response = c.get("http://testserver/mailman_django/lists/test1%40example.com/")

    Check the status code so that it is now 200.

        >>> response.status_code
        200

    Make sure we came to the list info page of the new list.

        >>> print "List information" in response.content
        True

    Finally delete the list to clean up the test.

        >>> response = c.post('/mailman_django/delete_list/test1%40example.com/',)

    ...and check that it's been deleted.

        >>> print "test1@example.com" in response.content
        False
    """
    _mm_listname = models.EmailField('List name:', max_length=200)
    _mm_created = models.BooleanField(default=False)
    #website = models.URLField()
    questions_essay = models.TextField("Questions for the essay:", 
                                       blank=True, max_length=200)
    fullname_req = models.BooleanField("Full name be required to subscribe:", 
                                       default=False)
    essay_req = models.BooleanField("Essay requred to subscribe:", 
                                    default=False)
    picture_req = models.BooleanField("Picture required to subscribe:", 
                                      default=False)
    dlist_enabled = models.BooleanField("Dlists enabled:", 
                                        default=True)

    def __unicode__(self):
        return u'%s' % (self._mm_listname)

    def get_absolute_url(self):
       return reverse('list_info', args=[self._mm_listname])

    def save(self, force_insert=False, force_update=False):
        """
        Save the Mailman list as well as Systers list.
        """
        # TODO: use logging instead of response in model

        if not self._mm_created:
            try:
                c = MailmanRESTClient(settings.REST_SERVER)
            except Exception, e:
                raise Exception(e)

            parts = self._mm_listname.split('@')
            try:
                domain = c.get_domain(parts[1])
            except IndexError:
                m = 'List name did not contain both a list name '
                m += 'and a domain, separated by "@".'
                raise IndexError(m)
            if domain.info == 404: # failed to get domain so try 
                                        # creating a new one
                try:
                    domain = c.create_domain(parts[1])
                except MailmanRESTClientError, e:
                    return HttpResponse(e)

            try:
                domain.create_list(parts[0])
                self._mm_created = True
            except MailmanRESTClientError, e:
                raise MailmanRESTClientError(e)

        super(MailingList, self).save(force_insert, force_update)

    def get_listname(self):
        return self._mm_listname

    # TODO: check if we need this function
    def set_listname(self, new_name):
        self._mm_listname = new_name
        #c = _get_connection()
        #c.set_listname(self._mm_listname, new_name)

    def get_list_owner(self):
        return "Not implemented."
        #c = _get_connection()
        #return c.get_list_owner(self._mm_listname)

    def set_list_owner(self, owner):
        pass
        #c = _get_connection()
        #c.set_list_owner(self._mm_listname, owner)

    def get_list_type(self):
        return "Not implemented."
        #c = _get_connection()
        #return c.get_list_type(self._mm_listname)

    def set_list_type(self, list_type):
        pass
        #c = _get_connection()
        #c.set_list_type(self._mm_listname, list_type)

    def get_languages(self):
        return "Not implemented."
        #c = _get_connection()
        #return c.get_languages(self._mm_listname)

    def set_languages(self, lang):
        pass
        #c = _get_connection()
        #c.set_languages(self._mm_listname, lang)


    def get_real_name(self):
        return "Not implemented."
        #c = _get_connection()
        #listinfo = c.read_list(self._mm_listname)
        #return listinfo.get('real_name')


def _get_connection():
    """Establish a connection to the rest server.
    """
    try:
        return MailmanRESTClient(settings.REST_SERVER)
    except Exception, e:
        print e