From 753c0e48a4ff64c10ffd7ea41b0e9bab9f37c497 Mon Sep 17 00:00:00 2001 From: Adam Sclafani <37944482+adamsclafani@users.noreply.github.com> Date: Fri, 28 May 2021 10:30:09 -1000 Subject: [PATCH] Improvements/Bug fixes (#548) * status code fix, bug fixes on the client_register action * simplification, remove unecessary path setup function * bring up registration page upon admin login if needed * ci feedback --- opwen_email_client/webapp/actions.py | 4 ++-- opwen_email_client/webapp/config.py | 1 + opwen_email_client/webapp/forms/register.py | 21 +++------------------ opwen_email_client/webapp/views.py | 14 ++++++-------- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/opwen_email_client/webapp/actions.py b/opwen_email_client/webapp/actions.py index bd940ef9..a99baca9 100644 --- a/opwen_email_client/webapp/actions.py +++ b/opwen_email_client/webapp/actions.py @@ -263,7 +263,7 @@ def _format_settings(self): 'OPWEN_MAX_UPLOAD_SIZE_MB': AppConfig.MAX_UPLOAD_SIZE_MB, 'OPWEN_SIM_TYPE': AppConfig.SIM_TYPE, 'OPWEN_EMAIL_SERVER_HOSTNAME': self.server_endpoint, - 'OPWEN_CLIENT_NAME': self.client_name.data.strip(), + 'OPWEN_CLIENT_NAME': self._client_name, 'OPWEN_ROOT_DOMAIN': root_domain, 'OPWEN_RESTART_PATH': restart_path, } @@ -271,7 +271,7 @@ def _format_settings(self): def _write_settings_to_file(self, client_values): client_values_list = ['{}={}'.format(key, value) for (key, value) in client_values.items()] - with open(self._path, 'w') as fobj: + with open(self._settings_path, 'w') as fobj: fobj.write('\n'.join(client_values_list)) def __call__(self): diff --git a/opwen_email_client/webapp/config.py b/opwen_email_client/webapp/config.py index ce6d8a99..3cbb36cd 100644 --- a/opwen_email_client/webapp/config.py +++ b/opwen_email_client/webapp/config.py @@ -46,6 +46,7 @@ class i8n(object): 'Use "*" for any value or "," to separate multiple values ' 'or "-" to define a range of values or "/" for step values.') FAILED_REGISTRATION = _('Registration failed. Please try again.') + REGISTER_AFTER_LOGIN = _('You are now logged in. Register a Lokole client now by filling in the fields') UNEXPECTED_ERROR = _('Unexpected error. Please contact your administrator.') PAGE_DOES_NOT_EXIST = _('This page does not exist.') USER_DOES_NOT_EXIST = _('This user does not exist.') diff --git a/opwen_email_client/webapp/forms/register.py b/opwen_email_client/webapp/forms/register.py index 5693922c..78e55112 100644 --- a/opwen_email_client/webapp/forms/register.py +++ b/opwen_email_client/webapp/forms/register.py @@ -1,6 +1,5 @@ from os import getenv from pathlib import Path -from shutil import chown from flask_wtf import FlaskForm from requests import post @@ -21,8 +20,8 @@ class RegisterForm(FlaskForm): submit = SubmitField() def register_client(self): - path = self._setup_path() - self._setup_client(path) + path = (Path(getenv('OPWEN_STATE_DIRECTORY', 'lokole/state')) / 'settings.env') + self._setup_client(str(path)) def _setup_client(self, path): name = self.client_name.data.strip() @@ -35,20 +34,6 @@ def _setup_client(self, path): response = post(client_create_url, json={'domain': client_domain}, headers={'Authorization': 'Bearer {}'.format(token)}) - if response.status_code != 200: + if response.status_code != 201: raise ValidationError(i8n.FAILED_REGISTRATION) register.delay(name, token, path) - - def _setup_path(self): - home = Path.home() - user = home.parts[-1] - path = (Path(getenv('OPWEN_STATE_DIRECTORY', 'lokole/state')) / 'settings.env').absolute() - parent = path.parent - parent.mkdir(parents=True, exist_ok=True) - is_in_home = parent.parts[:3] == home.parts - if is_in_home: - home_parts = parent.parts[3:] - for part in home_parts: - home /= part - chown(str(home), user, user) - return str(path) diff --git a/opwen_email_client/webapp/views.py b/opwen_email_client/webapp/views.py index d74c45b0..f8b28bf5 100644 --- a/opwen_email_client/webapp/views.py +++ b/opwen_email_client/webapp/views.py @@ -208,8 +208,12 @@ def login_complete() -> Response: if current_language: Session.store_current_language(current_language) - flash(i8n.LOGGED_IN, category='success') - return redirect(url_for('home')) + if current_user.is_admin and (not AppConfig.CLIENT_ID) and (AppConfig.SIM_TYPE != 'LocalOnly'): + flash(i8n.REGISTER_AFTER_LOGIN, category='success') + return redirect(url_for('register')) + else: + flash(i8n.LOGGED_IN, category='success') + return redirect(url_for('home')) @app.route(AppConfig.APP_ROOT + '/user/logout/complete') @@ -467,11 +471,5 @@ def _emails_view(emails: Iterable[dict], page: int, template: str = 'email.html' **kwargs) -@app.before_first_request -def check_client_registration() -> Response: - if not AppConfig.CLIENT_ID and AppConfig.SIM_TYPE != 'LocalOnly': - return redirect(url_for('register')) - - def _view(template: str, **kwargs) -> Response: return render_template(template, **kwargs)