This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
wcsinst/wcsinst/wcsinst/models.py

42 lines
1.3 KiB
Python

import json
import logging
import urllib2
from django.conf import settings
from django.db import models
logger = logging.getLogger(__name__)
class WcsInstance(models.Model):
title = models.CharField(max_length=50)
domain = models.CharField(max_length=100)
def __unicode__(self):
return '%s (%s)' % (self.title, self.domain)
def save(self, *args, **kwargs):
created = (self.id is None)
super(WcsInstance, self).save(*args, **kwargs)
# notify wcsinstd
if not settings.WCSINSTD_URL:
return
if created:
url = settings.WCSINSTD_URL + 'wcsinstd/create'
else:
url = settings.WCSINSTD_URL + 'wcsinstd/%s/' % self.domain
request = urllib2.Request(url)
request.add_header('Accept', 'application/json')
request.add_header('Content-Type', 'application/json;charset=UTF-8')
request.add_data(json.dumps({'title': self.title, 'domain': self.domain}))
try:
p = urllib2.urlopen(request)
except urllib2.HTTPError as e:
logger.error('wcsinstd HTTP error (%s)', str(e))
print e.read()
except urllib2.URLError as e:
print e
logger.error('wcsinstd URL error (%s)', str(e))
else:
out_data = p.read()
p.close()