Beispiel #1
0
def save_cntable_lum(m, args):
    m.name = args['name']
    m.description = utils.remove_space(args['description'])
    m.cn_a = args['cn_a']
    m.cn_b = args['cn_b']
    m.cn_c = args['cn_c']
    m.cn_d = args['cn_d']
    m.treat = utils.remove_space(args['treat'])
    m.cond_cov = utils.remove_space(args['cond_cov'])
    return m.save()
Beispiel #2
0
def save_landuse_args(self, m, args):
	m.name = args['name']
	m.description = args['description']
	m.cal_group = utils.remove_space(args['cal_group'])
	m.urb_ro = args['urb_ro']

	m.plnt_com_id = self.get_id_from_name(Plant_ini, args['plnt_com_name'])
	m.mgt_id = self.get_id_from_name(Management_sch, args['mgt_name'])
	m.cn2_id = self.get_id_from_name(Cntable_lum, args['cn2_name'])
	m.cons_prac_id = self.get_id_from_name(Cons_prac_lum, args['cons_prac_name'])
	m.urban_id = self.get_id_from_name(Urban_urb, args['urban_name'])
	m.ov_mann_id = self.get_id_from_name(Ovn_table_lum, args['ov_mann_name'])
	m.tile_id = self.get_id_from_name(Tiledrain_str, args['tile_name'])
	m.sep_id = self.get_id_from_name(Septic_str, args['sep_name'])
	m.vfs_id = self.get_id_from_name(Filterstrip_str, args['vfs_name'])
	m.grww_id = self.get_id_from_name(Grassedww_str, args['grww_name'])
	m.bmp_id = self.get_id_from_name(Bmpuser_str, args['bmp_name'])

	return m.save()
Beispiel #3
0
	def put(self, project_db):
		try:
			SetupProjectDatabase.init(project_db)
			args = self.get_args('cntable_lum', project_db, True)

			remove_spaces = ['description', 'treat', 'cond_cov']

			param_dict = {}
			for key in args.keys():
				if args[key] is not None and key != 'selected_ids':
					param_dict[key] = utils.remove_space(args[key]) if key in remove_spaces else args[key]

			query = Cntable_lum.update(param_dict).where(Cntable_lum.id.in_(args['selected_ids']))
			result = query.execute()

			if result > 0:
				return 200

			abort(400, message='Unable to update curve number tables.')
		except Exception as ex:
			abort(400, message="Unexpected error {ex}".format(ex=ex))
Beispiel #4
0
    def put(self, project_db):
        SetupProjectDatabase.init(project_db)
        args = get_landuse_args(True)

        try:
            param_dict = {}

            if args['cal_group'] is not None:
                param_dict['cal_group'] = utils.remove_space(args['cal_group'])
            if args['urb_ro'] is not None:
                param_dict['urb_ro'] = args['urb_ro']

            if args['plnt_com_name'] is not None:
                param_dict['plnt_com_id'] = self.get_id_from_name(
                    Plant_ini, args['plnt_com_name'])
            if args['mgt_name'] is not None:
                param_dict['mgt_id'] = self.get_id_from_name(
                    Management_sch, args['mgt_name'])
            if args['cn2_name'] is not None:
                param_dict['cn2_id'] = self.get_id_from_name(
                    Cntable_lum, args['cn2_name'])
            if args['cons_prac_name'] is not None:
                param_dict['cons_prac_id'] = self.get_id_from_name(
                    Cons_prac_lum, args['cons_prac_name'])
            if args['urban_name'] is not None:
                param_dict['urban_id'] = self.get_id_from_name(
                    Urban_urb, args['urban_name'])
            if args['ov_mann_name'] is not None:
                param_dict['ov_mann_id'] = self.get_id_from_name(
                    Ovn_table_lum, args['ov_mann_name'])
            if args['tile_name'] is not None:
                param_dict['tile_id'] = self.get_id_from_name(
                    Tiledrain_str, args['tile_name'])
            if args['sep_name'] is not None:
                param_dict['sep_id'] = self.get_id_from_name(
                    Septic_str, args['sep_name'])
            if args['vfs_name'] is not None:
                param_dict['vfs_id'] = self.get_id_from_name(
                    Filterstrip_str, args['vfs_name'])
            if args['grww_name'] is not None:
                param_dict['grww_id'] = self.get_id_from_name(
                    Grassedww_str, args['grww_name'])
            if args['bmp_name'] is not None:
                param_dict['bmp_id'] = self.get_id_from_name(
                    Bmpuser_str, args['bmp_name'])

            query = Landuse_lum.update(param_dict).where(
                Landuse_lum.id.in_(args['selected_ids']))
            result = query.execute()

            if result > 0:
                return 200

            abort(400, message='Unable to update channel properties.')
        except Plant_ini.DoesNotExist:
            abort(400,
                  message=invalid_name_msg.format(name=args['plnt_com_name']))
        except Management_sch.DoesNotExist:
            abort(400, message=invalid_name_msg.format(name=args['mgt_name']))
        except Cntable_lum.DoesNotExist:
            abort(400, message=invalid_name_msg.format(name=args['cn2_name']))
        except Cons_prac_lum.DoesNotExist:
            abort(400,
                  message=invalid_name_msg.format(name=args['cons_prac_name']))
        except Urban_urb.DoesNotExist:
            abort(400,
                  message=invalid_name_msg.format(name=args['urban_name']))
        except Ovn_table_lum.DoesNotExist:
            abort(400,
                  message=invalid_name_msg.format(name=args['ov_mann_name']))
        except Tiledrain_str.DoesNotExist:
            abort(400, message=invalid_name_msg.format(name=args['tile_name']))
        except Septic_str.DoesNotExist:
            abort(400, message=invalid_name_msg.format(name=args['sep_name']))
        except Filterstrip_str.DoesNotExist:
            abort(400, message=invalid_name_msg.format(name=args['vfs_name']))
        except Grassedww_str.DoesNotExist:
            abort(400, message=invalid_name_msg.format(name=args['grww_name']))
        except Bmpuser_str.DoesNotExist:
            abort(400, message=invalid_name_msg.format(name=args['bmp_name']))
        except Exception as ex:
            abort(400, message="Unexpected error {ex}".format(ex=ex))
