Refactor how sender is assigned for new messages, to avoid bogus commits.

Man I really wish I could find the right pattern for default values with
FormAlchemy...
This commit is contained in:
Lance Edgar 2016-02-11 18:47:39 -06:00
parent 7649bf142b
commit 1dd5b11414

View file

@ -189,23 +189,44 @@ class MessagesView(MasterView):
return form return form
def configure_fieldset(self, fs): def configure_fieldset(self, fs):
fs.sender.set(label="From") # TODO: A fair amount of this still seems hacky...
if self.creating: if self.creating:
# Must create a new 'sender' field so that we can feed it the
# current user as default value, but prevent attaching user to the
# (new) underlying message instance...ugh
fs.append(formalchemy.Field('sender', value=self.request.user,
renderer=forms.renderers.UserFieldRenderer,
label="From", readonly=True))
# Sort of the same thing for recipients, although most of that logic is below.
fs.append(RecipientsField('recipients', label="To", renderer=RecipientsFieldRenderer)) fs.append(RecipientsField('recipients', label="To", renderer=RecipientsFieldRenderer))
fs.configure(include=[ fs.configure(include=[
fs.sender.with_renderer(forms.renderers.UserFieldRenderer).readonly(), fs.sender,
fs.recipients, fs.recipients,
fs.subject, fs.subject,
fs.body.textarea(size='50x15'), fs.body.textarea(size='50x15'),
]) ])
# We'll assign some properties directly on the new message;
# apparently that's safe and won't cause it to be committed.
# Notably, we can't assign the sender yet. Also the actual
# recipients assignment is handled by that field's sync().
message = fs.model message = fs.model
message.sender = self.request.user
if self.replying: if self.replying:
old_message = self.get_instance() old_message = self.get_instance()
message.subject = "Re: {}".format(old_message.subject) message.subject = "Re: {}".format(old_message.subject)
message.body = self.get_reply_body(old_message) message.body = self.get_reply_body(old_message)
# Determine an initial set of recipients, based on reply
# method. This value will be set to a 'pseudo' field to avoid
# touching the new model instance and causing a crap commit.
# If replying to all, massage the list a little so that the
# current user is not listed, and the sender is listed first.
if self.replying == 'all': if self.replying == 'all':
value = [(r.recipient.uuid, r.recipient.person.display_name) value = [(r.recipient.uuid, r.recipient.person.display_name)
for r in old_message.recipients for r in old_message.recipients
@ -217,18 +238,33 @@ class MessagesView(MasterView):
if old_message.sender is not self.request.user and old_message.sender.active: if old_message.sender is not self.request.user and old_message.sender.active:
value.insert(0, old_message.sender_uuid) value.insert(0, old_message.sender_uuid)
fs.recipients.set(value=value) fs.recipients.set(value=value)
# Just a normal reply, to sender only.
elif self.filter_reply_recipient(old_message.sender): elif self.filter_reply_recipient(old_message.sender):
fs.recipients.set(value=[old_message.sender.uuid]) fs.recipients.set(value=[old_message.sender.uuid])
# Set focus to message body instead of recipients, when replying.
fs.focus = fs.body fs.focus = fs.body
elif self.viewing: elif self.viewing:
# Viewing an existing message is a heck of a lot easier...
fs.configure(include=[ fs.configure(include=[
fs.sender.with_renderer(SenderFieldRenderer), fs.sender.with_renderer(SenderFieldRenderer).label("From"),
fs.recipients.with_renderer(RecipientsFieldRenderer).label("To"), fs.recipients.with_renderer(RecipientsFieldRenderer).label("To"),
fs.sent.with_renderer(forms.renderers.DateTimeFieldRenderer(self.rattail_config)), fs.sent.with_renderer(forms.renderers.DateTimeFieldRenderer(self.rattail_config)),
fs.subject, fs.subject,
]) ])
def before_create(self, form):
"""
This is where we must assign the current user as sender for new
messages, for now. I'm still not quite happy with this...
"""
super(MessagesView, self).before_create(form)
message = form.fieldset.model
message.sender = self.request.user
def filter_reply_recipient(self, user): def filter_reply_recipient(self, user):
return user.active return user.active