Skip to content

Commit

Permalink
Improvements/Bug fixes (#548)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adamsclafani authored May 28, 2021
1 parent aa7ac11 commit 753c0e4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
4 changes: 2 additions & 2 deletions opwen_email_client/webapp/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ 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,
}

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):
Expand Down
1 change: 1 addition & 0 deletions opwen_email_client/webapp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
21 changes: 3 additions & 18 deletions opwen_email_client/webapp/forms/register.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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)
14 changes: 6 additions & 8 deletions opwen_email_client/webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

0 comments on commit 753c0e4

Please sign in to comment.