summaryrefslogtreecommitdiff
path: root/coip/apps/consumer
diff options
context:
space:
mode:
Diffstat (limited to 'coip/apps/consumer')
-rw-r--r--coip/apps/consumer/__init__.py36
-rw-r--r--coip/apps/consumer/forms.py11
-rw-r--r--coip/apps/consumer/models.py25
-rw-r--r--coip/apps/consumer/urls.py10
-rw-r--r--coip/apps/consumer/views.py64
5 files changed, 146 insertions, 0 deletions
diff --git a/coip/apps/consumer/__init__.py b/coip/apps/consumer/__init__.py
new file mode 100644
index 0000000..fd6cc15
--- /dev/null
+++ b/coip/apps/consumer/__init__.py
@@ -0,0 +1,36 @@
+
+__author__ = 'leifj'
+
+from django.conf import settings
+from django.utils.importlib import import_module
+
+_consumer_provider_modules = list()
+
+def autodiscover():
+ for app in settings.INSTALLED_APPS:
+ mod = import_module("%.models" % app)
+ if hasattr(mod,'consumer_providers' and hasattr(mod.consumer_providers,'__call__')):
+ _consumer_provider_modules.append(mod)
+
+def consumer_providers():
+ p = list()
+ for mod in _consumer_provider_modules:
+ p.extend(mod.token_providers())
+ return p
+
+def consumer_provider(name):
+ for mod in _consumer_provider_modules:
+ for p in mod.token_providers():
+ if p.name == name:
+ return p
+ return None
+
+autodiscover()
+
+# self.authorization_uri
+# self.logo
+# 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