Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Added get_fieldname_text()
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Turner committed Feb 19, 2012
1 parent df0fd72 commit d0598c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
30 changes: 24 additions & 6 deletions smartlinks/fields.py
Expand Up @@ -66,14 +66,23 @@ class SmartLinkField(ModelCharField):
class Quote(models.Model):
link = smartlinks.SmartLinkField()
Provides ``get_<fieldname>_url`` method, which will automatically resolve
smartlink. EG in our example you can do::
This fields provides three magic methods:
``get_<fieldname>_url`` method, which will automatically resolve
smartlink. eg. in our example you can do::
q = Quote.objects.all()[0]
url = q.link_url() # Returns URI, or empty string when resolution fails.
url = q.get_link_url() # Returns URI, or empty string when resolution fails.
``get_<fieldname>_text`` method, which will automatically resolve
smartlink. eg. in our example you can do::
To get the actual object, use ``get_<fieldname>_object`` method. It
will return ``None`` if lookup failed or was ambiguous.
q = Quote.objects.all()[0]
url = q.get_link_text() # Returns text to use in the link.
To get the actual object referred to by the smartlink, use
``get_<fieldname>_object`` method. It will return ``None`` if lookup failed
or was ambiguous.
Note that the field returns the object URI, and not the rendered
``<a href=...></a>`` tag.
Expand Down Expand Up @@ -140,9 +149,18 @@ def resolve_smartlink_url(instance):
url = getattr(obj, conf.url_field, u"")
return url() if callable(url) else url

def resolve_smartlink_text(instance):
"""
Return the Text of the smartlink.
"""
link = getattr(instance, self.name)
parser = SmartLinkParser(smartlinks_conf)
return parser.get_smartlink_text(link) # sets verbose_text

setattr(cls, 'get_%s_object' % self.name, resolve_smartlink)
setattr(cls, 'get_%s_url' % self.name, resolve_smartlink_url)

setattr(cls, 'get_%s_text' % self.name, resolve_smartlink_text)

def south_field_triple(self):
"""
Return a suitable description of this field for South.
Expand Down
6 changes: 6 additions & 0 deletions smartlinks/parser.py
Expand Up @@ -36,6 +36,12 @@ def get_smartlinked_object(self, value):
"""
return self._find_object(self.finder.match(value))

def get_smartlink_text(self, value):
match = self.finder.match(value)
query = match.group("Query").strip()
return (match.groupdict().get('VerboseText',
'') or query).strip()

def parse(self, match):
"""
:param match: Regexp match object.
Expand Down

0 comments on commit d0598c1

Please sign in to comment.