Ejemplo n.º 1
0
    def get(self, db, table):
        SetupDatasetsDatabase.init(db)
        m = Var_range.select().where(Var_range.table == table)

        values = {}
        for v in m:
            options = []
            for o in v.options:
                if o.text_only:
                    options.append({'value': o.text, 'text': o.text})
                elif o.text_value is not None:
                    options.append({'value': o.text_value, 'text': o.text})
                else:
                    options.append({'value': o.value, 'text': o.text})

            values[v.variable] = {
                'name': v.variable,
                'type': v.type,
                'min_value': v.min_value,
                'max_value': v.max_value,
                'default_value': v.default_value,
                'default_text': v.default_text,
                'units': v.units,
                'description': v.description,
                'options':
                options  #[model_to_dict(o, recurse=False) for o in v.options]
            }

        return values
Ejemplo n.º 2
0
 def get(self, datasets_db, table_type):
     table = ds.D_table_dtl
     SetupDatasetsDatabase.init(datasets_db)
     args = self.get_table_args()
     sort = self.get_arg(args, 'sort', 'name')
     reverse = self.get_arg(args, 'reverse', 'n')
     page = self.get_arg(args, 'page', 1)
     per_page = self.get_arg(args, 'per_page', 50)
     filter_val = self.get_arg(args, 'filter', None)
     return get_dtable_list(table, table_type, sort, reverse, page,
                            per_page, filter_val)
Ejemplo n.º 3
0
    def get(self, db, table, variable):
        SetupDatasetsDatabase.init(db)
        m = Var_code.select().where((Var_code.table == table)
                                    & (Var_code.variable == variable))

        values = []
        for v in m:
            values.append({
                'value':
                v.code,
                'text':
                '{code} - {description}'.format(code=v.code,
                                                description=v.description)
            })

        return values
Ejemplo n.º 4
0
 def base_get_datasets_name(self,
                            datasets_db,
                            name,
                            table,
                            description,
                            back_refs=False):
     SetupDatasetsDatabase.init(datasets_db)
     try:
         m = table.get(table.name == name)
         if back_refs:
             return model_to_dict(m, backrefs=True, max_depth=1)
         else:
             return model_to_dict(m, recurse=False)
     except table.DoesNotExist:
         abort(404,
               message='{description} {name} does not exist'.format(
                   description=description, name=name))
Ejemplo n.º 5
0
	def __init__(self, project_db, new_version, datasets_db=None, update_project_values=False, reimport_gis=False):
		SetupProjectDatabase.init(project_db)
		try:
			m = self.check_config(new_version)

			base_path = os.path.dirname(project_db)
			rel_project_db = os.path.relpath(project_db, base_path)

			if datasets_db is None:
				datasets_db = utils.full_path(project_db, m.reference_db)
			else:				
				rel_datasets_db = os.path.relpath(datasets_db, base_path)
				m.reference_db = rel_datasets_db

			# Ensure correct version of datasets db
			ver_check = SetupDatasetsDatabase.check_version(datasets_db, new_version)
			if ver_check is not None:
				sys.exit(ver_check)

			# Backup original db before beginning
			try:
				self.emit_progress(2, 'Backing up project database...')
				filename, file_extension = os.path.splitext(rel_project_db)
				bak_filename = filename + '_v' + m.editor_version.replace('.', '_') + '_' + time.strftime('%Y%m%d-%H%M%S') + file_extension
				bak_dir = os.path.join(base_path, 'DatabaseBackups')
				if not os.path.exists(bak_dir):
					os.makedirs(bak_dir)
				backup_db_file = os.path.join(bak_dir, bak_filename)
				copyfile(project_db, os.path.join(bak_dir, bak_filename))
			except IOError as err:
				sys.exit(err)
			
			did_update = False
			if m.editor_version == '1.2.0':
				self.updates_for_1_2_1(project_db, backup_db_file)
				did_update = True
				reimport_gis = False
			if m.editor_version == '1.1.0' or m.editor_version == '1.1.1' or m.editor_version == '1.1.2':
				self.updates_for_1_2_0(project_db, update_project_values, backup_db_file)
				self.updates_for_1_2_1(project_db, backup_db_file)
				did_update = True
			elif m.editor_version == '1.0.0':
				self.updates_for_1_1_0(project_db, datasets_db, backup_db_file)
				self.updates_for_1_2_0(project_db, update_project_values, backup_db_file)
				self.updates_for_1_2_1(project_db, backup_db_file)
				did_update = True
			
			m.editor_version = new_version
			result = m.save()

			if did_update and reimport_gis:
				ReimportGis(project_db, new_version, None, m.project_name, datasets_db, True, m.is_lte)
		except Project_config.DoesNotExist:
			sys.exit("Could not retrieve project configuration data.")
