Commit d473db926f3e17f74b41160b53ce6a1188117fec
1 parent
8c9e775c
Exists in
master
and in
3 other branches
Resolvendo os bugs do tipo list index out of range em videos do youtube
Showing
1 changed file
with
13 additions
and
9 deletions
Show diff stats
youtube_video/forms.py
| @@ -3,6 +3,10 @@ from django import forms | @@ -3,6 +3,10 @@ from django import forms | ||
| 3 | from django.utils.translation import ugettext_lazy as _ | 3 | from django.utils.translation import ugettext_lazy as _ |
| 4 | from django.forms.models import inlineformset_factory | 4 | from django.forms.models import inlineformset_factory |
| 5 | 5 | ||
| 6 | + | ||
| 7 | +import requests | ||
| 8 | +import re | ||
| 9 | + | ||
| 6 | from subjects.models import Tag | 10 | from subjects.models import Tag |
| 7 | 11 | ||
| 8 | from pendencies.forms import PendenciesForm | 12 | from pendencies.forms import PendenciesForm |
| @@ -10,19 +14,20 @@ from pendencies.models import Pendencies | @@ -10,19 +14,20 @@ from pendencies.models import Pendencies | ||
| 10 | 14 | ||
| 11 | from .models import YTVideo | 15 | from .models import YTVideo |
| 12 | 16 | ||
| 17 | + | ||
| 13 | class YTVideoForm(forms.ModelForm): | 18 | class YTVideoForm(forms.ModelForm): |
| 14 | subject = None | 19 | subject = None |
| 15 | control_subject = forms.CharField(widget = forms.HiddenInput()) | 20 | control_subject = forms.CharField(widget = forms.HiddenInput()) |
| 16 | - | 21 | + |
| 17 | def __init__(self, *args, **kwargs): | 22 | def __init__(self, *args, **kwargs): |
| 18 | super(YTVideoForm, self).__init__(*args, **kwargs) | 23 | super(YTVideoForm, self).__init__(*args, **kwargs) |
| 19 | 24 | ||
| 20 | self.subject = kwargs['initial'].get('subject', None) | 25 | self.subject = kwargs['initial'].get('subject', None) |
| 21 | - | 26 | + |
| 22 | if self.instance.id: | 27 | if self.instance.id: |
| 23 | self.subject = self.instance.topic.subject | 28 | self.subject = self.instance.topic.subject |
| 24 | self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) | 29 | self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) |
| 25 | - | 30 | + |
| 26 | self.initial['control_subject'] = self.subject.id | 31 | self.initial['control_subject'] = self.subject.id |
| 27 | 32 | ||
| 28 | self.fields['students'].queryset = self.subject.students.all() | 33 | self.fields['students'].queryset = self.subject.students.all() |
| @@ -44,7 +49,7 @@ class YTVideoForm(forms.ModelForm): | @@ -44,7 +49,7 @@ class YTVideoForm(forms.ModelForm): | ||
| 44 | 49 | ||
| 45 | def clean_name(self): | 50 | def clean_name(self): |
| 46 | name = self.cleaned_data.get('name', '') | 51 | name = self.cleaned_data.get('name', '') |
| 47 | - | 52 | + |
| 48 | topics = self.subject.topic_subject.all() | 53 | topics = self.subject.topic_subject.all() |
| 49 | 54 | ||
| 50 | for topic in topics: | 55 | for topic in topics: |
| @@ -52,7 +57,7 @@ class YTVideoForm(forms.ModelForm): | @@ -52,7 +57,7 @@ class YTVideoForm(forms.ModelForm): | ||
| 52 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() | 57 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() |
| 53 | else: | 58 | else: |
| 54 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() | 59 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() |
| 55 | - | 60 | + |
| 56 | if same_name > 0: | 61 | if same_name > 0: |
| 57 | self._errors['name'] = [_('This subject already has a youtube video with this name')] | 62 | self._errors['name'] = [_('This subject already has a youtube video with this name')] |
| 58 | 63 | ||
| @@ -62,8 +67,7 @@ class YTVideoForm(forms.ModelForm): | @@ -62,8 +67,7 @@ class YTVideoForm(forms.ModelForm): | ||
| 62 | 67 | ||
| 63 | def clean_url(self): | 68 | def clean_url(self): |
| 64 | url = self.cleaned_data.get('url', '') | 69 | url = self.cleaned_data.get('url', '') |
| 65 | - | ||
| 66 | - if not 'youtube' in url: | 70 | + if not 'youtube' in url or re.compile('[htps:/]*w*\.youtube\.com/?').fullmatch(url) or requests.get(url).status_code == 404: |
| 67 | self._errors['url'] = [_('Invalid URL. It should be an YouTube link.')] | 71 | self._errors['url'] = [_('Invalid URL. It should be an YouTube link.')] |
| 68 | 72 | ||
| 69 | return ValueError | 73 | return ValueError |
| @@ -83,7 +87,7 @@ class YTVideoForm(forms.ModelForm): | @@ -83,7 +87,7 @@ class YTVideoForm(forms.ModelForm): | ||
| 83 | for prev in previous_tags: | 87 | for prev in previous_tags: |
| 84 | if not prev.name in tags: | 88 | if not prev.name in tags: |
| 85 | self.instance.tags.remove(prev) | 89 | self.instance.tags.remove(prev) |
| 86 | - | 90 | + |
| 87 | for tag in tags: | 91 | for tag in tags: |
| 88 | tag = tag.strip() | 92 | tag = tag.strip() |
| 89 | 93 | ||
| @@ -99,4 +103,4 @@ class YTVideoForm(forms.ModelForm): | @@ -99,4 +103,4 @@ class YTVideoForm(forms.ModelForm): | ||
| 99 | 103 | ||
| 100 | return self.instance | 104 | return self.instance |
| 101 | 105 | ||
| 102 | -InlinePendenciesFormset = inlineformset_factory(YTVideo, Pendencies, form = PendenciesForm, extra = 1, max_num = 3, validate_max = True, can_delete = True) | ||
| 103 | \ No newline at end of file | 106 | \ No newline at end of file |
| 107 | +InlinePendenciesFormset = inlineformset_factory(YTVideo, Pendencies, form = PendenciesForm, extra = 1, max_num = 3, validate_max = True, can_delete = True) |