1
1
# -*- 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
6
7
from django .conf import settings
7
8
from django .core .exceptions import ValidationError
8
- from django .db import models
9
9
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
11
13
12
14
from djangocms_attributes_field .fields import AttributesField
13
15
@@ -43,24 +45,25 @@ def get_templates():
43
45
)
44
46
)
45
47
48
+ HOSTNAME = getattr (
49
+ settings ,
50
+ 'DJANGOCMS_LINK_INTRANET_HOSTNAME_PATTERN' ,
51
+ None
52
+ )
53
+
46
54
TARGET_CHOICES = (
47
55
('_blank' , _ ('Open in new window' )),
48
56
('_self' , _ ('Open in same window' )),
49
57
('_parent' , _ ('Delegate to parent' )),
50
58
('_top' , _ ('Delegate to top' )),
51
59
)
52
60
53
-
54
61
@python_2_unicode_compatible
55
62
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
60
64
search_fields = ('name' , )
61
65
62
- url_validators = [IntranetURLValidator (intranet_host_re = getattr (
63
- settings , 'DJANGOCMS_LINK_INTRANET_HOSTNAME_PATTERN' , None )), ]
66
+ url_validators = [IntranetURLValidator (intranet_host_re = HOSTNAME ),]
64
67
65
68
template = models .CharField (
66
69
verbose_name = _ ('Template' ),
@@ -73,7 +76,7 @@ class AbstractLink(CMSPlugin):
73
76
blank = True ,
74
77
max_length = 255 ,
75
78
)
76
- # Re : max_length, see: http://stackoverflow.com/questions/417142/
79
+ # re : max_length, see: http://stackoverflow.com/questions/417142/
77
80
external_link = models .URLField (
78
81
verbose_name = _ ('External link' ),
79
82
blank = True ,
@@ -152,34 +155,26 @@ class Meta:
152
155
abstract = True
153
156
154
157
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 ):
171
166
if self .phone :
172
- link = 'tel:%s' % self .phone
167
+ link = 'tel:{}' . format ( self .phone . replace ( ' ' , '' ))
173
168
elif self .mailto :
174
- link = 'mailto:%s' % self .mailto
169
+ link = 'mailto:{}' . format ( self .mailto )
175
170
elif self .external_link :
176
171
link = self .external_link
177
172
elif self .internal_link_id :
178
173
link = self .internal_link .get_absolute_url ()
179
174
else :
180
175
link = ''
181
176
if (self .external_link or self .internal_link or not link ) and self .anchor :
182
- link += '#%s' % self .anchor
177
+ link += '#{}' . format ( self .anchor )
183
178
return link
184
179
185
180
0 commit comments