add force param to publications put/delete

This commit is contained in:
Dmitry Belyaev 2021-09-24 15:24:11 +03:00
parent ab6ab52f24
commit f715fb11dc
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
2 changed files with 20 additions and 17 deletions

View File

@ -117,11 +117,11 @@ class WebPub1C:
return publication_data(publication)
def add(self, ibname: str, url: Optional[str] = None, file: str = '') -> str:
def add(self, ibname: str, url: Optional[str] = None, file: str = '', force: bool = False) -> str:
""" Add new publication """
publication = self._apache_cfg.create_publication(ibname, url, file)
self._apache_cfg.add_publication(publication)
publication = self._apache_cfg.create_publication(ibname, url, file, force)
self._apache_cfg.add_publication(publication, force)
self._log.info(f'publication added: {ibname}')
return publication.url_path
@ -137,25 +137,27 @@ class WebPub1C:
self._apache_cfg.add_publication(publication)
self._log.info(f'publication changed: {ibname}')
def remove(self, ibname: str):
def remove(self, ibname: str, force: bool = False):
""" Remove publication """
self._apache_cfg.remove_publication(ibname)
self._apache_cfg.remove_publication(ibname, force=force)
self._log.info(f'publication removed: {ibname}')
pub_parser = reqparse.RequestParser()
pub_parser.add_argument('name', required=True, type=str, help='name required')
pub_parser.add_argument('url', type=str)
pub_parser.add_argument('file', type=str, default='')
pub_parser.add_argument('force', type=bool, default=False)
url_parser = reqparse.RequestParser()
url_parser.add_argument('url', type=str)
url_parser.add_argument('file', type=str, default='')
pub_parser_with_name = pub_parser.copy()
pub_parser_with_name.add_argument('name', required=True, type=str, help='name required')
url_req_parser = reqparse.RequestParser()
url_req_parser.add_argument('url', type=str, required=True)
remove_parser = reqparse.RequestParser()
remove_parser.add_argument('force', type=bool, default=False)
def load_config(filename: str):
with open(filename, 'r', encoding='utf-8') as cfg_file:
@ -235,14 +237,14 @@ class Publications(Resource):
return webpub.publications()
def put(self):
args = pub_parser.parse_args()
args = pub_parser_with_name.parse_args()
validate_url(args.url)
webpub = get_webpub1c()
url = args.url
if args.name in webpub.list():
if not args.force and args.name in webpub.list():
abort(409, message=f'publication exists: {args.name}')
try:
url = webpub.add(args.name, url, args.file)
url = webpub.add(args.name, url, args.file, args.force)
except Exception as e:
abort(422, message=f'publication failed: {args.name}', traceback=traceback.format_exc())
return {'message': 'created', 'name': args.name, 'url': url}, 201
@ -258,24 +260,25 @@ class Publication(Resource):
}
def put(self, name: str):
args = url_parser.parse_args()
args = pub_parser.parse_args()
validate_url(args.url)
webpub = get_webpub1c()
url = args.url
if name in webpub.list():
if not args.force and name in webpub.list():
abort(409, message=f'publication exists: {name}')
try:
url = webpub.add(name, url, args.file)
url = webpub.add(name, url, args.file, args.force)
except Exception as e:
abort(422, message=f'publication failed: {name}', traceback=traceback.format_exc())
return {'message': 'created', 'name': name, 'url': url}, 201
def delete(self, name: str):
args = remove_parser.parse_args()
webpub = get_webpub1c()
if name not in webpub.list():
abort(404, message=f'publication not found: {name}')
try:
webpub.remove(name)
webpub.remove(name, args.force)
except Exception as e:
abort(422, message='delete failed', traceback=traceback.format_exc())
return {'message': 'deleted', 'name': name}

@ -1 +1 @@
Subproject commit 7835636398f7168611fa959eaf4af2b21a3dfc80
Subproject commit 6f2c64e4b999e8f1e6db88112140650c281c02da