Exemple #1
0
    def get(self):
        username = self.get_cookie('stuID')
        if not adm.is_admin(username):
            self.redirect('/error')

        self.render('upload_resource.html', cookie_name=username,
                    is_admin=adm.is_admin(username),
                    messages=md.fetch_message(username))
Exemple #2
0
 def get(self):
     username = self.get_cookie('stuID')
     self.render('index.html',
                 cookie_name=username,
                 is_admin=adm.is_admin(username),
                 announcements=ad.fetch_all(),
                 messages=md.fetch_message(username))
Exemple #3
0
    def post(self):
        username = self.get_cookie('stuID')
        if not adm.is_admin(username):
            self.redirect('/error')

        title = self.get_argument('resource_title')
        author = username
        content = self.get_argument('resource_content')
        date_ = time.strftime('%Y-%m-%d %H:%M:%S')

        resource_id = rd.get_last_id()
        filepath = ''
        resource_files = self.request.files.get('resource_files')
        if resource_files:
            os.mkdir(os.path.join('data', 'resource', 'res_' + str(resource_id)))
            assert len(resource_files) == 1
            for res_file in resource_files:
                filename = res_file['filename']
                # TODO: try catch
                filepath = os.path.join(
                    'data', 'resource', 'res_' + str(resource_id), filename)
                # TODO: this method is unsafe, handle the comflict.
                with open(filepath, 'wb') as upfile:
                    upfile.write(res_file['body'])
        # TODO: find a better file upload way.

        rd.insert(author, title, content, date_, filepath)
        # TODO: homework is (title, author, ...), unify them.
        self.redirect('/resource')
Exemple #4
0
    def get(self, para):
        username = self.get_cookie('stuID')
        if not username:
            self.redirect('/error')

        assert para in ['resource', 'homework', 'submission']
        if para == 'resource':
            data_id = int(self.get_argument('res_id'))
            data = rd.fetch_by_id(data_id)

            self.set_header('Content-Type', 'application/octet-stream')
            filename = os.path.basename(data['file_path'])
            self.set_header('Content-Disposition',
                            'attachment; filename=' + filename)
            with open(data['file_path'], 'rb') as f:
                self.write(f.read())

        if para == 'homework':
            # TODO: find a better way to download zip file
            if not adm.is_admin(username):
                self.redirect('/error')
            homework_id = int(self.get_argument('hw_id'))
            self.set_header('Content-Type', 'application/octet-stream')
            self.set_header('Content-Disposition',
                            'attachment; filename=hw_' + str(homework_id) + '.zip')
            zfile = zipfile.ZipFile('temp.zip', 'w')
            startdir = './data/homework/hw_' + str(homework_id)
            for dirpath, dirnames, filenames in os.walk(startdir):
                for filename in filenames:
                    zfile.write(os.path.join(dirpath, filename))
            zfile.close()
            with open('temp.zip', 'rb') as f:
                self.write(f.read())
Exemple #5
0
    def get(self, para):
        username = self.get_cookie('stuID')
        if not username:
            self.redirect('/error')

        if para == 'view':
            homework_list = hd.fetch_all()
            status_dict = {}
            enable_submit = {}   # True or False
            for hw in homework_list:
                hw_id = hw['homework_id']
                assert hw_id
                submission = sbd.fetch_one_submission(username, hw_id)
                if not submission:
                    status_dict[hw_id] = 'notsubmit'
                else:
                    status_dict[hw_id] = submission['status']
                    assert status_dict[hw_id] != 'notsubmit'

                ddl = time.mktime(time.strptime(hw['deadline'], '%Y-%m-%d %H:%M:%S'))
                now = time.mktime(time.localtime())
                is_enable = now - ddl < 0
                enable_submit[hw_id] = is_enable

            self.render('homework.html', cookie_name=username,
                        homework_list=homework_list,
                        is_admin=adm.is_admin(username),
                        status_dict=status_dict,
                        enable_submit=enable_submit,
                        messages=md.fetch_message(username))

        if para == 'assign':
            if not adm.is_admin(username):
                self.redirect('/error')
            self.render('assign_homework.html', cookie_name=username,
                        is_admin=adm.is_admin(username),
                        messages=md.fetch_message(username))

        if para == 'delete':
            if not adm.is_admin(username):
                self.redirect('/error')
            homework_id = int(self.get_argument('hw_id'))
            hd.delete(homework_id)
            shutil.rmtree(os.path.join('data', 'homework', 'hw_' + str(homework_id)))
            self.redirect('/homework/view')
