Skip to content

Commit

Permalink
Try #3213:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors-mailu[bot] committed Mar 29, 2024
2 parents efb3892 + 4287674 commit 7f2c023
Show file tree
Hide file tree
Showing 33 changed files with 285 additions and 159 deletions.
9 changes: 6 additions & 3 deletions core/admin/mailu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import hmac


class NoPingFilter(logging.Filter):
def filter(self, record):
if record.args['r'].endswith(' /ping HTTP/1.1'):
Expand All @@ -18,6 +19,7 @@ def filter(self, record):
return False
return True


class Logger(glogging.Logger):
def setup(self, cfg):
super().setup(cfg)
Expand All @@ -26,6 +28,7 @@ def setup(self, cfg):
logger = logging.getLogger("gunicorn.access")
logger.addFilter(NoPingFilter())


def create_app_from_config(config):
""" Create a new application based on the given configuration
"""
Expand Down Expand Up @@ -76,9 +79,9 @@ def create_app_from_config(config):
def inject_defaults():
signup_domains = models.Domain.query.filter_by(signup_enabled=True).all()
return dict(
signup_domains= signup_domains,
config = app.config,
get_locale = utils.get_locale,
signup_domains=signup_domains,
config=app.config,
get_locale=utils.get_locale,
)

# Jinja filters
Expand Down
3 changes: 3 additions & 0 deletions core/admin/mailu/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask_restx import apidoc
from . import v1 as APIv1


def register(app, web_api_root):

APIv1.app = app
Expand All @@ -13,6 +14,7 @@ def register(app, web_api_root):

# add redirect to current api version
redirect_api = Blueprint('redirect_api', __name__)

@redirect_api.route('/')
def redir():
return redirect(url_for(f'{APIv1.blueprint.name}.root'))
Expand All @@ -25,6 +27,7 @@ def redir():
app.config.RESTX_MASK_SWAGGER = False
else:
api = Blueprint('api', __name__)

@api.route('/', defaults={'path': ''})
@api.route('/<path:path>')
def api_token_missing(path):
Expand Down
2 changes: 2 additions & 0 deletions core/admin/mailu/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask_restx import abort
from sqlalchemy.sql.expression import label


def fqdn_in_use(name):
d = models.db.session.query(label('name', models.Domain.name))
a = models.db.session.query(label('name', models.Alternative.name))
Expand All @@ -16,6 +17,7 @@ def fqdn_in_use(name):
return True
return False


""" Decorator for validating api token for authentication """
def api_token_authorization(func):
@wraps(func)
Expand Down
2 changes: 1 addition & 1 deletion core/admin/mailu/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
error_fields = api.model('Error', {
'errors': fields.Nested(api.model('Error_Key', {
'key': fields.String,
'message':fields.String
'message': fields.String
})),
'message': fields.String,
})
Expand Down
62 changes: 32 additions & 30 deletions core/admin/mailu/api/v1/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def post(self):

alias_found = models.Alias.query.filter_by(email = data['email']).first()
if alias_found:
return { 'code': 409, 'message': f'Duplicate alias {data["email"]}'}, 409
return {'code': 409, 'message': f'Duplicate alias {data["email"]}'}, 409

alias_model = models.Alias(email=data["email"],destination=data['destination'])
alias_model = models.Alias(email=data["email"], destination=data['destination'])
if 'comment' in data:
alias_model.comment = data['comment']
if 'wildcard' in data:
Expand All @@ -55,6 +55,7 @@ def post(self):

return {'code': 200, 'message': f'Alias {data["email"]} to destination {data["destination"]} has been created'}, 200


@alias.route('/<string:alias>')
class Alias(Resource):
@alias.doc('find_alias')
Expand All @@ -64,11 +65,11 @@ class Alias(Resource):
@common.api_token_authorization
def get(self, alias):
""" Look up the specified alias """
alias_found = models.Alias.query.filter_by(email = alias).first()
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
return {'code': 404, 'message': f'Alias {alias} cannot be found'}, 404
else:
return marshal(alias_found,alias_fields), 200
return marshal(alias_found, alias_fields), 200

@alias.doc('update_alias')
@alias.expect(alias_fields_update)
Expand All @@ -78,49 +79,50 @@ def get(self, alias):
@alias.doc(security='Bearer')
@common.api_token_authorization
def patch(self, alias):
""" Update the specfied alias """
data = api.payload
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
if 'comment' in data:
alias_found.comment = data['comment']
if 'destination' in data:
alias_found.destination = data['destination']
if 'wildcard' in data:
alias_found.wildcard = data['wildcard']
db.session.add(alias_found)
db.session.commit()
return {'code': 200, 'message': f'Alias {alias} has been updated'}
""" Update the specfied alias """
data = api.payload
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
if 'comment' in data:
alias_found.comment = data['comment']
if 'destination' in data:
alias_found.destination = data['destination']
if 'wildcard' in data:
alias_found.wildcard = data['wildcard']
db.session.add(alias_found)
db.session.commit()
return {'code': 200, 'message': f'Alias {alias} has been updated'}

@alias.doc('delete_alias')
@alias.response(200, 'Success', response_fields)
@alias.response(404, 'Alias not found', response_fields)
@alias.doc(security='Bearer')
@common.api_token_authorization
def delete(self, alias):
""" Delete the specified alias """
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
db.session.delete(alias_found)
db.session.commit()
return {'code': 200, 'message': f'Alias {alias} has been deleted'}, 200
""" Delete the specified alias """
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
db.session.delete(alias_found)
db.session.commit()
return {'code': 200, 'message': f'Alias {alias} has been deleted'}, 200


@alias.route('/destination/<string:domain>')
class AliasWithDest(Resource):
@alias.doc('find_alias_filter_domain')
@alias.marshal_with(alias_fields, code=200, description='Success' ,as_list=True, skip_none=True, mask=None)
@alias.marshal_with(alias_fields, code=200, description='Success', as_list=True, skip_none=True, mask=None)
@alias.response(404, 'Alias or domain not found', response_fields)
@alias.doc(security='Bearer')
@common.api_token_authorization
def get(self, domain):
""" Look up the aliases of the specified domain """
domain_found = models.Domain.query.filter_by(name=domain).first()
if domain_found is None:
return { 'code': 404, 'message': f'Domain {domain} cannot be found'}, 404
return {'code': 404, 'message': f'Domain {domain} cannot be found'}, 404
aliases_found = domain_found.aliases
if aliases_found.count == 0:
return { 'code': 404, 'message': f'No alias can be found for domain {domain}'}, 404
return {'code': 404, 'message': f'No alias can be found for domain {domain}'}, 404
else:
return marshal(aliases_found, alias_fields, as_list=True), 200
return marshal(aliases_found, alias_fields, as_list=True), 200
Loading

0 comments on commit 7f2c023

Please sign in to comment.