-
Notifications
You must be signed in to change notification settings - Fork 16
/
export-users
66 lines (57 loc) · 1.64 KB
/
export-users
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
#!/bin/env python
import csv
import sys
from time import time
from datetime import datetime
from mist.api.users.models import User
def nice_date(tstamp):
if not tstamp:
return None
try:
return str(datetime.fromtimestamp(int(tstamp)))
except:
return 'ERROR'
def print_csv():
"""Export all users to csv."""
users = User.objects(state='confirmed')
with open('mist-%s.csv' % int(time()), 'w') as csvfile:
mistwriter = csv.writer(csvfile)
index = 1
mistwriter.writerow([
'email',
'first_name',
'last_name',
'status',
'registration_date',
'activation_date',
'last_login',
'monitored_machines',
])
for user in users:
try:
if user.first_name:
first_name = user.first_name
else:
first_name = ''
if user.last_name:
last_name = user.last_name
else:
last_name = ''
if user.email:
mistwriter.writerow([
user.email,
first_name,
last_name,
user.status,
nice_date(user.registration_date),
nice_date(user.activation_date),
nice_date(user.last_login),
])
except:
print(sys.exc_info()[1])
pass
index += 1
def main():
print_csv()
if __name__ == "__main__":
main()