Skip to content

Commit

Permalink
adaption from comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FinalAngel committed Sep 14, 2016
1 parent c7cad4e commit 958b154
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ need to manually configure this feature:
* run ``pip install django-select2>=4.3,<5.0``
* add ``django_select2`` to your ``INSTALLED_APPS``
* add ``url(r'^select2/', include('django_select2.urls')),`` to your ``urls.py``
* set ``DJANGOCMS_LINK_SELECT2 = True`` in your ``settings.py``
* set ``DJANGOCMS_LINK_USE_SELECT2 = True`` in your ``settings.py``


Running Tests
Expand Down
2 changes: 1 addition & 1 deletion aldryn_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Form(forms.BaseForm):
The following settings need to be configured on your project separately
as we don't want to expose them as aldryn configurations yet:
DJANGOCMS_LINK_INTRANET_HOSTNAME_PATTERN
DJANGOCMS_LINK_SELECT2
DJANGOCMS_LINK_USE_SELECT2
"""

def clean(self):
Expand Down
2 changes: 2 additions & 0 deletions djangocms_link/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.contrib.sites.models import Site
Expand Down
8 changes: 6 additions & 2 deletions djangocms_link/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

ENABLE_SELECT2 = getattr(
settings,
'DJANGOCMS_LINK_SELECT2',
'DJANGOCMS_LINK_USE_SELECT2',
False
)


if 'django_select2' in settings.INSTALLED_APPS and ENABLE_SELECT2:
if ENABLE_SELECT2 and 'django_select2' in settings.INSTALLED_APPS:
from django_select2.fields import AutoModelSelect2Field

class PageSearchField(AutoModelSelect2Field):
Expand Down Expand Up @@ -50,3 +50,7 @@ def prepare_value(self, value):
if not value:
return None
return super(UserSearchField, self).prepare_value(value)
else:
from cms.forms.fields import PageSelectFormField

PageSearchField = PageSelectFormField
17 changes: 5 additions & 12 deletions djangocms_link/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@

from djangocms_attributes_field.widgets import AttributesWidget

from .fields import PageSearchField
from .models import Link


class LinkForm(ModelForm):
try:
from djangocms_link.fields import PageSearchField
internal_link = PageSearchField(
label=_('Internal link'),
required=False,
)
except ImportError:
from cms.forms.fields import PageSelectFormField
internal_link = PageSelectFormField(
label=_('Internal link'),
required=False,
)
internal_link = PageSearchField(
label=_('Internal link'),
required=False,
)

def for_site(self, site):
# override the internal_link fields queryset to contains just pages for
Expand Down
12 changes: 4 additions & 8 deletions djangocms_link/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Enables the user to add a "Link" plugin that displays a link
using the HTML <a> tag.
"""
from __future__ import unicode_literals

from django.db import models
from django.conf import settings
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -63,7 +65,6 @@ class AbstractLink(CMSPlugin):
external_link = models.URLField(
verbose_name=_('External link'),
blank=True,
null=True,
max_length=2040,
validators=url_validators,
help_text=_('Provide a valid url to an external website.'),
Expand All @@ -88,13 +89,11 @@ class AbstractLink(CMSPlugin):
mailto = models.EmailField(
verbose_name=_('Email address'),
blank=True,
null=True,
max_length=255,
)
phone = models.CharField(
verbose_name=_('Phone'),
blank=True,
null=True,
max_length=255,
)
# advanced options
Expand Down Expand Up @@ -129,7 +128,7 @@ def __str__(self):
def get_short_description(self):
if self.name:
return '{} ({})'.format(self.name, self.get_link())
return self.get_link()
return self.get_link() or ugettext('<link is missing>')

def get_link(self):
if self.phone:
Expand All @@ -142,11 +141,8 @@ def get_link(self):
link = self.internal_link.get_absolute_url()
else:
link = ''
if (self.external_link or self.internal_link_id or not link) and self.anchor:
if (not self.phone and not self.mailto) and self.anchor:
link += '#{}'.format(self.anchor)
# link can be empty if a page attached to it has been removed
if link == '':
link = '<link is missing>'

return link

Expand Down

0 comments on commit 958b154

Please sign in to comment.