Login

notify admin what fields have changed in form submission

Author:
pjv
Posted:
December 15, 2010
Language:
Python
Version:
1.2
Tags:
fields forms changed
Score:
0 (after 2 ratings)

here is some working code from a site that maintains profile information (address, phone number, etc) for users ("Partners").

this snippet handles the submitted form (after validation) and checks to see if any fields have been changed by the partner. if so, it shoots off an email to the admin showing the current profile information with changed fields marked with asterisks.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
updated_fields = []
changed_fields = form.changed_data
for field_name, field in form.fields.items():
    space = 30 - len(field.label)
    if field_name in changed_fields:
        updated_fields.append("**%s: %s".rjust(space,' ') % (field.label, form.cleaned_data[field_name]))
    else:
        updated_fields.append("%s: %s".rjust(space,' ') % (field.label, form.cleaned_data[field_name]))

# save the updated data
form.save()

# send an email to admin
subject  = "Partner updated profile information"
message  = "Partner: " + partner.__unicode__() + " "
message += "modified information in their profile.\n\n"
message += "Here is the current information (** = changed):\n\n"

for line in updated_fields:
    message += line + '\n'

mail_managers(subject, message)

# message user
request.user.message_set.create(message="Your Profile information has been updated.")

# go back to home page
return HttpResponseRedirect('/reports')

More like this

  1. User post_save signal to auto create 'admin' profile by marinho 7 years, 7 months ago
  2. Custom admin widgets by field type by dgouldin 6 years, 4 months ago
  3. change a widget attribute in ModelForm without define the field by jedie 7 years ago
  4. Combine ProfileForm an UserForm in one. by sarg 5 years, 1 month ago
  5. Selectively change fields, widgets or labels in forms created from models by danjak 8 years, 5 months ago

Comments

Please login first before commenting.