From 68b324096f850aa5514dae27ae86adae657601a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Ar=C4=B1c=C4=B1?= Date: Sat, 7 Aug 2010 13:42:02 +0000 Subject: [PATCH] Refactored querying related forms, templates, views etc.. Added an error issuing def. "issue_error" in views.py. --- .../mustafa_branch/djagen/collector/forms.py | 6 +- .../mustafa_branch/djagen/collector/views.py | 109 ++++++++++-------- .../djagen/templates/main/query.html | 2 +- DJAGEN/branches/mustafa_branch/djagen/urls.py | 2 +- 4 files changed, 64 insertions(+), 55 deletions(-) diff --git a/DJAGEN/branches/mustafa_branch/djagen/collector/forms.py b/DJAGEN/branches/mustafa_branch/djagen/collector/forms.py index 2b914ae..f7faf58 100755 --- a/DJAGEN/branches/mustafa_branch/djagen/collector/forms.py +++ b/DJAGEN/branches/mustafa_branch/djagen/collector/forms.py @@ -19,6 +19,6 @@ class QueryForm(forms.Form): q_author_name = forms.CharField(max_length=25, required = False, label = 'Adı') q_author_surname = forms.CharField(max_length=25, required = False, label = 'Soyadı') q_text = forms.CharField(required = False, label = 'Aradığınız metin', widget = forms.widgets.Textarea() ) - q_date_year = forms.DateField(input_formats = '%y',required = False, label = 'Blog girdisine ait yıl(Örn:2010)', widget=forms.widgets.DateTimeInput()) - q_date_month = forms.DateField(input_formats = '%m',required = False, label = 'Blog girdisine ait ay(Örn:03)', widget=forms.widgets.DateTimeInput()) - q_date_day = forms.DateField(input_formats = '%d',required = False, label = 'Blog girdisine ait gün (Örn:27)', widget=forms.widgets.DateTimeInput()) + q_date_year = forms.IntegerField(required = False, label = 'Blog girdisine ait yıl(Örn:2010)', widget=forms.widgets.DateTimeInput()) + q_date_month = forms.IntegerField(required = False, label = 'Blog girdisine ait ay(Örn:03)', widget=forms.widgets.DateTimeInput()) + q_date_day = forms.IntegerField(required = False, label = 'Blog girdisine ait gün (Örn:27)', widget=forms.widgets.DateTimeInput()) diff --git a/DJAGEN/branches/mustafa_branch/djagen/collector/views.py b/DJAGEN/branches/mustafa_branch/djagen/collector/views.py index b08b5af..95e42da 100755 --- a/DJAGEN/branches/mustafa_branch/djagen/collector/views.py +++ b/DJAGEN/branches/mustafa_branch/djagen/collector/views.py @@ -114,47 +114,51 @@ def list_members(request): authors = Authors.objects.all() return render_response(request, 'main/members.html', {'members': authors, 'BASE_URL': BASE_URL,'info_area' : info_area }) -#############################Under Construction###################### + def query(request): - return (HttpResponse("query page")) -# if(request.method == 'GET'): -# form = QueryForm(request.GET) -# if form.is_valid(): -# if(('q_author_name' in request.GET) or -# ('q_author_surname' in request.GET) or -# ('q_text' in request.GET) or -# ('q_date_year' in request.GET) or -# ('q_date_month' in request.GET) or -# ('q_date_day' in request.GET)): -# redirect_url = BASE_URL + "/archive" -# if ('q_date_year' in request.GET and request.GET['q_date_year']): -# redirect_url += "/" + request.GET['q_date_year'] -# if ('q_date_month' in request.GET and request.GET['q_date_month']): -# redirect_url += "/" + request.GET['q_date_month'] -# if('q_date_day' in request.GET and request.GET['q_date_day']): -# redirect_url += "/"+str(request.GET['q_date_day']) -# return HttpResponseRedirect(redirect_url) -# else: -# q_form = QueryForm() -# return render_response(request,'main/query.html',{ -# 'BASE_URL' : BASE_URL, -# 'q_form':q_form, -# }) -# else: -# q_form = QueryForm() -# return render_response(request,'main/query.html',{ -# 'BASE_URL' : BASE_URL, -# 'q_form':q_form, -# }) -# -# -# else: -# -# q_form = QueryForm() -# return render_response(request,'main/query.html',{ -# 'BASE_URL' : BASE_URL, -# 'q_form':q_form, -# }) + # Determine if method is POST. + if (request.method == 'POST'): + ## If Yes: + form = QueryForm(request.POST) + + # Determine if all of them were valid. + if (form.is_valid()): + + ## If Yes: + + q_author_name = request.POST['q_author_name'] + q_author_surname = request.POST['q_author_surname'] + q_text = request.POST['q_text'] + q_date_year = request.POST['q_date_year'] + q_date_month = request.POST['q_date_month'] + q_date_day = request.POST['q_date_day'] + # Redirect or call /archive/ view with the existing GET arguments. + #++ Complex string operations in order to form needed url. + args_part = "?q_author_name=%s&q_author_surname=%s&q_text=%s" % (q_author_name,q_author_surname,q_text) + date_part = '' + if (q_date_year): + date_part = q_date_year + if(q_date_month): + date_part += "/" + q_date_month + if(q_date_day): + date_part += "/" + q_date_day + "/" + target_url = BASE_URL+"/archive/" + date_part + args_part + #-- + return HttpResponseRedirect(target_url) + else: + # Issue an error message and show the form again. + form = QueryForm() + info_area = "query" + + return render_to_response('main/query.html', {'q_form': form, 'BASE_URL': BASE_URL,'info_area':info_area}) + else: + # Show the form. + + form = QueryForm() + info_area = "query" + + return render_to_response('main/query.html', {'q_form': form, 'BASE_URL': BASE_URL,'info_area':info_area}) + def archive(request,archive_year=None,archive_month=None,archive_day=None): info_area = 'query' @@ -170,28 +174,28 @@ def archive(request,archive_year=None,archive_month=None,archive_day=None): if ('q_author_name' in request.GET and request.GET['q_author_name']): #If that exists and not empty q_author_name = request.GET['q_author_name'] else: - q_author_name = None + q_author_name = "" if ('q_author_surname' in request.GET and request.GET['q_author_surname']): #If that exists and not empty q_author_surname = request.GET['q_author_surname'] else: - q_author_surname = None + q_author_surname = "" if ('q_text' in request.GET and request.GET['q_text']): #If that exists and not empty q_text = request.GET['q_text'] else: - q_text = None + q_text = "" if ('q_label_personal' in request.GET and request.GET['q_label_personal'] == '1'): #If that exists and not empty q_label_personal = request.GET['q_label_personal'] else: - q_label_personal = None + q_label_personal = "" if ('q_label_community' in request.GET and request.GET['q_label_community'] == '1'): q_label_community = request.GET['q_label_community'] else: - q_label_community = None + q_label_community = "" if ('q_label_lkd' in request.GET and request.GET['q_label_lkd']=='1'): q_label_lkd = request.GET['q_label_lkd'] else: - q_label_lkd = None + q_label_lkd = "" #-- #-- @@ -206,7 +210,7 @@ def archive(request,archive_year=None,archive_month=None,archive_day=None): elif(q_author_surname): entries_list = entries_list.filter(entry_id__author_surname__iexact = q_author_surname) - + # Label based queries. if(q_label_personal): entries_list = entries_list.filter(entry_id__label_personal = 1) if(q_label_community): @@ -215,10 +219,10 @@ def archive(request,archive_year=None,archive_month=None,archive_day=None): entries_list = entries_list.filter(entry_id__label_lkd = 1) - + # Text search. if(q_text): - entries_list = entries.list.filter(content_text__icontains = q_text) - + entries_list = entries_list.filter(content_text__icontains = q_text) + # Date based queries. if(archive_year): entries_list = entries_list.filter(date__year = archive_year) if(archive_month): @@ -257,3 +261,8 @@ def archive(request,archive_year=None,archive_month=None,archive_day=None): 'BASE_URL':BASE_URL, }) +def issue_error(request,error_msg=None): + info_area = 'error' + if not(error_msg): + error_msg = 'Bilinmeyen bir hata oluştu!' + return render_response(request, 'main/error.html', {'error_msg': error_msg,'info_area' : info_area }) \ No newline at end of file diff --git a/DJAGEN/branches/mustafa_branch/djagen/templates/main/query.html b/DJAGEN/branches/mustafa_branch/djagen/templates/main/query.html index fa78223..3e96059 100755 --- a/DJAGEN/branches/mustafa_branch/djagen/templates/main/query.html +++ b/DJAGEN/branches/mustafa_branch/djagen/templates/main/query.html @@ -11,7 +11,7 @@ {% endcomment %} -
+ {% for field in q_form %}
{% if field.errors %} diff --git a/DJAGEN/branches/mustafa_branch/djagen/urls.py b/DJAGEN/branches/mustafa_branch/djagen/urls.py index eeb43e9..f8a66d8 100755 --- a/DJAGEN/branches/mustafa_branch/djagen/urls.py +++ b/DJAGEN/branches/mustafa_branch/djagen/urls.py @@ -22,7 +22,7 @@ urlpatterns = patterns('', (r'^archive/$',archive), (r'^archive/(?P\d{4})/$', archive), (r'^archive/(?P\d{4})/(?P\d{1,2})/$', archive), - (r'^archive/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})$', archive), + (r'^archive/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/$', archive), (r'^djagen/$',main), (r'^query/$',query), )