コード例 #1
0
class CopyFileForm(Form):
    filename = TextField(_l('filename'), [validators.Required()])
    boxes = QuerySelectField(_l('select target user'), [validators.Required()],
                             query_factory=query_all_user_boxes,
                             get_label='email')
コード例 #2
0
class AddForm(Form):  # used for both add/edit article
    division = QuerySelectField(label="Division: ", validators=[Required()])
    article = StringField("Title: ", validators=[Required()])
    pic = StringField("Link of the profile picture: ")
    content = TextAreaField("Content: ")
    submit = SubmitField("Submit")
コード例 #3
0
ファイル: forms.py プロジェクト: coderpradeep/rook
class CsvToDatabaseForm(DynamicForm):
    # pylint: disable=E0211
    def all_db_items():
        return db.session.query(models.Database)

    name = StringField(
        _('Table Name'),
        description=_('Name of table to be created from csv data.'),
        validators=[DataRequired()],
        widget=BS3TextFieldWidget())
    csv_file = FileField(
        _('CSV File'),
        description=_('Select a CSV file to be uploaded to a database.'),
        validators=[
            FileRequired(),
            FileAllowed(['csv'], _('CSV Files Only!'))
        ])
    con = QuerySelectField(query_factory=all_db_items,
                           get_pk=lambda a: a.id,
                           get_label=lambda a: a.database_name)
    sep = StringField(
        _('Delimiter'),
        description=_('Delimiter used by CSV file (for whitespace use \s+).'),
        validators=[DataRequired()],
        widget=BS3TextFieldWidget())
    if_exists = SelectField(
        _('Table Exists'),
        description=_('If table exists do one of the following: '
                      'Fail (do nothing), Replace (drop and recreate table) '
                      'or Append (insert data).'),
        choices=[('fail', _('Fail')), ('replace', _('Replace')),
                 ('append', _('Append'))],
        validators=[DataRequired()])
    schema = StringField(
        _('Schema'),
        description=_('Specify a schema (if database flavour supports this).'),
        validators=[Optional()],
        widget=BS3TextFieldWidget(),
        filters=[lambda x: x or None])
    header = IntegerField(_('Header Row'),
                          description=_(
                              'Row containing the headers to use as '
                              'column names (0 is first line of data). '
                              'Leave empty if there is no header row.'),
                          validators=[Optional()],
                          widget=BS3TextFieldWidget(),
                          filters=[lambda x: x or None])
    index_col = IntegerField(_('Index Column'),
                             description=_(
                                 'Column to use as the row labels of the '
                                 'dataframe. Leave empty if no index column.'),
                             validators=[Optional(),
                                         NumberRange(0, 1E+20)],
                             widget=BS3TextFieldWidget(),
                             filters=[lambda x: x or None])
    mangle_dupe_cols = BooleanField(
        _('Mangle Duplicate Columns'),
        description=_('Specify duplicate columns as "X.0, X.1".'))
    skipinitialspace = BooleanField(
        _('Skip Initial Space'), description=_('Skip spaces after delimiter.'))
    skiprows = IntegerField(
        _('Skip Rows'),
        description=_('Number of rows to skip at start of file.'),
        validators=[Optional(), NumberRange(0, 1E+20)],
        widget=BS3TextFieldWidget(),
        filters=[lambda x: x or None])
    nrows = IntegerField(_('Rows to Read'),
                         description=_('Number of rows of file to read.'),
                         validators=[Optional(),
                                     NumberRange(0, 1E+20)],
                         widget=BS3TextFieldWidget(),
                         filters=[lambda x: x or None])
    skip_blank_lines = BooleanField(
        _('Skip Blank Lines'),
        description=_('Skip blank lines rather than interpreting them '
                      'as NaN values.'))
    parse_dates = BooleanField(_('Parse Dates'),
                               description=_('Parse date values.'))
    infer_datetime_format = BooleanField(
        _('Infer Datetime Format'),
        description=_('Use Pandas to interpret the datetime format '
                      'automatically.'))
    decimal = StringField(
        _('Decimal Character'),
        description=_('Character to interpret as decimal point.'),
        validators=[Optional()],
        widget=BS3TextFieldWidget(),
        filters=[lambda x: x or '.'])
    index = BooleanField(_('Dataframe Index'),
                         description=_('Write dataframe index as a column.'))
    index_label = StringField(
        _('Column Label(s)'),
        description=_('Column label for index column(s). If None is given '
                      'and Dataframe Index is True, Index Names are used.'),
        validators=[Optional()],
        widget=BS3TextFieldWidget(),
        filters=[lambda x: x or None])
コード例 #4
0
ファイル: forms.py プロジェクト: janwawruszczak1998/SasinMed
class AddDiagnosisForm(FlaskForm):
    visit = QuerySelectField('Wizyta',
                             query_factory=lambda: Visit.query.all(),
                             validators=[InputRequired()])
    symptoms = StringField('Objawy', render_kw={"placeholder": "Objawy"})
