Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: hook to populate UI with default values on entity creation #491

Open
sglebs opened this issue Jan 27, 2024 · 5 comments
Open
Labels
enhancement New feature or request

Comments

@sglebs
Copy link

sglebs commented Jan 27, 2024

Is your feature request related to a problem? Please describe.

Currently the UI to add an entity does not populate it with default values.

#327 does not solve it. The UI still opens with blank values.

Describe the solution you'd like

I was expecting before_create from #327 to do this, but it seems to run AFTER the SAVE button is clicked. The UI is opened blank, still. Maybe we need another hook, before_ui_opens or something like that?

Describe alternatives you've considered

I have overriden before_create but it is only run AFTER. Same with before_edit

Additional context

In before_create I have put logic to honor SQLALchemy's default values:

    async def before_create(self, request: Request, data: Dict[str, Any], knowledge_ingestion_profile: Any) -> None:
        # We basically want to honor all columns with default values defined by SQLAlchemy
        if knowledge_ingestion_profile.requests_per_second is None:
            knowledge_ingestion_profile.requests_per_second = KnowledgeIngestionProfile.requests_per_second.default.arg
        if knowledge_ingestion_profile.verify_ssl is None:
            knowledge_ingestion_profile.verify_ssl = KnowledgeIngestionProfile.verify_ssl.default.arg
        if knowledge_ingestion_profile.http_headers is None or len(knowledge_ingestion_profile.http_headers.strip()) <= 0:
            knowledge_ingestion_profile.http_headers = KnowledgeIngestionProfile.http_headers.default.arg
      ...
      ...

Unfortunately it does not run soon enough to populate the UI when the user clicks "New "

@sglebs sglebs added the enhancement New feature or request label Jan 27, 2024
@sglebs
Copy link
Author

sglebs commented Jan 27, 2024

Note: I have had this need for a long time. See #287 . Unfortunately #327 did not solve it for me, as it runs "too late" :-(

@hasansezertasan
Copy link
Contributor

hasansezertasan commented Jan 27, 2024

I think we need an attribute called default in the BaseField class.

I just checked the templates, and saw these lines...

<div class="card-body border-bottom py-3">
{% block create_form %}
{% for field in model.get_fields_list(request, 'CREATE' | ra) %}
<div class="mb-3">
{% with action=('CREATE'| ra), data=(None if not obj else obj[field.name]), error=errors.get(field.name, None) if errors else None %}
{% include field.label_template %}
{% include field.form_template %}
{% endwith %}
</div>
{% endfor %}
{% endblock %}
</div>

The data that input fields take is calculated inside the template (see obj[field.name]), so overriding before_create or before_edit won't work. We need to move that functionality from Jinja2 to Python and call it from Jinja2, it'll make it easier to override. Otherwise, you would have to override +10 template files.

@jowilf
Copy link
Owner

jowilf commented Jan 27, 2024

I think we need an attribute called default in the BaseField class.

Yes, and read the default value from the column definition (SQLAlchemy), Field(mongoengine)...

@hasansezertasan
Copy link
Contributor

hasansezertasan commented Jan 29, 2024

I think we need an attribute called default in the BaseField class.

Yes, and read the default value from the column definition (SQLAlchemy), Field(mongoengine)...

Yep, that's what I'm talking about. Do you have a work in progress about this? If not, I may try to implement some stuff (I missed your reviews 😂, they are teaching!).

@jowilf
Copy link
Owner

jowilf commented Jan 30, 2024

Yep, that's what I'm talking about. Do you have a work in progress about this? If not, I may try to implement some stuff (I missed your reviews 😂, they are teaching!).

I'm glad you are learning from my reviews. Feel free to submit a PR for this. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants