docs: def initdoc(request, namespace, docid, mode, template, context=None, configuration=None):
      """Initialise a document (not invoked directly)"""
      perspective = request.GET.get('perspective','document')
      if context is None: context = {}
      if 'configuration' in request.session:
          configuration = request.session['configuration']
      elif configuration is None:
          return fatalerror(request, "No configuration specified")
      if configuration not in settings.CONFIGURATIONS:
          return fatalerror(request, "Specified configuration does not exist")
      flatargs = {
          'setdefinitions': True,
          'declarations': True, #implies provenance as well
          'metadata': True,
          'toc': True,
          'slices': request.GET.get('slices',settings.CONFIGURATIONS[configuration].get('slices','p:25,s:100')), #overriden either by configuration or by user
          'customslicesize': 0, #disabled for initial probe
          'textclasses': True,
      }
      error = False
      try:
          doc = flat.comm.query(request, "USE " + namespace + "/" + docid + " PROBE", **flatargs) #retrieves only the meta information, not document content
          context.update(getcontext(request,namespace,docid, doc, mode, configuration))
      except Exception as e:
          context.update(docserveerror(e))
          error = True
  
      if not error:
          dorequiredeclaration = 'requiredeclaration' in settings.CONFIGURATIONS[configuration] and settings.CONFIGURATIONS[configuration]['requiredeclaration']
          if dorequiredeclaration:
              if not 'declarations' in doc:
                  return fatalerror(request, "Refusing to load document, missing expected declarations, none declared")
              declarations = doc['declarations']
              for annotationtype, annotationset in settings.CONFIGURATIONS[configuration]['requiredeclaration']:
                  found = False
                  for d in declarations:
                      if annotationtype == d['annotationtype'] and (not annotationset or annotationset == d['set']):
                          found = True
                          break
                  if not found:
                      if annotationset:
                          return fatalerror(request, "Refusing to load document, missing expected declaration for annotation type " + annotationtype + "/" + annotationset)
                      else:
                          return fatalerror(request, "Refusing to load document, missing expected declaration for annotation type " + annotationtype)
  
          dometadataindex = 'metadataindex' in settings.CONFIGURATIONS[configuration] and settings.CONFIGURATIONS[configuration]['metadataindex']
          if dometadataindex:
              metadata = json.loads(context['metadata'])
              for metakey in settings.CONFIGURATIONS[configuration]['metadataindex']:
                  if metakey in metadata:
                      MetadataIndex.objects.update_or_create(namespace=namespace,docid=docid, key=metakey,defaults={'value':metadata[metakey]})
      response = render(request, template, context)
      if 'fatalerror' in context:
          response.status_code = 500
      return response