#852: psycopg.ProgrammingError exceptions leave the connection to the DB in an
unusable state
---------------------------------------+------------------------------------
 Reporter:  [EMAIL PROTECTED]  |        Owner:  adrian 
     Type:  defect                     |       Status:  closed 
 Priority:  normal                     |    Milestone:         
Component:  Database wrapper           |      Version:         
 Severity:  normal                     |   Resolution:  wontfix
 Keywords:                             |  
---------------------------------------+------------------------------------
Comment (by [EMAIL PROTECTED]):

 I just hit this issue, as of today, here is my solution running the latest
 bleeding-edge django
 
 {{{
 #!python
 
 from psycopg2 import IntegrityError
 from django.db import transaction
 
 @transaction.commit_manually
 def add_academic_year(request, info_msg=""):
     info_msg = get_success_msg(request)
 
     if request.method == "POST":
         form = AcademicYearForm(request.POST)
 
         if form.is_valid():
             try:
                 #check if it's a dupe
                 ay = AcademicYear(**form.clean_data)
                 ay.save()
                 transaction.commit()
 
                 request.session["success_msg"] = "Academic year %s added"
 % ay
                 return HttpResponseRedirect("/change/academic_year/%s" %
 ay.id)
             except IntegrityError:
                 transaction.rollback()
                 error_msg = "An academic year has already been defined
 with the same start/end date"
                 context = dict(form=form, error_msg=error_msg)
         else:
             context = dict(form=form, error_msg="Please correct the
 error(s) below")
     else:
         form = AcademicYearForm()
         context = dict(form=form, info_msg=info_msg)
 
     return render_to_response("academic_year.html", context)
 
 #Funnily enough this doesnt need to be handled specially when updating
 data like in the method below
 
 def change_academic_year(request, academic_year_id, info_msg=""):
     try:
         #get the success message and reset it
         info_msg = request.session.get("success_msg")
         request.session["success_msg"] = None
     except KeyError: pass
 
     if request.method == "POST":
         form = AcademicYearForm(request.POST)
 
         if form.is_valid():
             try:
                 ay = AcademicYear(id=request.POST["id"],
 **form.clean_data)
                 ay.save()
 
                 info_msg = "%s updated" % ay
                 context = dict(form=form, info_msg=info_msg,
 academic_year_id=ay.id, show_delete_button=True)
             except IntegrityError:
                 error_msg = "An academic year already exists with the same
 start/end date"
                 context = dict(form=form, error_msg=error_msg,
 academic_year_id=request.POST["academic_year_id"],
 show_delete_button=True)
         else:
             error_msg = "Please correct the error(s) below"
             context = dict(form=form, error_msg=error_msg,
 show_delete_button=True,
 academic_year_id=request.POST["academic_year_id"])
     else:
         ay = AcademicYear.objects.get(pk=academic_year_id)
         form = AcademicYearForm(dict(start_date=ay.start_date,
                                      end_date=ay.end_date,
                                      friendly_name=ay.friendly_name))
 
         context = dict(form=form, academic_year_id=academic_year_id,
 info_msg=info_msg, show_delete_button=True)
 
     return render_to_response("academic_year.html", context)
 
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/852#comment:>
Django <http://code.djangoproject.org/>
The web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to