Skip to content
Browse files

minot tidying up for links

  • Loading branch information...
1 parent 7afc9ea commit 87cfc22b90b2525193b79c3eeae0785799e80f15 @evildmp committed Dec 5, 2011
Showing with 88 additions and 11 deletions.
  1. +62 −0 docs/links.txt
  2. +6 −1 links/link_functions.py
  3. +19 −9 links/link_schemas.py
  4. +1 −1 links/schema_registry.py
View
62 docs/links.txt
@@ -0,0 +1,62 @@
+Generic Links App
+
+The links app allows generic links between any models. You need to add a setting similar to this:
+
+To register specific models that they can be links, a link_schema.py module needs to be created in
+your app. The registry system is comparable with how django.contrib.admin works.
+Every linkable needs a certain list of attributes. Either defined on the model itself or provided with a wrapper.
+A list of the needed attributes is in `links.schema_registry.ATTRIBUTES`. Be aware that 'title' and 'description'
+are also used in the autocomplete widget.
+
+There a multiple ways how you can define a link/wrapper:
+
+`from links import schema
+from news_and_events import models
+from news_and_events import admin`
+
+1. Very simple case. Your model has all the needed attributes already
+
+`schema.register(models.Event, search_fields=admin.EventAdmin.search_fields)`
+
+
+2. The attributes on the model are named different:
+
+`schema.register(models.Event, search_fields=admin.EventADmin.search_fields,
+ title='name', description='some_foreign_key.title', heading='"Event"')`
+
+Each named argument after search_fields represents a attribute the wrapper will have.
+A string means the dot noted path will be used to traverse through object, starting from
+the linked object. `description='some_foreign_key.title'` will result in using
+`theobj.some_foreign_key.title` for the title. It does not matter if the attributes are properties
+or callable methods, both work.
+
+`heading='"Event"'` will use a static string for the heading. Both `"'foo'"` and `'"bar"'` work.
+
+3. You want to do some extra calculation.
+
+This works by providing a callable that takes one argument: the linked object
+It can be a lamda function or a external function
+
+`def some_very_complicated_function(obj):
+ r = []
+ for x in obj.something.all():
+ if x.y:
+ r.append(u"foo: %s" % x.bar)
+ else:
+ r.append(u"stuff: %s" % x.bar)
+ return "<br />".join(r)
+schema.register(models.Event, search_fields=admin.EventAdmin.search_fields,
+ title=lambda obj: u"%s (%s)" % (unicode(obj), obj.active_count),
+ description=some_very_complicated_function)`
+
+
+4. You have a quite complex wrapper: make your own Wrapper subclass
+
+`from links import LinkWrapper
+class MyEventLinkWrapper(LinkWrapper):
+ search_fields = admin.EventAdmin.search_fields
+ def title(self):
+ return self.obj.something(extra=' asdfsadf')
+ def description(self):
+ return "static description"
+schema.register_wrapper(Event, MyEventLinkWrapper)`
View
7 links/link_functions.py
@@ -4,7 +4,12 @@
def object_links(content_object):
model = ContentType.objects.get_for_model(content_object)
links = ObjectLink.objects.filter(content_type__pk=model.id, object_id = content_object.id).order_by('destination_content_type')
- links = [link for link in links if link.wrapped_destination_obj.obj]
+ # for link in links:
+ # print "**", link.wrapped_destination_obj.obj, link.heading()
+
+ # test for link.heading() - if "", then no heading was defined for this type
+ # this is a way to catch cases where an image was added as a file link
+ links = [link for link in links if link.wrapped_destination_obj.obj and link.heading()]
def mykey(obj): # would a lambda be nicer here?
return obj.wrapped_destination_obj.heading()
View
28 links/link_schemas.py
@@ -38,20 +38,30 @@ def heading(self):
if 'filer' in settings.INSTALLED_APPS:
from filer.models import File
from filer.admin import FileAdmin
+
def the_path(obj, items=[]):
items[0:0] = [obj.name]
if obj.parent:
items = the_path(obj.parent, items)
return items
- def file_description(obj):
- if obj.folder:
- s = u" &raquo; ".join(the_path(obj.folder))
- else:
- s = obj.logical_folder.name
- #return u"Folder: %s" % s
- return obj.description
+
+ class FileLinkWrapper(LinkWrapper):
+ search_fields = ['name', 'original_filename', 'sha1', 'description']
+ def description(self):
+ obj = self.obj
+ if obj.folder:
+ s = u" &raquo; ".join(the_path(obj.folder))
+ else:
+ s = obj.logical_folder.name
+ #return u"Folder: %s" % s
+ return obj.description
+
+ def heading(self):
+ return u"Files"
+
+ schema.register_wrapper(File, FileLinkWrapper)
- schema.register(File, FileAdmin.search_fields, title="label", description=file_description, heading="'Files'", url="url", #short_text = 'name'
- )
+ # schema.register(File, FileAdmin.search_fields, title="label", description="'file_description'", heading="'Files'", url="url", #short_text = 'name'
+ # )
View
2 links/schema_registry.py
@@ -24,7 +24,7 @@ def metatag(self):
return call_if_callable(getattr(self.obj, 'metatag', 'no metatag defined'))
def heading(self):
- return call_if_callable(getattr(self.obj, 'heading', 'no heading defined'))
+ return call_if_callable(getattr(self.obj, 'heading', ''))
ATTRIBUTES = [

0 comments on commit 87cfc22

Please sign in to comment.
Something went wrong with that request. Please try again.