-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
76 lines (69 loc) · 2.53 KB
/
tasks.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
from __future__ import absolute_import
from enrollment.api import add_enrollment, get_enrollment
from celery.task import task
from django.contrib.auth.models import User
from celery.result import AsyncResult
@task
def enrollment_task(user, course):
course_id = course
users = user
print(enrollment_task.request.id)
#users_len -> count all users length
users_len = (User.objects.count())
print('total users len count :--',users_len)
if not users :
i = 100
count = 0
cou = 0
while True:
users = User.objects.values_list('username', flat=True)[count:i]
for user_name in users:
try:
if not get_enrollment(user_name, course_id):
try:
add_enrollment(user_name, course_id, is_active=True)
cou += 1
except:
pass
else:
print('user {0} is enroll in this course {1}'.format(user_name, course_id))
else:
cou += 1
print('user {0} is already enroll in this course {1}'.format(user_name, course_id))
print(cou)
pass
except Exception as e:
print(e)
len_user = cou
if (users_len == cou):
break
count = i
i += 100
else:
len_user = len(users)
i = 1
count = 0
cou = 0
while True:
selected_users = users[count:i]
for user_name in selected_users:
try:
if not get_enrollment(user_name, course_id):
try:
add_enrollment(user_name, course_id, is_active=True)
cou += 1
except:
pass
else:
print('user {0} is enroll in this course {1}'.format(user_name, course_id))
else:
print('user {0} is already enroll in this course {1}'.format(user_name, course_id))
cou += 1
pass
except Exception as e:
print(e)
if (len_user == cou):
break
count = i
i += 1
return ("Enrollment complete")