コード例 #1
0
ファイル: file.py プロジェクト: rcr615/compair
    def post(self):
        uploaded_file = request.files.get('file')

        if not uploaded_file:
            abort(
                400,
                title="File Not Uploaded",
                message=
                "Sorry, no file was found to upload. Please try uploading again."
            )
        elif not allowed_file(
                uploaded_file.filename,
                current_app.config['ATTACHMENT_ALLOWED_EXTENSIONS']):
            extensions = [
                extension.upper() for extension in list(
                    current_app.config['ATTACHMENT_ALLOWED_EXTENSIONS'])
            ]
            extensions.sort()
            extensions = ", ".join(extensions)
            abort(
                400,
                title="File Not Uploaded",
                message=
                "Please try again with an approved file type, which includes: "
                + extensions + ".")

        on_save_file.send(self,
                          event_name=on_save_file.name,
                          user=current_user,
                          data={'file': uploaded_file.filename})

        try:
            db_file = File(user_id=current_user.id,
                           name='',
                           alias=uploaded_file.filename)
            db.session.add(db_file)
            db.session.commit()

            # use uuid generated by file model for name
            name = db_file.uuid + '.' + uploaded_file.filename.lower().rsplit(
                '.', 1)[1]

            # create new file with name
            full_path = os.path.join(
                current_app.config['ATTACHMENT_UPLOAD_FOLDER'], name)
            uploaded_file.save(full_path)
            current_app.logger.debug("Saved attachment {}/{}".format(
                current_app.config['ATTACHMENT_UPLOAD_FOLDER'], name))

            # update file record with name
            db_file.name = name
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            raise e

        return {'file': marshal(db_file, dataformat.get_file())}
コード例 #2
0
ファイル: classlist.py プロジェクト: ubc/acj-versus
    def post(self, course_uuid):
        course = Course.get_active_by_uuid_or_404(course_uuid)
        user_course = UserCourse(course_id=course.id)
        require(EDIT, user_course,
            title="Class List Not Imported",
            message="Sorry, your role in this course does not allow you to import or otherwise change the class list.")

        if current_app.config.get('DEMO_INSTALLATION', False):
            from data.fixtures import DemoDataFixture
            if course.id == DemoDataFixture.DEFAULT_COURSE_ID:
                abort(400, title="Class List Not Imported", message="Sorry, you cannot import users for the default demo course.")

        params = import_classlist_parser.parse_args()
        import_type = params.get('import_type')

        if import_type not in [ThirdPartyType.cas.value, ThirdPartyType.saml.value, None]:
            abort(400, title="Class List Not Imported", message="Please select a way for students to log in and try importing again.")
        elif import_type == ThirdPartyType.cas.value and not current_app.config.get('CAS_LOGIN_ENABLED'):
            abort(400, title="Class List Not Imported", message="Please select another way for students to log in and try importing again. Students are not able to use CWL logins based on the current settings.")
        elif import_type == ThirdPartyType.saml.value and not current_app.config.get('SAML_LOGIN_ENABLED'):
            abort(400, title="Class List Not Imported", message="Please select another way for students to log in and try importing again. Students are not able to use CWL logins based on the current settings.")
        elif import_type is None and not current_app.config.get('APP_LOGIN_ENABLED'):
            abort(400, title="Class List Not Imported", message="Please select another way for students to log in and try importing again. Students are not able to use the ComPAIR logins based on the current settings.")

        uploaded_file = request.files['file']
        results = {'success': 0, 'invalids': []}

        if not uploaded_file:
            abort(400, title="Class List Not Imported", message="No file was found to upload. Please try uploading again.")
        elif not allowed_file(uploaded_file.filename, current_app.config['UPLOAD_ALLOWED_EXTENSIONS']):
            abort(400, title="Class List Not Imported", message="Sorry, only CSV files can be imported. Please try again with a CSV file.")

        unique = str(uuid.uuid4())
        filename = unique + secure_filename(uploaded_file.filename)
        tmp_name = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
        uploaded_file.save(tmp_name)
        current_app.logger.debug("Importing for course " + str(course.id) + " with " + filename)
        with open(tmp_name, 'rb') as csvfile:
            spamreader = csv.reader(csvfile)
            users = []
            for row in spamreader:
                if row:
                    users.append(row)

            if len(users) > 0:
                results = import_users(import_type, course, users)

            on_classlist_upload.send(
                self,
                event_name=on_classlist_upload.name,
                user=current_user,
                course_id=course.id)
        os.remove(tmp_name)
        current_app.logger.debug("Class Import for course " + str(course.id) + " is successful. Removed file.")
        return results
