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 | 3 | from django.utils.translation import ugettext_lazy as _ |
| 4 | 4 | from django.forms.models import inlineformset_factory |
| 5 | 5 | |
| 6 | + | |
| 7 | +import requests | |
| 8 | +import re | |
| 9 | + | |
| 6 | 10 | from subjects.models import Tag |
| 7 | 11 | |
| 8 | 12 | from pendencies.forms import PendenciesForm |
| ... | ... | @@ -10,19 +14,20 @@ from pendencies.models import Pendencies |
| 10 | 14 | |
| 11 | 15 | from .models import YTVideo |
| 12 | 16 | |
| 17 | + | |
| 13 | 18 | class YTVideoForm(forms.ModelForm): |
| 14 | 19 | subject = None |
| 15 | 20 | control_subject = forms.CharField(widget = forms.HiddenInput()) |
| 16 | - | |
| 21 | + | |
| 17 | 22 | def __init__(self, *args, **kwargs): |
| 18 | 23 | super(YTVideoForm, self).__init__(*args, **kwargs) |
| 19 | 24 | |
| 20 | 25 | self.subject = kwargs['initial'].get('subject', None) |
| 21 | - | |
| 26 | + | |
| 22 | 27 | if self.instance.id: |
| 23 | 28 | self.subject = self.instance.topic.subject |
| 24 | 29 | self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) |
| 25 | - | |
| 30 | + | |
| 26 | 31 | self.initial['control_subject'] = self.subject.id |
| 27 | 32 | |
| 28 | 33 | self.fields['students'].queryset = self.subject.students.all() |
| ... | ... | @@ -44,7 +49,7 @@ class YTVideoForm(forms.ModelForm): |
| 44 | 49 | |
| 45 | 50 | def clean_name(self): |
| 46 | 51 | name = self.cleaned_data.get('name', '') |
| 47 | - | |
| 52 | + | |
| 48 | 53 | topics = self.subject.topic_subject.all() |
| 49 | 54 | |
| 50 | 55 | for topic in topics: |
| ... | ... | @@ -52,7 +57,7 @@ class YTVideoForm(forms.ModelForm): |
| 52 | 57 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() |
| 53 | 58 | else: |
| 54 | 59 | same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() |
| 55 | - | |
| 60 | + | |
| 56 | 61 | if same_name > 0: |
| 57 | 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 | 67 | |
| 63 | 68 | def clean_url(self): |
| 64 | 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 | 71 | self._errors['url'] = [_('Invalid URL. It should be an YouTube link.')] |
| 68 | 72 | |
| 69 | 73 | return ValueError |
| ... | ... | @@ -83,7 +87,7 @@ class YTVideoForm(forms.ModelForm): |
| 83 | 87 | for prev in previous_tags: |
| 84 | 88 | if not prev.name in tags: |
| 85 | 89 | self.instance.tags.remove(prev) |
| 86 | - | |
| 90 | + | |
| 87 | 91 | for tag in tags: |
| 88 | 92 | tag = tag.strip() |
| 89 | 93 | |
| ... | ... | @@ -99,4 +103,4 @@ class YTVideoForm(forms.ModelForm): |
| 99 | 103 | |
| 100 | 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 | 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) | ... | ... |