Login

notify admin what fields have changed in form submission

Author:
pjv
Posted:
December 15, 2010
Language:
Python
Version:
1.2
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. codigo alto nivel by MrRocklion 1 month ago
  2. Load template from specific app by Krzysiek555 1 month, 3 weeks ago
  3. PostgreSQL JSON subqueries by dolamroth 1 month, 3 weeks ago
  4. "Magic Link" Management Command by webology 7 months ago
  5. Closest ORM models to a latitude/longitude point by simonw 7 months ago

Comments

Please login first before commenting.