Tweak form handling for sending a new message, for more customization

This commit is contained in:
Lance Edgar 2017-02-13 17:37:16 -06:00
parent 70e5915e51
commit 051b52bd4b
4 changed files with 46 additions and 22 deletions

View file

@ -3,8 +3,12 @@
<%def name="title()">New ${model_title}</%def> <%def name="title()">New ${model_title}</%def>
<%def name="head_tags()"> <%def name="extra_javascript()">
${parent.head_tags()} ${parent.extra_javascript()}
${self.disable_button_js()}
</%def>
<%def name="disable_button_js()">
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {

View file

@ -13,6 +13,25 @@
% endfor % endfor
]); ]);
// validate message before sending
function validate_message_form() {
var form = $('#new-message');
if (! form.find('input[name="Message--recipients"]').val()) {
alert("You must specify some recipient(s) for the message.");
$('.recipients input').data('ui-tagit').tagInput.focus();
return false;
}
if (! form.find('input[name="Message--subject"]').val()) {
alert("You must provide a subject for the message.");
form.find('input[name="Message--subject"]').focus();
return false;
}
return true;
}
$(function() { $(function() {
var recipients = $('.recipients input'); var recipients = $('.recipients input');
@ -57,26 +76,19 @@
// set focus to recipients field // set focus to recipients field
recipients.data('ui-tagit').tagInput.focus(); recipients.data('ui-tagit').tagInput.focus();
});
// validate message before sending </script>
${self.validate_message_js()}
</%def>
<%def name="validate_message_js()">
<script type="text/javascript">
$(function() {
$('#new-message').submit(function() { $('#new-message').submit(function() {
var form = $(this); return validate_message_form();
if (! form.find('input[name="Message--recipients"]').val()) {
alert("You must specify some recipient(s) for the message.");
recipients.data('ui-tagit').tagInput.focus();
return false;
}
if (! form.find('input[name="Message--subject"]').val()) {
alert("You must provide a subject for the message.");
form.find('input[name="Message--subject"]').focus();
return false;
}
}); });
}); });
</script> </script>
</%def> </%def>

View file

@ -132,11 +132,14 @@ class MasterView(View):
# let save_create_form() return alternate object if necessary # let save_create_form() return alternate object if necessary
obj = self.save_create_form(form) or form.fieldset.model obj = self.save_create_form(form) or form.fieldset.model
self.after_create(obj) self.after_create(obj)
self.request.session.flash("{} has been created: {}".format( self.flash_after_create(obj)
self.get_model_title(), self.get_instance_title(obj)))
return self.redirect_after_create(obj) return self.redirect_after_create(obj)
return self.render_to_response('create', {'form': form}) return self.render_to_response('create', {'form': form})
def flash_after_create(self, obj):
self.request.session.flash("{} has been created: {}".format(
self.get_model_title(), self.get_instance_title(obj)))
def save_create_form(self, form): def save_create_form(self, form):
self.before_create(form) self.before_create(form)
form.save() form.save()

View file

@ -209,6 +209,7 @@ class MessagesView(MasterView):
if self.creating: if self.creating:
form.id = 'new-message' form.id = 'new-message'
form.cancel_url = self.request.get_referrer(default=self.request.route_url('messages.inbox')) form.cancel_url = self.request.get_referrer(default=self.request.route_url('messages.inbox'))
form.create_label = "Send Message"
return form return form
def configure_fieldset(self, fs): def configure_fieldset(self, fs):
@ -288,6 +289,10 @@ class MessagesView(MasterView):
message = form.fieldset.model message = form.fieldset.model
message.sender = self.request.user message.sender = self.request.user
def flash_after_create(self, obj):
self.request.session.flash("Message has been sent: {}".format(
self.get_instance_title(obj)))
def filter_reply_recipient(self, user): def filter_reply_recipient(self, user):
return user.active return user.active