def test_picAPIConnectionForPicCCByXML(self): picserver = picAPIClient(self.picAPIURL) """testing for XML output""" userInfo = {'title':'test_title', 'workurl':'test_work_url', 'type':'test_type', 'year':'test_year', 'description':'test_desc', 'creator':'test_creator', 'holder':'test_holder', 'outputType':'xml'} output2 = picserver.getLicense('pic_cc_by', userInfo) tree = lxml.etree.parse(StringIO(output2)) root = tree.getroot() self.failUnlessEqual(root.tag, "result")
def test_picAPIConnectionForPicZeroXML(self): picserver = picAPIClient(self.picAPIURL) """testing for XML output""" userInfo = {'title':'test_title', 'attribution_url':'test_attr_url','territory':'US','creator':'test_creator','outputType':'xml'} output2 = picserver.getLicense('pic_cc_zero', userInfo) tree = lxml.etree.parse(StringIO(output2)) root = tree.getroot() self.failUnlessEqual(root.tag, "result")
def test_picAPIConnectionForPicZeroXML(self): picserver = picAPIClient(self.picAPIURL) """testing for XML output""" userInfo = { 'title': 'test_title', 'attribution_url': 'test_attr_url', 'territory': 'US', 'creator': 'test_creator', 'outputType': 'xml' } output2 = picserver.getLicense('pic_cc_zero', userInfo) tree = lxml.etree.parse(StringIO(output2)) root = tree.getroot() self.failUnlessEqual(root.tag, "result")
def test_picAPIConnectionForPicCCByHTML(self): picserver = picAPIClient(self.picAPIURL) """testing for HTML output""" userInfo = { 'title': 'test_title', 'workurl': 'test_work_url', 'type': 'test_type', 'year': 'test_year', 'description': 'test_desc', 'creator': 'test_creator', 'holder': 'test_holder', 'outputType': 'html' } output = picserver.getLicense('pic_cc_by', userInfo) tree = lxml.etree.parse(StringIO(output)) root = tree.getroot() self.failUnlessEqual(root.tag, "html")
def createBadge(request): errHandler = ErrorHandler() # what license to generate? licenseType = request.POST['licenseType'] formID = request.POST['formID'] # PIC RESTful webservice URL = http://testservername.org/picbadgeapi... # creating pic client api object. picserver = picAPIClient(settings.PICBADGEAPI_URL + settings.PICBADGEAPI_PATH) # We've got a pic_CCBy license application. if licenseType == "pic_cc_by": form = ccByUserInfoForm(request.POST) # We've got a pic_zero license application. elif licenseType == 'pic_cc_zero': form = cc0UserInfoForm(request.POST) # Invalid call else: response = { "status": "error", "response": errHandler.InvalidInputError() } # checks if the form data is valid w.r.t. CC by form specification. if form.is_valid(): try: cd = form.cleaned_data userInfo = cd.copy() # removing the form element that are not required for licensing. del userInfo['agreementconfirm'] except: errMsg = errHandler.PythonError() response = {"status": "error", "response": errMsg} try: # this module of the client api forms the answers xml and calls PIC Licensing webservice licenseOutput = picserver.getLicense(licenseType, userInfo) response = {"status": "license", "response": licenseOutput} except: errMsg = errHandler.PICAPIError() response = {"status": "error", "response": errMsg} else: t = get_template('form_template.html') formHtml = t.render(Context({'formID': formID, 'dataForm': form})) response = {"status": "validationError", "response": formHtml} #Notify the metrics services log.debug("Notifying metrics service.") try: metricRec = metricRecorder() metricRec.newInstance(request, 'PICBadgeGUI', 'Libre') # We've got a pic_CCBy license application. if licenseType == "pic_cc_by": metrics_url = request.POST['workurl'] # We've got a pic_zero license application. elif licenseType == 'pic_cc_zero': metrics_url = request.POST['attribution_url'] metricRec.addMetric("Territory", request.POST['territory']) metrics_title = request.POST['title'] metrics_api_HttpCodeResponse = picserver.getHttpResponse() if metrics_api_HttpCodeResponse == 200: metrics_success = "True" else: metrics_success = "False" metricRec.addMetric("LicenseType", licenseType) metricRec.addMetric("Url", metrics_url) metricRec.addMetric("Title", metrics_title) metricRec.addMetric("Success", metrics_success) metricRec.sendMetric() except IOError as e: log.error("Error occurred sending metrics to service.") log.error(e) jsonOutput = simplejson.dumps(response) log.debug(jsonOutput) return HttpResponse(jsonOutput, mimetype='application/json')
def createBadge(request): errHandler = ErrorHandler() # what license to generate? licenseType = request.POST['licenseType'] formID = request.POST['formID'] # PIC RESTful webservice URL = http://testservername.org/picbadgeapi... # creating pic client api object. picserver = picAPIClient(settings.PICBADGEAPI_URL + settings.PICBADGEAPI_PATH) # We've got a pic_CCBy license application. if licenseType == "pic_cc_by": form = ccByUserInfoForm(request.POST) # We've got a pic_zero license application. elif licenseType == 'pic_cc_zero': form = cc0UserInfoForm(request.POST) # Invalid call else: response = {"status": "error", "response": errHandler.InvalidInputError()} # checks if the form data is valid w.r.t. CC by form specification. if form.is_valid(): try: cd = form.cleaned_data userInfo = cd.copy() # removing the form element that are not required for licensing. del userInfo['agreementconfirm'] except: errMsg = errHandler.PythonError() response = {"status": "error", "response": errMsg} try: # this module of the client api forms the answers xml and calls PIC Licensing webservice licenseOutput = picserver.getLicense(licenseType, userInfo) response = {"status": "license", "response": licenseOutput} except: errMsg = errHandler.PICAPIError() response = {"status": "error", "response": errMsg} else: t = get_template('form_template.html') formHtml = t.render(Context({'formID' : formID, 'dataForm' : form})) response = {"status": "validationError", "response": formHtml} #Notify the metrics services log.debug("Notifying metrics service.") try: metricRec= metricRecorder() metricRec.newInstance(request,'PICBadgeGUI','Libre') # We've got a pic_CCBy license application. if licenseType == "pic_cc_by": metrics_url = request.POST['workurl'] # We've got a pic_zero license application. elif licenseType == 'pic_cc_zero': metrics_url = request.POST['attribution_url'] metricRec.addMetric("Territory",request.POST['territory']) metrics_title = request.POST['title'] metrics_api_HttpCodeResponse= picserver.getHttpResponse() if metrics_api_HttpCodeResponse==200: metrics_success="True" else: metrics_success="False" metricRec.addMetric("LicenseType",licenseType) metricRec.addMetric("Url",metrics_url) metricRec.addMetric("Title",metrics_title) metricRec.addMetric("Success",metrics_success) metricRec.sendMetric() except IOError as e: log.error("Error occurred sending metrics to service.") log.error (e) jsonOutput = simplejson.dumps(response) log.debug( jsonOutput ) return HttpResponse(jsonOutput, mimetype='application/json')