def insert_object(request, name, id=None, **kwargs): """Insert or update an object of type name into the database. Arguemnts: request: The django request object. name: the name of the object to be inserted. id: the id of an existing object that should be updated. **kwargs: any further arguments to the modify_<name> function. Returns: response: A HttpResponse object indicating whether the insertion was successful or not. """ try: content_type = RestView.get_content_type(request) data = request.raw_post_data logger.info("Attempt to insert %s in table: %s" % (data, name)) inserted_object = None inserter = insert.get_inserter(content_type) function = getattr(inserter, 'modify_%s' % (name)) inserted_object = function(data, id, **kwargs) values = {name: inserted_object} response = RestView.render_response(request, name, values) if id: response.status_code = RestView.OK_STATUS else: response.status_code = RestView.CREATED_STATUS response['location'] = '%s/%s/%s' % (service_url, name, inserted_object.id) except InvalidDataException, exc: logger.error(exc) response = HttpResponse(exc.message) response.status_code = RestView.BAD_REQUEST_STATUS
def _insert_publication(request, publication_id): content_type = RestView.get_content_type(request) data = request.raw_post_data logger.info("Attempt to insert publication: %s" % (data)) requester = request.user if 'application/x-bibtex' in content_type: inserted_publication = insert.insert_bibtex_publication(data, requester) else: inserter = insert.get_inserter(content_type) inserted_publication = inserter.modify_publication(data, publication_id, requester=requester) return inserted_publication
def POST(request): """Inserts publications via POST request.""" try: content_type = RestView.get_content_type(request) data = request.raw_post_data logger.info("Attempt to insert publication: %s" % (data)) requester = request.user inserted_publications = None if 'application/x-bibtex' in content_type: inserted_publications = insert.insert_bibtex_publication(data, requester) else: inserter = insert.get_inserter(content_type) inserted_publications = inserter.modify_publication(data, requester=requester) values = {'publication_list': inserted_publications} response = RestView.render_response(request, 'publications', values) response.status_code = RestView.CREATED_STATUS if type(inserted_publications) == type(Publication): response['Location'] = "%s/publication/%s" % (service_url, inserted_publications.id) except InvalidDataException, e: logger.error(e) response = HttpResponse(e.message) response.status_code = RestView.BAD_REQUEST_STATUS