コード例 #5
0
ファイル: views.py プロジェクト: zrxpower/incubator-superset
class TableModelView(DatasourceModelView, DeleteMixin, YamlExportMixin):
    datamodel = SQLAInterface(models.SqlaTable)
    include_route_methods = RouteMethod.CRUD_SET

    list_title = _("Tables")
    show_title = _("Show Table")
    add_title = _("Import a table definition")
    edit_title = _("Edit Table")

    list_columns = ["link", "database_name", "changed_by_", "modified"]
    order_columns = ["modified"]
    add_columns = ["database", "schema", "table_name"]
    edit_columns = [
        "table_name",
        "sql",
        "filter_select_enabled",
        "fetch_values_predicate",
        "database",
        "schema",
        "description",
        "owners",
        "main_dttm_col",
        "default_endpoint",
        "offset",
        "cache_timeout",
        "is_sqllab_view",
        "template_params",
    ]
    base_filters = [["id", DatasourceFilter, lambda: []]]
    show_columns = edit_columns + ["perm", "slices"]
    related_views = [TableColumnInlineView, SqlMetricInlineView]
    base_order = ("changed_on", "desc")
    search_columns = ("database", "schema", "table_name", "owners",
                      "is_sqllab_view")
    description_columns = {
        "slices":
        _("The list of charts associated with this table. By "
          "altering this datasource, you may change how these associated "
          "charts behave. "
          "Also note that charts need to point to a datasource, so "
          "this form will fail at saving if removing charts from a "
          "datasource. If you want to change the datasource for a chart, "
          "overwrite the chart from the 'explore view'"),
        "offset":
        _("Timezone offset (in hours) for this datasource"),
        "table_name":
        _("Name of the table that exists in the source database"),
        "schema":
        _("Schema, as used only in some databases like Postgres, Redshift "
          "and DB2"),
        "description":
        Markup(
            'Supports <a href="https://daringfireball.net/projects/markdown/">'
            "markdown</a>"),
        "sql":
        _("This fields acts a Superset view, meaning that Superset will "
          "run a query against this string as a subquery."),
        "fetch_values_predicate":
        _("Predicate applied when fetching distinct value to "
          "populate the filter control component. Supports "
          "jinja template syntax. Applies only when "
          "`Enable Filter Select` is on."),
        "default_endpoint":
        _("Redirects to this endpoint when clicking on the table "
          "from the table list"),
        "filter_select_enabled":
        _("Whether to populate the filter's dropdown in the explore "
          "view's filter section with a list of distinct values fetched "
          "from the backend on the fly"),
        "is_sqllab_view":
        _("Whether the table was generated by the 'Visualize' flow "
          "in SQL Lab"),
        "template_params":
        _("A set of parameters that become available in the query using "
          "Jinja templating syntax"),
        "cache_timeout":
        _("Duration (in seconds) of the caching timeout for this table. "
          "A timeout of 0 indicates that the cache never expires. "
          "Note this defaults to the database timeout if undefined."),
    }
    label_columns = {
        "slices": _("Associated Charts"),
        "link": _("Table"),
        "changed_by_": _("Changed By"),
        "database": _("Database"),
        "database_name": _("Database"),
        "changed_on_": _("Last Changed"),
        "filter_select_enabled": _("Enable Filter Select"),
        "schema": _("Schema"),
        "default_endpoint": _("Default Endpoint"),
        "offset": _("Offset"),
        "cache_timeout": _("Cache Timeout"),
        "table_name": _("Table Name"),
        "fetch_values_predicate": _("Fetch Values Predicate"),
        "owners": _("Owners"),
        "main_dttm_col": _("Main Datetime Column"),
        "description": _("Description"),
        "is_sqllab_view": _("SQL Lab View"),
        "template_params": _("Template parameters"),
        "modified": _("Modified"),
    }
    edit_form_extra_fields = {
        "database":
        QuerySelectField(
            "Database",
            query_factory=lambda: db.session().query(models.Database),
            widget=Select2Widget(extra_classes="readonly"),
        )
    }

    def pre_add(self, table):
        with db.session.no_autoflush:
            table_query = db.session.query(models.SqlaTable).filter(
                models.SqlaTable.table_name == table.table_name,
                models.SqlaTable.schema == table.schema,
                models.SqlaTable.database_id == table.database.id,
            )
            if db.session.query(table_query.exists()).scalar():
                raise Exception(get_datasource_exist_error_msg(
                    table.full_name))

        # Fail before adding if the table can't be found
        try:
            table.get_sqla_table_object()
        except Exception as e:
            logger.exception(f"Got an error in pre_add for {table.name}")
            raise Exception(
                _("Table [{}] could not be found, "
                  "please double check your "
                  "database connection, schema, and "
                  "table name, error: {}").format(table.name, str(e)))

    def post_add(self, table, flash_message=True):
        table.fetch_metadata()
        security_manager.add_permission_view_menu("datasource_access",
                                                  table.get_perm())
        if table.schema:
            security_manager.add_permission_view_menu("schema_access",
                                                      table.schema_perm)

        if flash_message:
            flash(
                _("The table was created. "
                  "As part of this two-phase configuration "
                  "process, you should now click the edit button by "
                  "the new table to configure it."),
                "info",
            )

    def post_update(self, table):
        self.post_add(table, flash_message=False)

    def _delete(self, pk):
        DeleteMixin._delete(self, pk)

    @expose("/edit/<pk>", methods=["GET", "POST"])
    @has_access
    def edit(self, pk):
        """Simple hack to redirect to explore view after saving"""
        resp = super(TableModelView, self).edit(pk)
        if isinstance(resp, str):
            return resp
        return redirect("/superset/explore/table/{}/".format(pk))

    @action("refresh", __("Refresh Metadata"), __("Refresh column metadata"),
            "fa-refresh")
    def refresh(self, tables):
        if not isinstance(tables, list):
            tables = [tables]
        successes = []
        failures = []
        for t in tables:
            try:
                t.fetch_metadata()
                successes.append(t)
            except Exception:
                failures.append(t)

        if len(successes) > 0:
            success_msg = _(
                "Metadata refreshed for the following table(s): %(tables)s",
                tables=", ".join([t.table_name for t in successes]),
            )
            flash(success_msg, "info")
        if len(failures) > 0:
            failure_msg = _(
                "Unable to retrieve metadata for the following table(s): %(tables)s",
                tables=", ".join([t.table_name for t in failures]),
            )
            flash(failure_msg, "danger")

        return redirect("/tablemodelview/list/")
