Example #1
0
    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)
Example #2
0
    def test_youtube_url_cleaner(self):
        """
        Cleans Vimeo urls and make sure that they are perfect.
        """
        should_throw_exception = [
            "",
            "www.,google.com",
            None,
            "www.vimeo.com",
            "http://www.vimeo.com",
            "vimeo.com",
            "http://www.vimeo.com/watch?annotation_id=annotation_742384&feature=iv&src_vid=tpNIgJQXPuM&w=yDS9MH1jRuw",
            "http://www.vimeo.com/watch?v=9349994",
            "http://www.vimeo.com/watch?",
            "http://www.vimeo.com/watch?v",
        ]

        for url in should_throw_exception:
            self.assertRaises(tools.InvalidVimeoLink, tools.vimeo_url_cleaner, url)
        clean_tests = [
            ("http://vimeo.com/57691682", "http://vimeo.com/57691682"),
            (
                "http://vimeo.com/57691682watch?annotation_id=annotation_742384&feature=iv&src_vid=tpNIgJQXPuM&v=yDS9MH1jRuw",
                "http://vimeo.com/57691682",
            ),
        ]

        for (url, correct) in clean_tests:
            self.assertEqual(tools.vimeo_url_cleaner(url), correct)