Skip to content

Commit e0aa9dd

Browse files
committed
further cleanup
1 parent d333a7a commit e0aa9dd

13 files changed

+91
-125
lines changed

.coveragerc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[run]
22
branch = True
3-
source = djangocms_picture
3+
source = djangocms_link
44
omit =
55
migrations/*
66
tests/*

djangocms_link/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# -*- coding: utf-8 -*-
2-
32
__version__ = '1.8.3rc1'

djangocms_link/cms_plugins.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import, print_function, unicode_literals
2+
from django.conf import settings
3+
from django.utils.translation import ugettext_lazy as _
4+
from django.contrib.sites.models import Site
35

46
from cms.plugin_base import CMSPluginBase
57
from cms.plugin_pool import plugin_pool
6-
from django.conf import settings
7-
from django.contrib.sites.models import Site
8-
from django.utils.translation import ugettext_lazy as _
98

109
from .forms import LinkForm
1110
from .models import Link
@@ -15,7 +14,6 @@ class LinkPlugin(CMSPluginBase):
1514
model = Link
1615
form = LinkForm
1716
name = _('Link')
18-
render_template = 'cms/plugins/link.html'
1917
text_enabled = True
2018
allow_children = True
2119

@@ -43,7 +41,7 @@ def get_render_template(self, context, instance, placeholder):
4341
return 'djangocms_link/{}/link.html'.format(instance.template)
4442

4543
def render(self, context, instance, placeholder):
46-
context['link'] = instance.link()
44+
context['link'] = instance.get_link()
4745
return super(LinkPlugin, self).render(context, instance, placeholder)
4846

4947
def get_form(self, request, obj=None, **kwargs):

djangocms_link/fields.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import, print_function, unicode_literals
3-
42
from django.conf import settings
53

4+
65
if 'django_select2' in settings.INSTALLED_APPS:
76
from django_select2.fields import AutoModelSelect2Field
87

djangocms_link/forms.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import, print_function, unicode_literals
3-
42
from django.forms import ValidationError
53
from django.forms.models import ModelForm
64
from django.forms.widgets import Media
@@ -38,13 +36,24 @@ def __init__(self, *args, **kwargs):
3836
super(LinkForm, self).__init__(*args, **kwargs)
3937
self.fields['attributes'].widget = AttributesWidget()
4038

41-
def _get_media(self):
42-
"""
43-
Provide a description of all media required to render the widgets on this form
44-
"""
45-
media = Media()
46-
for field in self.fields.values():
47-
media = media + field.widget.media
48-
media._js = ['cms/js/libs/jquery.min.js'] + media._js
49-
return media
50-
media = property(_get_media)
39+
def clean(self):
40+
cleaned_data = super(LinkForm, self).clean()
41+
external_link = cleaned_data.get('external_link')
42+
internal_link = cleaned_data.get('internal_link')
43+
mailto = cleaned_data.get('mailto')
44+
phone = cleaned_data.get('phone')
45+
anchor = cleaned_data.get('anchor')
46+
if not any([external_link, internal_link, mailto, phone, anchor]):
47+
raise ValidationError(_('At least one link is required.'))
48+
return cleaned_data
49+
50+
# def _get_media(self):
51+
# """
52+
# Provide a description of all media required to render the widgets on this form
53+
# """
54+
# media = Media()
55+
# for field in self.fields.values():
56+
# media = media + field.widget.media
57+
# media._js = ['cms/js/libs/jquery.min.js'] + media._js
58+
# return media
59+
# media = property(_get_media)

djangocms_link/migrations/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

djangocms_link/models.py

+28-33
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import, print_function, unicode_literals
3-
4-
from cms.models import CMSPlugin, Page
5-
2+
"""
3+
Enables the user to add a "Link" plugin that displays a link
4+
using the HTML <a> tag.
5+
"""
6+
from django.db import models
67
from django.conf import settings
78
from django.core.exceptions import ValidationError
8-
from django.db import models
99
from django.utils.encoding import python_2_unicode_compatible
10-
from django.utils.translation import ugettext_lazy as _
10+
from django.utils.translation import ugettext, ugettext_lazy as _
11+
12+
from cms.models import CMSPlugin, Page
1113

1214
from djangocms_attributes_field.fields import AttributesField
1315

@@ -43,24 +45,25 @@ def get_templates():
4345
)
4446
)
4547

48+
HOSTNAME = getattr(
49+
settings,
50+
'DJANGOCMS_LINK_INTRANET_HOSTNAME_PATTERN',
51+
None
52+
)
53+
4654
TARGET_CHOICES = (
4755
('_blank', _('Open in new window')),
4856
('_self', _('Open in same window')),
4957
('_parent', _('Delegate to parent')),
5058
('_top', _('Delegate to top')),
5159
)
5260

53-
5461
@python_2_unicode_compatible
5562
class AbstractLink(CMSPlugin):
56-
"""
57-
A link to an other page or to an external website
58-
"""
59-
# Used by django-cms search
63+
# used by django CMS search
6064
search_fields = ('name', )
6165

62-
url_validators = [IntranetURLValidator(intranet_host_re=getattr(
63-
settings, 'DJANGOCMS_LINK_INTRANET_HOSTNAME_PATTERN', None)), ]
66+
url_validators = [IntranetURLValidator(intranet_host_re=HOSTNAME),]
6467

6568
template = models.CharField(
6669
verbose_name=_('Template'),
@@ -73,7 +76,7 @@ class AbstractLink(CMSPlugin):
7376
blank=True,
7477
max_length=255,
7578
)
76-
# Re: max_length, see: http://stackoverflow.com/questions/417142/
79+
# re: max_length, see: http://stackoverflow.com/questions/417142/
7780
external_link = models.URLField(
7881
verbose_name=_('External link'),
7982
blank=True,
@@ -152,34 +155,26 @@ class Meta:
152155
abstract = True
153156

154157
def __str__(self):
155-
return self.name
156-
157-
def clean(self):
158-
if not self.external_link or self.internal_link:
159-
raise ValidationError(_('Error'))
160-
# cleaned_data = super(LinkForm, self).clean()
161-
# url = cleaned_data.get('url')
162-
# internal_link = cleaned_data.get('internal_link')
163-
# mailto = cleaned_data.get('mailto')
164-
# phone = cleaned_data.get('phone')
165-
# anchor = cleaned_data.get('anchor')
166-
# if not any([url, internal_link, mailto, phone, anchor]):
167-
# raise ValidationError(_('At least one link is required.'))
168-
# return cleaned_data
169-
170-
def link(self):
158+
return self.name or str(self.pk)
159+
160+
def get_short_description(self):
161+
if self.name:
162+
return '{} ({})'.format(self.name, self.get_link())
163+
return self.get_link()
164+
165+
def get_link(self):
171166
if self.phone:
172-
link = 'tel:%s' % self.phone
167+
link = 'tel:{}'.format(self.phone.replace(' ', ''))
173168
elif self.mailto:
174-
link = 'mailto:%s' % self.mailto
169+
link = 'mailto:{}'.format(self.mailto)
175170
elif self.external_link:
176171
link = self.external_link
177172
elif self.internal_link_id:
178173
link = self.internal_link.get_absolute_url()
179174
else:
180175
link = ''
181176
if (self.external_link or self.internal_link or not link) and self.anchor:
182-
link += '#%s' % self.anchor
177+
link += '#{}'.format(self.anchor)
183178
return link
184179

185180

djangocms_link/validators.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import, print_function, unicode_literals
3-
42
import re
53

64
from django.core.validators import URLValidator

tests/requirements-tests.txt

-5
This file was deleted.

tests/requirements.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
django-cms>=2.3
2-
Django-Select2>=3.1.2
1+
# requirements from setup.py
2+
Django-Select2
3+
# other requirements
4+
djangocms-helper>=0.9.2,<0.10
5+
tox
6+
coverage

tests/settings.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
HELPER_SETTINGS = {
5+
'INSTALLED_APPS': [
6+
'django_select2',
7+
],
8+
'ALLOWED_HOSTS': ['localhost'],
9+
'CMS_LANGUAGES': {
10+
1: [{
11+
'code': 'en',
12+
'name': 'English',
13+
}]
14+
},
15+
'LANGUAGE_CODE': 'en',
16+
}
17+
18+
def run():
19+
from djangocms_helper import runner
20+
runner.cms('djangocms_link')
21+
22+
if __name__ == '__main__':
23+
run()

tests/test_settings.py

-50
This file was deleted.

tests/tests_model.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import, print_function, unicode_literals
3-
42
import django
5-
from cms.api import add_plugin, create_page
6-
from cms.plugin_rendering import PluginContext, render_placeholder
3+
74
from django.core.management import call_command
5+
from django.utils import unittest
86
from django.utils.encoding import force_text
97
from django.utils.six import StringIO
10-
from djangocms_helper.base_test import BaseTestCase
118

12-
# Need the copy of unittest2 bundled with Django for @skipIf on Python 2.6.
13-
try:
14-
from django.utils import unittest
15-
except ImportError:
16-
import unittest
9+
from cms.api import add_plugin, create_page
10+
from cms.plugin_rendering import PluginContext, render_placeholder
11+
12+
from djangocms_helper.base_test import BaseTestCase
1713

1814

1915
class LinkTestCase(BaseTestCase):
@@ -69,7 +65,6 @@ def test_render(self):
6965
# when rendered, it counts as a space in html, which leads to incorrect rendering
7066
self.assertEqual(output, '<span class="plugin_link"><a href="http://example.com" >text body</a></span>')
7167

72-
@unittest.skipIf(django.VERSION[:2] < (1, 7), 'Skipping Django 1.7 test.')
7368
def test_makemigrations(self):
7469
"""
7570
Fail if there are schema changes with no migrations.

0 commit comments

Comments
 (0)