コード例 #6
0
class SqlMetricInlineView(  # pylint: disable=too-many-ancestors
    CompactCRUDMixin, SupersetModelView
):
    datamodel = SQLAInterface(models.SqlMetric)
    include_route_methods = RouteMethod.RELATED_VIEW_SET | RouteMethod.API_SET

    list_title = _("Metrics")
    show_title = _("Show Metric")
    add_title = _("Add Metric")
    edit_title = _("Edit Metric")

    list_columns = ["metric_name", "verbose_name", "metric_type"]
    edit_columns = [
        "metric_name",
        "description",
        "verbose_name",
        "metric_type",
        "expression",
        "table",
        "d3format",
        "extra",
        "warning_text",
    ]
    description_columns = {
        "expression": utils.markdown(
            "a valid, *aggregating* SQL expression as supported by the "
            "underlying backend. Example: `count(DISTINCT userid)`",
            True,
        ),
        "d3format": utils.markdown(
            "d3 formatting string as defined [here]"
            "(https://github.com/d3/d3-format/blob/master/README.md#format). "
            "For instance, this default formatting applies in the Table "
            "visualization and allow for different metric to use different "
            "formats",
            True,
        ),
        "extra": utils.markdown(
            "Extra data to specify metric metadata. Currently supports "
            'certification data of the format: `{ "certification": "certified_by": '
            '"Data Platform Team", "details": "This metric is the source of truth." '
            "} }`. This should be modified from the edit datasource model in "
            "Explore to ensure correct formatting.",
            True,
        ),
    }
    add_columns = edit_columns
    page_size = 500
    label_columns = {
        "metric_name": _("Metric"),
        "description": _("Description"),
        "verbose_name": _("Verbose Name"),
        "metric_type": _("Type"),
        "expression": _("SQL Expression"),
        "table": _("Table"),
        "d3format": _("D3 Format"),
        "extra": _("Extra"),
        "warning_text": _("Warning Message"),
    }

    add_form_extra_fields = {
        "table": QuerySelectField(
            "Table",
            query_factory=lambda: db.session.query(models.SqlaTable),
            allow_blank=True,
            widget=Select2Widget(extra_classes="readonly"),
        )
    }

    edit_form_extra_fields = add_form_extra_fields
コード例 #7
0
ファイル: orm.py プロジェクト: imfht/flaskapps
    def convert(self, model, mapper, prop, field_args, *args):
        kwargs = {'validators': [], 'filters': []}

        if field_args:
            kwargs.update(field_args)

        if hasattr(prop, 'direction'):
            remote_model = prop.mapper.class_
            local_column = prop.local_remote_pairs[0][0]

            kwargs.update({
                'allow_blank':
                local_column.nullable,
                'label':
                self._get_label(prop.key, kwargs),
                'query_factory':
                lambda: self.view.session.query(remote_model)
            })
            if local_column.nullable:
                kwargs['validators'].append(validators.Optional())
            elif prop.direction.name not in ('MANYTOMANY', 'ONETOMANY'):
                kwargs['validators'].append(validators.Required())

            # Override field type if necessary
            override = self._get_field_override(prop.key)
            if override:
                return override(**kwargs)

            if prop.direction.name == 'MANYTOONE':
                return QuerySelectField(widget=form.ChosenSelectWidget(),
                                        **kwargs)
            elif prop.direction.name == 'ONETOMANY':
                # Skip backrefs
                if not local_column.foreign_keys and self.view.hide_backrefs:
                    return None

                return QuerySelectMultipleField(
                    widget=form.ChosenSelectWidget(multiple=True), **kwargs)
            elif prop.direction.name == 'MANYTOMANY':
                return QuerySelectMultipleField(
                    widget=form.ChosenSelectWidget(multiple=True), **kwargs)
        else:
            # Ignore pk/fk
            if hasattr(prop, 'columns'):
                column = prop.columns[0]

                # Column can be SQL expressions
                # WTForms cannot convert them
                if not isinstance(column, Column):
                    return None

                # Do not display foreign keys - use relations
                if column.foreign_keys:
                    return None

                unique = False

                if column.primary_key:
                    # By default, don't show primary keys either
                    if self.view.fields is None:
                        return None

                    # If PK is not explicitly allowed, ignore it
                    if prop.key not in self.view.fields:
                        return None

                    kwargs['validators'].append(
                        Unique(self.view.session, model, column))
                    unique = True

                # If field is unique, validate it
                if column.unique and not unique:
                    kwargs['validators'].append(
                        Unique(self.view.session, model, column))

                if column.nullable:
                    kwargs['validators'].append(validators.Optional())
                else:
                    kwargs['validators'].append(validators.Required())

            # Apply label
            kwargs['label'] = self._get_label(prop.key, kwargs)

            # Override field type if necessary
            override = self._get_field_override(prop.key)
            if override:
                return override(**kwargs)

            return super(AdminModelConverter,
                         self).convert(model, mapper, prop, kwargs)
