def test_error_return_none(self, get): """If requests throws an exception, return None.""" # Test a non-200 status code get.return_value = requests.Response() get.return_value.status_code = 500 eq_(generate_bitly_link('test'), None) # Test an exception get.side_effect = requests.exceptions.RequestException eq_(generate_bitly_link('test'), None)
def bitly_link(self): """Returns a mzl.la shortlink for this video, generating one if one doesn't exist. """ if self.bitly_link_db: return self.bitly_link_db # Generate a URL, remove the locale (let the site handle redirecting # to the proper locale), and finally add the domain to the URL. url = reverse('flicks.videos.details', kwargs={'video_id': self.id}) locale, url = split_path(url) url = absolutify(url) # Don't actually generate a shortlink if we're developing locally. if settings.DEV: bitly_link = None else: bitly_link = generate_bitly_link(url) # Fallback to long URL if needed. if bitly_link is None: return url else: Video.objects.filter(pk=self.pk).update(bitly_link_db=bitly_link) return bitly_link