Those demos demonstrate ability of Flask-Starter to easily show same content inside both modal and regular windows.
Also, chain loading, submitting of forms from modal windows with return to page inside modal or normal redirect.
Everything easily configured on View-level and by html classes.
This is only brief overview of RemoteModal and Requests JS modules capabilities. But for beginners the structure of project itself should be more interesting.
Lorem ipsum demo
Separate pageModal page
{%code "html+jinja" %}
{%- raw %}
Separate pageModal page
{%- endraw %}
{%- endcode -%}
{%code "python" %}
{%- raw %}
class LoremIpsum(MainPage):
page_header = "Lorem Ipsum"
template = "demos/lorem_ipsum.html"
{%- endraw %}
{%- endcode -%}
Lorem ipsum, login required
Separate pageModal page
{%code "html+jinja" %}
{%- raw %}
Separate pageModal page
{%- endraw %}
{%- endcode -%}
{%code "python" %}
{%- raw %}
class LoremIpsumLoginRequired(MainPage):
login_required = True
page_header = "Lorem Ipsum, Login Required"
template = "demos/lorem_ipsum.html"
{%- endraw %}
{%- endcode -%}
action class forces request to link to be made with POST method with CSRF key
{%code "html+jinja" %}
{%- raw %}
1) POST it with traversing page
{%- endraw %}
{%- endcode -%}
{%code "python" %}
{%- raw %}
class ActionRequiresPost(MainPage):
track = False #no need to track such actions
page_header = "Action submitted"
def get(self,*k,**kk):
#if came by regular link -> just redirect to current page
return redirect(self.tracker.current)
@action #forces csrf check
def post(self,*k,**kk):
if self.method=='modal':
return render_template("demos/action_submitted.html",**self.context)
else:
self.context['page_header']="Submitted"
return redirect(url_for('main.action_submitted'))
{%- endraw %}
{%- endcode -%}
2) POST it in background with modal
action-remote class sends request by AJAX and expect response, also sets method to "modal", so same View actualy used
Same View called two times, one time this view returns confirmation URL and if user confirms it View called again, but this time confirmation verification passes.
Yet again, control on server side, just action and action-remote in html.
Also take a note, in Views/Pages that not designated to be viewed by regular GET request it is better to specify data-href and not href, so crawlers wont follow it and user also wont follow if some error occured.
But in views like Lorem ipsum it is better to leave HREF, so bots will follow it and see full page, while user sees modal. Same link, altering on-click.
{%code "html+jinja" %}
{%- raw %}
DeleteRoll Dice!
{%- endraw %}
{%- endcode -%}
{%code "python" %}
{%- raw %}
class ActionWithConfirmation(MainPage):
get=None
@action
def post(self,*k,**kk):
if request.args.get('confirmed'):
return response_ok(message="Item deleted")
else:
return response_request_confirmation(
message="Are you sure?",
confirmation_url=url_for('.action_with_confirmation',confirmed=1)
)
class ActionWithConfirmationCreate(MainPage):
template = "demos/created_item_description.html"
page_header = "Done!"
get=None
@action
def post(self,*k,**kk):
if request.args.get('confirmed'):
return render_template(self.template,**self.context)
else:
return response_request_confirmation(
message="Are you sure? You got only one chance!",
confirmation_url=url_for('.action_with_confirmation_create',confirmed=1,method=self.method)
)
{%- endraw %}
{%- endcode -%}
Another popular issue is links inside modal windows. Some links should be opened in same modal, some in new window, some should redirect to another page.
Everything is possible by simple classes!
By default links are opened in modal and forms submitted to modal. While .direct_link, .popup_window can give desired behaviour.
Just redefine view_level_permissions in your View class, attach User object to self.item_user and it will be compared with current_user automaticaly
{%code "python" %}
{%- raw %}
class PageWithItemUser(MainPage):
template = "demos/page_with_item_user.html"
page_header = "By Tigra for Tigra"
def view_level_permissions(self,*k,**kk):
self.item_user=User("Tigra")
return True
#in Real world however, something like this usualy done
def view_level_permissions(self,item=item_id,*k,**kk):
self.__item=Item.get(item_id)
if self.__item is None:
return False
self.item_user=User.get(self.__item.user_id)
return True
{%- endraw %}
{%- endcode -%}
And what about callbacks?
Also easy. Just attach callback to the action. Alternative way will be to return your JS {{ ' inside modal HTML