Beispiel #5
0
def read_csv_file(file_name,
                  table,
                  db,
                  expected_cols,
                  ignore_id_col=False,
                  convert_name_to_lower=False,
                  overwrite=FileOverwrite.ignore,
                  remove_spaces_cols=[]):
    csv_file = open(file_name, "r")

    dialect = csv.Sniffer().sniff(csv_file.readline())
    csv_file.seek(0)
    replace_commas = dialect is not None and dialect.delimiter != ','
    hasHeader = csv.Sniffer().has_header(csv_file.readline())
    csv_file.seek(0)

    csv_reader = csv.reader(csv_file, dialect)

    if hasHeader:
        headerLine = next(csv_reader)

    i = 1
    rows = []
    fields = table._meta.sorted_fields
    for val in csv_reader:
        if expected_cols > 0 and len(val) < expected_cols:
            raise IndexError(
                'Improperly formatted %s file. Expecting %s columns. Please refer to the SWAT+ IO documentation.'
                % (ntpath.basename(file_name), expected_cols))

        if replace_commas:
            val = [item.replace(',', '.', 1) for item in val]

        row = {}
        j = 0
        for field in fields:
            skip = False
            if ignore_id_col and field.name == "id":
                skip = True

            if not skip:
                value = None
                if len(val) > j:
                    if convert_name_to_lower and field.name == "name":
                        value = val[j].lower()
                    elif field.name == "description":
                        value = val[j] if val[j] != 'null' else None
                    else:
                        value = val[j]

                if type(value) is str:
                    if value == 'null':
                        value = None

                row[field.
                    name] = value if field.name not in remove_spaces_cols else utils.remove_space(
                        value)

                j += 1

        add_row = True
        if overwrite != FileOverwrite.ignore:
            try:
                m = table.get(table.name == row['name'])

                if overwrite == FileOverwrite.replace:
                    query = table.update(row).where(table.id == m.id)
                    result = query.execute()
                    add_row = False
                elif overwrite == FileOverwrite.rename:
                    k = 1
                    while table.select().where(
                            table.name == '{name}{num}'.format(
                                name=row['name'], num=k)).exists():
                        k += 1

                    row['name'] = '{name}{num}'.format(name=row['name'], num=k)
            except table.DoesNotExist:
                pass

        if add_row:
            rows.append(row)

    db_lib.bulk_insert(db, table, rows)
Beispiel #6
0
def read_file(file_name,
              table,
              db,
              expected_cols,
              ignore_id_col=False,
              start_line=3,
              csv=False,
              convert_name_to_lower=False,
              overwrite=FileOverwrite.ignore,
              remove_spaces_cols=[]):
    file = open(file_name, "r")

    if csv:
        start_line = 2

    i = 1
    rows = []
    fields = table._meta.sorted_fields
    for line in file:
        if i >= start_line:
            val = line.split() if not csv else line.split(',')
            if expected_cols > 0 and len(val) < expected_cols:
                raise IndexError(
                    'Improperly formatted %s file. Expecting %s columns. Please refer to the SWAT+ IO documentation.'
                    % (ntpath.basename(file_name), expected_cols))

            row = {}
            j = 0
            for field in fields:
                skip = False
                if ignore_id_col and field.name == "id":
                    skip = True

                if not skip:
                    value = None
                    if len(val) > j:
                        if convert_name_to_lower and field.name == "name":
                            value = val[j].lower()
                        elif field.name == "description":
                            value = val[j] if val[j] != 'null' else None
                        else:
                            value = val[j]

                    if type(value) is str:
                        value = value.replace('"', '')
                        if value == 'null':
                            value = None

                    row[field.
                        name] = value if field.name not in remove_spaces_cols else utils.remove_space(
                            value)

                    j += 1

            add_row = True
            if overwrite != FileOverwrite.ignore:
                try:
                    m = table.get(table.name == row['name'])

                    if overwrite == FileOverwrite.replace:
                        query = table.update(row).where(table.id == m.id)
                        result = query.execute()
                        add_row = False
                    elif overwrite == FileOverwrite.rename:
                        k = 1
                        while table.select().where(
                                table.name == '{name}{num}'.format(
                                    name=row['name'], num=k)).exists():
                            k += 1

                        row['name'] = '{name}{num}'.format(name=row['name'],
                                                           num=k)
                except table.DoesNotExist:
                    pass

            if add_row:
                rows.append(row)

        i += 1

    db_lib.bulk_insert(db, table, rows)