Created
December 20, 2011 18:48
-
-
Save marcelcaraciolo/1502716 to your computer and use it in GitHub Desktop.
iesde.py
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
# -*- coding: utf-8 -*- | |
import urllib2 | |
import datetime | |
from django.utils import simplejson as json | |
from django.conf import settings | |
from xml.dom import minidom | |
from celery.task import Task | |
from core.utils import get_send_mail | |
WS_BASE_URL = 'http://ws.videoaulasonline.com.br' | |
WS_HEADERS = { | |
'Content-Type': 'text/xml; charset=UTF-8', | |
'User-Agent': 'Orygens', | |
'SOAPAction': '' | |
} | |
class IESDEError(Exception): | |
pass | |
def cadastro(WS_LOGIN, WS_PASSWORD, email, cpf, curso, duracao, tipo): | |
xml_request = """<?xml version="1.0" encoding="UTF-8"?> | |
<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" | |
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> | |
<soap:Body> | |
<cadastro xmlns="urn:cadastro"> | |
<login xsi:type="xsd:string">%s</login> | |
<senha xsi:type="xsd:string">%s</senha> | |
<loginAluno xsi:type="xsd:string">%s</loginAluno> | |
<cpf xsi:type="xsd:string">%s</cpf> | |
<curso xsi:type="xsd:string">%s</curso> | |
<duracao xsi:type="xsd:string">%s</duracao> | |
<tipo xsi:type="xsd:string">%s</tipo> | |
</cadastro> | |
</soap:Body> | |
</soap:Envelope>""" % (WS_LOGIN, WS_PASSWORD, email, cpf, curso, duracao, tipo) | |
duracao = int(duracao) | |
request = urllib2.Request(WS_BASE_URL + '/aluno/cadastro', xml_request, WS_HEADERS) | |
response = urllib2.urlopen(request) | |
raw_response = response.read() | |
xml_response = minidom.parseString(raw_response) | |
resultado = xml_response.getElementsByTagName('resultado')[0].childNodes[0].data | |
if not 'sucesso' in resultado: | |
raise IESDEError(resultado) | |
else: | |
subscription = filter( | |
lambda s: s['cpf'] == cpf, | |
matriculas(WS_LOGIN, WS_PASSWORD, curso, tipo) | |
)[0] | |
if subscription['duracao'] < duracao: | |
duracao -= subscription['duracao'] | |
date = datetime.datetime.strptime(subscription['dtcad'], '%d/%m/%Y') +\ | |
datetime.timedelta(days=duracao + 1) | |
GrantAccessTask.apply_async(args=[WS_LOGIN, WS_PASSWORD, email, | |
cpf, curso, duracao, tipo], eta=date) | |
elif subscription['duracao'] > duracao: | |
get_send_mail()( | |
u'[IESDE] Erro ao conceder acesso ao curso %s para o usuário %s' % (subscription['curso'], email), | |
u''' | |
Foram cadastrados %d dias a mais no IESDE de acesso ao curso | |
%s para o usuário %s. | |
''' % (subscription['duracao'] - duracao, subscription['curso'], email), | |
settings.DEFAULT_FROM_EMAIL, | |
[settings.DEFAULT_FROM_EMAIL] | |
) | |
return resultado | |
def aulas(WS_LOGIN, WS_PASSWORD, tipo, ancora= "",curso_id=""): | |
xml_request = """<?xml version="1.0" encoding="UTF-8"?> | |
<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" | |
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> | |
<soap:Body> | |
<obtemAulas xmlns="urn:obtemAulas"> | |
<login xsi:type="xsd:string">%s</login> | |
<senha xsi:type="xsd:string">%s</senha> | |
<loginAluno xsi:type="xsd:string"></loginAluno> | |
<cpf xsi:type="xsd:string"></cpf> | |
<curso xsi:type="xsd:string">%s</curso> | |
<ancora xsi:type="xsd:string">%s</ancora> | |
<tipo xsi:type="xsd:string">%s</tipo> | |
</obtemAulas> | |
</soap:Body> | |
</soap:Envelope>""" % (WS_LOGIN, WS_PASSWORD,curso_id, ancora, tipo ) | |
request = urllib2.Request(WS_BASE_URL + "/acoes_webservice/obtemAulas", xml_request, WS_HEADERS) | |
response = urllib2.urlopen(request) | |
raw_response = response.read() | |
xml_response = minidom.parseString(raw_response) | |
resultado = xml_response.getElementsByTagName('resultado')[0].childNodes[0].data | |
return json.loads(resultado) | |
def matriculas(WS_LOGIN, WS_PASSWORD, curso, tipo): | |
xml_request = """<?xml version="1.0" encoding="UTF-8"?> | |
<soap:Envelope | |
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" | |
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> | |
<soap:Body> | |
<obtemMatriculas xmlns="urn:obtemMatriculas"> | |
<login xsi:type="xsd:string">%s</login> | |
<senha xsi:type="xsd:string">%s</senha> | |
<curso xsi:type="xsd:string">%s</curso> | |
<tipo xsi:type="xsd:string">%s</tipo> | |
</obtemMatriculas> | |
</soap:Body> | |
</soap:Envelope>""" % (WS_LOGIN, WS_PASSWORD, curso, tipo) | |
request = urllib2.Request( | |
WS_BASE_URL + "/acoes_webservice/obtemMatriculas", | |
xml_request, | |
WS_HEADERS | |
) | |
response = urllib2.urlopen(request) | |
raw_response = response.read() | |
xml_response = minidom.parseString(raw_response) | |
resultado = xml_response.getElementsByTagName('resultado')[0].childNodes[0].data | |
return json.loads(resultado) | |
def troca_curso(WS_LOGIN, WS_PASSWORD, email, cpf, curso, tipo, novo_curso): | |
xml_request = """<?xml version="1.0" encoding="UTF-8"?> | |
<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" | |
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> | |
<soap:Body> | |
<trocaCurso xmlns="urn:trocaCurso"> | |
<login xsi:type="xsd:string">%s</login> | |
<senha xsi:type="xsd:string">%s</senha> | |
<loginAluno xsi:type="xsd:string">%s</loginAluno> | |
<cpf xsi:type="xsd:string">%s</cpf> | |
<curso xsi:type="xsd:string">%s</curso> | |
<tipo xsi:type="xsd:string">%s</tipo> | |
<novoCurso xsi:type="xsd:string">%s</tipo> | |
</trocaCurso> | |
</soap:Body> | |
</soap:Envelope>""" % (WS_LOGIN, WS_PASSWORD, email, cpf, curso, tipo, novo_curso) | |
request = urllib2.Request(WS_BASE_URL + '/acoes_webservice/trocaCurso', xml_request, WS_HEADERS) | |
response = urllib2.urlopen(request) | |
raw_response = response.read() | |
xml_response = minidom.parseString(raw_response) | |
resultado = xml_response.getElementsByTagName('resultado')[0].childNodes[0].data | |
if not 'sucesso' in resultado: | |
raise IESDEError(resultado) | |
else: | |
return resultado | |
class GrantAccessTask(Task): | |
def run(self, *args): | |
cadastro(*args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment