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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.