app-MAIL-temp/app/api/views/export.py

73 lines
2.0 KiB
Python
Raw Normal View History

2021-03-06 21:56:42 +01:00
import csv
from io import StringIO
from flask import g
from flask import jsonify
from flask import make_response
from app.api.base import api_bp, require_api_auth
from app.models import Alias, Client, CustomDomain
@api_bp.route("/export/data", methods=["GET"])
@require_api_auth
def export_data():
"""
Get user data
Output:
Alias, custom domain and app info
"""
user = g.user
data = {
"email": user.email,
"name": user.name,
"aliases": [],
"apps": [],
"custom_domains": [],
}
for alias in Alias.filter_by(user_id=user.id).all(): # type: Alias
data["aliases"].append(dict(email=alias.email, enabled=alias.enabled))
for custom_domain in CustomDomain.filter_by(user_id=user.id).all():
data["custom_domains"].append(custom_domain.domain)
for app in Client.filter_by(user_id=user.id): # type: Client
2021-04-06 19:46:21 +02:00
data["apps"].append(dict(name=app.name, home_url=app.home_url))
2021-03-06 21:56:42 +01:00
return jsonify(data)
@api_bp.route("/export/aliases", methods=["GET"])
@require_api_auth
def export_aliases():
"""
Get user aliases as importable CSV file
Output:
Importable CSV file
"""
user = g.user
data = [["alias", "note", "enabled", "mailboxes"]]
2021-03-06 21:56:42 +01:00
for alias in Alias.filter_by(user_id=user.id).all(): # type: Alias
2021-03-10 22:35:04 +01:00
# Always put the main mailbox first
# It is seen a primary while importing
alias_mailboxes = alias.mailboxes
2021-03-13 15:37:28 +01:00
alias_mailboxes.insert(
0, alias_mailboxes.pop(alias_mailboxes.index(alias.mailbox))
)
2021-03-10 22:35:04 +01:00
mailboxes = " ".join([mailbox.email for mailbox in alias_mailboxes])
data.append([alias.email, alias.note, alias.enabled, mailboxes])
2021-03-06 21:56:42 +01:00
si = StringIO()
cw = csv.writer(si)
cw.writerows(data)
output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=aliases.csv"
output.headers["Content-type"] = "text/csv"
return output