diff options
-rw-r--r-- | ogcp/forms/action_forms.py | 3 | ||||
-rw-r--r-- | ogcp/static/js/ogcp.js | 17 | ||||
-rw-r--r-- | ogcp/templates/actions/repos_details.html | 3 | ||||
-rw-r--r-- | ogcp/templates/repos.html | 10 | ||||
-rw-r--r-- | ogcp/views.py | 46 |
5 files changed, 70 insertions, 9 deletions
diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py index 0a9bd32..c15d254 100644 --- a/ogcp/forms/action_forms.py +++ b/ogcp/forms/action_forms.py @@ -100,9 +100,10 @@ class ImageRestoreForm(FlaskForm): class RepoForm(FlaskForm): server = HiddenField() + repo_id = HiddenField() name = StringField(label=_l('Name')) ip = StringField(label=_l('IP')) - create = SubmitField(label=_l('Create')) + submit = SubmitField(label=_l('Submit')) class ClientDetailsForm(FlaskForm): server = HiddenField() diff --git a/ogcp/static/js/ogcp.js b/ogcp/static/js/ogcp.js index 5028993..0ac67fe 100644 --- a/ogcp/static/js/ogcp.js +++ b/ogcp/static/js/ogcp.js @@ -329,6 +329,23 @@ function checkImageServer() { }); } +function checkRepoServer() { + const repos = $('input:checkbox[form|="reposForm"][name!="repos-server"]') + repos.on('change', function() { + const selectedServer = $('#' + $.escapeSelector(this.dataset.server)); + const serversSelector = 'input:checkbox[name|="repos-server"]'; + const nonSelectedServers = $(serversSelector).not(selectedServer); + + selectedServer.prop('checked', true); + + nonSelectedServers.each(function() { + $(this).prop('checked', false); + const checkboxes = $('input:checkbox[data-server|="' + this.id + '"]'); + checkboxes.prop('checked', false); + }); + }); +} + function limitCheckboxes() { const checkboxes = $('input:checkbox[form|="scopesForm"]'); diff --git a/ogcp/templates/actions/repos_details.html b/ogcp/templates/actions/repos_details.html index 32b96bf..2768492 100644 --- a/ogcp/templates/actions/repos_details.html +++ b/ogcp/templates/actions/repos_details.html @@ -10,7 +10,8 @@ <h1 class="m-5">{{_('Repo details')}}</h1> {{ wtf.quick_form(form, + action=url_for('repo_add_post'), method='post', - button_map={'create': 'primary'}, + button_map={'submit': 'primary'}, extra_classes="mx-5") }} {% endblock %} diff --git a/ogcp/templates/repos.html b/ogcp/templates/repos.html index 881057b..8477d29 100644 --- a/ogcp/templates/repos.html +++ b/ogcp/templates/repos.html @@ -17,8 +17,8 @@ {% set server_ip_port = repos["server"].ip ~ ":" ~ repos["server"].port %} {% set repos_list = repos["json"]["repositories"] %} <li class="nav-item"> - <input class="form-check-input" type="checkbox" form="reposForm" - value="{{ server_ip_port }}" name="selected-server" /> + <input id="{{ server_ip_port }}"class="form-check-input" type="checkbox" form="reposForm" + value="{{ server_ip_port }}" name="repos-server" /> <a class="nav-link {% if not repos_list %}disabled{% endif %}" href="#server{{loop.index}}" {% if repos_list %}data-toggle="collapse"{% endif %}> {{ server_name }} @@ -27,8 +27,9 @@ {% for r in repos_list %} <li class="nav-item"> <input class="form-check-input" type="checkbox" form="reposForm" + data-server="{{server_ip_port}}" value="{{ r["id"] }}" - name="selected-repo" /> + name="{{ r["name"]~_~r["id"] }}" /> {{ r["name"] }} </li> {% endfor %} @@ -42,6 +43,7 @@ document.addEventListener('readystatechange', () => { if (document.readyState === 'complete') { keepReposTreeState() + checkRepoServer() } }); </script> @@ -50,6 +52,8 @@ {% block commands %} <input class="btn btn-light {% block nav_repo_add %}{% endblock %}" type="submit" value="{{ _('Add repo') }}" form="reposForm" formaction="{{ url_for('repo_add_get') }}" formmethod="get"> + <input class="btn btn-light {% block nav_repo_delete %}{% endblock %}" type="submit" value="{{ _('Delete repo') }}" + form="reposForm" formaction="{{ url_for('action_repo_delete') }}" formmethod="get"> {% if btn_back %} <button class="btn btn-danger ml-3" type="button" id="backButton" onclick="history.back()"> diff --git a/ogcp/views.py b/ogcp/views.py index fb7d6bb..53bad26 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -97,7 +97,7 @@ def validate_elements(elements, min_len=1, max_len=float('inf')): def parse_elements(checkboxes_dict): unwanted_elements = ['csrf_token', 'scope-server', 'scope-center', - 'scope-room', 'image-server'] + 'scope-room', 'image-server', 'repos-server'] elements = set() for key, elements_list in checkboxes_dict.items(): if key not in unwanted_elements: @@ -1486,12 +1486,11 @@ def manage_repos(): def repo_add_get(): form = RepoForm() params = request.args.to_dict() - if not params.get('selected-server'): + if not params.get('repos-server'): flash(_('Please, select a server'), category='error') return redirect(url_for('manage_repos')) - form.server.data = params['selected-server'] - form.create.render_kw = {"formaction": url_for('repo_add_post')} + form.server.data = params['repos-server'] responses = multi_request('get', '/repositories') return render_template('actions/repos_details.html', form=form, @@ -1516,6 +1515,45 @@ def repo_add_post(): flash(_('Repo added successfully'), category='info') return redirect(url_for("manage_repos")) +@app.route('/action/repo/delete', methods=['GET', 'POST']) +@login_required +def action_repo_delete(): + form = RepoForm(request.form) + if request.method == 'POST': + server = get_server_from_ip_port(form.server.data) + payload = { 'id': form.repo_id.data } + r = server.post('/repository/delete', payload) + if r.status_code != requests.codes.ok: + flash(_('ogServer: error deleting repo'), + category='error') + else: + flash(_('Repo deleted successfully'), + category='info') + return redirect(url_for('manage_repos')) + else: + params = request.args.to_dict() + repos = parse_elements(params) + print(repos) + if not validate_elements(repos, max_len=1): + return redirect(url_for('manage_repos')) + repo_id = repos.pop() + if not repo_id: + flash(_('Please, select a repo'), category='error') + return redirect(url_for('manage_repos')) + repo_id = int(repo_id) + server_ip_port = params.get('repos-server') + server = get_server_from_ip_port(server_ip_port) + repository = get_repository(repo_id, server) + form.server.data = server_ip_port + form.repo_id.data = repo_id + form.name.data = repository['name'] + form.name.render_kw = {'readonly': True} + form.ip.data = repository['ip'] + form.ip.render_kw = {'readonly': True} + responses = multi_request('get', '/repositories') + return render_template('actions/delete_repo.html', form=form, + repos_resp=responses) + @app.route('/servers/', methods=['GET']) @login_required def manage_servers(): |