def news_patch(context, data_dict): '''Patch a news. See news_create for possible fields. The difference between the update and patch methods is that the patch will perform an update of the provided parameters, while leaving all other parameters unchanged, whereas the update method deletes all parameters not explicitly provided in the data_dict :param id: The id of the news. :type id: string :returns: a patched news :rtype: dictionary ''' log.info('News patch: %r', data_dict) logic.check_access('news_patch', context, data_dict) news_patch_schema = schema.news_patch_schema() fields = news_patch_schema.keys() # Exclude fields from the schema that are not in data_dict for field in fields: if field not in data_dict.keys() and field != 'id': news_patch_schema.pop(field) data, errors = df.validate(data_dict, news_patch_schema, context) if errors: raise toolkit.ValidationError(errors) news = ckanextNews.get(key=data['id'], attr='id') if news is None: raise logic.NotFound fields = news_patch_schema.keys() for field in fields: setattr(news, field, data.get(field)) news.save() out = news_dictize(news) return out
def news_show(context, data_dict): '''Return the metadata of a news. :param id: the id of the news :type id: string :rtype: dictionary ''' log.info('News show: %r', data_dict) id = toolkit.get_or_bust(data_dict, 'id') news = ckanextNews.get(key=id, attr='id') if news is None: raise logic.NotFound out = news_dictize(news) return out
def news_update(context, data_dict): '''Update a news. This will update all fields. See news_create for possible fields. :param id: The id of the news. :type id: string :returns: an updated news :rtype: dictionary ''' log.info('News update: %r', data_dict) logic.check_access('news_update', context, data_dict) news_update_schema = schema.news_update_schema() data, errors = df.validate(data_dict, news_update_schema, context) if errors: raise toolkit.ValidationError(errors) news = ckanextNews.get(key=data['id'], attr='id') if news is None: raise logic.NotFound news.title = data.get('title') news.content = data.get('content', u'') news.meta = data.get('meta', u'{}') news.expiration_date = data.get('expiration_date') news.image_url = data.get('image_url', u'') news.save() out = news_dictize(news) return out