コード例 #1
0
ファイル: views.py プロジェクト: NadiaTahiri/EnceFAL
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()
コード例 #2
0
ファイル: views.py プロジェクト: NadiaUQAM/EnceFAL
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()
コード例 #3
0
ファイル: views.py プロジェクト: AESSUQAM/EnceFAL
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()
コード例 #4
0
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()