Ejemplo n.º 6
0
    def __init__(self,
                 project_db,
                 editor_version,
                 project_name=None,
                 datasets_db=None,
                 constant_ps=True,
                 is_lte=False,
                 project_description=None):
        self.__abort = False

        base_path = os.path.dirname(project_db)
        rel_project_db = os.path.relpath(project_db, base_path)

        if datasets_db is None:
            conn = lib.open_db(project_db)
            if not lib.exists_table(conn, 'project_config'):
                sys.exit(
                    'No datasets database provided and the project_config table in your project database does not exist. Please provide either a datasets database file or an existing project database.'
                )

            SetupProjectDatabase.init(project_db)
            try:
                config = Project_config.get()
                datasets_db = utils.full_path(project_db, config.reference_db)
                if project_name is None:
                    project_name = config.project_name
            except Project_config.DoesNotExist:
                sys.exit('Could not retrieve project configuration data.')

        rel_datasets_db = os.path.relpath(datasets_db, base_path)

        ver_check = SetupDatasetsDatabase.check_version(
            datasets_db, editor_version)
        if ver_check is not None:
            sys.exit(ver_check)

        # Backup original db before beginning
        do_gis = False
        if os.path.exists(project_db):
            do_gis = True
            try:
                self.emit_progress(2, 'Backing up GIS database...')
                filename, file_extension = os.path.splitext(rel_project_db)
                bak_filename = filename + '_bak_' + time.strftime(
                    '%Y%m%d-%H%M%S') + file_extension
                bak_dir = os.path.join(base_path, 'DatabaseBackups')
                if not os.path.exists(bak_dir):
                    os.makedirs(bak_dir)
                backup_db_file = os.path.join(bak_dir, bak_filename)
                copyfile(project_db, backup_db_file)
            except IOError as err:
                sys.exit(err)

        try:
            SetupProjectDatabase.init(project_db, datasets_db)
            self.emit_progress(10, 'Creating database tables...')
            SetupProjectDatabase.create_tables()
            self.emit_progress(50,
                               'Copying data from SWAT+ datasets database...')
            description = project_description if project_description is not None else project_name
            SetupProjectDatabase.initialize_data(
                description, is_lte, overwrite_plants=OVERWRITE_PLANTS)

            config = Project_config.get_or_create_default(
                editor_version=editor_version,
                project_name=project_name,
                project_db=rel_project_db,
                reference_db=rel_datasets_db,
                project_directory='',
                is_lte=is_lte)

            conn = lib.open_db(project_db)
            plant_cols = lib.get_column_names(conn, 'plants_plt')
            plant_col_names = [v['name'] for v in plant_cols]
            if 'days_mat' not in plant_col_names:
                migrator = SqliteMigrator(SqliteDatabase(project_db))
                migrate(
                    migrator.rename_column('plants_plt', 'plnt_hu',
                                           'days_mat'))
                for p in project_plants:
                    dp = dataset_plants.get_or_none(
                        dataset_plants.name == p.name)
                    if dp is not None:
                        p.days_mat = dp.days_mat
                    else:
                        p.days_mat = 0
                    p.save()
        except Exception as ex:
            if backup_db_file is not None:
                self.emit_progress(50,
                                   "Error occurred. Rolling back database...")
                SetupProjectDatabase.rollback(project_db, backup_db_file)
                self.emit_progress(100, "Error occurred.")
            sys.exit(str(ex))

        if do_gis:
            api = GisImport(project_db, True, constant_ps, backup_db_file)
            api.insert_default()
Ejemplo n.º 7
0
	def create(self, version):
		SetupDatasetsDatabase.create_tables()
		SetupDatasetsDatabase.initialize_data(version)
Ejemplo n.º 8
0
	def __init__(self, db_file):
		self.__abort = False
		SetupDatasetsDatabase.init(db_file)
Ejemplo n.º 9
0
 def update_2_1_0(self, db_file):
     SetupDatasetsDatabase.create_tables()
     SetupDatasetsDatabase.update_2_1_0(db_file)
Ejemplo n.º 10
0
    def get_args(self,
                 table_name,
                 project_db,
                 get_selected_ids=False,
                 extra_args=[]):
        parser = reqparse.RequestParser()

        if get_selected_ids:
            parser.add_argument('selected_ids',
                                type=int,
                                action='append',
                                required=False,
                                location='json')
        else:
            parser.add_argument('id',
                                type=int,
                                required=False,
                                location='json')
            parser.add_argument('name',
                                type=str,
                                required=False,
                                location='json')
            parser.add_argument('description',
                                type=str,
                                required=False,
                                location='json')

        for extra in extra_args:
            parser.add_argument(extra['name'],
                                type=extra['type'],
                                required=False,
                                location='json')

        try:
            c = Project_config.get()

            datasets_db = c.reference_db
            if not os.path.exists(c.reference_db):
                datasets_db = os.path.normpath(
                    os.path.join(os.path.dirname(project_db), c.reference_db))

            SetupDatasetsDatabase.init(datasets_db)
            m = Var_range.select().where(Var_range.table == table_name)

            types = {
                'float': float,
                'int': int,
                'text': str,
                'string': str,
                'select': str,
                'lookup': str
            }

            for v in m:
                parser.add_argument(v.variable,
                                    type=types.get(v.type, str),
                                    required=False,
                                    location='json')
        except Project_config.DoesNotExist:
            abort(404,
                  message="Could not retrieve project configuration data.")

        args = parser.parse_args(strict=False)
        return args