Commits

Ryan Herriman committed 71ea523

Added support for viewing attachments via the API.

Comments (0)

Files changed (2)

 	for f in obj._meta.fields:
 		if f.serialize and f.name not in exclude:
 			d[f.name] = _serialize(getattr(obj, f.name))
-	# Add the meta property if we're dealing with a Leaf.
+	# Add the attachment and meta properties if we're dealing with a Leaf.
 	if isinstance(obj, Leaf):
+		d['attachments'] = [model_to_dict(a, exclude=('leaf',)) for a in obj.attachment_list]
 		d['meta'] = obj.meta
 	return d
 
 		else:
 			return self.get_absolute_url()
 
+	def get_attachments(self):
+		"""
+		Returns a list of all attachments for this leaf. Cached so subsequent
+		accesses do not hit the database.
+		"""
+		if not hasattr(self, '_attachment_cache'):
+			self._attachment_cache = [a for a in self.attachments.all()]
+		return self._attachment_cache
+	attachment_list = property(get_attachments)
+
 	def get_meta(self):
 		"""
 		Returns a dictionary of all user-defined key/value pairs for this leaf.