def get(self, studentId): """List all the files at destination to a specific student """ student = Student.get_by_id(studentId) if not student: self.abort(404) current_user = self.login_required() if (not current_user.is_staff and not current_user.is_admin and not current_user.is_domain_admin and current_user.student_id != student.key.id()): self.abort(403) return cursor_key = self.request.GET.get('cursor') # using cheap request and ndb entities cache file_keys, cursor, _ = models.Document.get_files(student.key, cursor_key, keys_only=True) ffiles = [k.get_async() for k in file_keys] self.render_json({ 'files': map(self.file_dict, [ff.get_result() for ff in ffiles]), 'cursor': cursor.urlsafe() if cursor else '' })
def test_archive_year(self): Student.new_student( 'bob', 'bob smith', email='*****@*****.**', year=2016 ).key.get(use_cache=False) Student.new_student( 'alice', 'alice smith', email='*****@*****.**', year=2015 ).key.get(use_cache=False) bob = self.new_user(is_staff=True) headers = self.login(user=bob) self.app.delete( '%s/dashboard/pgy/2016' % api.path, headers=headers, ) self.assertTrue(Student.get_by_id('A0002', use_cache=False).is_active) self.assertFalse(Student.get_by_id('A0001', use_cache=False).is_active)
def stats_handler(data): pos, line = data if not line: raise StopIteration try: stats = json.loads(line) except (ValueError, TypeError): logging.error("Failed to process rosh review stats: %s", line) raise StopIteration student_email = stats.get('email', '').lower() student_id = User.email_to_student_id(student_email) if student_id is None: logging.error( "Failed to process rosh review stats: invalid student email (%s)", line) raise StopIteration student = Student.get_by_id(student_id) if student is None: logging.error( "Failed to process rosh review stats: student not found (%s)", line) raise StopIteration user_stats = RoshReviewUserStats.new_stats(student, stats, commit=False) user_topic_stats = user_stats.update_topic_stats(commit=False) yield op.db.Put(user_stats) for ts in user_topic_stats: yield op.db.Put(ts) yield op.counters.Increment('User stats', 1) yield op.counters.Increment('User topic stats', len(user_topic_stats))
def get(self, studentId): """List all the files at destination to a specific student """ student = Student.get_by_id(studentId) if not student: self.abort(404) current_user = self.login_required() if ( not current_user.is_staff and not current_user.is_admin and not current_user.is_domain_admin and current_user.student_id != student.key.id() ): self.abort(403) return cursor_key = self.request.GET.get('cursor') # using cheap request and ndb entities cache file_keys, cursor, _ = models.Document.get_files( student.key, cursor_key, keys_only=True ) ffiles = [k.get_async() for k in file_keys] self.render_json({ 'files': map( self.file_dict, [ff.get_result() for ff in ffiles] ), 'cursor': cursor.urlsafe() if cursor else '' })
def test_delete_student_logged_off(self): headers = self.logoff() self.app.delete('%s/dashboard/students/%s' % (api.path, self.bob.key.id()), headers=headers, status=401) self.assertIsNotNone( Student.get_by_id(self.bob.key.id(), use_cache=False))
def test_delete_student(self): alice = self.new_user(is_staff=True) headers = self.login(user=alice) self.app.delete('%s/dashboard/students/%s' % (api.path, self.bob.key.id()), headers=headers) self.assertIsNone(Student.get_by_id(self.bob.key.id(), use_cache=False))
def test_archive_year(self): Student.new_student('bob', 'bob smith', email='*****@*****.**', year=2016).key.get(use_cache=False) Student.new_student('alice', 'alice smith', email='*****@*****.**', year=2015).key.get(use_cache=False) bob = self.new_user(is_staff=True) headers = self.login(user=bob) self.app.delete( '%s/dashboard/pgy/2016' % api.path, headers=headers, ) self.assertTrue(Student.get_by_id('A0002', use_cache=False).is_active) self.assertFalse(Student.get_by_id('A0001', use_cache=False).is_active)
def test_delete_student_logged_off(self): headers = self.logoff() self.app.delete( '%s/dashboard/students/%s' % (api.path, self.bob.key.id()), headers=headers, status=401 ) self.assertIsNotNone( Student.get_by_id(self.bob.key.id(), use_cache=False) )
def test_delete_student(self): alice = self.new_user(is_staff=True) headers = self.login(user=alice) self.app.delete( '%s/dashboard/students/%s' % (api.path, self.bob.key.id()), headers=headers ) self.assertIsNone( Student.get_by_id(self.bob.key.id(), use_cache=False) )
def delete(self, studentId, fileId): """Delete a document. """ self.admin_required() student = Student.get_by_id(studentId) document = models.Document.get_by_id(fileId) if (not student or not document or document.dest_ref.id() != student.key.id()): self.abort(404) document.delete() self.render_json({'success': True})
def new_file(cls, dest_id, blob_info, doc_type, sender=None, name=None): dest = Student.get_by_id(dest_id) if dest is None: raise ValueError("Couldn't find the student to send the file to.") data = { "name": name if name else blob_info.filename, "type": doc_type, "sender": sender.display_name if sender else "System", "dest": dest.display_name, "lastDownloadAt": "", } doc = cls(id=str(blob_info.key()), data=data, sender_ref=sender.key if sender else None, dest_ref=dest.key) doc.put() return doc
def _get_exams_by_student_id(self, student_id): student = Student.get_by_id(student_id) if not student: self.abort(404, 'User not found') current_user = self.login_required() if (not current_user.is_staff and not current_user.is_admin and not current_user.is_domain_admin and student.key.id() != current_user.student_id): self.abort(403) exams = models.AssessmentExam.get_by_student_id(student.key.id()) self.render_json({ 'cursor': '', 'exams': [e.summary() for e in exams], 'student': student.summary() })
def delete(self, studentId, fileId): """Delete a document. """ self.admin_required() student = Student.get_by_id(studentId) document = models.Document.get_by_id(fileId) if ( not student or not document or document.dest_ref.id() != student.key.id() ): self.abort(404) document.delete() self.render_json({'success': True})
def new_file(cls, dest_id, blob_info, doc_type, sender=None, name=None): dest = Student.get_by_id(dest_id) if dest is None: raise ValueError("Couldn't find the student to send the file to.") data = { 'name': name if name else blob_info.filename, 'type': doc_type, 'sender': sender.display_name if sender else 'System', 'dest': dest.display_name, 'lastDownloadAt': '' } doc = cls( id=str(blob_info.key()), data=data, sender_ref=sender.key if sender else None, dest_ref=dest.key ) doc.put() return doc
def _get_exams_by_student_id(self, student_id): student = Student.get_by_id(student_id) if not student: self.abort(404, 'User not found') current_user = self.login_required() if ( not current_user.is_staff and not current_user.is_admin and not current_user.is_domain_admin and student.key.id() != current_user.student_id ): self.abort(403) exams = models.AssessmentExam.get_by_student_id(student.key.id()) self.render_json({ 'cursor': '', 'exams': [e.summary() for e in exams], 'student': student.summary() })
def stats_handler(data): pos, line = data if not line: raise StopIteration try: stats = json.loads(line) except (ValueError, TypeError): logging.error("Failed to process rosh review stats: %s", line) raise StopIteration student_email = stats.get('email', '').lower() student_id = User.email_to_student_id(student_email) if student_id is None: logging.error( "Failed to process rosh review stats: invalid student email (%s)", line ) raise StopIteration student = Student.get_by_id(student_id) if student is None: logging.error( "Failed to process rosh review stats: student not found (%s)", line ) raise StopIteration user_stats = RoshReviewUserStats.new_stats( student, stats, commit=False ) user_topic_stats = user_stats.update_topic_stats(commit=False) yield op.db.Put(user_stats) for ts in user_topic_stats: yield op.db.Put(ts) yield op.counters.Increment('User stats', 1) yield op.counters.Increment('User topic stats', len(user_topic_stats))