- reduced view code redundancy
- code cleanup
1 parent cefc31f commit 8ec014124a2ce48341f22bfdee1f774de831e320
@Florian Fuchs Florian Fuchs authored on 7 Feb 2012
Showing 3 changed files
View
104
src/mailman_django/models.py
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
 
import logging
 
from django.conf import settings
from django.db import models
from django.http import Http404
from mailman.client import Client
from urllib2 import HTTPError
 
# Create your models here.
 
logger = logging.getLogger(__name__)
 
 
class MailmanApiError(Exception):
"""Raised if the API is not available.
"""
pass
 
class Mailman404Error(Exception):
"""Proxy exception. Raised if the API returns 404."""
pass
 
 
class MailmanRestManager(object):
"""Manager class to give a model class CRUD access to the API.
Returns objects (or lists of objects) retrived from the API.
"""
def __init__(self, resource_name, resource_name_plural, cls_name=None):
logger.debug(cls_name)
self.client = Client('%s/3.0' % settings.REST_SERVER,
settings.API_USER, settings.API_PASS)
self.resource_name = resource_name
self.resource_name_plural = resource_name_plural
 
def all(self):
try:
return getattr(self.client, self.resource_name_plural)
except AttributeError:
raise MailmanApiError
 
def get(self, **kwargs):
try:
method = getattr(self.client, 'get_' + self.resource_name)
return method(**kwargs)
except AttributeError, e:
raise MailmanApiError(e)
except HTTPError, e:
if e.code == 404:
raise Mailman404Error
else:
raise
 
def get_or_404(self, **kwargs):
"""Similar to `self.get` but raises standard Django 404 error.
"""
try:
return self.get(**kwargs)
except Mailman404Error:
raise Http404
 
def create(self, **kwargs):
try:
method = getattr(self.client, 'create_' + self.resource_name)
return method(**kwargs)
except AttributeError, e:
raise MailmanApiError(e)
except HTTPError, e:
if e.code == 409:
raise MailmanApiError
else:
raise
 
def delete(self):
"""Not implemented since the objects returned from the API
have a `delete` method of their own.
"""
pass
 
 
class MailmanRestModel(object):
"""Simple REST Model class to make REST API calls Django style.
"""
MailmanApiError = MailmanApiError
DoesNotExist = Mailman404Error
 
def __init__(self, **kwargs):
self.kwargs = kwargs
 
def save(self):
"""Proxy function for `objects.create`.
(REST API uses `create`, while Django uses `save`.)
"""
self.objects.create(**self.kwargs)
 
 
class Domain(MailmanRestModel):
"""Domain model class.
"""
objects = MailmanRestManager('domain', 'domains')
 
 
class List(MailmanRestModel):
"""List model class.
"""
objects = MailmanRestManager('list', 'lists')
View
35
src/mailman_django/tests/tests.py
==============================
Tests Login and Permissions
==============================
 
This document both acts as a test for all the functions implemented
in the UI as well as documenting what can be done
 
Test Pre Requirements
=====================
 
* We've created a special Testobject which will run it's own instance of Mailman3 with a new empty Database.
Setup Testobject (starts Mailman3 with an empty Database).
 
>>> from setup import setup_mm, Testobject, teardown_mm
>>> testobject = setup_mm(Testobject())
 
.. note::
You need to stop all Mailman3 instances before running the tests
 
* Modules needed
Modules needed
As we can't make sure that you're running the same language as we did we made sure that each test below is executed using the exact same translation mechanism as we use to Display you Status Messages and other GUI Texts.
 
Import Translation Module to check success messages
>>> from django.utils.translation import gettext as _
Import HTTPRedirectObject to check whether a response redirects
>>> from django.http import HttpResponseRedirect
 
Getting Started
===============
 
Starting the test module we do use a special Django Test Client which needs to be imported first.
Import Django test client.
 
>>> from django.test.client import Client
>>> c = Client()
 
Once this is created we can try accessing our first Page and check that this was done successful
 
List index
==========
 
Try accessing the list index page.
 
>>> response = c.get('/lists/',)
>>> response.status_code
200
 
 
Login Required
==================================================
 
View
src/mailman_django/views.py