def livre(request): assert ('isbn' in request.GET) assert ('nb' in request.GET) assert (len(request.GET['isbn']) in [10, 13]) reponse = None livre = None isbn = request.GET['isbn'] nb = request.GET['nb'] try: livre = Livre.objects.get(isbn=isbn) except Livre.DoesNotExist: pass if not livre: query = ISBN_DB_BASE_QUERY.format(settings.ISBNDB_API_KEY, isbn) reponse_query = json.load(urllib.urlopen(query)) if 'error' not in reponse_query: reponse_query = reponse_query['data'][0] reponse = { 'titre': reponse_query['title'], 'auteur': reponse_query['author_data'][0]['name'], 'nb': nb } else: reponse = {'titre': livre.titre, 'auteur': livre.auteur, 'nb': nb} if reponse: return HttpResponse(json.dumps(reponse), content_type="application/json") else: return HttpResponseNotFound()
def livre(request): assert('isbn' in request.GET) assert('nb' in request.GET) assert(len(request.GET['isbn']) in [10,13]) reponse = None livre = None isbn = request.GET['isbn'] nb = request.GET['nb'] try: livre = Livre.objects.get(isbn=isbn) except Livre.DoesNotExist: pass if not livre: query = ISBN_DB_BASE_QUERY.format(settings.ISBNDB_API_KEY, isbn) reponse_query = json.load(urllib.urlopen(query)) if 'error' not in reponse_query: reponse_query = reponse_query['data'][0] reponse = {'titre':reponse_query['title'], 'auteur':reponse_query['author_data'][0]['name'], 'nb':nb} else: reponse = {'titre':livre.titre, 'auteur':livre.auteur, 'nb':nb} if reponse: return HttpResponse(json.dumps(reponse), content_type="application/json") else: return HttpResponseNotFound()
def livre(request): if not request.user.is_authenticated(): return HttpResponseNotFound() assert('isbn' in request.GET) assert('nb' in request.GET) assert(len(request.GET['isbn']) in [10,13]) reponse = None livre = None isbn = request.GET['isbn'] nb = request.GET['nb'] try: livre = Livre.objects.get(isbn=isbn) except Livre.DoesNotExist: pass if not livre: query = ISBN_DB_BASE_QUERY.format(settings.ISBNDB_API_KEY, isbn) reponse_query = json.load(urllib.urlopen(query)) if 'error' not in reponse_query: reponse_query = reponse_query['data'][0] titre = reponse_query['title'] if reponse_query['title'] else '' auteur = reponse_query['author_data'][0]['name'] if reponse_query['author_data'] else '' reponse = {'titre':titre, 'auteur':auteur, 'nb':nb} else: query = COOP_UQAM_BASE_QUERY.format(isbn) reponse_query = urllib.urlopen(query) tree = html.fromstring(reponse_query.read()) titres = tree.cssselect("h3 a") if titres: titre = titres[0].text auteur = tree.cssselect("h3 + p")[0].text_content().split(' : ')[1].split('\r')[0].strip() reponse = {'titre':titre, 'auteur':auteur, 'nb':nb} else: reponse = {'titre':livre.titre, 'auteur':livre.auteur, 'nb':nb} if reponse: return HttpResponse(json.dumps(reponse), content_type="application/json") else: return HttpResponseNotFound()
def livre(request): if not request.user.is_authenticated(): return HttpResponseNotFound() assert ('isbn' in request.GET) assert ('nb' in request.GET) assert (len(request.GET['isbn']) in [10, 13]) reponse = None livre = None isbn = request.GET['isbn'] nb = request.GET['nb'] try: livre = Livre.objects.get(isbn=isbn) except Livre.DoesNotExist: pass if not livre: query = ISBN_DB_BASE_QUERY.format(settings.ISBNDB_API_KEY, isbn) reponse_query = json.load(urllib.urlopen(query)) if 'error' not in reponse_query: reponse_query = reponse_query['data'][0] titre = reponse_query['title'] if reponse_query['title'] else '' auteur = reponse_query['author_data'][0]['name'] if reponse_query[ 'author_data'] else '' reponse = {'titre': titre, 'auteur': auteur, 'nb': nb} else: query = COOP_UQAM_BASE_QUERY.format(isbn) reponse_query = urllib.urlopen(query) tree = html.fromstring(reponse_query.read()) titres = tree.cssselect("h3 a") if titres: titre = titres[0].text auteur = tree.cssselect("h3 + p")[0].text_content().split( ' : ')[1].split('\r')[0].strip() reponse = {'titre': titre, 'auteur': auteur, 'nb': nb} else: reponse = {'titre': livre.titre, 'auteur': livre.auteur, 'nb': nb} if reponse: return HttpResponse(json.dumps(reponse), content_type="application/json") else: return HttpResponseNotFound()