コード例 #8
0
class CsvToDatabaseForm(DynamicForm):
    # pylint: disable=E0211
    def csv_allowed_dbs():
        csv_allowed_dbs = []
        csv_enabled_dbs = db.session.query(
            models.Database).filter_by(allow_csv_upload=True).all()
        for csv_enabled_db in csv_enabled_dbs:
            if CsvToDatabaseForm.at_least_one_schema_is_allowed(
                    csv_enabled_db):
                csv_allowed_dbs.append(csv_enabled_db)
        return csv_allowed_dbs

    @staticmethod
    def at_least_one_schema_is_allowed(database):
        """
        If the user has access to the database or all datasource
            1. if schemas_allowed_for_csv_upload is empty
                a) if database does not support schema
                    user is able to upload csv without specifying schema name
                b) if database supports schema
                    user is able to upload csv to any schema
            2. if schemas_allowed_for_csv_upload is not empty
                a) if database does not support schema
                    This situation is impossible and upload will fail
                b) if database supports schema
                    user is able to upload to schema in schemas_allowed_for_csv_upload
        elif the user does not access to the database or all datasource
            1. if schemas_allowed_for_csv_upload is empty
                a) if database does not support schema
                    user is unable to upload csv
                b) if database supports schema
                    user is unable to upload csv
            2. if schemas_allowed_for_csv_upload is not empty
                a) if database does not support schema
                    This situation is impossible and user is unable to upload csv
                b) if database supports schema
                    user is able to upload to schema in schemas_allowed_for_csv_upload
        """
        if (security_manager.database_access(database)
                or security_manager.all_datasource_access()):
            return True
        schemas = database.get_schema_access_for_csv_upload()
        if (schemas and security_manager.schemas_accessible_by_user(
                database, schemas, False)):
            return True
        return False

    name = StringField(
        _('Table Name'),
        description=_('Name of table to be created from csv data.'),
        validators=[DataRequired()],
        widget=BS3TextFieldWidget())
    csv_file = FileField(
        _('CSV File'),
        description=_('Select a CSV file to be uploaded to a database.'),
        validators=[
            FileRequired(),
            FileAllowed(['csv'], _('CSV Files Only!'))
        ])
    con = QuerySelectField(_('Database'),
                           query_factory=csv_allowed_dbs,
                           get_pk=lambda a: a.id,
                           get_label=lambda a: a.database_name)
    schema = StringField(
        _('Schema'),
        description=_('Specify a schema (if database flavor supports this).'),
        validators=[Optional()],
        widget=BS3TextFieldWidget())
    sep = StringField(
        _('Delimiter'),
        description=_('Delimiter used by CSV file (for whitespace use \s+).'),
        validators=[DataRequired()],
        widget=BS3TextFieldWidget())
    if_exists = SelectField(
        _('Table Exists'),
        description=_('If table exists do one of the following: '
                      'Fail (do nothing), Replace (drop and recreate table) '
                      'or Append (insert data).'),
        choices=[('fail', _('Fail')), ('replace', _('Replace')),
                 ('append', _('Append'))],
        validators=[DataRequired()])
    header = IntegerField(_('Header Row'),
                          description=_(
                              'Row containing the headers to use as '
                              'column names (0 is first line of data). '
                              'Leave empty if there is no header row.'),
                          validators=[Optional(),
                                      NumberRange(min=0)],
                          widget=BS3TextFieldWidget())
    index_col = IntegerField(_('Index Column'),
                             description=_(
                                 'Column to use as the row labels of the '
                                 'dataframe. Leave empty if no index column.'),
                             validators=[Optional(),
                                         NumberRange(min=0)],
                             widget=BS3TextFieldWidget())
    mangle_dupe_cols = BooleanField(
        _('Mangle Duplicate Columns'),
        description=_('Specify duplicate columns as "X.0, X.1".'))
    skipinitialspace = BooleanField(
        _('Skip Initial Space'), description=_('Skip spaces after delimiter.'))
    skiprows = IntegerField(
        _('Skip Rows'),
        description=_('Number of rows to skip at start of file.'),
        validators=[Optional(), NumberRange(min=0)],
        widget=BS3TextFieldWidget())
    nrows = IntegerField(_('Rows to Read'),
                         description=_('Number of rows of file to read.'),
                         validators=[Optional(),
                                     NumberRange(min=0)],
                         widget=BS3TextFieldWidget())
    skip_blank_lines = BooleanField(
        _('Skip Blank Lines'),
        description=_('Skip blank lines rather than interpreting them '
                      'as NaN values.'))
    parse_dates = CommaSeparatedListField(
        _('Parse Dates'),
        description=_('A comma separated list of columns that should be '
                      'parsed as dates.'),
        filters=[filter_not_empty_values])
    infer_datetime_format = BooleanField(
        _('Infer Datetime Format'),
        description=_('Use Pandas to interpret the datetime format '
                      'automatically.'))
    decimal = StringField(
        _('Decimal Character'),
        default='.',
        description=_('Character to interpret as decimal point.'),
        validators=[Optional(), Length(min=1, max=1)],
        widget=BS3TextFieldWidget())
    index = BooleanField(_('Dataframe Index'),
                         description=_('Write dataframe index as a column.'))
    index_label = StringField(
        _('Column Label(s)'),
        description=_('Column label for index column(s). If None is given '
                      'and Dataframe Index is True, Index Names are used.'),
        validators=[Optional()],
        widget=BS3TextFieldWidget())
コード例 #9
0
class UserForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    firstname = StringField('First name', validators=[DataRequired()])
    lastname = StringField('Last name', validators=[DataRequired()])
    age = IntegerField('Age')
    group = QuerySelectField('Group', query_factory=get_groups, allow_blank=False)
コード例 #10
0
ファイル: forms.py プロジェクト: EsMACGIA/SAGPA
class RegistrationFormProcessGroupWithDSPGPGC2(FlaskForm):
    description = StringField('Description', validators=[DataRequired()])
    discipline_id = QuerySelectField(query_factory=lambda: DSPGPGC.query.all())
    submit = SubmitField('Registrar')
コード例 #11
0
class DeployForm(Form):
    cloud_provider = QuerySelectField(_('Cloud provider'), [Required()], get_label='name', \
                                       query_factory=get_providers, \
                                       allow_blank=True, blank_text=_('Please choose a provider ...'))
コード例 #12
0
class CreateAPIToken(FlaskForm):
    name = StringField("Name", [InputRequired(), Length(1, 30)])
    package      = QuerySelectField("Limit to package", allow_blank=True, \
      get_pk=lambda a: a.id, get_label=lambda a: a.title)
    submit = SubmitField("Save")
コード例 #13
0
ファイル: forms.py プロジェクト: thomasDOTwtf/ffbackbone
class FormNameserver(Form):
    fqdn = StringField('FQDN', validators=[DataRequired()])
    community = QuerySelectField('Community', validators=[DataRequired()])
    submit = SubmitField('Submit')
コード例 #14
0
ファイル: forms.py プロジェクト: thomasDOTwtf/ffbackbone
class SessionForm(Form):
    type = QuerySelectField('Type', validators=[DataRequired()])
    submit = SubmitField('Create Sessions')
コード例 #15
0
ファイル: forms.py プロジェクト: maximebardou/gbi-client
class SelectCoverage(Form):
    select_coverage = QuerySelectField(
        lazy_gettext('select coverage'),
        query_factory=get_all_projects_withs_coverages,
        get_label='title')
コード例 #16
0
class RegisterRecruiterForm(FlaskForm):
    name = TextField(validators=[Required()])
    phone_number = TextField()
    email = EmailField()
    agency = QuerySelectField(query_factory=enabled_agencies,
                              allow_blank=True)
