From 39b0d44079989e73dda9a3a2b7708d5a273c845a Mon Sep 17 00:00:00 2001 From: Dimitri Huisman Date: Tue, 13 Dec 2022 10:35:42 +0000 Subject: [PATCH] Use first() instead of all() for better performance Actually return all data for Get user Remove non-used code --- core/admin/mailu/api/v1/alias.py | 4 ++-- core/admin/mailu/api/v1/relay.py | 1 - core/admin/mailu/api/v1/user.py | 25 ++++++++++++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/core/admin/mailu/api/v1/alias.py b/core/admin/mailu/api/v1/alias.py index 7635aa19..3605c68f 100644 --- a/core/admin/mailu/api/v1/alias.py +++ b/core/admin/mailu/api/v1/alias.py @@ -41,7 +41,7 @@ class Aliases(Resource): """ Create a new alias """ data = api.payload - alias_found = models.Alias.query.filter_by(email = data['email']).all() + alias_found = models.Alias.query.filter_by(email = data['email']).first() if alias_found: return { 'code': 409, 'message': f'Duplicate alias {data["email"]}'}, 409 @@ -64,7 +64,7 @@ class Alias(Resource): @common.api_token_authorization def get(self, alias): """ Find alias """ - alias_found = models.Alias.query.filter_by(email = alias).all() + alias_found = models.Alias.query.filter_by(email = alias).first() if alias_found is None: return { 'code': 404, 'message': f'Alias {alias} cannot be found'}, 404 else: diff --git a/core/admin/mailu/api/v1/relay.py b/core/admin/mailu/api/v1/relay.py index 6b52c7d5..f4588d22 100644 --- a/core/admin/mailu/api/v1/relay.py +++ b/core/admin/mailu/api/v1/relay.py @@ -44,7 +44,6 @@ class Relays(Resource): if not validators.domain(name): return { 'code': 400, 'message': f'Relayed domain {name} is not a valid domain'}, 400 - relay_found = models.Relay.query.filter_by(name=data['name']).all() if common.fqdn_in_use(data['name']): return { 'code': 409, 'message': f'Duplicate domain {data["name"]}'}, 409 relay_model = models.Relay(name=data['name']) diff --git a/core/admin/mailu/api/v1/user.py b/core/admin/mailu/api/v1/user.py index 79875c04..953d246b 100644 --- a/core/admin/mailu/api/v1/user.py +++ b/core/admin/mailu/api/v1/user.py @@ -12,6 +12,25 @@ user = api.namespace('user', description='User operations') user_fields_get = api.model('UserGet', { 'email': fields.String(description='The email address of the user', example='John.Doe@example.com', attribute='_email'), 'password': fields.String(description="Hash of the user's password; Example='$bcrypt-sha256$v=2,t=2b,r=12$fmsAdJbYAD1gGQIE5nfJq.$zLkQUEs2XZfTpAEpcix/1k5UTNPm0jO'"), + 'comment': fields.String(description='A description for the user. This description is shown on the Users page', example='my comment'), + 'quota_bytes': fields.Integer(description='The maximum quota for the user’s email box in bytes', example='1000000000'), + 'global_admin': fields.Boolean(description='Make the user a global administrator'), + 'enabled': fields.Boolean(description='Enable the user. When an user is disabled, the user is unable to login to the Admin GUI or webmail or access his email via IMAP/POP3 or send mail'), + 'enable_imap': fields.Boolean(description='Allow email retrieval via IMAP'), + 'enable_pop': fields.Boolean(description='Allow email retrieval via POP3'), + 'allow_spoofing': fields.Boolean(description='Allow the user to spoof the sender (send email as anyone)'), + 'forward_enabled': fields.Boolean(description='Enable auto forwarding'), + 'forward_destination': fields.List(fields.String(description='Email address to forward emails to'), example='Other@example.com'), + 'forward_keep': fields.Boolean(description='Keep a copy of the forwarded email in the inbox'), + 'reply_enabled': fields.Boolean(description='Enable automatic replies. This is also known as out of office (ooo) or out of facility (oof) replies'), + 'reply_subject': fields.String(description='Optional subject for the automatic reply', example='Out of office'), + 'reply_body': fields.String(description='The body of the automatic reply email', example='Hello, I am out of office. I will respond when I am back.'), + 'reply_startdate': fields.Date(description='Start date for automatic replies in YYYY-MM-DD format.', example='2022-02-10'), + 'reply_enddate': fields.Date(description='End date for automatic replies in YYYY-MM-DD format.', example='2022-02-22'), + 'displayed_name': fields.String(description='The display name of the user within the Admin GUI', example='John Doe'), + 'spam_enabled': fields.Boolean(description='Enable the spam filter'), + 'spam_mark_as_read': fields.Boolean(description='Enable marking spam mails as read'), + 'spam_threshold': fields.Integer(description='The user defined spam filter tolerance', example='80'), }) user_fields_post = api.model('UserCreate', { @@ -105,7 +124,7 @@ class Users(Resource): if 'enable_pop' in data: user_new.enable_pop = data['enable_pop'] if 'allow_spoofing' in data: - user.allow_spoofing = data['allow_spoofing'] + user_new.allow_spoofing = data['allow_spoofing'] if 'forward_enabled' in data: user_new.forward_enabled = data['forward_enabled'] if 'forward_destination' in data: @@ -170,7 +189,7 @@ class User(Resource): data = api.payload if not validators.email(email): return { 'code': 400, 'message': f'Provided email address {data["email"]} is not a valid email address'}, 400 - user_found = models.User.query.filter_by(email=email).first() + user_found = models.User.query.get(email) if not user_found: return {'code': 404, 'message': f'User {email} cannot be found'}, 404 @@ -189,7 +208,7 @@ class User(Resource): if 'enable_pop' in data: user_found.enable_pop = data['enable_pop'] if 'allow_spoofing' in data: - user.allow_spoofing = data['allow_spoofing'] + user_found.allow_spoofing = data['allow_spoofing'] if 'forward_enabled' in data: user_found.forward_enabled = data['forward_enabled'] if 'forward_destination' in data: