Created
November 13, 2011 16:18
-
-
Save felipe-prenholato/1362280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class URLHist(Model): | |
url = models.TextField() | |
hostname = models.CharField(max_length=250) | |
hostname_type = models.CharField(max_length=1,choices=(('I','Interno'),('E','Externo'))) | |
last_status = models.IntegerField() # a html status code | |
class Meta: | |
verbose_name = u'Histórico de URL' | |
verbose_name_plural = u'Histórico de URLs' | |
@property | |
def is_internal(self): | |
return self.hostname_type == 'I' | |
def test_url(self): | |
""" | |
Test the url and return status code | |
""" | |
makeurl = lambda root: '%s%s%s' % ('http://' if not '://' in self.hostname else '', self.hostname, self.url) | |
if self.is_internal: | |
urlresult = ullib.urlopen(makeurl(url)) | |
self.queries.create(urlhist=self,status_code=url.code) | |
return url.code | |
else: | |
urlresult = ullib.urlopen(settings.URL_CHECKER_URI % makeurl(url)) | |
if url.code == 200: | |
urlstatus = simplejson.loads(urlresult.read()) | |
self.queries.create(urlhist=self,status_code=urlstatus['http_code']) | |
return urlstatus['http_code'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment