Skip to content

Commit

Permalink
Support for export and import of JSON account data
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jun 25, 2019
1 parent 0f27a48 commit a0f60ab
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/appier_extras/parts/admin/models/account.py
Expand Up @@ -502,6 +502,46 @@ def create_s(cls, username, email, password = "", send_email = False):
if send_email: account.email_new(password = password)
return account

@classmethod
@appier.operation(
name = "Import JSON",
parameters = (
("JSON File", "file", "file"),
("Empty source", "empty", bool, False)
)
)
def import_json_s(cls, file, empty):

def callback(item):

enabled = item["enabled"]
username = item["username"]
type = item["type"]
password = item.get("password", None)
email = item.get("email", None)
key = item.get("key", None)
description = item.get("description", None)
last_login = item.get("last_login", None)
avatar = item.get("avatar", None)
roles = item.get("roles", [])
account = cls(
enabled = enabled,
username = username,
type = type,
password = password,
password_confirm = password,
email = email,
key = key,
description = description,
last_login = last_login,
avatar = avatar,
roles = roles
)
account.save()

if empty: cls.delete_c()
cls._json_import(file, callback)

@classmethod
@appier.operation(
name = "Import CSV",
Expand Down Expand Up @@ -600,8 +640,11 @@ def pre_save(self):

def pre_create(self):
base.Base.pre_create(self)
self.key = self.secret()
self.confirmation_token = self.secret()
if not hasattr(self, "key") or not self.key:
self.key = self.secret()
if not hasattr(self, "confirmation_token") or\
not self.confirmation_token:
self.confirmation_token = self.secret()
if not hasattr(self, "avatar") or not self.avatar:
self._set_avatar_d()

Expand Down Expand Up @@ -877,6 +920,16 @@ def view_avatar_url(self, absolute = False):
absolute = absolute
)

@classmethod
@appier.link(name = "Export JSON", context = True)
def export_url(cls, view = None, context = None, absolute = False):
return appier.get_app().url_for(
"admin.export_accounts_json",
view = view,
context = context,
absolute = absolute
)

@property
def confirmed(self):
return self.enabled
Expand Down
16 changes: 16 additions & 0 deletions src/appier_extras/parts/admin/part.py
Expand Up @@ -250,6 +250,7 @@ def routes(self):
(("GET",), "/admin/accounts/new", self.new_account),
(("POST",), "/admin/accounts", self.create_account),
(("GET",), "/admin/accounts/me", self.me_account),
(("GET",), "/admin/accounts/export.json", self.export_accounts_json, None, True),
(("GET",), "/admin/accounts/<str:username>", self.show_account),
(("GET",), "/admin/accounts/<str:username>/mail", self.mail_account),
(("GET",), "/admin/accounts/<str:username>/avatar", self.avatar_account),
Expand Down Expand Up @@ -624,6 +625,21 @@ def me_account(self):
account = account
)

@appier.ensure(context = "admin")
def export_accounts_json(self):
account_c = self._get_cls(self.account_c)
object = appier.get_object(
alias = True,
find = True,
limit = 0
)
return self.admin_part._find_view(
account_c,
map = True,
rules = False,
**object
)

@appier.ensure(token = "admin.accounts", context = "admin")
def show_account(self, username):
account_c = self._get_cls(self.account_c)
Expand Down

0 comments on commit a0f60ab

Please sign in to comment.