From 0955f13593a1be588b189d32e4dec4f05e3e678c Mon Sep 17 00:00:00 2001 From: Son NK Date: Sun, 22 Dec 2019 16:20:56 +0000 Subject: [PATCH] User can export their data --- .../templates/dashboard/setting.html | 10 ++++++ app/dashboard/views/setting.py | 33 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/dashboard/templates/dashboard/setting.html b/app/dashboard/templates/dashboard/setting.html index f7c3337b..dd064264 100644 --- a/app/dashboard/templates/dashboard/setting.html +++ b/app/dashboard/templates/dashboard/setting.html @@ -57,6 +57,16 @@ +
+

Export Data

+
+ You can download all aliases you have created on SimpleLogin along with other data. +
+
+ + +
+

Delete Account

Please note that this operation is irreversible. diff --git a/app/dashboard/views/setting.py b/app/dashboard/views/setting.py index d73100eb..168012a6 100644 --- a/app/dashboard/views/setting.py +++ b/app/dashboard/views/setting.py @@ -1,7 +1,8 @@ +import json from io import BytesIO import arrow -from flask import render_template, request, redirect, url_for, flash +from flask import render_template, request, redirect, url_for, flash, Response from flask_login import login_required, current_user, logout_user from flask_wtf import FlaskForm from flask_wtf.file import FileField @@ -20,6 +21,8 @@ from app.models import ( User, GenEmail, DeletedAlias, + CustomDomain, + Client, ) from app.utils import random_string @@ -118,6 +121,34 @@ def setting(): logout_user() return redirect(url_for("auth.register")) + elif request.form.get("form-name") == "export-data": + data = { + "email": current_user.email, + "name": current_user.name, + "aliases": [], + "apps": [], + "custom_domains": [], + } + + for alias in GenEmail.filter_by( + user_id=current_user.id + ).all(): # type: GenEmail + data["aliases"].append(dict(email=alias.email, enabled=alias.enabled)) + + for custom_domain in CustomDomain.filter_by(user_id=current_user.id).all(): + data["custom_domains"].append(custom_domain.domain) + + for app in Client.filter_by(user_id=current_user.id): # type: Client + data["apps"].append( + dict(name=app.name, home_url=app.home_url, published=app.published) + ) + + return Response( + json.dumps(data), + mimetype="text/json", + headers={"Content-Disposition": "attachment;filename=data.json"}, + ) + return redirect(url_for("dashboard.setting")) return render_template(