Beispiel #1
0
 def get_plain_content(self):
     from digipal import utils as dputils
     return dputils.get_plain_text_from_xmltext(self.content)
Beispiel #2
0
 def get_plain_content(self):
     from digipal import utils as dputils
     return dputils.get_plain_text_from_xmltext(self.content)
Beispiel #3
0
def text_api_view(request, item_partid, content_type, location_type=u'default', location=''):

    format = dputils.get_request_var(request, 'format', 'html').strip().lower()
    if request.is_ajax():
        format = 'json'

    from digipal.utils import is_model_visible
    if not is_model_visible('textcontentxml', request):
        raise Http404('Text view not enabled')
    max_size = MAX_FRAGMENT_SIZE if format == 'json' else None

    response = None

    # DELEGATE TO A CUSTOM VIEW FOR THE GIVEN CONTENT TYPE

    # Look up the content_type in the function name
    # e.g. content_type = image => text_api_view_image
    text_api_view_content_type = globals().get(
        'text_api_view_' + content_type, None)
    content_type_record = None

    if not text_api_view_content_type:
        # Look up the content_type in the TextContentType table
        # e.g. content_type = translation or transcription, we assume it must
        # be a TextContentXML
        from digipal_text.models import TextContentType
        content_type_record = TextContentType.objects.filter(
            slug=content_type).first()

        if content_type_record:
            text_api_view_content_type = text_api_view_text

    if text_api_view_content_type:
        response = text_api_view_content_type(
            request, item_partid, content_type,
            location_type, location, content_type_record, max_size=max_size)

    # we didn't find a custom function for this content type
    if response is None:
        response = {'status': 'error',
                    'message': 'Invalid Content Type (%s)' % content_type}

    # If sublocation is not defined by specific function we just return
    # the desired sublocation.
    # If specific function want to remove they can set it to []
    response['sub_location'] = response.get(
        'sub_location', get_sub_location_from_request(request))
    if not response['sub_location']:
        del response['sub_location']

    # HANDLE location_type = sync

    # We take care of syncing logic here, customisations don't need to worry
    # about it.
    # Sync in => sync out. For UI/client logic purpose.
    # If we don't return sync, client assumes we can't support sync.
    # Only exception is in resolve_default_location() below.
    if response.get('location_type', '') == 'sync':
        response['location_type'] = location_type
        response['location'] = location
        response['content'] = 'Synchronising panel...'
        set_message(response, 'Synchronising panel...', '')

    ret = None

    # RESPONSE FORMATTING

    if format == 'json':
        ret = HttpResponse(json.dumps(response),
                           content_type='application/json')

    if format == 'html':
        context = {'response': response}
        context['display_classes'] = ' '.join(
            (dputils.get_request_var(request, 'ds', '').split(',')))
        context['content_type_key'] = content_type
        ret = render(request, 'digipal_text/text_view.html', context)

    if format == 'tei':
        tei = get_tei_from_text_response(response, item_partid, content_type)
        ret = HttpResponse(tei, content_type='text/xml; charset=utf-8')

    if format == 'plain':
        plain_text = dputils.get_plain_text_from_xmltext(
            response.get('content', ''))
        ret = HttpResponse(
            plain_text, content_type='text/plain; charset=utf-8')

    if not ret:
        raise Exception('Unknown output format: "%s"' % format)

    return ret
Beispiel #4
0
def text_api_view(request,
                  item_partid,
                  content_type,
                  location_type=u'default',
                  location=''):

    format = dputils.get_request_var(request, 'format', 'html').strip().lower()
    if request.is_ajax():
        format = 'json'

    from digipal.utils import is_model_visible
    if not is_model_visible('textcontentxml', request):
        raise Http404('Text view not enabled')
    max_size = MAX_FRAGMENT_SIZE if format == 'json' else None

    response = None

    # DELEGATE TO A CUSTOM VIEW FOR THE GIVEN CONTENT TYPE

    # Look up the content_type in the function name
    # e.g. content_type = image => text_api_view_image
    text_api_view_content_type = globals().get('text_api_view_' + content_type,
                                               None)
    content_type_record = None

    if not text_api_view_content_type:
        # Look up the content_type in the TextContentType table
        # e.g. content_type = translation or transcription, we assume it must
        # be a TextContentXML
        from digipal_text.models import TextContentType
        content_type_record = TextContentType.objects.filter(
            slug=content_type).first()

        if content_type_record:
            text_api_view_content_type = text_api_view_text

    if text_api_view_content_type:
        response = text_api_view_content_type(request,
                                              item_partid,
                                              content_type,
                                              location_type,
                                              location,
                                              content_type_record,
                                              max_size=max_size)

    # we didn't find a custom function for this content type
    if response is None:
        response = {
            'status': 'error',
            'message': 'Invalid Content Type (%s)' % content_type
        }

    # If sublocation is not defined by specific function we just return
    # the desired sublocation.
    # If specific function want to remove they can set it to []
    response['sub_location'] = response.get(
        'sub_location', get_sub_location_from_request(request))
    if not response['sub_location']:
        del response['sub_location']

    # HANDLE location_type = sync

    # We take care of syncing logic here, customisations don't need to worry
    # about it.
    # Sync in => sync out. For UI/client logic purpose.
    # If we don't return sync, client assumes we can't support sync.
    # Only exception is in resolve_default_location() below.
    if response.get('location_type', '') == 'sync':
        response['location_type'] = location_type
        response['location'] = location
        response['content'] = 'Synchronising panel...'
        set_message(response, 'Synchronising panel...', '')

    ret = None

    # RESPONSE FORMATTING

    if format == 'json':
        ret = HttpResponse(json.dumps(response),
                           content_type='application/json')

    if format == 'html':
        context = {'response': response}
        context['display_classes'] = ' '.join(
            (dputils.get_request_var(request, 'ds', '').split(',')))
        context['content_type_key'] = content_type
        ret = render(request, 'digipal_text/text_view.html', context)

    if format == 'tei':
        tei = get_tei_from_text_response(response, item_partid, content_type)
        ret = HttpResponse(tei, content_type='text/xml; charset=utf-8')

    if format == 'plain':
        plain_text = dputils.get_plain_text_from_xmltext(
            response.get('content', ''))
        ret = HttpResponse(plain_text,
                           content_type='text/plain; charset=utf-8')

    if not ret:
        raise Exception('Unknown output format: "%s"' % format)

    return ret