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

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