Skip to content

Instantly share code, notes, and snippets.

@felipe-prenholato
Created November 13, 2011 16:18
Show Gist options
  • Save felipe-prenholato/1362280 to your computer and use it in GitHub Desktop.
Save felipe-prenholato/1362280 to your computer and use it in GitHub Desktop.
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