瀏覽代碼

Implementation of ExtendedURLField type field for django models

master
Dawid Lorenz 14 年之前
父節點
當前提交
09f4894bc5
共有 2 個檔案被更改,包括 60 行新增1 行删除
  1. 4
    1
      oscar/apps/promotions/abstract_models.py
  2. 56
    0
      oscar/core/fields.py

+ 4
- 1
oscar/apps/promotions/abstract_models.py 查看文件

@@ -4,6 +4,8 @@ from django.utils.translation import ugettext as _
4 4
 from django.core.urlresolvers import reverse
5 5
 from django.core.exceptions import ValidationError
6 6
 
7
+from oscar.core.fields import ExtendedURLField
8
+
7 9
 BANNER_FOLDER = settings.OSCAR_BANNER_FOLDER
8 10
 POD_FOLDER = settings.OSCAR_POD_FOLDER
9 11
 
@@ -15,6 +17,7 @@ POSITION_CHOICES = (
15 17
     (RAW_HTML, _("Raw HTML"))
16 18
 )
17 19
 
20
+
18 21
 class AbstractPromotion(models.Model):
19 22
     u"""
20 23
     A promotion model.
@@ -24,7 +27,7 @@ class AbstractPromotion(models.Model):
24 27
 
25 28
     """
26 29
     name = models.CharField(_("Name"), max_length=128)
27
-    link_url = models.URLField(blank=True, null=True, help_text="""This is 
30
+    link_url = ExtendedURLField(blank=True, null=True, help_text="""This is 
28 31
         where this promotion links to""")
29 32
 
30 33
     # Three ways of supplying the content

+ 56
- 0
oscar/core/fields.py 查看文件

@@ -0,0 +1,56 @@
1
+from django.core.exceptions import ValidationError
2
+from django.core.validators import URLValidator
3
+from django.core.urlresolvers import resolve
4
+from django.db import models
5
+from django.http import Http404
6
+
7
+class ExtendedURLField(models.CharField):
8
+    u"""
9
+    Custom field similar to URLField type field, however also accepting 
10
+    and validating local relative URLs, ie. '/product/'
11
+    """
12
+    description = "Extended URL"
13
+
14
+    def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
15
+        kwargs['max_length'] = kwargs.get('max_length', 200)
16
+        models.CharField.__init__(self, verbose_name, name, **kwargs)
17
+        self.is_local_url = False
18
+        self.verify_exists = verify_exists
19
+        
20
+    def get_prep_value(self, value):
21
+        u"""
22
+        Make sure local URLs have preceding and trailing slashes 
23
+        """
24
+        if self.is_local_url == True:
25
+            value = self.fix_local_url(value)
26
+        return value
27
+
28
+    def validate(self, value, model_instance):
29
+        u"""
30
+        Overrides global vaidate method to check whether URL is valid
31
+        """
32
+        v = URLValidator(verify_exists=self.verify_exists)
33
+        try:
34
+            v(value)
35
+        except ValidationError, e:
36
+            if (e.code == 'invalid_link'):
37
+                raise ValidationError('This link appears to be broken')
38
+            self.validate_local_url(value)
39
+
40
+    def validate_local_url(self, value):
41
+        u"""
42
+        Validate local URL name
43
+        """
44
+        try:
45
+            value = self.fix_local_url(value)
46
+            resolve(value)
47
+            self.is_local_url = True
48
+        except Http404:
49
+            raise ValidationError('Specified page does not exist')
50
+
51
+    def fix_local_url(self, value):
52
+        u"""
53
+        Puts preceding and trailing slashes to local URL name 
54
+        """
55
+        return '/' + value.strip('/') + '/'
56
+    

Loading…
取消
儲存