コード例 #17
0
ファイル: forms.py プロジェクト: maximebardou/gbi-client
class ServerSearchForm(Form):
    search_source = QuerySelectField(lazy_gettext('source'),
                                     query_factory=get_server_search_sources,
                                     get_label=lambda a: a.label,
                                     get_pk=lambda a: a.value)
コード例 #18
0
class DruidClusterModelView(SupersetModelView, DeleteMixin, YamlExportMixin):
    datamodel = SQLAInterface(models.DruidCluster)

    list_title = _("Druid Clusters")
    show_title = _("Show Druid Cluster")
    add_title = _("Add Druid Cluster")
    edit_title = _("Edit Druid Cluster")

    add_columns = [
        "verbose_name",
        "broker_host",
        "broker_port",
        "broker_user",
        "broker_pass",
        "broker_endpoint",
        "cache_timeout",
        "cluster_name",
    ]
    edit_columns = add_columns
    list_columns = ["cluster_name", "metadata_last_refreshed"]
    search_columns = ("cluster_name",)
    label_columns = {
        "cluster_name": _("Cluster"),
        "broker_host": _("Broker Host"),
        "broker_port": _("Broker Port"),
        "broker_user": _("Broker Username"),
        "broker_pass": _("Broker Password"),
        "broker_endpoint": _("Broker Endpoint"),
        "verbose_name": _("Verbose Name"),
        "cache_timeout": _("Cache Timeout"),
        "metadata_last_refreshed": _("Metadata Last Refreshed"),
    }
    description_columns = {
        "cache_timeout": _(
            "Duration (in seconds) of the caching timeout for this cluster. "
            "A timeout of 0 indicates that the cache never expires. "
            "Note this defaults to the global timeout if undefined."
        ),
        "broker_user": _(
            "Druid supports basic authentication. See "
            "[auth](http://druid.io/docs/latest/design/auth.html) and "
            "druid-basic-security extension"
        ),
        "broker_pass": _(
            "Druid supports basic authentication. See "
            "[auth](http://druid.io/docs/latest/design/auth.html) and "
            "druid-basic-security extension"
        ),
    }

    edit_form_extra_fields = {
        "cluster_name": QuerySelectField(
            "Cluster",
            query_factory=lambda: db.session().query(models.DruidCluster),
            widget=Select2Widget(extra_classes="readonly"),
        )
    }

    def pre_add(self, cluster):
        security_manager.add_permission_view_menu("database_access", cluster.perm)

    def pre_update(self, cluster):
        self.pre_add(cluster)

    def _delete(self, pk):
        DeleteMixin._delete(self, pk)
