-
Notifications
You must be signed in to change notification settings - Fork 13
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
ModelView discovery from models #207
Comments
Models are currently overloaded with helper methods and data meant for views. This includes statemanager labels, transition metadata, and various methods and properties. These should really belong in a view, except we have the problem of being unable to discover these items from a template if they are placed anywhere other than the model. This requirement goes beyond the view method discovery offered by
Using this, a template can access a view helper method like this: |
This can be abstracted into a RegistryMixin that works for forms as well as views: class RegistryMixin(object):
"""
Provides the :attr:`forms` and :attr:`views` registries. Accessing
a member of these registries via the class will return the member,
while accessing via the instance will instantiate the member with
an ``obj`` parameter.
"""
forms = Registry()
views = Registry() Unlike views, form instances are not safe to re-use, so the access protocol will have to change: Model.forms.add_item('main', ModelForm)
Model.views.add_item('main', ModelView)
Model.views.main() # Returns `ModelView`
model_instance.views.main() # Returns `ModelView(obj=model_instance)` There is no cache for instances. The caller is now responsible for this. The somewhat verbose keyword 'add_item' is reserved here and cannot be used as the name of an added item. It is chosen to avoid conflict with typical item names. |
This is a deviation from Pythonic syntax, where the variation between class and instance should be in the Model.views.main # is `ModelView`
Model.views.main() # Returns `ModelView()`
model_instance.views.main() # Returns `ModelView(obj=model_instance)`
model_instance.views.main # Returns a callable that must be called to get the instance |
If we accept this latter syntax, then registering a new item could simply be: Model.views.main = ModelView No reserved keyword like |
@vidya-ram @iambibhas question for you both: how do you anticipate this will be used in templates to access helper methods? This is still a somewhat crummy syntax: {{ obj.views.main().helper() }} Whereas if we place attributes directly on the model, we get: {{ obj.helper() }} |
Resolved in #215. |
It'll be nice to be able to do something like this:
This replaces or complements the However, the Funnel experience suggests this will be problematic. Funnel views work like this: class BaseView(ModelView):
pass # All the views are here
@route('/main/app/path')
class AppView(BaseView):
pass
AppView.init_app(app)
Model.views.main = AppView
@route('/legacy/app/path')
class FunnelView(BaseView):
pass
FunnelView.init_app(app)
Model.views.funnel = FunnelView Both views use the same templates, and the templates use
These methods could be added to |
Closed in #219. |
UrlForMixin
combined withUrlForView
allows a model to be queried for the URL to a view. However, we often need more metadata:Whether the view is available to the current user. At this time the logic for this is embedded into the template, making it difficult to maintain.
A title, icon and category when displaying link buttons (once again hardcoded into the template at present).
Enumeration of available views, or available related views.
Similar to
model.url_for(action)
, we could have amodel.view_for(action)
that returns theViewHandler
from aModelView
affiliated with the model. View handlers have adata
attribute that could be used by the template:{{ model.view_for('edit').data['title'] }}
.If #179 (
requires_roles
) is implemented using theViewDecorator
class, it can also offer anis_available
property similar to that offered byStateTransition
:coaster/coaster/sqlalchemy/statemanager.py
Lines 554 to 559 in 6720965
A limited form of view discovery is available from the
current_view
proxy, which returns the current view class and allows examining its contents. There is a justifiable need for discoveringModelView
classes that are not currently active.The text was updated successfully, but these errors were encountered: