Commit 20f4243581c259005c56af818e5a0147812801eb

Authored by Jose Guilherme
1 parent fcc17379

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 4 from django.utils.html import strip_tags
5 5 from django.core.exceptions import ValidationError
6 6  
  7 +import validators
  8 +
7 9 from subjects.models import Tag
8 10 from subjects.forms import ParticipantsMultipleChoiceField
9 11  
... ... @@ -15,22 +17,22 @@ class LinkForm(forms.ModelForm):
15 17 MAX_UPLOAD_SIZE = 10*1024*1024
16 18  
17 19 students = ParticipantsMultipleChoiceField(queryset = None, required = False)
18   -
  20 +
19 21 def __init__(self, *args, **kwargs):
20 22 super(LinkForm, self).__init__(*args, **kwargs)
21 23  
22 24 self.subject = kwargs['initial'].get('subject', None)
23   -
  25 +
24 26 if self.instance.id:
25 27 self.subject = self.instance.topic.subject
26 28 self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True))
27   -
  29 +
28 30 self.fields['students'].queryset = self.subject.students.all()
29 31 self.fields['groups'].queryset = self.subject.group_subject.all()
30 32  
31 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 36 class Meta:
35 37 model = Link
36 38 fields = ['name','link_url', 'brief_description', 'all_students', 'students', 'groups', 'visible']
... ... @@ -49,13 +51,13 @@ class LinkForm(forms.ModelForm):
49 51  
50 52 cleaned_data = self.cleaned_data
51 53  
52   -
  54 +
53 55 return cleaned_data
54   -
  56 +
55 57  
56 58 def clean_name(self):
57 59 name = self.cleaned_data.get('name', '')
58   -
  60 +
59 61 topics = self.subject.topic_subject.all()
60 62  
61 63 for topic in topics:
... ... @@ -63,7 +65,7 @@ class LinkForm(forms.ModelForm):
63 65 same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count()
64 66 else:
65 67 same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count()
66   -
  68 +
67 69 if same_name > 0:
68 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 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 87 def save(self, commit = True):
78 88 super(LinkForm, self).save(commit = True)
... ... @@ -87,7 +97,7 @@ class LinkForm(forms.ModelForm):
87 97 for prev in previous_tags:
88 98 if not prev.name in tags:
89 99 self.instance.tags.remove(prev)
90   -
  100 +
91 101 for tag in tags:
92 102 tag = tag.strip()
93 103  
... ... @@ -101,4 +111,4 @@ class LinkForm(forms.ModelForm):
101 111 if not new_tag in self.instance.tags.all():
102 112 self.instance.tags.add(new_tag)
103 113  
104   - return self.instance
105 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 10  
11 11 # Create your models here.
12 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 16 class Meta:
17 17 verbose_name = "Link"
18 18 verbose_name_plural = "Links"
... ... @@ -28,6 +28,6 @@ class Link(Resource):
28 28  
29 29 def delete_link(self):
30 30 return 'links:delete'
31   -
  31 +
32 32 def delete_message(self):
33   - return _('Are you sure you want delete the Website link')
34 33 \ No newline at end of file
  34 + return _('Are you sure you want delete the Website link')
... ...