Create MspAccountManager with a cleanup() method to cleanup obsolete MSP link when the cleanupauthentic command is run (refs #5573)
This commit is contained in:
parent
7e4f09f8b9
commit
bf0c122390
21
models.py
21
models.py
|
@ -5,12 +5,29 @@ import urlparse
|
|||
|
||||
from requests_oauthlib import OAuth2Session
|
||||
|
||||
from django.db import models
|
||||
from django.db import models, transaction
|
||||
from django.db.models.query import Q
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from . import app_settings
|
||||
|
||||
class MspAccountManager(models.Manager):
|
||||
def cleanup(self):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
for msp_account in self.filter(Q(user__isnull=True)
|
||||
|Q(user__deleteduser__isnull=False)):
|
||||
try:
|
||||
with transaction.commit_on_success():
|
||||
if msp_account.refresh_token():
|
||||
if msp_account.token:
|
||||
self.api_call('app/rest/agc', method='delete')
|
||||
msp_account.delete()
|
||||
except:
|
||||
logger.exception('unable to delete msp account %s', msp_account)
|
||||
|
||||
|
||||
class MspAccount(models.Model):
|
||||
user = models.OneToOneField(get_user_model(),
|
||||
verbose_name=_('user'),
|
||||
|
@ -20,6 +37,8 @@ class MspAccount(models.Model):
|
|||
agc = models.CharField(max_length=64, verbose_name=_('access grant code'))
|
||||
token = models.TextField(verbose_name=_('access token'))
|
||||
|
||||
objects = MspAccountManager()
|
||||
|
||||
def api_call(self, api_path, method='get', **kwargs):
|
||||
url = urlparse.urljoin(app_settings.api_url, api_path)
|
||||
session = OAuth2Session(app_settings.client_id,
|
||||
|
|
Reference in New Issue