diff options
author | Leif Johansson <leifj@sunet.se> | 2012-05-11 13:43:54 +0200 |
---|---|---|
committer | Leif Johansson <leifj@sunet.se> | 2012-05-11 13:43:54 +0200 |
commit | fc0d16f8819a1107e81f977504a1fbd48d746a4f (patch) | |
tree | 0938e8a0c2b25d04382fc1d6c5aad60333ef78b8 | |
parent | 04a6491e5d83b8cb33223122b9868c14ebbb7ce4 (diff) |
consumer code
-rw-r--r-- | coip/apps/consumer/__init__.py (renamed from coip/apps/services/__init__.py) | 15 | ||||
-rw-r--r-- | coip/apps/consumer/forms.py | 11 | ||||
-rw-r--r-- | coip/apps/consumer/models.py | 25 | ||||
-rw-r--r-- | coip/apps/consumer/urls.py | 10 | ||||
-rw-r--r-- | coip/apps/consumer/views.py | 64 | ||||
-rw-r--r-- | coip/settings.py | 2 | ||||
-rw-r--r-- | coip/urls.py | 5 | ||||
-rw-r--r-- | templates/apps/consumer/edit.html | 3 | ||||
-rw-r--r-- | templates/apps/consumer/list.html | 18 |
9 files changed, 144 insertions, 9 deletions
diff --git a/coip/apps/services/__init__.py b/coip/apps/consumer/__init__.py index dc56a88..fd6cc15 100644 --- a/coip/apps/services/__init__.py +++ b/coip/apps/consumer/__init__.py @@ -4,22 +4,22 @@ __author__ = 'leifj' from django.conf import settings from django.utils.importlib import import_module -_token_provider_modules = list() +_consumer_provider_modules = list() def autodiscover(): for app in settings.INSTALLED_APPS: mod = import_module("%.models" % app) - if hasattr(mod,'token_providers' and hasattr(mod.token_providers,'__call__')): - _token_provider_modules.append(mod) + if hasattr(mod,'consumer_providers' and hasattr(mod.consumer_providers,'__call__')): + _consumer_provider_modules.append(mod) -def consumers(): +def consumer_providers(): p = list() - for mod in _token_provider_modules: + for mod in _consumer_provider_modules: p.extend(mod.token_providers()) return p -def consumer(name): - for mod in _token_provider_modules: +def consumer_provider(name): + for mod in _consumer_provider_modules: for p in mod.token_providers(): if p.name == name: return p @@ -32,4 +32,5 @@ autodiscover() # self.name # self.description # self.service_uri +# self.is_authorized(user) diff --git a/coip/apps/consumer/forms.py b/coip/apps/consumer/forms.py new file mode 100644 index 0000000..1973f48 --- /dev/null +++ b/coip/apps/consumer/forms.py @@ -0,0 +1,11 @@ +from django.forms import forms +from form_utils.forms import BetterModelForm +from coip.apps.consumer.models import Consumer + +__author__ = 'leifj' + +class ConsumerForm(BetterModelForm): + + class Meta: + model = Consumer + fields = ['name','consumer_name','inherit'] diff --git a/coip/apps/consumer/models.py b/coip/apps/consumer/models.py new file mode 100644 index 0000000..3585776 --- /dev/null +++ b/coip/apps/consumer/models.py @@ -0,0 +1,25 @@ +from django.contrib.auth.models import User +from django.db import models +from django.db.models import fields, ForeignKey, BooleanField +from tastypie.fields import DateTimeField +from coip.apps.name.models import Name +from coip.apps.consumer import consumer_providers, consumer_provider + +__author__ = 'leifj' + +class Consumer(models.Model): + name = ForeignKey(Name) + user = ForeignKey(User) + inherit = BooleanField(default=False) + consumer_name = fields.CharField(choices=[c.name for c in consumer_providers()]) + lastupdated = DateTimeField(auto_now=True) + timecreated = DateTimeField(auto_now_add=True) + + def __unicode_(self): + return "%s connected to %s" % (self.name,self.consumer_name) + + def _consumer_provider(self): + return consumer_provider(self.name) + + consumer_provider = property(_consumer_provider) + diff --git a/coip/apps/consumer/urls.py b/coip/apps/consumer/urls.py new file mode 100644 index 0000000..40076da --- /dev/null +++ b/coip/apps/consumer/urls.py @@ -0,0 +1,10 @@ +''' +Created on Nov 7, 2011 + +@author: leifj +''' +from django.conf.urls.defaults import patterns, url, include + +urlpatterns = patterns('coip.apps.consumer.views', + url(r'^(?P<nid>[0-9]+)$', view='list'), +)
\ No newline at end of file diff --git a/coip/apps/consumer/views.py b/coip/apps/consumer/views.py new file mode 100644 index 0000000..2d32381 --- /dev/null +++ b/coip/apps/consumer/views.py @@ -0,0 +1,64 @@ +from django.http import HttpResponseBadRequest +from django.shortcuts import get_object_or_404, redirect +from coip.apps.consumer import consumer_providers +from coip.apps.consumer.forms import ConsumerForm +from coip.apps.consumer.models import Consumer +from coip.apps.name.models import Name +from coip.multiresponse import render403, respond_to, json_response + +__author__ = 'leifj' + + +def _consumer2json(c,e): + return { + 'name': c.name, + 'description': c.description, + 'authorization_uri': c.authorization_uri, + 'service_uri': c.service_uri, + 'logo': c.logo, + 'enabled': c in e, + } + +def _consumers(name): + enabled_consumers = Consumer.objects.filter(name=name) + return [_consumer2json(c,enabled_consumers) for c in consumer_providers()] + +def list(request,nid): + name = get_object_or_404(Name,pk=nid) + + if request.method == 'GET': + if not name.has_permission(request.user,'r'): + return render403(request,"You do not have permission to inspect consumers for %s" % name) + + return respond_to(request, + {'application/json': json_response(_consumers(name)), + 'text/html': "apps/consumers/list.html"},{'consumers': _consumers(name)}) + + else: + return HttpResponseBadRequest() + +def add(request,nid): + name = get_object_or_404(Name,pk=nid) + + if not name.has_permission(request.user,'w'): + return render403(request,"You do not have permission to create consumers for %s" % name) + + if request.method == 'POST': + consumer = Consumer(user=request.user,name=name) + form = ConsumerForm(request.POST,instance=consumer) + if form.is_valid(): + consumer = form.save() + return redirect("/name/%d/consumers" % nid) + + return respond_to(request,{'text/html': 'apps/consumers/edit.html'},{'form': form}) + + +def remove(request,nid,cid): + name = get_object_or_404(Name,pk=nid) + + if not name.has_permission(request.user,'w'): + return render403(request,"You do not have permission to remove consumers from %s" % name) + + consumer = get_object_or_404(Consumer,pk=cid) + consumer.delete() + return redirect("/name/%d/consumers" % nid)
\ No newline at end of file diff --git a/coip/settings.py b/coip/settings.py index bf6d273..5f8be8a 100644 --- a/coip/settings.py +++ b/coip/settings.py @@ -123,7 +123,7 @@ INSTALLED_APPS = ( 'coip.apps.saml2', 'coip.apps.resource', 'coip.apps.scim', - 'coip.apps.services' + 'coip.apps.consumer' ) OAUTH_REALM_KEY_NAME = 'http://coip-test.sunet.se' diff --git a/coip/urls.py b/coip/urls.py index 59051fb..8b9098c 100644 --- a/coip/urls.py +++ b/coip/urls.py @@ -47,7 +47,10 @@ urlpatterns = patterns('', (r'^name/(?P<id>[0-9]+)/add$', 'coip.apps.name.views.add'), (r'^name/(?P<id>[0-9]+)/join$', 'coip.apps.membership.views.join'), (r'^name/(?P<id>[0-9]+)/join/(?P<membername>[^\/]+)$', 'coip.apps.membership.views.join'), - (r'^name/(?P<id>[0-9]+)/leave/(?P<membername>[^\/]+)$', 'coip.apps.membership.views.leave'), + (r'^name/(?P<id>[0-9]+)/leave/(?P<membername>[^\/]+)$', 'coip.apps.membership.views.leave'), + (r'^name/(?P<nid>[0-9]+)/consumers(?:\.([^\.]+))?$', 'coip.apps.consumer.views.list'), + (r'^name/(?P<nid>[0-9]+)/consumers/add$', 'coip.apps.consumer.views.add'), + (r'^name/(?P<nid>[0-9]+)/consumers/(?P<cid>[0-9]+)/remove$', 'coip.apps.consumer.views.remove'), # Name Links (r'^name/(?P<id>[0-9]+)/link/(?P<type>[0-9]+).json$', 'coip.apps.name.views.links'), # ACL diff --git a/templates/apps/consumer/edit.html b/templates/apps/consumer/edit.html new file mode 100644 index 0000000..a2e1b3a --- /dev/null +++ b/templates/apps/consumer/edit.html @@ -0,0 +1,3 @@ +{% extends "edit.html" %} +{% block widgets %} +{% endblock %}
\ No newline at end of file diff --git a/templates/apps/consumer/list.html b/templates/apps/consumer/list.html new file mode 100644 index 0000000..8efeb42 --- /dev/null +++ b/templates/apps/consumer/list.html @@ -0,0 +1,18 @@ +{% block content %} +<div id="acl" style="margin-bottom: 20px;"> + {% for c in consumers %} + <div id="{{c.id}}"> + <h3 class="listheader">{{c}}</h3> + <div> + {{c.consumer_provider.description}} + </div> + </div> + {% endfor %} +</div> +<ul class="ilist"> + <li class="button"><a href="/consumer/{{name.id}}/add">Add Consumer</a></li> + {% if name %} + <li class="button right"><input type="button" onClick="document.location='{{name.url}}'" value="Cancel"/></li> + {% endif %} +</ul> +{% endblock %}
\ No newline at end of file |