def save(self, *args, **kwargs): """ Before saving do the following: - Clean the url before saving - Fill in the name """ # You can't timetravel into the future if self.used and self.used > timezone.now(): self.used = timezone.now() if not self.url: raise ValueError("url field must be set") if not ("http://" in self.url or "https://" in self.url): self.url = "http://" + self.url # Note this may raise urllib2.URLException # if so, use the existing title try: self.title = tools.link_title(self.url) except urllib2.URLError: pass if tools.is_youtube(self.url): self.url = tools.youtube_url_cleaner(self.url) self.title = tools.youtube_clean_title(self.title) elif tools.is_vimeo(self.url): self.url = tools.vimeo_url_cleaner(self.url) self.title = tools.vimeo_clean_title(self.title) super(Video, self).save(*args, **kwargs)
def test_youtube_url_cleaner(self): """ Cleans youtube urls and make sure that they are perfect. """ should_throw_exception = [ "", "www.,google.com", None, "www.youtube.com", "http://www.youtube.com", "youtube.com", "http://www.youtube.com/watch?annotation_id=annotation_742384&feature=iv&src_vid=tpNIgJQXPuM&w=yDS9MH1jRuw", "http://www.youtube.com/watch?v=", "http://www.youtube.com/watch?", "http://www.youtube.com/watch?v", "http://youtu.be", ] for url in should_throw_exception: self.assertRaises(tools.InvalidYouTubeLink, tools.youtube_url_cleaner, url) clean_tests = [ ("http://www.youtube.com/watch?v=i9WvkSaR8Ho", "http://www.youtube.com/watch?v=i9WvkSaR8Ho"), ( "http://www.youtube.com/watch?annotation_id=annotation_742384&feature=iv&src_vid=tpNIgJQXPuM&v=yDS9MH1jRuw", "http://www.youtube.com/watch?v=yDS9MH1jRuw", ), ("http://youtu.be/iYHjpZe5NDY?t=2s", "http://www.youtube.com/watch?v=iYHjpZe5NDY"), ] for (url, correct) in clean_tests: self.assertEqual(tools.youtube_url_cleaner(url), correct)