Basic authentication views
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.core import serializers
|
||||
from fedireads.models import Book, Work
|
||||
from fedireads.models import Author, Book, Work
|
||||
import requests
|
||||
|
||||
openlibrary_url = 'https://openlibrary.org'
|
||||
@ -22,15 +22,28 @@ def get_book(request, olkey):
|
||||
for work_id in data['works']:
|
||||
work_id = work_id['key']
|
||||
book.works.add(get_or_create_work(work_id))
|
||||
for author_id in data['authors']:
|
||||
author_id = author_id['key']
|
||||
book.authors.add(get_or_create_author(author_id))
|
||||
return HttpResponse(serializers.serialize('json', [book]))
|
||||
|
||||
def get_or_create_work(olkey):
|
||||
try:
|
||||
work = Work.objects.get(openlibary_key=olkey)
|
||||
except ObjectDoesNotExist:
|
||||
response = requests.get(openlibrary_url + '/work/' + olkey +'.json')
|
||||
response = requests.get(openlibrary_url + olkey + '.json')
|
||||
data = response.json()
|
||||
work = Work(openlibary_key=olkey, data=data)
|
||||
work.save()
|
||||
return work
|
||||
|
||||
def get_or_create_author(olkey):
|
||||
try:
|
||||
author = Author.objects.get(openlibary_key=olkey)
|
||||
except ObjectDoesNotExist:
|
||||
response = requests.get(openlibrary_url + olkey + '.json')
|
||||
data = response.json()
|
||||
author = Author(openlibary_key=olkey, data=data)
|
||||
author.save()
|
||||
return author
|
||||
|
||||
|
Reference in New Issue
Block a user