Commit 20f4243581c259005c56af818e5a0147812801eb
1 parent
fcc17379
Exists in
master
and in
2 other branches
bug fix for site link creation
Showing
2 changed files
with
27 additions
and
17 deletions
Show diff stats
links/forms.py
@@ -4,6 +4,8 @@ from django.utils.translation import ugettext_lazy as _ | @@ -4,6 +4,8 @@ from django.utils.translation import ugettext_lazy as _ | ||
4 | from django.utils.html import strip_tags | 4 | from django.utils.html import strip_tags |
5 | from django.core.exceptions import ValidationError | 5 | from django.core.exceptions import ValidationError |
6 | 6 | ||
7 | +import validators | ||
8 | + | ||
7 | from subjects.models import Tag | 9 | from subjects.models import Tag |
8 | from subjects.forms import ParticipantsMultipleChoiceField | 10 | from subjects.forms import ParticipantsMultipleChoiceField |
9 | 11 | ||
@@ -15,22 +17,22 @@ class LinkForm(forms.ModelForm): | @@ -15,22 +17,22 @@ class LinkForm(forms.ModelForm): | ||
15 | MAX_UPLOAD_SIZE = 10*1024*1024 | 17 | MAX_UPLOAD_SIZE = 10*1024*1024 |
16 | 18 | ||
17 | students = ParticipantsMultipleChoiceField(queryset = None, required = False) | 19 | students = ParticipantsMultipleChoiceField(queryset = None, required = False) |
18 | - | 20 | + |
19 | def __init__(self, *args, **kwargs): | 21 | def __init__(self, *args, **kwargs): |
20 | super(LinkForm, self).__init__(*args, **kwargs) | 22 | super(LinkForm, self).__init__(*args, **kwargs) |
21 | 23 | ||
22 | self.subject = kwargs['initial'].get('subject', None) | 24 | self.subject = kwargs['initial'].get('subject', None) |
23 | - | 25 | + |
24 | if self.instance.id: | 26 | if self.instance.id: |
25 | self.subject = self.instance.topic.subject | 27 | self.subject = self.instance.topic.subject |
26 | self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) | 28 | self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) |
27 | - | 29 | + |
28 | self.fields['students'].queryset = self.subject.students.all() | 30 | self.fields['students'].queryset = self.subject.students.all() |
29 | self.fields['groups'].queryset = self.subject.group_subject.all() | 31 | self.fields['groups'].queryset = self.subject.group_subject.all() |
30 | 32 | ||
31 | tags = forms.CharField(label = _('Tags'), required = False) | 33 | tags = forms.CharField(label = _('Tags'), required = False) |
32 | - link_url = forms.URLField(label = _('Website URL'),required=True) | ||
33 | - | 34 | + link_url = forms.CharField(label = _('Website URL'),required=True) |
35 | + | ||
34 | class Meta: | 36 | class Meta: |
35 | model = Link | 37 | model = Link |
36 | fields = ['name','link_url', 'brief_description', 'all_students', 'students', 'groups', 'visible'] | 38 | fields = ['name','link_url', 'brief_description', 'all_students', 'students', 'groups', 'visible'] |
@@ -49,13 +51,13 @@ class LinkForm(forms.ModelForm): | @@ -49,13 +51,13 @@ class LinkForm(forms.ModelForm): | ||
49 | 51 | ||
50 | cleaned_data = self.cleaned_data | 52 | cleaned_data = self.cleaned_data |
51 | 53 | ||
52 | - | 54 | + |
53 | return cleaned_data | 55 | return cleaned_data |
54 | - | 56 | + |
55 | 57 | ||
56 | def clean_name(self): | 58 | def clean_name(self): |
57 | name = self.cleaned_data.get('name', '') | 59 | name = self.cleaned_data.get('name', '') |
58 | - | 60 | + |
59 | topics = self.subject.topic_subject.all() | 61 | topics = self.subject.topic_subject.all() |
60 | 62 | ||
61 | for topic in topics: | 63 | for topic in topics: |
@@ -63,7 +65,7 @@ class LinkForm(forms.ModelForm): | @@ -63,7 +65,7 @@ class LinkForm(forms.ModelForm): | ||
63 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() | 65 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() |
64 | else: | 66 | else: |
65 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() | 67 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() |
66 | - | 68 | + |
67 | if same_name > 0: | 69 | if same_name > 0: |
68 | self._errors['name'] = [_('There is already a link with this name on this subject')] | 70 | self._errors['name'] = [_('There is already a link with this name on this subject')] |
69 | 71 | ||
@@ -72,7 +74,15 @@ class LinkForm(forms.ModelForm): | @@ -72,7 +74,15 @@ class LinkForm(forms.ModelForm): | ||
72 | return name | 74 | return name |
73 | 75 | ||
74 | 76 | ||
75 | - | 77 | + |
78 | + def clean_link_url(self): | ||
79 | + link_url = self.cleaned_data.get('link_url','') | ||
80 | + if link_url[0].lower() != "h": link_url = "https://" + link_url | ||
81 | + if validators.url(link_url) != True: | ||
82 | + self._errors['link_url'] = [_('Invalid URL. It should be an valid link.')] | ||
83 | + return ValueError | ||
84 | + | ||
85 | + return link_url | ||
76 | 86 | ||
77 | def save(self, commit = True): | 87 | def save(self, commit = True): |
78 | super(LinkForm, self).save(commit = True) | 88 | super(LinkForm, self).save(commit = True) |
@@ -87,7 +97,7 @@ class LinkForm(forms.ModelForm): | @@ -87,7 +97,7 @@ class LinkForm(forms.ModelForm): | ||
87 | for prev in previous_tags: | 97 | for prev in previous_tags: |
88 | if not prev.name in tags: | 98 | if not prev.name in tags: |
89 | self.instance.tags.remove(prev) | 99 | self.instance.tags.remove(prev) |
90 | - | 100 | + |
91 | for tag in tags: | 101 | for tag in tags: |
92 | tag = tag.strip() | 102 | tag = tag.strip() |
93 | 103 | ||
@@ -101,4 +111,4 @@ class LinkForm(forms.ModelForm): | @@ -101,4 +111,4 @@ class LinkForm(forms.ModelForm): | ||
101 | if not new_tag in self.instance.tags.all(): | 111 | if not new_tag in self.instance.tags.all(): |
102 | self.instance.tags.add(new_tag) | 112 | self.instance.tags.add(new_tag) |
103 | 113 | ||
104 | - return self.instance | ||
105 | \ No newline at end of file | 114 | \ No newline at end of file |
115 | + return self.instance |
links/models.py
@@ -10,9 +10,9 @@ from django.core.urlresolvers import reverse_lazy | @@ -10,9 +10,9 @@ from django.core.urlresolvers import reverse_lazy | ||
10 | 10 | ||
11 | # Create your models here. | 11 | # Create your models here. |
12 | class Link(Resource): | 12 | class Link(Resource): |
13 | - link_url = models.URLField(verbose_name = _("Link_URL")) | ||
14 | - | ||
15 | - | 13 | + link_url = models.CharField( _("Link_URL"),max_length=250 ) |
14 | + | ||
15 | + | ||
16 | class Meta: | 16 | class Meta: |
17 | verbose_name = "Link" | 17 | verbose_name = "Link" |
18 | verbose_name_plural = "Links" | 18 | verbose_name_plural = "Links" |
@@ -28,6 +28,6 @@ class Link(Resource): | @@ -28,6 +28,6 @@ class Link(Resource): | ||
28 | 28 | ||
29 | def delete_link(self): | 29 | def delete_link(self): |
30 | return 'links:delete' | 30 | return 'links:delete' |
31 | - | 31 | + |
32 | def delete_message(self): | 32 | def delete_message(self): |
33 | - return _('Are you sure you want delete the Website link') | ||
34 | \ No newline at end of file | 33 | \ No newline at end of file |
34 | + return _('Are you sure you want delete the Website link') |