def gen_entries(self): """Generate entries.""" for journal in self.journals: for node in journal.node_set.all(): if node.type == Node.ENTRYDEADLINE: entry = factory.make_entry(node.preset.forced_template) entry.late = faker.boolean() entry.save() factory.make_grade(entry, self.users['Teacher'].pk, random.randint(1, 10), faker.boolean()) node.entry = entry if journal.assignment.format.template_set.filter(archived=False, preset_only=False).count() > 0: random_entries = random.randint(0, 8) for _ in range(random_entries): template = random.choice(journal.assignment.format.template_set.filter(archived=False, preset_only=False)) entry = factory.make_entry(template) entry.late = faker.boolean() entry.save() factory.make_grade(entry, self.users['Teacher'].pk, random.randint(1, 10), faker.boolean()) factory.make_node(journal, entry)
def setUp(self): """Setup.""" self.u1 = factory.make_user("Zi", "pass", "*****@*****.**") self.jf1 = factory.make_format() self.jf2 = factory.make_format() self.a1 = factory.make_assignment('tcolloq', 'description', format=self.jf1) self.et1 = factory.make_entry_template('temp1') self.et2 = factory.make_entry_template('temp2') self.f1 = factory.make_field(self.et1, "test0", "1") self.f2 = factory.make_field(self.et1, "test2", "2") self.f3 = factory.make_field(self.et2, "test1", "1") self.j1 = factory.make_journal(self.a1, self.u1) self.e1 = factory.make_entry(self.et1) self.e2 = factory.make_entry(self.et2) self.c1 = factory.make_content(self.e1, "testcontent1", self.f1) self.c2 = factory.make_content(self.e1, "testcontent2", self.f2) self.c3 = factory.make_content(self.e2, "testcontent3", self.f3) self.jf1.available_templates.add() self.jf2.available_templates.add() self.usr = factory.make_user('teun', '1234', email='*****@*****.**', lti_id='a') self.crs = factory.make_course('test course please ignore', 'XXXX', startdate=datetime.date.today())
def test_check_if_need_VLE_publish_journals(self): """Hopefully doesnt crash.""" entry = factory.make_entry( Template.objects.filter(name='Default Template')[0]) entry.published = True entry.vle_coupling = Entry.GRADING entry.save() factory.make_node(self.journal, entry) entry1 = factory.make_entry( Template.objects.filter(name='Default Template')[0]) entry1.save() factory.make_node(self.journal, entry1) lti_grade.check_if_need_VLE_publish(self.assignment)
def test_get_comments(self): """Test update comment function.""" course = factory.make_course('Portfolio', 'PAV', author=self.rein) template = factory.make_entry_template('template') format = factory.make_format([template]) assignment = factory.make_assignment('Colloq', 'description1', format=format, courses=[course]) student_user, student_pass, student = test.set_up_user_and_auth( 'student', 'pass', '*****@*****.**') test.set_up_participation(student, course, 'Student') journal = factory.make_journal(assignment, student) entry = factory.make_entry(template) factory.make_node(journal, entry) comment = factory.make_comment(entry, self.rein, 'Excellent!', True) login = test.logging_in(self, self.rein_user, self.rein_pass) update_dict = {'text': 'Bad!'} test.api_patch_call(self, '/comments/' + str(comment.pk) + '/', update_dict, login) q_comment = Comment.objects.get(pk=comment.pk) self.assertEquals(q_comment.text, 'Bad!')
def test_sorted(self): """Test is the sort function works.""" entry = factory.make_entry(self.template) node = factory.make_node(self.j_rick_colloq, entry) nodes = timeline.get_sorted_nodes(self.j_rick_colloq) self.assertEquals(nodes[0].preset, self.deadlineentry) self.assertEquals(nodes[1], node) self.assertEquals(nodes[2].preset, self.progressnode)
def test_replace_result_no_url(self): """Hopefully doesnt crash.""" entry = factory.make_entry( Template.objects.filter(name='Default Template')[0]) entry.published = True entry.save() factory.make_node(self.journal, entry) self.journal.sourcedid = None self.journal.grade_url = None self.journal.save() self.assertEqual(lti_grade.replace_result(self.journal), None)
def test_change_Entry_vle_coupling(self): """Hopefully doesnt crash.""" entry = factory.make_entry( Template.objects.filter(name='Default Template')[0]) entry.published = True entry.save() factory.make_node(self.journal, entry) lti_grade.change_Entry_vle_coupling(self.journal, Entry.GRADING) for entry in Entry.objects.filter( published=True, node__journal=self.journal).exclude( vle_coupling=Entry.LINK_COMPLETE): self.assertEqual(entry.vle_coupling, Entry.GRADING)
def gen_entries(self): """Generate entries.""" for journal in self.journals: for node in journal.node_set.all(): if node.type == Node.ENTRYDEADLINE: entry = factory.make_entry(node.preset.forced_template) entry.late = faker.boolean() entry.grade = random.randint(1, 10) entry.save() node.entry = entry if journal.assignment.format.available_templates.count() > 0: random_entries = random.randint(0, 8) for _ in range(random_entries): template = random.choice(journal.assignment.format.available_templates.all()) entry = factory.make_entry(template) entry.late = faker.boolean() entry.grade = random.randint(1, 10) entry.save() factory.make_node(journal, entry)
def set_up_entries(template, n): """Set up some entries. Arguments: template -- the template to be used for the entries n -- number of entriest to be made Returns a list of entries that uses the same template. """ entries = [] for i in range(n): entries.append(factory.make_entry(template)) return entries
def add_entry_to_node(node, template): if not (node.preset and node.preset.forced_template == template): raise VLEBadRequest('Invalid template for preset node.') if node.type != Node.ENTRYDEADLINE: raise VLEBadRequest('Passed node is not an EntryDeadline node.') if node.entry: raise VLEBadRequest('Passed node already contains an entry.') if node.preset.is_locked(): raise VLEBadRequest('The lock date for this node has passed.') entry = factory.make_entry(template) node.entry = entry node.save() return entry
def test_json(self): """Test is the to dict function works correctly.""" entry = factory.make_entry(self.template) factory.make_node(self.j_rick_colloq, entry) nodes = timeline.get_nodes(self.j_rick_colloq, self.u_rick) self.assertEquals(len(nodes), 4) self.assertEquals(nodes[0]['type'], 'd') self.assertEquals(nodes[0]['entry'], None) self.assertEquals(nodes[1]['type'], 'e') self.assertEquals(nodes[2]['type'], 'a') self.assertEquals(nodes[3]['type'], 'p') self.assertEquals(nodes[3]['target'], 10)
def test_foreignkeys(self): """Test the foreign keys in the database.""" user_test = factory.make_user('lers', 'lers123', '*****@*****.**', '123456') course_test = factory.make_course('tname', 'XXXX', datetime.date.today()) factory.make_format() template = factory.make_entry_template("some_template") entr_test = factory.make_entry(template) field = factory.make_field(template, "test1", "1") factory.make_content(entr_test, "data", field) course_test.author = user_test ass_test = factory.make_assignment(name='tcolloq', description='description') ass_test.courses.add(course_test) journ_test = factory.make_journal(user=user_test, assignment=ass_test) self.assertEquals(entr_test.template.pk, template.pk) self.assertEquals(journ_test.user.pk, user_test.pk) self.assertEquals(journ_test.assignment.pk, ass_test.pk) self.assertEquals(course_test.author.pk, user_test.pk)
def test_get_comments(self): """Test get comments function.""" course = factory.make_course('Portfolio', 'PAV', author=self.rein) template = factory.make_entry_template('template') format = factory.make_format([template]) assignment = factory.make_assignment('Colloq', 'description1', format=format, courses=[course]) student_user, student_pass, student = test.set_up_user_and_auth( 'student', 'pass', '*****@*****.**') test.set_up_participation(student, course, 'Student') journal = factory.make_journal(assignment, student) entry = factory.make_entry(template) factory.make_node(journal, entry) factory.make_comment(entry, self.rein, 'Excellent!', True) login = test.logging_in(self, student_user, student_pass) result = test.api_get_call(self, '/comments/', login, params={ 'entry_id': entry.pk }).json() self.assertEquals(result['comments'][0]['text'], 'Excellent!') # permissions and authorization check for the api call. login = test.logging_in(self, self.no_perm_user, self.no_perm_pass) test.api_get_call(self, '/comments/', login, status=403, params={'entry_id': entry.pk}) test.api_get_call(self, '/comments/', login, status=404, params={'entry_id': self.not_found_pk}) test.test_unauthorized_api_get_call(self, '/comments/', params={'entry_id': entry.pk})
def create(self, request): """Create a new entry. Deletes remaining temporary user files if successful. Arguments: request -- the request that was send with journal_id -- the journal id template_id -- the template id to create the entry with node_id -- optional: the node to bind the entry to (only for entrydeadlines) content -- the list of {tag, data} tuples to bind data to a template field. """ journal_id, template_id, content_list = utils.required_params( request.data, "journal_id", "template_id", "content") node_id, = utils.optional_params(request.data, "node_id") journal = Journal.objects.get(pk=journal_id, user=request.user) assignment = journal.assignment template = Template.objects.get(pk=template_id) request.user.check_permission('can_have_journal', assignment) if assignment.is_locked(): return response.forbidden( 'The assignment is locked, entries can no longer be edited/changed.' ) if journal.needs_lti_link(): return response.forbidden(journal.outdated_link_warning_msg) # Check if the template is available if not (node_id or assignment.format.template_set.filter( archived=False, preset_only=False, pk=template.pk).exists()): return response.forbidden('Entry template is not available.') entry_utils.check_fields(template, content_list) # Node specific entry if node_id: node = Node.objects.get(pk=node_id, journal=journal) entry = entry_utils.add_entry_to_node(node, template) # Template specific entry else: entry = factory.make_entry(template) node = factory.make_node(journal, entry) for content in content_list: field_id, = utils.required_typed_params(content, (int, 'id')) data, = utils.required_params(content, 'data') field = Field.objects.get(pk=field_id) created_content = factory.make_content(node.entry, data, field) if field.type in field.FILE_TYPES: # Image, file or PDF user_file = file_handling.get_temp_user_file( request.user, assignment, content['data']) if user_file is None and field.required: node.entry.delete() # If there is a newly created node, delete that as well if not node_id: node.delete() return response.bad_request( 'One of your files was not correctly uploaded, please try again.' ) elif user_file: file_handling.make_permanent_file_content( user_file, created_content, node) # Notify teacher on new entry if (node.journal.sourcedid and node.entry.vle_coupling == Entry.NEED_SUBMISSION): lti_tasks.needs_grading.delay(node.pk) # Delete old user files file_handling.remove_temp_user_files(request.user) return response.created({ 'added': entry_utils.get_node_index(journal, node, request.user), 'nodes': timeline.get_nodes(journal, request.user), 'entry': serialize.EntrySerializer(entry, context={ 'user': request.user }).data })