Want more features on Pastebin? Sign Up, it's FREE!
Guest

Zeynel

By: a guest on Jan 22nd, 2010  |  syntax: Python  |  size: 4.94 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def search(request):
  2.     if request.method == 'GET':
  3.         form = SearchForm(request.GET)
  4.         if form.is_valid():
  5.             last_name = form.cleaned_data['last_name']
  6.             first_name = form.cleaned_data['first_name']
  7.             lawyers = Lawyer.objects.all()
  8.             if last_name and not first_name:
  9.                 lawyers = lawyers.filter(last__iexact=last_name)
  10.                 if len(lawyers)==0:
  11.                     form = SearchForm()    
  12.                     return render_to_response('last_only_no_match.html', {'last': last_name, 'form': form})
  13.                 if len(lawyers)==1:
  14.                     form = SearchForm()
  15.                     q_first_name = lawyers.values_list('first', flat=True)
  16.                     q_school = lawyers.values_list('school', flat=True)
  17.                     q_firm_url = lawyers.values_list('firm_url', flat=True)
  18.                     q_firm_name = lawyers.values_list('firm_name', flat=True)
  19.                     q_year_graduated = lawyers.values_list('year_graduated', flat=True)
  20.                     lawyers1 = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year_graduated[0]).exclude(last__icontains=last_name)
  21.                     return render_to_response('last_only_1_match.html', {'lawyers': lawyers, 'lawyers1': lawyers1, 'last': last_name, 'first': q_first_name[0], 'school': q_school[0], 'firm': q_firm_name[0], 'firm_url': q_firm_url[0], 'class_of': q_year_graduated[0], 'form': form})
  22.                 if len(lawyers)>1:
  23.                     form = SearchForm(initial={'last_name': last_name})
  24.                     return render_to_response('last_only_1_plus_match.html', {'lawyers': lawyers, 'last': last_name, 'form': form})
  25.             if last_name and first_name:
  26.                 lawyers = lawyers.filter(last__iexact=last_name).filter(first__icontains=first_name)
  27.                 if len(lawyers)==0:
  28.                     form = SearchForm()
  29.                     return render_to_response('first_and_last_no_match.html', {'first': first_name, 'last': last_name, 'form': form})
  30.                 if len(lawyers)==1:
  31.                     form = SearchForm()
  32.                     q_first_name = lawyers.values_list('first', flat=True)
  33.                     q_school = lawyers.values_list('school', flat=True)
  34.                     q_firm_url = lawyers.values_list('firm_url', flat=True)
  35.                     q_firm_name = lawyers.values_list('firm_name', flat=True)
  36.                     q_year_graduated = lawyers.values_list('year_graduated', flat=True)
  37.                     lawyers1 = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year_graduated[0]).exclude(last__icontains=last_name)
  38.                     return render_to_response('first_and_last_1_match.html', {'lawyers': lawyers, 'lawyers1': lawyers1, 'last': last_name, 'first': q_first_name[0], 'school': q_school[0], 'firm': q_firm_name[0], 'firm_url': q_firm_url[0], 'class_of': q_year_graduated[0], 'form': form})
  39.                 if len(lawyers)>1:
  40.                     form = SearchForm(initial={'last_name': last_name})
  41.                     return render_to_response('first_and_last_1_plus_match.html', {'lawyers': lawyers, 'first': first_name, 'last': last_name, 'form': form})
  42.             if first_name and not last_name:
  43.                 lawyers = lawyers.filter(first__iexact=first_name)
  44.                 if len(lawyers)==0:
  45.                     form = SearchForm()
  46.                     return render_to_response('first_only_no_match.html', {'first': first_name, 'last': last_name, 'form': form})
  47.                 if len(lawyers)==1:
  48.                     form = SearchForm()
  49.                     q_first_name = lawyers.values_list('first', flat=True)
  50.                     q_school = lawyers.values_list('school', flat=True)
  51.                     q_firm_url = lawyers.values_list('firm_url', flat=True)
  52.                     q_firm_name = lawyers.values_list('firm_name', flat=True)
  53.                     q_year_graduated = lawyers.values_list('year_graduated', flat=True)
  54.                     lawyers1 = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year_graduated[0]).exclude(last__icontains=last_name)
  55.                     return render_to_response('first_only_1_match.html', {'lawyers': lawyers, 'lawyers1': lawyers1, 'last': last_name, 'first': q_first_name[0], 'school': q_school[0], 'firm': q_firm_name[0], 'firm_url': q_firm_url[0], 'class_of': q_year_graduated[0], 'form': form})
  56.                 if len(lawyers)>1:
  57.                     form = SearchForm(initial={'first_name': first_name})
  58.                     return render_to_response('first_only_1_plus_match.html', {'lawyers': lawyers, 'first': first_name, 'last': last_name, 'form': form})        
  59.         form = SearchForm()
  60.         return render_to_response('search.html', {'form': form})            
  61. #    else:
  62. #        form = SearchForm()
  63. #        return render_to_response('search.html', {'form': form})
clone this paste RAW Paste Data