コード例 #19
0
class TableModelView(  # pylint: disable=too-many-ancestors
    DatasourceModelView, DeleteMixin, YamlExportMixin
):
    datamodel = SQLAInterface(models.SqlaTable)
    include_route_methods = RouteMethod.CRUD_SET

    list_title = _("Tables")
    show_title = _("Show Table")
    add_title = _("Import a table definition")
    edit_title = _("Edit Table")

    list_columns = ["link", "database_name", "changed_by_", "modified"]
    order_columns = ["modified"]
    add_columns = ["database", "schema", "table_name"]
    edit_columns = [
        "table_name",
        "sql",
        "filter_select_enabled",
        "fetch_values_predicate",
        "database",
        "schema",
        "description",
        "owners",
        "main_dttm_col",
        "default_endpoint",
        "offset",
        "cache_timeout",
        "is_sqllab_view",
        "template_params",
        "extra",
    ]
    base_filters = [["id", DatasourceFilter, lambda: []]]
    show_columns = edit_columns + ["perm", "slices"]
    related_views = [
        TableColumnInlineView,
        SqlMetricInlineView,
    ]
    base_order = ("changed_on", "desc")
    search_columns = ("database", "schema", "table_name", "owners", "is_sqllab_view")
    description_columns = {
        "slices": _(
            "The list of charts associated with this table. By "
            "altering this datasource, you may change how these associated "
            "charts behave. "
            "Also note that charts need to point to a datasource, so "
            "this form will fail at saving if removing charts from a "
            "datasource. If you want to change the datasource for a chart, "
            "overwrite the chart from the 'explore view'"
        ),
        "offset": _("Timezone offset (in hours) for this datasource"),
        "table_name": _("Name of the table that exists in the source database"),
        "schema": _(
            "Schema, as used only in some databases like Postgres, Redshift " "and DB2"
        ),
        "description": Markup(
            'Supports <a href="https://daringfireball.net/projects/markdown/">'
            "markdown</a>"
        ),
        "sql": _(
            "This fields acts a Superset view, meaning that Superset will "
            "run a query against this string as a subquery."
        ),
        "fetch_values_predicate": _(
            "Predicate applied when fetching distinct value to "
            "populate the filter control component. Supports "
            "jinja template syntax. Applies only when "
            "`Enable Filter Select` is on."
        ),
        "default_endpoint": _(
            "Redirects to this endpoint when clicking on the table "
            "from the table list"
        ),
        "filter_select_enabled": _(
            "Whether to populate the filter's dropdown in the explore "
            "view's filter section with a list of distinct values fetched "
            "from the backend on the fly"
        ),
        "is_sqllab_view": _(
            "Whether the table was generated by the 'Visualize' flow " "in SQL Lab"
        ),
        "template_params": _(
            "A set of parameters that become available in the query using "
            "Jinja templating syntax"
        ),
        "cache_timeout": _(
            "Duration (in seconds) of the caching timeout for this table. "
            "A timeout of 0 indicates that the cache never expires. "
            "Note this defaults to the database timeout if undefined."
        ),
        "extra": utils.markdown(
            "Extra data to specify table metadata. Currently supports "
            'certification data of the format: `{ "certification": { "certified_by": '
            '"Data Platform Team", "details": "This table is the source of truth." '
            "} }`.",
            True,
        ),
    }
    label_columns = {
        "slices": _("Associated Charts"),
        "link": _("Table"),
        "changed_by_": _("Changed By"),
        "database": _("Database"),
        "database_name": _("Database"),
        "changed_on_": _("Last Changed"),
        "filter_select_enabled": _("Enable Filter Select"),
        "schema": _("Schema"),
        "default_endpoint": _("Default Endpoint"),
        "offset": _("Offset"),
        "cache_timeout": _("Cache Timeout"),
        "table_name": _("Table Name"),
        "fetch_values_predicate": _("Fetch Values Predicate"),
        "owners": _("Owners"),
        "main_dttm_col": _("Main Datetime Column"),
        "description": _("Description"),
        "is_sqllab_view": _("SQL Lab View"),
        "template_params": _("Template parameters"),
        "extra": _("Extra"),
        "modified": _("Modified"),
    }
    edit_form_extra_fields = {
        "database": QuerySelectField(
            "Database",
            query_factory=lambda: db.session.query(models.Database),
            widget=Select2Widget(extra_classes="readonly"),
        )
    }

    def pre_add(self, item: "TableModelView") -> None:
        validate_sqlatable(item)

    def post_add(  # pylint: disable=arguments-differ
        self,
        item: "TableModelView",
        flash_message: bool = True,
        fetch_metadata: bool = True,
    ) -> None:
        if fetch_metadata:
            item.fetch_metadata()
        create_table_permissions(item)
        if flash_message:
            flash(
                _(
                    "The table was created. "
                    "As part of this two-phase configuration "
                    "process, you should now click the edit button by "
                    "the new table to configure it."
                ),
                "info",
            )

    def post_update(self, item: "TableModelView") -> None:
        self.post_add(item, flash_message=False, fetch_metadata=False)

    def _delete(self, pk: int) -> None:
        DeleteMixin._delete(self, pk)

    @expose("/edit/<pk>", methods=["GET", "POST"])
    @has_access
    def edit(self, pk: str) -> FlaskResponse:
        """Simple hack to redirect to explore view after saving"""
        resp = super().edit(pk)
        if isinstance(resp, str):
            return resp
        return redirect("/superset/explore/table/{}/".format(pk))

    @action(
        "refresh", __("Refresh Metadata"), __("Refresh column metadata"), "fa-refresh"
    )
    def refresh(  # pylint: disable=no-self-use, too-many-branches
        self, tables: Union["TableModelView", List["TableModelView"]]
    ) -> FlaskResponse:
        if not isinstance(tables, list):
            tables = [tables]

        @dataclass
        class RefreshResults:
            successes: List[TableModelView] = field(default_factory=list)
            failures: List[TableModelView] = field(default_factory=list)
            added: Dict[str, List[str]] = field(default_factory=dict)
            removed: Dict[str, List[str]] = field(default_factory=dict)
            modified: Dict[str, List[str]] = field(default_factory=dict)

        results = RefreshResults()

        for table_ in tables:
            try:
                metadata_results = table_.fetch_metadata()
                if metadata_results.added:
                    results.added[table_.table_name] = metadata_results.added
                if metadata_results.removed:
                    results.removed[table_.table_name] = metadata_results.removed
                if metadata_results.modified:
                    results.modified[table_.table_name] = metadata_results.modified
                results.successes.append(table_)
            except Exception:  # pylint: disable=broad-except
                results.failures.append(table_)

        if len(results.successes) > 0:
            success_msg = _(
                "Metadata refreshed for the following table(s): %(tables)s",
                tables=", ".join([t.table_name for t in results.successes]),
            )
            flash(success_msg, "info")
        if results.added:
            added_tables = []
            for table, cols in results.added.items():
                added_tables.append(f"{table} ({', '.join(cols)})")
            flash(
                _(
                    "The following tables added new columns: %(tables)s",
                    tables=", ".join(added_tables),
                ),
                "info",
            )
        if results.removed:
            removed_tables = []
            for table, cols in results.removed.items():
                removed_tables.append(f"{table} ({', '.join(cols)})")
            flash(
                _(
                    "The following tables removed columns: %(tables)s",
                    tables=", ".join(removed_tables),
                ),
                "info",
            )
        if results.modified:
            modified_tables = []
            for table, cols in results.modified.items():
                modified_tables.append(f"{table} ({', '.join(cols)})")
            flash(
                _(
                    "The following tables update column metadata: %(tables)s",
                    tables=", ".join(modified_tables),
                ),
                "info",
            )
        if len(results.failures) > 0:
            failure_msg = _(
                "Unable to refresh metadata for the following table(s): %(tables)s",
                tables=", ".join([t.table_name for t in results.failures]),
            )
            flash(failure_msg, "danger")

        return redirect("/tablemodelview/list/")

    @expose("/list/")
    @has_access
    def list(self) -> FlaskResponse:
        if not is_feature_enabled("ENABLE_REACT_CRUD_VIEWS"):
            return super().list()

        return super().render_app_template()
