Esempio n. 1
0
def addDocs(request,store_name):
  """Add a list of documents."""
  try:
    store = ContentStore.objects.get(name=store_name)
  except ContentStore.DoesNotExist:
    resp = {
      'ok' : False,
      'msg' : 'store: %s does not exist.' % store_name
    }
    return HttpResponseNotFound(json.dumps(resp))
  docs = request.POST.get('docs');
  if not docs:
    resp = {'ok':False,'error':'no docs posted'}
    return HttpResponseBadRequest(json.dumps(resp))
  else:
    my_validator, error = validator.get_validator(store_name)
    if not my_validator:
      resp = {
        'ok': False,
        'msg': error,
      }
      return HttpResponse(json.dumps(resp))

    try:
      jsonDocs = json.loads(docs.encode('utf-8'))
      messages = []
      for doc in jsonDocs:
        (valid, error) = my_validator.validate(doc)
        if not valid:
          logging.warn("Found an invalid doc for store %s when adding docs" % store_name)
          resp = {'ok': False,'numPosted':0, 'error':error}
          return HttpResponseBadRequest(json.dumps(resp))
        str = json.dumps(doc).encode('utf-8')
        messages.append(str)

      if messages:
        with samples_lock:
          my_samples = samples.get(store_name)
          if my_samples is None:
            my_samples = []
            samples[store_name] = my_samples

          if len(my_samples) < 2:
            uid = jsonDocs[-1].get('id')
            if uid is not None:
              my_samples.append(uid)
        kafka_send(messages, store.unique_name.encode('utf-8'))

      resp = {'ok':True,'numPosted':len(messages)}
      return HttpResponse(json.dumps(resp))
    except ValueError:
      resp = {'ok':False,'error':'invalid json: %s' % docs}
      return HttpResponseBadRequest(json.dumps(resp))
    except Exception as e:
      logging.exception(e)
      resp = {'ok':False,'error':e.message}
      return HttpResponseServerError(json.dumps(resp))
Esempio n. 2
0
def updateDoc(request,store_name):
  try:
    store = None
    try:
      store = ContentStore.objects.get(name=store_name)
    except ContentStore.DoesNotExist:
      resp = {
        'ok' : False,
        'msg' : 'store: %s does not exist.' % store_name
      }
      return HttpResponseNotFound(json.dumps(resp))

    doc = request.POST.get('doc');

    if not doc:
      resp = {'ok':False,'error':'no doc posted'}
      return HttpResponseBadRequest(json.dumps(resp))
    else:
      jsonDoc = json.loads(doc.encode('utf-8'))

      my_validator, error = validator.get_validator(store_name)
      if not my_validator:
        resp = {
          'ok': False,
          'msg': error,
        }
        return HttpResponse(json.dumps(resp))

      (valid, error) = my_validator.validate(jsonDoc)
      if not valid:
        logging.warn("Found an invalid doc for store %s when updating a doc" % store_name)
        resp = {'ok': False,'numPosted':0, 'error':error}
        return HttpResponseBadRequest(json.dumps(resp))

      uid = long(jsonDoc['id'])
      existingDoc = find_doc(store,uid)

      if not existingDoc:
        resp = {'ok':False,'error':'doc: %d does not exist' % uid}
        return HttpResponseBadRequest(json.dumps(resp))

      existingDoc = json.loads(existingDoc)

      for k,v in jsonDoc.items():
        existingDoc[k]=v
      
      kafka_send([json.dumps(existingDoc).encode('utf-8')], store.unique_name.encode('utf-8'))
      resp = {'ok': True,'numPosted':1}
      return HttpResponse(json.dumps(resp))
  except ValueError:
    resp = {'ok':False,'error':'invalid json: %s' % doc}
    return HttpResponseBadRequest(json.dumps(resp))
  except Exception as e:
    logging.exception(e)
    resp = {'ok':False,'error':e.message}
  return HttpResponseServerError(json.dumps(resp))