Commits

Matthew Schinckel committed 79186c7

Use settings.HYPERMEDA['base_links'] to store system-wide links.

Comments (0)

Files changed (6)

hypermedia/base.py

 
 from hypermedia.models import log_action, ADDITION, CHANGE, DELETION
 from hypermedia.models import RelList
+from hypermedia import settings
 
 import mimeparse
 
+# Set up a base_links function, that looks at settings.
+def base_links(view):
+    if callable(settings.base_links):
+        return settings.base_links(view)
+    return settings.base_links
+
 class AcceptResponseMixin(object):
     CONTENT_TYPES = [
         "application/vnd.Collection+JSON", 
     
 class ResourceMixin(AcceptResponseMixin, ConditionalAccessMixin):
     def get_links(self, **kwargs):
-        pass
+        return []
     
     def get_queries(self, **kwargs):
-        pass
+        return []
     
     def get_items(self, **kwargs):
-        pass
+        return []
     
     def get_template(self, **kwargs):
-        pass
+        return {}
     
     def get_template_form(self, **kwargs):
         pass
                         context[item] = value
         if 'form' not in context:
             context['form'] = self.get_template_form()
+        # Inject settings.HYPERMEDIA['base_links'](self)
+        if 'links' in context:
+            context['links'] = base_links(self) + context['links']
         return context
 
 class Resource(ResourceMixin, generic.TemplateView):
         
     def get_template(self, **kwargs):
         return self.convert_model()
-    
-    def get_links(self, **kwargs):
-        return getattr((self.model or self.queryset.model)(), 'get_links', lambda x:None)(self.request)
-    
-    def get_queries(self, **kwargs):
-        return getattr((self.model or self.queryset.model)(), 'get_queries', lambda x:None)(self.request)
-            
+                
     def convert_model(self, instance=None):
         """
         Given a model instance, generate the code that is expected by the 'items' list objects,
     def get_items(self, **kwargs):
         return [self.convert_model(self.object)]
     
-    def get_links(self, **kwargs):
-        return getattr(self.object, 'get_links', lambda x:None)(self.request)
-
-    def get_queries(self, **kwargs):
-        return getattr(self.object, 'get_queries', lambda x:None)(self.request)
-
     def get_template_names(self):
         return super(DetailResource, self).get_template_names() + ['hypermedia/detail.html']
     

hypermedia/settings.py

+from django.conf import settings
+
+SETTINGS = getattr(settings, 'HYPERMEDIA', {})
+
+base_links = SETTINGS.get('base_links', [])
+

test/project/links.py

+from django.core.urlresolvers import reverse
+
+def base_links(view):
+    links = [
+        {"rel": "root", "href": reverse('root'), "prompt": "Home"},
+    ]
+    if view.request.user.is_anonymous():
+        links.append(
+            {"rel": "login", "href": reverse('admin:index'), "prompt": "Login"}
+        )
+    else:
+        links.append(
+            {"rel": "user", "href": reverse('user'), "prompt": "You"},
+        )
+    return links

test/project/settings.py

         },
     }
 }
+
+
+import links
+HYPERMEDIA = {
+    'base_links': links.base_links
+}

test/project/tasks/views.py

 
 from tasks.models import Task
 
-# Don't really like this, too tightly coupled.
-from project.views import base_links
-
 class TaskList(ListResource):
     model = Task
     
         return reverse('tasks:list')
     
     def get_links(self, **kwargs):
-        return base_links() + []
+        return []
 
 class TaskDetail(DetailResource):
     model = Task
         return reverse('tasks:list')
         
     def get_links(self, **kwargs):
-        return base_links() + [
+        return [
             {"rel": "collection", "href": reverse('tasks:list'), "prompt":"Task List"},
             {"rel": "self", "href": kwargs.get('object', self.object).get_absolute_url(), "prompt": "This Task"},
         ]

test/project/views.py

 
 from forms import UserDetailForm
 
-def base_links():
-    return [
-        {"rel": "root", "href": reverse('root'), "prompt": "Home"},
-        {"rel": "user", "href": reverse('user'), "prompt": "You"},
-    ]
 
 class Root(Resource):
     template_name = 'base.html'
     
     def get_links(self):
-        return base_links() + [
+        return [
             {"rel": "links", "href": reverse('tasks:list'), "prompt": "Task List"},
         ]
 
         ]
         if self.request.user.is_staff:
             links.append({"rel":"admin", "href": reverse("admin:index"), "prompt":"Admin"})
-        return base_links() + links
+        return links