コード例 #3
0
ファイル: file.py プロジェクト: ubc/acj-versus
    def post(self):
        uploaded_file = request.files.get('file')

        if not uploaded_file:
            abort(400, title="File Not Uploaded", message="Sorry, no file was found to upload. Please try uploading again.")
        elif not allowed_file(uploaded_file.filename, current_app.config['ATTACHMENT_ALLOWED_EXTENSIONS']):
            extensions = [extension.upper() for extension in list(current_app.config['ATTACHMENT_ALLOWED_EXTENSIONS'])]
            extensions.sort()
            extensions = ", ".join(extensions)
            abort(400, title="File Not Uploaded", message="Please try again with an approved file type, which includes: "+extensions+".")

        on_save_file.send(
            self,
            event_name=on_save_file.name,
            user=current_user,
            data={'file': uploaded_file.filename})

        try:
            db_file = File(
                user_id=current_user.id,
                name='',
                alias=uploaded_file.filename
            )
            db.session.add(db_file)
            db.session.commit()

            # use uuid generated by file model for name
            name = db_file.uuid + '.' + uploaded_file.filename.lower().rsplit('.', 1)[1]

            # create new file with name
            full_path = os.path.join(current_app.config['ATTACHMENT_UPLOAD_FOLDER'], name)
            uploaded_file.save(full_path)
            current_app.logger.debug("Saved attachment {}/{}".format(current_app.config['ATTACHMENT_UPLOAD_FOLDER'], name))

            # update file record with name
            db_file.name = name
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            raise e

        return {'file': marshal(db_file, dataformat.get_file())}
コード例 #4
0
ファイル: classlist.py プロジェクト: ubc/compair
    def post(self, course_uuid):
        course = Course.get_active_by_uuid_or_404(course_uuid)
        user_course = UserCourse(course_id=course.id)
        require(
            EDIT,
            user_course,
            title="Class List Not Imported",
            message=
            "Sorry, your role in this course does not allow you to import or otherwise change the class list."
        )

        if current_app.config.get('DEMO_INSTALLATION', False):
            from data.fixtures import DemoDataFixture
            if course.id == DemoDataFixture.DEFAULT_COURSE_ID:
                abort(
                    400,
                    title="Class List Not Imported",
                    message=
                    "Sorry, you cannot import users for the default demo course."
                )

        params = import_classlist_parser.parse_args()
        import_type = params.get('import_type')

        if import_type not in [
                ThirdPartyType.cas.value, ThirdPartyType.saml.value, None
        ]:
            abort(
                400,
                title="Class List Not Imported",
                message=
                "Please select a way for students to log in and try importing again."
            )
        elif import_type == ThirdPartyType.cas.value and not current_app.config.get(
                'CAS_LOGIN_ENABLED'):
            abort(
                400,
                title="Class List Not Imported",
                message=
                "Please select another way for students to log in and try importing again. Students are not able to use CWL logins based on the current settings."
            )
        elif import_type == ThirdPartyType.saml.value and not current_app.config.get(
                'SAML_LOGIN_ENABLED'):
            abort(
                400,
                title="Class List Not Imported",
                message=
                "Please select another way for students to log in and try importing again. Students are not able to use CWL logins based on the current settings."
            )
        elif import_type is None and not current_app.config.get(
                'APP_LOGIN_ENABLED'):
            abort(
                400,
                title="Class List Not Imported",
                message=
                "Please select another way for students to log in and try importing again. Students are not able to use the ComPAIR logins based on the current settings."
            )

        uploaded_file = request.files['file']
        results = {'success': 0, 'invalids': []}

        if not uploaded_file:
            abort(400,
                  title="Class List Not Imported",
                  message=
                  "No file was found to upload. Please try uploading again.")
        elif not allowed_file(uploaded_file.filename,
                              current_app.config['UPLOAD_ALLOWED_EXTENSIONS']):
            abort(
                400,
                title="Class List Not Imported",
                message=
                "Sorry, only CSV files can be imported. Please try again with a CSV file."
            )

        unique = str(uuid.uuid4())
        filename = unique + secure_filename(uploaded_file.filename)
        tmp_name = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
        uploaded_file.save(tmp_name)
        current_app.logger.debug("Importing for course " + str(course.id) +
                                 " with " + filename)
        with open(tmp_name, 'rb') as csvfile:
            spamreader = csv.reader(csvfile)
            users = []
            for row in spamreader:
                if row:
                    users.append(row)

            if len(users) > 0:
                results = import_users(import_type, course, users)

            on_classlist_upload.send(self,
                                     event_name=on_classlist_upload.name,
                                     user=current_user,
                                     course_id=course.id)
        os.remove(tmp_name)
        current_app.logger.debug("Class Import for course " + str(course.id) +
                                 " is successful. Removed file.")
        return results