コード例 #20
0
class DruidColumnInlineView(CompactCRUDMixin, SupersetModelView):
    datamodel = SQLAInterface(models.DruidColumn)

    list_title = _("Columns")
    show_title = _("Show Druid Column")
    add_title = _("Add Druid Column")
    edit_title = _("Edit Druid Column")

    list_widget = ListWidgetWithCheckboxes

    edit_columns = [
        "column_name",
        "verbose_name",
        "description",
        "dimension_spec_json",
        "datasource",
        "groupby",
        "filterable",
    ]
    add_columns = edit_columns
    list_columns = ["column_name", "verbose_name", "type", "groupby", "filterable"]
    can_delete = False
    page_size = 500
    label_columns = {
        "column_name": _("Column"),
        "type": _("Type"),
        "datasource": _("Datasource"),
        "groupby": _("Groupable"),
        "filterable": _("Filterable"),
    }
    description_columns = {
        "filterable": _(
            "Whether this column is exposed in the `Filters` section "
            "of the explore view."
        ),
        "dimension_spec_json": utils.markdown(
            "this field can be used to specify  "
            "a `dimensionSpec` as documented [here]"
            "(http://druid.io/docs/latest/querying/dimensionspecs.html). "
            "Make sure to input valid JSON and that the "
            "`outputName` matches the `column_name` defined "
            "above.",
            True,
        ),
    }

    add_form_extra_fields = {
        "datasource": QuerySelectField(
            "Datasource",
            query_factory=lambda: db.session().query(models.DruidDatasource),
            allow_blank=True,
            widget=Select2Widget(extra_classes="readonly"),
        )
    }

    edit_form_extra_fields = add_form_extra_fields

    def pre_update(self, col):
        # If a dimension spec JSON is given, ensure that it is
        # valid JSON and that `outputName` is specified
        if col.dimension_spec_json:
            try:
                dimension_spec = json.loads(col.dimension_spec_json)
            except ValueError as e:
                raise ValueError("Invalid Dimension Spec JSON: " + str(e))
            if not isinstance(dimension_spec, dict):
                raise ValueError("Dimension Spec must be a JSON object")
            if "outputName" not in dimension_spec:
                raise ValueError("Dimension Spec does not contain `outputName`")
            if "dimension" not in dimension_spec:
                raise ValueError("Dimension Spec is missing `dimension`")
            # `outputName` should be the same as the `column_name`
            if dimension_spec["outputName"] != col.column_name:
                raise ValueError(
                    "`outputName` [{}] unequal to `column_name` [{}]".format(
                        dimension_spec["outputName"], col.column_name
                    )
                )

    def post_update(self, col):
        col.refresh_metrics()

    def post_add(self, col):
        self.post_update(col)
コード例 #21
0
ファイル: forms.py プロジェクト: janwawruszczak1998/SasinMed
class AddVisitForm(FlaskForm):
    date_of_visit = DateField('Data wizyty', validators=[InputRequired()])
    time_of_visit = TimeField('Czas wizyty', validators=[InputRequired()])
    doctor = QuerySelectField('Lekarz',
                              query_factory=lambda: Doctor.query.all(),
                              validators=[InputRequired()])
コード例 #22
0
ファイル: forms.py プロジェクト: wasm-network/indico
class ContributionForm(IndicoForm):
    title = StringField(_("Title"), [DataRequired()])
    description = TextAreaField(_("Description"))
    start_dt = IndicoDateTimeField(
        _("Start date"), [
            DataRequired(),
            DateTimeRange(
                earliest=lambda form, field: form._get_earliest_start_dt(),
                latest=lambda form, field: form._get_latest_start_dt())
        ],
        allow_clear=False,
        description=_("Start date of the contribution"))
    duration = TimeDeltaField(
        _("Duration"),
        [DataRequired(), MaxDuration(timedelta(hours=24))],
        default=timedelta(minutes=20),
        units=('minutes', 'hours'))
    type = QuerySelectField(_("Type"),
                            get_label='name',
                            allow_blank=True,
                            blank_text=_("No type selected"))
    person_link_data = ContributionPersonLinkListField(_("People"))
    location_data = IndicoLocationField(_("Location"))
    keywords = IndicoTagListField(_('Keywords'))
    references = ReferencesField(
        _("External IDs"),
        reference_class=ContributionReference,
        description=_("Manage external resources for this contribution"))
    board_number = StringField(_("Board Number"))

    @generated_data
    def render_mode(self):
        return RenderMode.markdown

    def __init__(self, *args, **kwargs):
        self.event = kwargs.pop('event')
        self.contrib = kwargs.pop('contrib', None)
        self.session_block = kwargs.get('session_block')
        self.timezone = self.event.timezone
        to_schedule = kwargs.pop('to_schedule', False)
        super(ContributionForm, self).__init__(*args, **kwargs)
        self.type.query = self.event.contribution_types
        if self.event.type != 'conference':
            self.person_link_data.label.text = _("Speakers")
        if not self.type.query.count():
            del self.type
        if not to_schedule and (self.contrib is None
                                or not self.contrib.is_scheduled):
            del self.start_dt

    def _get_earliest_start_dt(self):
        return self.session_block.start_dt if self.session_block else self.event.start_dt

    def _get_latest_start_dt(self):
        return self.session_block.end_dt if self.session_block else self.event.end_dt

    def validate_duration(self, field):
        start_dt = self.start_dt.data if self.start_dt else None
        if start_dt:
            end_dt = start_dt + field.data
            if self.session_block and end_dt > self.session_block.end_dt:
                raise ValidationError(
                    _("With the current duration the contribution exceeds the block end date"
                      ))
            if end_dt > self.event.end_dt:
                raise ValidationError(
                    _('With the current duration the contribution exceeds the event end date'
                      ))

    @property
    def custom_field_names(self):
        return tuple([
            field_name for field_name in self._fields
            if field_name.startswith('custom_')
        ])
コード例 #23
0
ファイル: views.py プロジェクト: zrxpower/incubator-superset
class SqlMetricInlineView(CompactCRUDMixin, SupersetModelView):
    datamodel = SQLAInterface(models.SqlMetric)
    include_route_methods = RouteMethod.RELATED_VIEW_SET | RouteMethod.API_SET

    list_title = _("Metrics")
    show_title = _("Show Metric")
    add_title = _("Add Metric")
    edit_title = _("Edit Metric")

    list_columns = ["metric_name", "verbose_name", "metric_type"]
    edit_columns = [
        "metric_name",
        "description",
        "verbose_name",
        "metric_type",
        "expression",
        "table",
        "d3format",
        "warning_text",
    ]
    description_columns = {
        "expression":
        utils.markdown(
            "a valid, *aggregating* SQL expression as supported by the "
            "underlying backend. Example: `count(DISTINCT userid)`",
            True,
        ),
        "d3format":
        utils.markdown(
            "d3 formatting string as defined [here]"
            "(https://github.com/d3/d3-format/blob/master/README.md#format). "
            "For instance, this default formatting applies in the Table "
            "visualization and allow for different metric to use different "
            "formats",
            True,
        ),
    }
    add_columns = edit_columns
    page_size = 500
    label_columns = {
        "metric_name": _("Metric"),
        "description": _("Description"),
        "verbose_name": _("Verbose Name"),
        "metric_type": _("Type"),
        "expression": _("SQL Expression"),
        "table": _("Table"),
        "d3format": _("D3 Format"),
        "warning_text": _("Warning Message"),
    }

    add_form_extra_fields = {
        "table":
        QuerySelectField(
            "Table",
            query_factory=lambda: db.session().query(models.SqlaTable),
            allow_blank=True,
            widget=Select2Widget(extra_classes="readonly"),
        )
    }

    edit_form_extra_fields = add_form_extra_fields
