def batches_post(): try: json = new_batch_schema(request.json) except MultipleInvalid as e: return problem.invalid_params_response(e) batch = Batch.from_json(json) existing_batch = db.batch.find_one({"_id": batch.id}) if existing_batch: return problem.duplicate_resource_response("id") if batch.sku_id: existing_sku = db.sku.find_one({"_id": batch.sku_id}) if not existing_sku: return problem.invalid_params_response( problem.missing_resource_param_error( "sku_id", "must be an existing sku id")) admin_increment_code("BAT", batch.id) db.batch.insert_one(batch.to_mongodb_doc()) # Add text index if not yet created # TODO: This should probably be turned into a global flag if "name_text" not in db.batch.index_information().keys(): db.sku.create_index([("name", TEXT)]) return BatchEndpoint.from_batch(batch).created_success_response()
def json_to_data_model(in_json_dict): if in_json_dict['id'].startswith("BIN"): return Bin.from_json(in_json_dict) if in_json_dict['id'].startswith("SKU"): return Sku.from_json(in_json_dict) if in_json_dict['id'].startswith("BAT"): return Batch.from_json(in_json_dict)
def new_batch_bad_format_owned_codes(self, data, sku_id, bad_code): batch = data.draw(dst.batches_(sku_id=sku_id)) assume(batch.id not in self.model_batches.keys()) temp_batch = Batch.from_json(batch.to_json()) temp_batch.owned_codes.append(bad_code) resp = self.client.post('/api/batches', json=temp_batch.to_dict()) assert resp.status_code == 400 assert resp.is_json assert resp.json['type'] == 'validation-error' temp_batch = Batch.from_json(batch.to_json()) temp_batch.associated_codes.append(bad_code) resp = self.client.post('/api/batches', json=temp_batch.to_dict()) assert resp.status_code == 400 assert resp.is_json assert resp.json['type'] == 'validation-error'
def get_existing_batch(self, batch_id): rp = self.client.get(f"/api/batch/{batch_id}") assert rp.status_code == 200 assert rp.is_json found_batch = Batch.from_json(rp.json['state']) assert found_batch == self.model_batches[batch_id]