Exemple #6
0
    def get(self):
        username = self.get_cookie('stuID')
        if not username:
            self.redirect('/error')

        user_info_list = ui.fetch_member_info()
        self.render('member.html', cookie_name=username,
                    is_admin=adm.is_admin(username),
                    user_info_list=user_info_list,
                    messages=md.fetch_message(username))
Exemple #7
0
    def get(self):
        username = self.get_cookie('stuID')
        if not username:
            self.redirect('/error')

        resource_list = rd.fetch_all()

        self.render('resource.html', cookie_name=username,
                    is_admin=adm.is_admin(username),
                    resource_list=resource_list,
                    messages=md.fetch_message(username))
Exemple #8
0
    def post(self):
        '''only admin can post'''
        username = self.get_cookie('stuID')
        if not adm.is_admin(username):
            self.redirect('/error')
        title = self.get_argument('announcement_title')
        author = username
        content = self.get_argument('announcement_content')
        date_ = time.strftime('%Y-%m-%d %H:%M:%S')

        ad.insert(title, author, content, date_)
        self.redirect('/')
Exemple #9
0
    def get(self, para):
        username = self.get_cookie('stuID')
        if not adm.is_admin(username):
            self.redirect('/error')

        if para == 'add_admin':
            adm.set_TA(self.get_argument('username'))
            self.redirect('/member')

        if para == 'delete_user':
            adm.delete_user(self.get_argument('username'))
            self.redirect('/member')
Exemple #10
0
    def post(self, para):
        if para == 'view':
            self.redirect('/error')
        # only assign_homework accept post request
        username = self.get_cookie('stuID')
        if not adm.is_admin(username):
            self.redirect('/error')

        title = self.get_argument('homework_title')
        author = username
        content = self.get_argument('homework_content')
        date_ = time.strftime('%Y-%m-%d %H:%M:%S')
        deadline = self.get_argument('homework_deadline', '2013-01-08 23:57:32')
        hd.insert(title, author, content, date_, deadline)
        homework_id = hd.get_last_id()
        # TODO: try catch
        os.mkdir(os.path.join('data', 'homework', 'hw_' + str(homework_id)))
        self.redirect('/homework/view')
Exemple #11
0
    def get(self):
        username = self.get_cookie('stuID')
        if not username:
            self.redirect('/error')

        homework_id = int(self.get_argument('hw_id', '0'))
        if homework_id == 0:
            # cann't get homework id
            self.redirect('/error')

        homework_info = hd.fetch_by_id(homework_id)
        assert homework_info
        status = 'notsubmit'
        submission = sbd.fetch_one_submission(username, homework_id)
        if not submission:
            status = 'notsubmit'
        else:
            status = submission['status']
        self.render('submit_homework.html', cookie_name=username,
                    is_admin=adm.is_admin(username),
                    homework_info=homework_info,
                    status=status,
                    messages=md.fetch_message(username))
Exemple #12
0
    def get(self):
        username = self.get_cookie('stuID')
        if not username:
            self.redirect('/error')

        userinfo = ui.fetch(username)
        info_args = ['username', 'sex', 'email', 'truename', 'mobile', 'self_intro']
        info_dict = {}
        for item in info_args:
            try:
                info_dict[item] = userinfo[item]
            except Exception as e:
                print(e)
                info_dict[item] = ''

        njuid = ud.fetch(username)['njuid']
        self.render(
            'setting.html',
            cookie_name=username,
            is_admin=adm.is_admin(username),
            njuid=njuid,
            messages=md.fetch_message(username),
            **info_dict
        )
Exemple #13
0
 def post(self):
     username = self.get_cookie('stuID')
     if not adm.is_admin(username):
         self.redirect('/error')