コード例 #24
0
class ProductForm(ProductEditForm):
    category = QuerySelectField("Category of the product",
                                query_factory=lambda: Category.query.all(),
                                get_label='name')
    image = FileField('Image', validators=[FileRequired("PLEASE")])
コード例 #25
0
ファイル: views.py プロジェクト: zrxpower/incubator-superset
class TableColumnInlineView(CompactCRUDMixin, SupersetModelView):
    datamodel = SQLAInterface(models.TableColumn)
    # TODO TODO, review need for this on related_views
    include_route_methods = RouteMethod.RELATED_VIEW_SET | RouteMethod.API_SET

    list_title = _("Columns")
    show_title = _("Show Column")
    add_title = _("Add Column")
    edit_title = _("Edit Column")

    can_delete = False
    list_widget = ListWidgetWithCheckboxes
    edit_columns = [
        "column_name",
        "verbose_name",
        "description",
        "type",
        "groupby",
        "filterable",
        "table",
        "expression",
        "is_dttm",
        "python_date_format",
    ]
    add_columns = edit_columns
    list_columns = [
        "column_name",
        "verbose_name",
        "type",
        "groupby",
        "filterable",
        "is_dttm",
    ]
    page_size = 500
    description_columns = {
        "is_dttm":
        _("Whether to make this column available as a "
          "[Time Granularity] option, column has to be DATETIME or "
          "DATETIME-like"),
        "filterable":
        _("Whether this column is exposed in the `Filters` section "
          "of the explore view."),
        "type":
        _("The data type that was inferred by the database. "
          "It may be necessary to input a type manually for "
          "expression-defined columns in some cases. In most case "
          "users should not need to alter this."),
        "expression":
        utils.markdown(
            "a valid, *non-aggregating* SQL expression as supported by the "
            "underlying backend. Example: `substr(name, 1, 1)`",
            True,
        ),
        "python_date_format":
        utils.markdown(
            Markup(
                "The pattern of timestamp format. For strings use "
                '<a href="https://docs.python.org/2/library/'
                'datetime.html#strftime-strptime-behavior">'
                "python datetime string pattern</a> expression which needs to "
                'adhere to the <a href="https://en.wikipedia.org/wiki/ISO_8601">'
                "ISO 8601</a> standard to ensure that the lexicographical ordering "
                "coincides with the chronological ordering. If the timestamp "
                "format does not adhere to the ISO 8601 standard you will need to "
                "define an expression and type for transforming the string into a "
                "date or timestamp. Note currently time zones are not supported. "
                "If time is stored in epoch format, put `epoch_s` or `epoch_ms`."
                "If no pattern is specified we fall back to using the optional "
                "defaults on a per database/column name level via the extra parameter."
                ""),
            True,
        ),
    }
    label_columns = {
        "column_name": _("Column"),
        "verbose_name": _("Verbose Name"),
        "description": _("Description"),
        "groupby": _("Groupable"),
        "filterable": _("Filterable"),
        "table": _("Table"),
        "expression": _("Expression"),
        "is_dttm": _("Is temporal"),
        "python_date_format": _("Datetime Format"),
        "type": _("Type"),
    }
    validators_columns = {
        "python_date_format": [
            # Restrict viable values to epoch_s, epoch_ms, or a strftime format
            # which adhere's to the ISO 8601 format (without time zone).
            Regexp(
                re.compile(
                    r"""
                    ^(
                        epoch_s|epoch_ms|
                        (?P<date>%Y(-%m(-%d)?)?)([\sT](?P<time>%H(:%M(:%S(\.%f)?)?)?))?
                    )$
                    """,
                    re.VERBOSE,
                ),
                message=_("Invalid date/timestamp format"),
            )
        ]
    }

    add_form_extra_fields = {
        "table":
        QuerySelectField(
            "Table",
            query_factory=lambda: db.session().query(models.SqlaTable),
            allow_blank=True,
            widget=Select2Widget(extra_classes="readonly"),
        )
    }

    edit_form_extra_fields = add_form_extra_fields
コード例 #26
0
class WelcomeForm(FlaskForm):
    budget = DecimalField('Budget', validators=[DataRequired()])
    category = QuerySelectField('Category',
                                query_factory=lambda: Category.query.all(),
                                get_label='name')
コード例 #27
0
ファイル: forms.py プロジェクト: ALIKUSAMSON/KuziID
class ChoiceForm(Form):
    opts = QuerySelectField(query_factory=choice_query,
                            allow_blank=True,
                            get_label='namecategory')
コード例 #28
0
ファイル: forms.py プロジェクト: DW-dealings/dw
class EmployeeAssignForm(FlaskForm):
    role = QuerySelectField(query_factory=lambda: Role.query.all(),
                            get_label='role_name')
    user = QuerySelectField(query_factory=lambda: Employee.query.all(),
                            get_label='emp_id')
    submit = SubmitField('Submit')
コード例 #29
0
ファイル: forms.py プロジェクト: tanya1711/EmployeeBook
class EmployeeAssignmentForm(FlaskForm):
    department = QuerySelectField(query_factory=lambda: Department.query.all(),
                                  get_label="name")
    role = QuerySelectField(query_factory=lambda: Role.query.all(),
                            get_label="name")
    submit = SubmitField("Submit")
コード例 #30
0
ファイル: forms.py プロジェクト: patrikCze1/flask
class AddUserIntoGroup(FlaskForm):
    user = QuerySelectField('User',
                            query_factory=getUsers,
                            allow_blank=True,
                            get_pk=lambda item: item.id)
    submit = SubmitField('Add')