2
0
mirror of https://github.com/offen/website.git synced 2024-10-18 12:10:25 +02:00

Merge pull request #84 from offen/retired-accounts

Enable retiring of accounts
This commit is contained in:
Frederik Ring 2019-08-03 16:29:35 +02:00 committed by GitHub
commit 2213cbd1bc
2 changed files with 25 additions and 5 deletions

View File

@ -11,7 +11,7 @@ class Account(db.Model):
__tablename__ = "accounts" __tablename__ = "accounts"
account_id = db.Column(db.String(36), primary_key=True, default=generate_key) account_id = db.Column(db.String(36), primary_key=True, default=generate_key)
name = db.Column(db.Text, nullable=False) name = db.Column(db.Text, nullable=False)
users = db.relationship("AccountUserAssociation", back_populates="account") users = db.relationship("AccountUserAssociation", back_populates="account", cascade="delete")
def __repr__(self): def __repr__(self):
return self.name return self.name

View File

@ -20,7 +20,7 @@ class RemoteServerException(Exception):
) )
def create_remote_account(name, account_id): def _call_remote_server(account_id, method):
# expires in 30 seconds as this will mean the HTTP request would have # expires in 30 seconds as this will mean the HTTP request would have
# timed out anyways # timed out anyways
expiry = datetime.utcnow() + timedelta(seconds=30) expiry = datetime.utcnow() + timedelta(seconds=30)
@ -30,9 +30,18 @@ def create_remote_account(name, account_id):
algorithm="RS256", algorithm="RS256",
).decode("utf-8") ).decode("utf-8")
r = requests.post( do_request = None
if method == "POST":
do_request = requests.post
elif method == "DELETE":
do_request = requests.delete
if not do_request:
raise Exception("Received unsupported method {}, cannot continue.".format(method))
r = do_request(
"{}/accounts".format(app.config["SERVER_HOST"]), "{}/accounts".format(app.config["SERVER_HOST"]),
json={"name": name, "accountId": account_id}, json={"accountId": account_id},
headers={"X-RPC-Authentication": encoded}, headers={"X-RPC-Authentication": encoded},
) )
@ -43,6 +52,14 @@ def create_remote_account(name, account_id):
raise remote_err raise remote_err
def create_remote_account(account_id):
return _call_remote_server(account_id, "POST")
def retire_remote_account(account_id):
return _call_remote_server(account_id, "DELETE")
class AccountForm(Form): class AccountForm(Form):
name = StringField( name = StringField(
"Account Name", "Account Name",
@ -59,12 +76,15 @@ class AccountView(ModelView):
def after_model_change(self, form, model, is_created): def after_model_change(self, form, model, is_created):
if is_created: if is_created:
try: try:
create_remote_account(model.name, model.account_id) create_remote_account(model.account_id)
except RemoteServerException as server_error: except RemoteServerException as server_error:
db.session.delete(model) db.session.delete(model)
db.session.commit() db.session.commit()
raise server_error raise server_error
def after_model_delete(self, model):
retire_remote_account(model.account_id)
class UserView(ModelView): class UserView(ModelView):
inline_models = [(AccountUserAssociation, dict(form_columns=["id", "account"]))] inline_models = [(AccountUserAssociation, dict(form_columns=["id", "account"]))]