def create_test_offering(): """ main test course: interesting data for grades, marking, submission, groups """ from grades.models import Activity, LetterActivity, CalNumericActivity, CalLetterActivity from submission.models import SubmissionComponent from submission.models.code import CodeComponent from submission.models.pdf import PDFComponent from groups.models import Group, GroupMember from marking.models import ActivityComponent crs = CourseOffering.objects.get(slug=TEST_COURSE_SLUG) crs.set_labtut(True) crs.set_url("http://www.cs.sfu.ca/CC/165/common/") crs.set_taemail("*****@*****.**") crs.save() # create example activities a1 = NumericActivity.objects.get(offering=crs, slug='a1') a2 = NumericActivity(offering=crs, name="Assignment 2", short_name="A2", status="URLS", due_date=crs.semester.start + datetime.timedelta(days=70), percent=10, group=True, max_grade=20, position=2) a2.set_url("http://www.cs.sfu.ca/CC/165/common/a2") a2.save() pr = LetterActivity(offering=crs, name="Project", short_name="Proj", status="URLS", due_date=crs.semester.start + datetime.timedelta(days=80), percent=40, group=True, position=3) pr.save() re = LetterActivity(offering=crs, name="Report", short_name="Rep", status="URLS", due_date=crs.semester.start + datetime.timedelta(days=81), percent=10, group=False, position=4) re.save() ex = NumericActivity(offering=crs, name="Final Exam", short_name="Exam", status="URLS", due_date=None, percent=30, group=False, max_grade=90, position=5) ex.save() to = CalNumericActivity(offering=crs, name="Final Percent", short_name="Perc", status="INVI", due_date=None, percent=0, group=False, max_grade=100, formula="[[activitytotal]]", position=6) to.save() to = CalLetterActivity(offering=crs, name="Letter Grade", short_name="Letter", status="INVI", due_date=None, percent=0, group=False, numeric_activity=to, position=6) to.save() # make A1 submittable and markable s = CodeComponent(activity=a1, title=u"Cöde File", description="The code you're submitting.", allowed=".py,.java") s.save() s = PDFComponent(activity=a1, title="Report", description="Report on what you did.", specified_filename="report.pdf") s.save() m = ActivityComponent( numeric_activity=a1, max_mark=5, title=u"Part ➀", description="Part ➀ was done well and seems to work.", position=1, slug='part-1') m.save() m = ActivityComponent( numeric_activity=a1, max_mark=5, title="Part 2", description="Part 2 was done well and seems to work.", position=2) m.save() # create some groups members = list(Member.objects.filter(offering=crs, role='STUD')) random.shuffle(members) m = members.pop() g = Group(name="SomeGroup", courseoffering=crs, manager=m) g.save() for m in [m, members.pop()]: gm = GroupMember(group=g, student=m, confirmed=True, activity=a2) gm.save() m = members.pop() g = Group(name="AnotherGroup", courseoffering=crs, manager=m) g.save() for m in [m, members.pop(), members.pop()]: gm = GroupMember(group=g, student=m, confirmed=True, activity=a2) gm.save() gm = GroupMember(group=g, student=m, confirmed=True, activity=pr) gm.save() return itertools.chain( Activity.objects.all(), NumericActivity.objects.all(), LetterActivity.objects.all(), CalNumericActivity.objects.all(), CalLetterActivity.objects.all(), SubmissionComponent.objects.all(), CodeComponent.objects.all(), PDFComponent.objects.all(), Group.objects.all(), GroupMember.objects.all(), ActivityComponent.objects.all(), )
def assign_student(request, course_slug, group_slug): course = get_object_or_404(CourseOffering, slug=course_slug) group = get_object_or_404(Group, slug=group_slug, courseoffering=course) activities = Activity.objects.filter(offering=course, group=True, deleted=False) members = Member.objects.filter(offering=course, role='STUD').select_related('person') if request.method == "POST": add_act = [] for a in activities: form = ActivityForm(request.POST, prefix=a.slug) if form.is_valid() and form.cleaned_data['selected'] == True: add_act.append(a) for m in members: form = StudentForm(request.POST, prefix=m.person.userid) if form.is_valid() and form.cleaned_data['selected'] == True: for a in add_act: old_gm = GroupMember.objects.filter(activity_id=a.id, student=m) if len(old_gm) > 0: messages.error( request, "%s is already in a group for %s." % (m.person.name(), a.name)) else: gm = GroupMember(group=group, student=m, confirmed=True, activity_id=a.id) gm.save() messages.success( request, "%s added to group %s for %s." % (m.person.name(), group.name, a.name)) #LOG EVENT# l = LogEntry( userid=request.user.username, description="added %s to group %s for %s." % (m.person.userid, group.name, a), related_object=gm) l.save() return HttpResponseRedirect( reverse('offering:groups:view_group', kwargs={ 'course_slug': course.slug, 'group_slug': group.slug })) else: activity_data = [] for a in activities: form = ActivityForm(prefix=a.slug) activity_data.append({'form': form, 'act': a}) student_data = [] for m in members: form = StudentForm(prefix=m.person.userid) student_data.append({'form': form, 'member': m}) return render(request, 'groups/assign_student.html', \ {'course':course, 'group':group, 'activity_data': activity_data, 'student_data': student_data})
def invite(request, course_slug, group_slug): #TODO need to validate the student who is invited, cannot be the invitor him/herself. course = get_object_or_404(CourseOffering, slug=course_slug) group = get_object_or_404(Group, courseoffering=course, slug=group_slug) person = get_object_or_404(Person, userid=request.user.username) invitor = get_object_or_404(Member, ~Q(role='DROP'), person=person, offering=course) error_info = None group_max = course.group_max() from django import forms class StudentReceiverForm(forms.Form): name = forms.CharField() if request.method == "POST": student_receiver_form = StudentReceiverForm(request.POST) #student_receiver_form.activate_addform_validation(course_slug,group_slug) if student_receiver_form.is_valid(): name = student_receiver_form.cleaned_data['name'] existing = GroupMember.objects.filter( group=group).values('student').order_by().distinct().count() if group_max and existing >= group_max: messages.add_message( request, messages.ERROR, 'Group already has %s members, which is the maximum.' % (group_max)) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) members = Member.objects.filter(person__userid=name, offering=course, role="STUD") if not members: messages.add_message(request, messages.ERROR, 'Could not find userid "%s".' % (name)) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) member = members[0] # find out if this person is already in a group gms = group.groupmember_set.all() all_act = all_activities(gms) existing_memb = GroupMember.objects.filter(student=member, activity__in=all_act) if GroupMember.objects.filter(student=member, group=group): messages.add_message( request, messages.ERROR, "%s is already in this group" % (member.person.userid)) elif existing_memb: error = "%s is already in a group for %s" % ( member.person.userid, ", ".join(m.activity.name for m in existing_memb)) messages.add_message(request, messages.ERROR, error) else: #member = Member.objects.get(person = member.person, offering = course) for invitorMembership in GroupMember.objects.filter( group=group, student=invitor): newGroupMember = GroupMember( group=group, student=member, activity=invitorMembership.activity, confirmed=False) newGroupMember.save(member.person) #LOG EVENT# l = LogEntry( userid=request.user.username, description= "invited %s to join group %s for activity %s." % (newGroupMember.student.person.userid, group.name, newGroupMember.activity), related_object=newGroupMember) l.save() n = NewsItem( user=member.person, author=person, course=group.courseoffering, source_app="group", title="Group Invitation", content="You have been invited to join group %s." % (group.name), url=reverse('offering:groups:groupmanage', kwargs={'course_slug': course.slug})) n.save() messages.add_message( request, messages.SUCCESS, 'Your invitation to %s has been sent out.' % (member.person.name())) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) else: messages.add_message(request, messages.ERROR, "Invalid userid.") return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) else: student_receiver_form = StudentReceiverForm() context = {'course': course, 'form': student_receiver_form} return render(request, "groups/invite.html", context)
def submit(request, course_slug): person = get_object_or_404(Person, userid=request.user.username) course = get_object_or_404(CourseOffering, slug=course_slug) member = Member.objects.exclude(role='DROP').get(person=person, offering=course) is_staff = is_course_staff_by_slug(request, course_slug) span = course.group_span_activities() error_info = None name = request.POST.get('GroupName') if name: name = name[:30] #Check if group has a unique name if Group.objects.filter(name=name, courseoffering=course): error_info = "A group named \"%s\" already exists" % (name) messages.add_message(request, messages.ERROR, error_info) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) #Check if the group name is empty, these two checks may need to be moved to forms later. if name == "": error_info = "Group name cannot be empty: please enter a group name." messages.add_message(request, messages.ERROR, error_info) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) else: # find selected activities selected_act = [] activities = Activity.objects.filter(offering=course, group=True, deleted=False) if not is_staff: activities = activities.exclude(status='INVI') for activity in activities: activityForm = ActivityForm(request.POST, prefix=activity.slug) if activityForm.is_valid( ) and activityForm.cleaned_data['selected'] == True: selected_act.append(activity) # no selected activities: fail. if not selected_act: messages.add_message(request, messages.ERROR, "Group not created: no activities selected.") return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) # check groups_span_activities restriction if it's set if not span and not is_staff and len(selected_act) > 1: # students cannot violate groups_span_activities restriction, but instructors can messages.add_message( request, messages.ERROR, "Group not created: groups cannot last for more than one activity in this course." ) return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) groupForSemester = False #validate database integrity before saving anything. #If one student is in a group for an activity, he/she cannot be in another group for the same activity. if is_course_student_by_slug(request, course_slug): isStudentCreatedGroup = True studentList = [] studentList.append(member) elif is_course_staff_by_slug(request, course_slug): isStudentCreatedGroup = False studentList = [] students = Member.objects.select_related('person').filter( offering=course, role='STUD') for student in students: studentForm = StudentForm(request.POST, prefix=student.person.userid) if studentForm.is_valid( ) and studentForm.cleaned_data['selected'] == True: studentList.append(student) #Check if students has already in a group if _validateIntegrity(request, isStudentCreatedGroup, groupForSemester, course, studentList, selected_act) == False: return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) #No selected members,group creating will fail. if not studentList: messages.add_message(request, messages.ERROR, "Group not created: no members selected.") return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) group = Group(name=name, manager=member, courseoffering=course, groupForSemester=groupForSemester) group.save() #LOG EVENT# l = LogEntry(userid=request.user.username, description="created a new group %s for %s." % (group.name, course), related_object=group) l.save() if is_course_student_by_slug(request, course_slug): for activity in selected_act: groupMember = GroupMember(group=group, student=member, confirmed=True, activity_id=activity.id) groupMember.save() #LOG EVENT# l = LogEntry( userid=request.user.username, description= "automatically became a group member of %s for activity %s." % (group.name, groupMember.activity), related_object=groupMember) l.save() messages.add_message(request, messages.SUCCESS, 'Group Created') return HttpResponseRedirect( reverse('offering:groups:groupmanage', kwargs={'course_slug': course_slug})) elif is_course_staff_by_slug(request, course_slug): students = Member.objects.select_related('person').filter( offering=course, role='STUD') for student in students: studentForm = StudentForm(request.POST, prefix=student.person.userid) if studentForm.is_valid( ) and studentForm.cleaned_data['selected'] == True: for activity in selected_act: groupMember = GroupMember(group=group, student=student, confirmed=True, activity_id=activity.id) groupMember.save() #LOG EVENT# l = LogEntry( userid=request.user.username, description= "added %s as a group member to %s for activity %s." % (student.person.userid, group.name, groupMember.activity), related_object=groupMember) l.save() n = NewsItem(user=student.person, author=member.person, course=group.courseoffering, source_app="group", title="Added to Group", content="You have been added the group %s." % (group.name), url=reverse( 'offering:groups:groupmanage', kwargs={'course_slug': course.slug})) n.save() messages.add_message(request, messages.SUCCESS, 'Group Created') return HttpResponseRedirect( reverse('offering:groups:view_group', kwargs={ 'course_slug': course_slug, 'group_slug': group.slug })) else: return HttpResponseForbidden()
def test_group_submission_view(self): """ test if group submission can be viewed by group member and non group member """ now = datetime.datetime.now() _, course = create_offering() a1 = NumericActivity(name="Assignment 1", short_name="A1", status="RLS", offering=course, position=2, max_grade=15, due_date=now, group=True) a1.save() a2 = NumericActivity(name="Assignment 2", short_name="A2", status="RLS", offering=course, position=1, max_grade=15, due_date=now, group=True) a2.save() p = Person.objects.get(userid="ggbaker") member = Member(person=p, offering=course, role="INST", career="NONS", added_reason="UNK") member.save() c1 = URL.Component(activity=a1, title="URL Link", position=8) c1.save() c2 = Archive.Component(activity=a1, title="Archive File", position=1, max_size=100000) c2.save() c3 = Code.Component(activity=a1, title="Code File", position=3, max_size=2000, allowed=".py") c3.save() userid1 = "0aaa0" userid2 = "0aaa1" userid3 = "0aaa2" for u in [userid1, userid2, userid3]: p = Person.objects.get(userid=u) m = Member(person=p, offering=course, role="STUD", credits=3, career="UGRD", added_reason="UNK") m.save() m = Member.objects.get(person__userid=userid1, offering=course) g = Group(name="Test Group", manager=m, courseoffering=course) g.save() gm = GroupMember(group=g, student=m, confirmed=True, activity=a1) gm.save() gm = GroupMember(group=g, student=m, confirmed=True, activity=a2) gm.save() m = Member.objects.get(person__userid=userid2, offering=course) gm = GroupMember(group=g, student=m, confirmed=True, activity=a1) gm.save() gm = GroupMember(group=g, student=m, confirmed=True, activity=a2) gm.save() m = Member.objects.get(person__userid=userid3, offering=course) gm = GroupMember(group=g, student=m, confirmed=True, activity=a2) gm.save() client = Client() # login as "0aaa0", member of group : test_group for assignment1 and assgnment2 client.login_user("0aaa0") #submission page for assignment 1 url = reverse('offering:submission:show_components', kwargs={ 'course_slug': course.slug, 'activity_slug': a1.slug }) response = basic_page_tests(self, client, url) self.assertContains( response, "This is a group activity. You will submit on behalf of the group “Test Group”." ) self.assertContains( response, "You haven't made a submission for this component.")
def test_class_1(slug): """ main test course: 20 students, TA, some assignments """ crs = CourseOffering.objects.get(slug=slug) crs.set_labtut(True) crs.set_url("http://www.cs.sfu.ca/CC/165/common/") crs.set_taemail("*****@*****.**") crs.save() for i in range(10): lab = "D1%02i" % (random.randint(1, 4)) p = Person.objects.get(userid="0aaa%i" % (i)) if Member.objects.filter(person=p, offering=crs, role="STUD"): # randomly added by other student-adder: skip continue m = Member(person=p, offering=crs, role="STUD", credits=3, career="UGRD", added_reason="AUTO", labtut_section=lab) m.save() if not Member.objects.filter( person__userid='ggbaker', offering=crs, role='INST'): Member(person=Person.objects.get(userid='ggbaker'), offering=crs, role='INST').save() # create a TA p = Person(emplid=fake_emplid(), userid="0grad1", last_name="Gradstudent", first_name="Douglas", middle_name="", pref_first_name="Doug") p.save() m = Member(person=p, offering=crs, role="TA", credits=0, career="NONS", added_reason="AUTO", labtut_section=None) m.save() # create example activities crs.activity_set.all().update(deleted=True) a1 = NumericActivity(offering=crs, name="Assignment 1", short_name="A1", status="RLS", due_date=crs.semester.start + datetime.timedelta(days=60), percent=10, group=False, max_grade=10, position=1) a1.set_url("http://www.cs.sfu.ca/CC/165/common/a1") a1.save() a2 = NumericActivity(offering=crs, name="Assignment 2", short_name="A2", status="URLS", due_date=crs.semester.start + datetime.timedelta(days=70), percent=10, group=True, max_grade=20, position=2) a2.save() pr = LetterActivity(offering=crs, name="Project", short_name="Proj", status="URLS", due_date=crs.semester.start + datetime.timedelta(days=80), percent=40, group=True, position=3) pr.save() re = LetterActivity(offering=crs, name="Report", short_name="Rep", status="URLS", due_date=crs.semester.start + datetime.timedelta(days=81), percent=10, group=False, position=4) re.save() ex = NumericActivity(offering=crs, name="Final Exam", short_name="Exam", status="URLS", due_date=None, percent=30, group=False, max_grade=90, position=5) ex.save() to = CalNumericActivity(offering=crs, name="Final Percent", short_name="Perc", status="INVI", due_date=None, percent=0, group=False, max_grade=100, formula="[[activitytotal]]", position=6) to.save() to = CalLetterActivity(offering=crs, name="Letter Grade", short_name="Letter", status="INVI", due_date=None, percent=0, group=False, numeric_activity=to, position=6) to.save() # make A1 submittable and markable s = CodeComponent(activity=a1, title="Code File", description="The code you're submitting.", allowed=".py,.java") s.save() s = PDFComponent(activity=a1, title="Report", description="Report on what you did.", specified_filename="report.pdf") s.save() m = ActivityComponent( numeric_activity=a1, max_mark=5, title="Part 1", description="Part 1 was done well and seems to work.", position=1) m.save() m = ActivityComponent( numeric_activity=a1, max_mark=5, title="Part 2", description="Part 2 was done well and seems to work.", position=2) m.save() # create some groups g = Group(name="SomeGroup", courseoffering=crs, manager=Member.objects.get(offering=crs, person__userid="0aaa0", role='STUD')) g.save() for userid in ['0aaa0', '0aaa1', '0aaa5', '0aaa8']: gm = GroupMember(group=g, student=Member.objects.get(offering=crs, person__userid=userid), confirmed=True, activity=a2) gm.save() g = Group(name="AnotherGroup", courseoffering=crs, manager=Member.objects.get(offering=crs, person__userid="0aaa4")) g.save() for userid in ['0aaa4', '0aaa6', '0aaa7', '0aaa2']: gm = GroupMember(group=g, student=Member.objects.get(offering=crs, person__userid=userid), confirmed=True, activity=a2) gm.save() gm = GroupMember(group=g, student=Member.objects.get(offering=crs, person__userid=userid), confirmed=True, activity=pr) gm.save()