This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
/
cron.py
107 lines (84 loc) · 3.57 KB
/
cron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from ldap import TYPE_OR_VALUE_EXISTS
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
import commonware.log
import cronjobs
import larper
from users.models import UserProfile
log = commonware.log.getLogger('m.cron')
@cronjobs.register
def create_missing_profiles():
"""Create missing profiles on dev/stage.
The dev/staging servers are missing certain User objects, likely because
of users created before our UserProfile signal receivers. This will
create them and prevent us from getting annoying tracebacks from tests.
"""
users = User.objects.all()
for u in users:
try:
u.get_profile()
except ObjectDoesNotExist:
log.info('Created profile for user with email %s' % u.email)
UserProfile.objects.create(user=u)
@cronjobs.register
def create_missing_users():
"""Create missing users on dev/stage.
The dev/staging servers are missing certain User objects, likely because
of users created before our UserProfile signal receivers. This will
create them and prevent us from getting annoying tracebacks from tests.
Note that this needs to be run with the LDAP limit (temporarily) disabled.
"""
users = larper._return_all()
users_created = []
for u in users:
email = u[1]['mail'][0]
if not User.objects.filter(email=email):
try:
User.objects.create(username=email, email=email)
users_created.append(email)
log.info('Created user for LDAPer with email %s' % email)
except:
log.info('Could not create user for LDAPer with email %s'
% email)
log.info('Created %s users' % len(users_created))
@cronjobs.register
def vouchify():
"""Synchronizes LDAP vouch info into database.
This queries LDAP for users who's corresponding ``UserProfile`` has
``is_vouched`` as ``False``. It then updates ``is_vouched`` and
``vouched_by`` with up-to-date data.
"""
users = UserProfile.objects.filter(is_vouched=False)
for user in users:
person = user.get_ldap_person()
if person and 'mozilliansVouchedBy' in person[1]:
user.is_vouched = True
voucher = (person[1]['mozilliansVouchedBy'][0].split(',')[0]
.split('=')[1])
by = larper.get_user_by_uid(voucher)
if by:
email = by[1]['mail'][0]
try:
user.vouched_by = (User.objects.get(email=email)
.get_profile())
except User.DoesNotExist:
log.warning('No matching user for %s' % email)
except UserProfile.DoesNotExist:
log.warning('No matching user_profile for %s' % email)
user.save()
log.info('Data copied for %s' % user.user.username)
log.debug('%s is still unvouched... skipping' % user.user.username)
@cronjobs.register
def fix_bad_ldap_vouch():
"""Synchronizes MySQL vouch data into broken LDAP accounts.
Just vouches them as ZUUL because the MySQL vouch data is good and what
we'll use going forward.
"""
users = UserProfile.objects.filter(is_vouched=True)
for user in users:
person = user.get_ldap_person()
if person:
try:
larper.record_vouch('ZUUL', person[1]['uniqueIdentifier'][0])
except TYPE_OR_VALUE_EXISTS:
log.info('User is already vouched; no big.')