示例#1
0
def widget(request, isbn):
    """
    supply info for book panel. parameter is named isbn for historical reasons. can be isbn or work_id
    """

    if isbn == 'featured':
        work = featured_work()
    else:
        if len(isbn) == 10:
            isbn = regluit.core.isbn.convert_10_to_13(isbn)
        if len(isbn) == 13:
            try:
                identifier = models.Identifier.objects.get(type='isbn',
                                                           value=isbn)
                work = identifier.work
            except models.Identifier.DoesNotExist:
                return render(
                    request,
                    'widget.html',
                    {
                        'work': None,
                    },
                )
        else:
            work = models.safe_get_work(isbn)
    return render(
        request,
        'widget.html',
        {
            'work': work,
        },
    )
示例#2
0
文件: views.py 项目: kznmft/regluit
    def get(self, request, *args, **kwargs):
        work = request.GET.get('work', None)

        if work:
            try:
                work = models.safe_get_work(work)
            except models.Work.DoesNotExist:
                raise Http404
            return HttpResponse(onix.onix_feed_for_work(work),
                                content_type="text/xml")

        facet = kwargs.get('facet', 'all')

        if not facet:
            return HttpResponseBadRequest(content='No facet provided')

        max_records = request.GET.get('max', 100)

        try:
            max_records = int(max_records)
        except Exception:
            max_records = None

        facet_class = opds.get_facet_class(facet)()
        page = request.GET.get('page', None)
        try:
            page = int(page)
        except:
            page = None

        feed = onix.onix_feed(facet_class, max_records, page_number=page)
        return StreamingHttpResponse(feed, content_type="text/xml")
示例#3
0
def safe_get_work(work_id):
    """
    use this rather than querying the db directly for a work by id
    """
    try:
        work = models.safe_get_work(work_id)
    except models.Work.DoesNotExist:
        raise Http404
    return work
示例#4
0
 def get(self, request, *args, **kwargs):
     work = request.GET.get('work', None)
     if work:
         try:
             work = models.safe_get_work(work)
         except models.Work.DoesNotExist:
             raise Http404
         return HttpResponse(onix.onix_feed_for_work(work),
                             content_type="text/xml")
     facet = kwargs.get('facet', 'all')
     if facet:
         max = request.GET.get('max', 100)
         try:
             max = int(max)
         except:
             max = None
         facet_class = opds.get_facet_class(facet)()
         return HttpResponse(onix.onix_feed(facet_class, max),
                             content_type="text/xml")
示例#5
0
def get_edition_for_id(id_type, id_value, user=None):
    ''' the identifier is assumed to not be in database '''
    identifiers = {id_type: id_value}
    if id_type == 'http':
        # check for urls implying other identifiers
        identifiers.update(ids_from_urls(id_value))
        for new_id_type in identifiers.keys():
            idents = models.Identifier.objects.filter(
                type=new_id_type,
                value=identifiers[new_id_type],
            )
            if idents:
                ident = idents[0]
                return ident.edition if ident.edition else ident.work.preferred_edition

    #need to make a new edition
    if identifiers.has_key('goog'):
        edition = add_by_googlebooks_id(identifiers['goog'])
        if edition:
            return edition

    if identifiers.has_key('isbn'):
        edition = add_by_isbn(identifiers['isbn'])
        if edition:
            return edition

    if identifiers.has_key('oclc'):
        edition = add_by_oclc(identifiers['oclc'])
        if edition:
            return edition

    if identifiers.has_key('glue'):
        try:
            work = models.safe_get_work(identifiers['glue'])
            return work.preferred_edition
        except:
            pass

    if identifiers.has_key('http'):
        edition = add_by_webpage(identifiers['http'], user=user)
        return edition

    # return a dummy edition and identifier

    title = '!!! missing title !!!'
    work = models.Work.objects.create(title=title)
    edition = models.Edition.objects.create(title='!!! missing title !!!',
                                            work=work)
    for key in identifiers.keys():
        if key == 'glue':
            id_value = work.id
        if key not in ('http', 'goog', 'oclc', 'isbn'):
            if key in WORK_IDENTIFIERS:
                edid = str(edition.id)
                models.Identifier.objects.create(type='edid',
                                                 value=edid,
                                                 work=work,
                                                 edition=edition)
                models.Identifier.objects.create(type=key,
                                                 value=id_value,
                                                 work=work,
                                                 edition=None)
            else:
                models.Identifier.objects.create(type=key,
                                                 value=id_value,
                                                 work=work,
                                                 edition=edition)
    return edition