def update(self, id): """Update a tag and return it. :URL: ``PUT /tags/id`` :Request body: JSON object representing the tag with updated attribute values. :param str id: the ``id`` value of the tag to be updated. :returns: the updated tag model. """ tag = Session.query(Tag).get(int(id)) if tag: try: schema = TagSchema() values = json.loads(unicode(request.body, request.charset)) state = h.get_state_object(values) state.id = id data = schema.to_python(values, state) tag = update_tag(tag, data) # tag will be False if there are no changes (cf. update_tag). if tag: Session.add(tag) Session.commit() return tag else: response.status_int = 400 return { 'error': u'The update request failed because the submitted data were not new.' } except h.JSONDecodeError: response.status_int = 400 return h.JSONDecodeErrorResponse except Invalid, e: response.status_int = 400 return {'errors': e.unpack_errors()}
def update(self, id): """Update a tag and return it. :URL: ``PUT /tags/id`` :Request body: JSON object representing the tag with updated attribute values. :param str id: the ``id`` value of the tag to be updated. :returns: the updated tag model. """ tag = Session.query(Tag).get(int(id)) if tag: try: schema = TagSchema() values = json.loads(unicode(request.body, request.charset)) state = h.get_state_object(values) state.id = id data = schema.to_python(values, state) tag = update_tag(tag, data) # tag will be False if there are no changes (cf. update_tag). if tag: Session.add(tag) Session.commit() return tag else: response.status_int = 400 return {'error': u'The update request failed because the submitted data were not new.'} except h.JSONDecodeError: response.status_int = 400 return h.JSONDecodeErrorResponse except Invalid, e: response.status_int = 400 return {'errors': e.unpack_errors()}
def create(self): """Create a new tag resource and return it. :URL: ``POST /tags`` :request body: JSON object representing the tag to create. :returns: the newly created tag. """ try: schema = TagSchema() values = json.loads(unicode(request.body, request.charset)) data = schema.to_python(values) tag = create_new_tag(data) Session.add(tag) Session.commit() return tag except h.JSONDecodeError: response.status_int = 400 return h.JSONDecodeErrorResponse except Invalid, e: response.status_int = 400 return {'errors': e.unpack_errors()}
def create(self): """Create a new tag resource and return it. :URL: ``POST /tags`` :request body: JSON object representing the tag to create. :returns: the newly created tag. """ try: schema = TagSchema() values = json.loads(unicode(request.body, request.charset)) data = schema.to_python(values) tag = create_new_tag(data) Session.add(tag) Session.commit() return tag except h.JSONDecodeError: response.status_int = 400 return h.JSONDecodeErrorResponse except Invalid, e: response.status_int = 400 return {'errors': e.unpack_errors()}