Skip to content

Commit 753c0e4

Browse files
authored
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
1 parent aa7ac11 commit 753c0e4

File tree

4 files changed

+12
-28
lines changed

4 files changed

+12
-28
lines changed

opwen_email_client/webapp/actions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,15 @@ def _format_settings(self):
263263
'OPWEN_MAX_UPLOAD_SIZE_MB': AppConfig.MAX_UPLOAD_SIZE_MB,
264264
'OPWEN_SIM_TYPE': AppConfig.SIM_TYPE,
265265
'OPWEN_EMAIL_SERVER_HOSTNAME': self.server_endpoint,
266-
'OPWEN_CLIENT_NAME': self.client_name.data.strip(),
266+
'OPWEN_CLIENT_NAME': self._client_name,
267267
'OPWEN_ROOT_DOMAIN': root_domain,
268268
'OPWEN_RESTART_PATH': restart_path,
269269
}
270270

271271
def _write_settings_to_file(self, client_values):
272272
client_values_list = ['{}={}'.format(key, value) for (key, value) in client_values.items()]
273273

274-
with open(self._path, 'w') as fobj:
274+
with open(self._settings_path, 'w') as fobj:
275275
fobj.write('\n'.join(client_values_list))
276276

277277
def __call__(self):

opwen_email_client/webapp/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class i8n(object):
4646
'Use "*" for any value or "," to separate multiple values '
4747
'or "-" to define a range of values or "/" for step values.')
4848
FAILED_REGISTRATION = _('Registration failed. Please try again.')
49+
REGISTER_AFTER_LOGIN = _('You are now logged in. Register a Lokole client now by filling in the fields')
4950
UNEXPECTED_ERROR = _('Unexpected error. Please contact your administrator.')
5051
PAGE_DOES_NOT_EXIST = _('This page does not exist.')
5152
USER_DOES_NOT_EXIST = _('This user does not exist.')
Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from os import getenv
22
from pathlib import Path
3-
from shutil import chown
43

54
from flask_wtf import FlaskForm
65
from requests import post
@@ -21,8 +20,8 @@ class RegisterForm(FlaskForm):
2120
submit = SubmitField()
2221

2322
def register_client(self):
24-
path = self._setup_path()
25-
self._setup_client(path)
23+
path = (Path(getenv('OPWEN_STATE_DIRECTORY', 'lokole/state')) / 'settings.env')
24+
self._setup_client(str(path))
2625

2726
def _setup_client(self, path):
2827
name = self.client_name.data.strip()
@@ -35,20 +34,6 @@ def _setup_client(self, path):
3534
response = post(client_create_url,
3635
json={'domain': client_domain},
3736
headers={'Authorization': 'Bearer {}'.format(token)})
38-
if response.status_code != 200:
37+
if response.status_code != 201:
3938
raise ValidationError(i8n.FAILED_REGISTRATION)
4039
register.delay(name, token, path)
41-
42-
def _setup_path(self):
43-
home = Path.home()
44-
user = home.parts[-1]
45-
path = (Path(getenv('OPWEN_STATE_DIRECTORY', 'lokole/state')) / 'settings.env').absolute()
46-
parent = path.parent
47-
parent.mkdir(parents=True, exist_ok=True)
48-
is_in_home = parent.parts[:3] == home.parts
49-
if is_in_home:
50-
home_parts = parent.parts[3:]
51-
for part in home_parts:
52-
home /= part
53-
chown(str(home), user, user)
54-
return str(path)

opwen_email_client/webapp/views.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,12 @@ def login_complete() -> Response:
208208
if current_language:
209209
Session.store_current_language(current_language)
210210

211-
flash(i8n.LOGGED_IN, category='success')
212-
return redirect(url_for('home'))
211+
if current_user.is_admin and (not AppConfig.CLIENT_ID) and (AppConfig.SIM_TYPE != 'LocalOnly'):
212+
flash(i8n.REGISTER_AFTER_LOGIN, category='success')
213+
return redirect(url_for('register'))
214+
else:
215+
flash(i8n.LOGGED_IN, category='success')
216+
return redirect(url_for('home'))
213217

214218

215219
@app.route(AppConfig.APP_ROOT + '/user/logout/complete')
@@ -467,11 +471,5 @@ def _emails_view(emails: Iterable[dict], page: int, template: str = 'email.html'
467471
**kwargs)
468472

469473

470-
@app.before_first_request
471-
def check_client_registration() -> Response:
472-
if not AppConfig.CLIENT_ID and AppConfig.SIM_TYPE != 'LocalOnly':
473-
return redirect(url_for('register'))
474-
475-
476474
def _view(template: str, **kwargs) -> Response:
477475
return render_template(template, **kwargs)

0 commit comments

Comments
 (0)