Commit 103db8aa by Victor Shnayder

add conflict detection--users should be in at most one cohort per course

parent 5fa8cf5a
...@@ -117,6 +117,12 @@ def add_cohort(course_id, name): ...@@ -117,6 +117,12 @@ def add_cohort(course_id, name):
group_type=CourseUserGroup.COHORT, group_type=CourseUserGroup.COHORT,
name=name) name=name)
class CohortConflict(Exception):
"""
Raised when user to be added is already in another cohort in same course.
"""
pass
def add_user_to_cohort(cohort, username_or_email): def add_user_to_cohort(cohort, username_or_email):
""" """
Look up the given user, and if successful, add them to the specified cohort. Look up the given user, and if successful, add them to the specified cohort.
...@@ -131,15 +137,29 @@ def add_user_to_cohort(cohort, username_or_email): ...@@ -131,15 +137,29 @@ def add_user_to_cohort(cohort, username_or_email):
Raises: Raises:
User.DoesNotExist if can't find user. User.DoesNotExist if can't find user.
ValueError if user already present. ValueError if user already present in this cohort.
CohortConflict if user already in another cohort.
""" """
if '@' in username_or_email: if '@' in username_or_email:
user = User.objects.get(email=username_or_email) user = User.objects.get(email=username_or_email)
else: else:
user = User.objects.get(username=username_or_email) user = User.objects.get(username=username_or_email)
if cohort.users.filter(id=user.id).exists(): # If user in any cohorts in this course already, complain
raise ValueError("User {0} already present".format(user.username)) course_cohorts = CourseUserGroup.objects.filter(
course_id=cohort.course_id,
users__id=user.id,
group_type=CourseUserGroup.COHORT)
if course_cohorts.exists():
if course_cohorts[0] == cohort:
raise ValueError("User {0} already present in cohort {1}".format(
user.username,
cohort.name))
else:
raise CohortConflict("User {0} is in another cohort {1} in course"
.format(user.username,
course_cohorts[0].name))
cohort.users.add(user) cohort.users.add(user)
return user return user
......
...@@ -133,6 +133,8 @@ def add_users_to_cohort(request, course_id, cohort_id): ...@@ -133,6 +133,8 @@ def add_users_to_cohort(request, course_id, cohort_id):
'added': [{'username': username, 'added': [{'username': username,
'name': name, 'name': name,
'email': email}, ...], 'email': email}, ...],
'conflict': [{'username_or_email': ...,
'msg': ...}], # in another cohort
'present': [str1, str2, ...], # already there 'present': [str1, str2, ...], # already there
'unknown': [str1, str2, ...]} 'unknown': [str1, str2, ...]}
""" """
...@@ -146,6 +148,7 @@ def add_users_to_cohort(request, course_id, cohort_id): ...@@ -146,6 +148,7 @@ def add_users_to_cohort(request, course_id, cohort_id):
users = request.POST.get('users', '') users = request.POST.get('users', '')
added = [] added = []
present = [] present = []
conflict = []
unknown = [] unknown = []
for username_or_email in split_by_comma_and_whitespace(users): for username_or_email in split_by_comma_and_whitespace(users):
try: try:
...@@ -158,10 +161,15 @@ def add_users_to_cohort(request, course_id, cohort_id): ...@@ -158,10 +161,15 @@ def add_users_to_cohort(request, course_id, cohort_id):
present.append(username_or_email) present.append(username_or_email)
except User.DoesNotExist: except User.DoesNotExist:
unknown.append(username_or_email) unknown.append(username_or_email)
except cohorts.CohortConflict as err:
conflict.append({'username_or_email': username_or_email,
'msg': str(err)})
return JsonHttpReponse({'success': True, return JsonHttpReponse({'success': True,
'added': added, 'added': added,
'present': present, 'present': present,
'conflict': conflict,
'unknown': unknown}) 'unknown': unknown})
@ensure_csrf_cookie @ensure_csrf_cookie
......
...@@ -166,8 +166,10 @@ var CohortManager = (function ($) { ...@@ -166,8 +166,10 @@ var CohortManager = (function ($) {
function adder(note, color) { function adder(note, color) {
return function(item) { return function(item) {
var li = $('<li></li>') var li = $('<li></li>')
if (typeof item === "object") { if (typeof item === "object" && item.username) {
li.text(note + ' ' + item.name + ', ' + item.username + ', ' + item.email); li.text(note + ' ' + item.name + ', ' + item.username + ', ' + item.email);
} else if (typeof item === "object" && item.msg) {
li.text(note + ' ' + item.username_or_email + ', ' + item.msg);
} else { } else {
// string // string
li.text(note + ' ' + item); li.text(note + ' ' + item);
...@@ -176,10 +178,13 @@ var CohortManager = (function ($) { ...@@ -176,10 +178,13 @@ var CohortManager = (function ($) {
op_results.append(li); op_results.append(li);
} }
} }
op_results.empty();
if (response && response.success) { if (response && response.success) {
response.added.forEach(adder("Added", "green")); response.added.forEach(adder("Added", "green"));
response.present.forEach(adder("Already present:", "black")); response.present.forEach(adder("Already present:", "black"));
response.conflict.forEach(adder("In another cohort:", "purple"));
response.unknown.forEach(adder("Unknown user:", "red")); response.unknown.forEach(adder("Unknown user:", "red"));
users_area.val('')
} else { } else {
log_error(response.msg || "There was an error adding users"); log_error(response.msg || "There was an error adding users");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment