Skip to content

Instantly share code, notes, and snippets.

@benzkji
Last active August 19, 2016 14:04
Show Gist options
  • Save benzkji/d0dcf94b056a078bdc50153e5fb69be1 to your computer and use it in GitHub Desktop.
Save benzkji/d0dcf94b056a078bdc50153e5fb69be1 to your computer and use it in GitHub Desktop.
LinkModel Example
from ....
class LinkBase(models.Model):
link_text = models.CharField(verbose_name=_('Link Text'), max_length=255,
blank=True, default='')
link_type = models.CharField(verbose_name=_('Link'), max_length=20, choices=LINK_TYPE_CHOICES,
blank=True, default='')
page = PageField(null=True, blank=True)
file = FilerFileField(null=True, blank=True, related_name='link')
mailto = models.EmailField(null=True, blank=True)
external = models.URLField(null=True, blank=True)
free = models.CharField(max_length=512, null=True, blank=True)
class Meta:
abstract = True
def get_link(self):
if self.link_type:
for key, display in LINK_TYPE_CHOICES:
if not key == self.link_type:
setattr(self, key, None)
if self.page:
if self.page.site.id == settings.SITE_ID:
return self.page.get_absolute_url()
else:
return 'http://' + self.page.site.domain + self.page.get_absolute_url()
elif self.file:
return self.file.url
elif self.external:
link = self.external
if not link.startswith('http'):
link = 'http://%s' % link
return link
elif self.mailto:
return "mailto:%s" % self.mailto
def get_link_text(self):
obj = None
if self.name:
return self.name
if self.link_type:
obj = getattr(self, self.link_type, None)
elif self.page:
obj = self.page
elif self.file:
obj = self.file
elif self.external:
return self.external
elif self.mailto:
return self.mailto
if not object is None:
return unicode(obj)
return ''
def get_link_type(self):
if self.link_type:
return self.link_type
if self.page:
return "page"
elif self.file:
return "file"
elif self.external:
return "external"
elif self.mailto:
return "mailto"
def get_link_target(self):
type = self.get_link_type()
if type in ['file', 'external']\
or type == 'page' and not self.page.site.id == settings.SITE_ID:
return "_blank"
return ""
from lxml.html import fragment_fromstring, tostring
from django import template
from django.template.defaultfilters import stringfilter
from my.models import Link
register = template.Library()
@register.filter
@stringfilter
def whatever_add_links(html):
fragment = fragment_fromstring("<div>" + html + "</div>")
links = fragment.cssselect('a')
for link in links:
if link.get('data-link_type', None):
kwargs = {}
dummy_link = LMLink()
for key, value in link.items():
if key.startswith('data-'):
new_key = key.replace('data-', '', 1)
if new_key == 'page_2':
new_key = 'page'
if hasattr(dummy_link, new_key):
if hasattr(dummy_link, new_key + "_id"):
# set fk directly
new_key = new_key + "_id"
if not value:
value = None
kwargs[new_key] = value
link.attrib.pop(key)
real_link = LMLink(**kwargs)
link.set('href', real_link.get_link())
return tostring(fragment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment