Update Discourse group membership in daily cronjob
This commit is contained in:
parent
0b27a8bd5d
commit
6cc2b44711
|
@ -1,6 +1,7 @@
|
|||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.utils.timezone import now
|
||||
from apiserver.api import models, utils, utils_stats
|
||||
from django.contrib.auth.models import User
|
||||
from apiserver.api import models, utils, utils_stats, utils_auth
|
||||
|
||||
import time
|
||||
|
||||
|
@ -16,6 +17,55 @@ class Command(BaseCommand):
|
|||
|
||||
return active_members.count()
|
||||
|
||||
def update_discourse_groups(self):
|
||||
add_to_group = {
|
||||
'directors_current': [],
|
||||
'protospace_members': [],
|
||||
'protospace_members_former': [],
|
||||
'protospace_instructors': [],
|
||||
}
|
||||
remove_from_group = {
|
||||
'directors_current': [],
|
||||
'protospace_members': [],
|
||||
'protospace_members_former': [],
|
||||
'protospace_members_uber': [],
|
||||
'protospace_instructors': [],
|
||||
}
|
||||
|
||||
for user in User.objects.all():
|
||||
username = user.username
|
||||
|
||||
# handle non-member vs. member
|
||||
if user.member.paused_date:
|
||||
add_to_group['protospace_members_former'].append(username)
|
||||
remove_from_group['directors_current'].append(username)
|
||||
remove_from_group['protospace_members'].append(username)
|
||||
remove_from_group['protospace_members_uber'].append(username)
|
||||
remove_from_group['protospace_instructors'].append(username)
|
||||
|
||||
continue
|
||||
else:
|
||||
add_to_group['protospace_members'].append(username)
|
||||
remove_from_group['protospace_members_former'].append(username)
|
||||
|
||||
# handle directors
|
||||
if user.member.is_director:
|
||||
add_to_group['directors_current'].append(username)
|
||||
else:
|
||||
remove_from_group['directors_current'].append(username)
|
||||
|
||||
# handle instructors
|
||||
if user.member.is_instructor:
|
||||
add_to_group['protospace_instructors'].append(username)
|
||||
else:
|
||||
remove_from_group['protospace_instructors'].append(username)
|
||||
|
||||
for group_name, usernames in add_to_group.items():
|
||||
utils_auth.add_discourse_group_members(group_name, usernames)
|
||||
|
||||
for group_name, usernames in remove_from_group.items():
|
||||
utils_auth.remove_discourse_group_members(group_name, usernames)
|
||||
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.stdout.write('{} - Beginning daily tasks'.format(str(now())))
|
||||
|
@ -27,6 +77,9 @@ class Command(BaseCommand):
|
|||
count = utils_stats.calc_retain_counts()
|
||||
self.stdout.write('Tallied {} retained members'.format(count))
|
||||
|
||||
self.update_discourse_groups()
|
||||
self.stdout.write('Updated Discourse group memberships')
|
||||
|
||||
utils_stats.changed_card()
|
||||
self.stdout.write('Updated card change time')
|
||||
|
||||
|
|
|
@ -13,10 +13,10 @@ def discourse_is_configured():
|
|||
return bool(secrets.DISCOURSE_AUTH_API_URL and secrets.AUTH_API_KEY)
|
||||
|
||||
|
||||
def auth_api(url, data):
|
||||
def auth_api(url, data=None, json=None):
|
||||
try:
|
||||
headers = {'Authorization': 'Token ' + secrets.AUTH_API_KEY}
|
||||
r = requests.post(url, data=data, headers=headers, timeout=20)
|
||||
r = requests.post(url, data=data, json=json, headers=headers, timeout=20)
|
||||
return r.status_code
|
||||
except BaseException as e:
|
||||
logger.error('Auth {} - {} - {}'.format(url, e.__class__.__name__, str(e)))
|
||||
|
@ -24,16 +24,30 @@ def auth_api(url, data):
|
|||
|
||||
def set_wiki_password(data):
|
||||
auth_data = dict(
|
||||
username=data['username'],
|
||||
username=data['username'].lower(),
|
||||
password=data['password'],
|
||||
)
|
||||
return auth_api(secrets.WIKI_AUTH_API_URL + 'set-wiki-password', auth_data)
|
||||
return auth_api(secrets.WIKI_AUTH_API_URL + 'set-wiki-password', data=auth_data)
|
||||
|
||||
def set_discourse_password(data):
|
||||
auth_data = dict(
|
||||
username=data['username'],
|
||||
username=data['username'].lower(),
|
||||
password=data['password'],
|
||||
first_name=data['first_name'],
|
||||
email=data['email'],
|
||||
)
|
||||
return auth_api(secrets.DISCOURSE_AUTH_API_URL + 'set-discourse-password', auth_data)
|
||||
return auth_api(secrets.DISCOURSE_AUTH_API_URL + 'set-discourse-password', data=auth_data)
|
||||
|
||||
def add_discourse_group_members(group_name, usernames):
|
||||
json = dict(
|
||||
group_name=group_name,
|
||||
usernames=usernames,
|
||||
)
|
||||
return auth_api(secrets.DISCOURSE_AUTH_API_URL + 'add-discourse-group-members', json=json)
|
||||
|
||||
def remove_discourse_group_members(group_name, usernames):
|
||||
json = dict(
|
||||
group_name=group_name,
|
||||
usernames=usernames,
|
||||
)
|
||||
return auth_api(secrets.DISCOURSE_AUTH_API_URL + 'remove-discourse-group-members', json=json)
|
||||
|
|
Loading…
Reference in New Issue
Block a user