示例#1
0
 def handle_database_error(e: SQLAlchemyError):
     logging.exception(e)
     try:
         db.session.rollback()
     except Exception as e:
         logging.exception(e)
     return InvalidUsage('Database error').make_response()
示例#2
0
    def parse_csv_line(self, line):
        if len(line) < 4:
            raise InvalidUsage(
                'At least 4 columns (name, type, type description, space requirement)\
                 are required for CSV file')
        result = {}
        result['name'] = ascii_str(line[0], 127)
        result['type'] = ascii_str(line[1] + ' ' + line[2], 127)
        space_numbers = [float(s) for s in re.findall(r"\d+\.?\d*", line[3])]
        use_cm = 'cm' in line[3]
        if use_cm:
            space_numbers = [n / 100 for n in space_numbers]
        result['space_x'] = space_numbers[0] if len(space_numbers) > 0 else 2.0
        result['space_y'] = space_numbers[1] if len(space_numbers) > 1 else 2.0
        result['space_z'] = space_numbers[2] if len(space_numbers) > 2 else 2.0

        # parse other optional parameters
        if len(line) > 4:
            prototype_nums = [
                float(s) for s in re.findall(r"\d+\.?\d*", line[4])
            ]
            use_cm = 'cm' in line[4]
            result['prototype_weight'] = prototype_nums[3] if len(
                prototype_nums) > 3 else 0
            if use_cm:
                prototype_nums = [n / 100 for n in prototype_nums]
            result['prototype_x'] = prototype_nums[0] if len(
                prototype_nums) > 0 else 0
            result['prototype_y'] = prototype_nums[1] if len(
                prototype_nums) > 1 else 0
            result['prototype_z'] = prototype_nums[2] if len(
                prototype_nums) > 2 else 0

            mapping = {
                5: (int, 'power_points_count'),
                6: (int, 'pedestal_big_count'),
                7: (int, 'pedestal_small_count'),
                8: (ascii_str, 'pedestal_description'),
                9: (int, 'monitor_count'),
                10: (int, 'tv_count'),
                11: (int, 'table_count'),
                12: (int, 'chair_count'),
                13: (int, 'hdmi_to_vga_adapter_count'),
                14: (int, 'hdmi_cable_count'),
                16: (ascii_str, 'remark')
            }
            for i in range(5, len(line)):
                if i in mapping:
                    dtype = mapping[i][0]
                    dkey = mapping[i][1]
                    try:
                        dvalue = dtype(line[i])
                        result[dkey] = dvalue
                    except Exception:
                        pass
        return result
示例#3
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('email', type=str, required=True)
        parser.add_argument('password', type=str, required=True)
        parser.add_argument('full_name', type=str, required=True)
        args = parser.parse_args()

        if User.query.filter_by(email=args['email']).first():
            raise InvalidUsage('User already exists')

        user = User(email=args['email'],
                    password=args['password'],
                    full_name=args['full_name'])
        db.session.add(user)
        db.session.commit()

        return user
示例#4
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('name', type=str, required=True)
        parser.add_argument('type', type=str, required=True)
        parser.add_argument('space_x', type=float, required=True)
        parser.add_argument('space_y', type=float, required=True)
        parser.add_argument('space_z', type=float, required=True)
        parser.add_argument('prototype_x', type=float, default=0)
        parser.add_argument('prototype_y', type=float, default=0)
        parser.add_argument('prototype_z', type=float, default=0)
        parser.add_argument('prototype_weight', type=float, default=0)
        parser.add_argument('power_points_count', type=int, default=0)
        parser.add_argument('pedestal_big_count', type=int, default=0)
        parser.add_argument('pedestal_small_count', type=int, default=0)
        parser.add_argument('pedestal_description', type=str, default=0)
        parser.add_argument('monitor_count', type=int, default=0)
        parser.add_argument('tv_count', type=int, default=0)
        parser.add_argument('table_count', type=int, default=0)
        parser.add_argument('chair_count', type=int, default=0)
        parser.add_argument('hdmi_to_vga_adapter_count', type=int, default=0)
        parser.add_argument('hdmi_cable_count', type=int, default=0)
        parser.add_argument('remark', type=str, default='')
        args = parser.parse_args()

        # Students are only allowed to submit one project
        if not auth.current_user.is_admin and len(
                auth.current_user.projects) > 0:
            raise InvalidUsage('Only one submission allowed')

        project_attributes = dict(args)
        project_attributes['creator'] = auth.current_user
        project = Project(**project_attributes)

        db.session.add(project)
        db.session.commit()

        return project
示例#5
0
    def post(self):
        # for CSV uploading
        file = request.files.get('file')
        if file and self.allowed_file(file.filename, 'csv'):
            auth.admin_required(lambda: None)()

            filename = secure_filename(file.filename)
            filepath = os.path.join(current_app.config['UPLOAD_FOLDER'],
                                    filename)
            file.save(filepath)

            new_projects = []
            with open(filepath, 'r') as csv_file:
                reader = csv.reader(csv_file)
                next(reader)  # skip headers
                for line in reader:
                    data_dict = self.parse_csv_line(line)
                    data_dict['creator'] = auth.current_user
                    new_project = Project(**data_dict)
                    new_projects.append(new_project)
                    db.session.add(new_project)

            db.session.commit()

            return new_projects

        # for normal uploading
        parser = reqparse.RequestParser()
        parser.add_argument('name', type=str, required=True)
        parser.add_argument('type', type=str, required=True)
        parser.add_argument('space_x', type=float, required=True)
        parser.add_argument('space_y', type=float, required=True)
        parser.add_argument('space_z', type=float, required=True)
        parser.add_argument('prototype_x', type=float, default=0)
        parser.add_argument('prototype_y', type=float, default=0)
        parser.add_argument('prototype_z', type=float, default=0)
        parser.add_argument('prototype_weight', type=float, default=0)
        parser.add_argument('power_points_count', type=int, default=0)
        parser.add_argument('pedestal_big_count', type=int, default=0)
        parser.add_argument('pedestal_small_count', type=int, default=0)
        parser.add_argument('pedestal_description', type=str, default=0)
        parser.add_argument('monitor_count', type=int, default=0)
        parser.add_argument('tv_count', type=int, default=0)
        parser.add_argument('table_count', type=int, default=0)
        parser.add_argument('chair_count', type=int, default=0)
        parser.add_argument('hdmi_to_vga_adapter_count', type=int, default=0)
        parser.add_argument('hdmi_cable_count', type=int, default=0)
        parser.add_argument('remark', type=str, default='')
        args = parser.parse_args()

        # Students are only allowed to submit one project
        if not auth.current_user.is_admin and len(
                auth.current_user.projects) > 0:
            raise InvalidUsage('Only one submission allowed')

        project_attributes = dict(args)
        project_attributes['creator'] = auth.current_user
        project = Project(**project_attributes)

        db.session.add(project)
        db.session.commit()

        return project