Restructured create form to use templatetags and use Javascript for autcomplete.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Sun, 16 Jan 2011 17:04:13 +0530
changeset 425 e15749fab2e4
parent 424 5ed0362bd674
child 426 7b009bbd2bfc
Restructured create form to use templatetags and use Javascript for autcomplete.
pytask/taskapp/urls.py
pytask/taskapp/views.py
pytask/templates/base.html
pytask/templates/task/create.html
pytask/templates/task/create_textbook.html
pytask/templates/task/edit.html
--- a/pytask/taskapp/urls.py	Sun Jan 16 17:02:02 2011 +0530
+++ b/pytask/taskapp/urls.py	Sun Jan 16 17:04:13 2011 +0530
@@ -20,6 +20,7 @@
   url(r'^submit/report/(?P<task_id>\d+)$', 'submit_report',
       name='submit_report'),
   url(r'^browse/$', 'browse_tasks', name='browse_tasks'),
+  url(r'^suggest_tags/$', 'suggest_task_tags', name='suggest_task_tags'),
 )
 
 # URL patterns specific to textbook projects.
--- a/pytask/taskapp/views.py	Sun Jan 16 17:02:02 2011 +0530
+++ b/pytask/taskapp/views.py	Sun Jan 16 17:04:13 2011 +0530
@@ -1,11 +1,12 @@
 from datetime import datetime
 
 from django import shortcuts
-from django.http import Http404
+from django import http
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.models import User
 from django.core.context_processors import csrf
 from django.core.urlresolvers import reverse
+from django.utils import simplejson as json
 
 from pytask.views import show_msg
 
@@ -46,7 +47,7 @@
                   'task/create.html', context)
         else:
             form = taskapp_forms.CreateTaskForm()
-            context.update({'form':form})
+            context.update({'form': form})
             return shortcuts.render_to_response('task/create.html', context)
     else:
         return show_msg(user, 'You are not authorised to create a task.')
@@ -176,7 +177,7 @@
     is_creator = True if user == task.created_by else False
     can_edit = True if task.status == "UP" and is_creator else False
     if not can_edit:
-        raise Http404
+        raise http.Http404
 
     context = {"user": user,
                "profile": profile,
@@ -207,7 +208,7 @@
     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
 
     if profile.rights not in ["MG", "DC"] or task.status != "UP":
-        raise Http404
+        raise http.Http404
 
     context = {"user": user,
                "profile": profile,
@@ -225,7 +226,7 @@
     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
 
     if profile.rights not in ["MG", "DC"] or task.status != "UP":
-        raise Http404
+        raise http.Http404
 
     task.approved_by = user
     task.approval_datetime = datetime.now()
@@ -250,7 +251,7 @@
 
     can_mod_reviewers = True if profile.rights in ["MG", "DC"] else False
     if not can_mod_reviewers:
-        raise Http404
+        raise http.Http404
 
     context = {"user": user,
                "profile": profile,
@@ -319,10 +320,8 @@
 @login_required
 def view_report(request, report_id):
 
-    try:
-        report = taskapp_models.WorkReport.objects.get(pk=report_id)
-    except taskapp_models.WorkReport.DoesNotExist:
-        raise Http404
+    report = shortcuts.get_object_or_404(taskapp_models.WorkReport,
+                                         pk=report_id)
 
     user = request.user
     context = {"report": report,
@@ -348,7 +347,7 @@
     old_reports = task.reports.all()
 
     if not task.status == "WR":
-        raise Http404
+        raise http.Http404
 
     can_upload = True if user in task.selected_users.all() else False
 
@@ -396,7 +395,7 @@
 
     can_create = True if profile.rights != "CT" else False
     if not can_create:
-        raise Http404
+        raise http.Http404
 
     context = {"user": user,
                "profile": profile,
@@ -494,7 +493,7 @@
                        else False
 
     if not can_edit:
-        raise Http404
+        raise http.Http404
 
     context = {"user": user,
                "profile": profile,
@@ -523,7 +522,7 @@
     task = shortcuts.get_object_or_404(taskapp_models.Task, pk=task_id)
 
     if task.status == "UP":
-        raise Http404
+        raise http.Http404
 
     user = request.user
     profile = user.get_profile()
@@ -631,7 +630,7 @@
             return show_msg(user, 'There are no pending claims for this task',
                             task_url, 'view the task')
     else:
-        raise Http404
+        raise http.Http404
 
 @login_required
 def approve_textbook(request, task_id):
@@ -642,7 +641,7 @@
     textbook = shortcuts.get_object_or_404(taskapp_models.TextBook, pk=task_id)
 
     if profile.rights not in ["MG", "DC"] or textbook.status != "UP":
-        raise Http404
+        raise http.Http404
 
     context = {"user": user,
                "profile": profile,
@@ -660,7 +659,7 @@
     textbook = shortcuts.get_object_or_404(taskapp_models.TextBook, pk=task_id)
 
     if profile.rights not in ["MG", "DC"] or textbook.status != "UP":
-        raise Http404
+        raise http.Http404
 
     textbook.approved_by = user
     textbook.approval_datetime = datetime.now()
@@ -673,3 +672,12 @@
               }
 
     return shortcuts.render_to_response("task/approved_textbook.html", context)
+
+def suggest_task_tags(request):
+    """Returns the tags matching the query for the AJAXy autocomplete
+    to get tags related to tasks.
+    """
+
+    taskapp_models.Task.objects.filter(tags)
+    json_response = json.dumps(['abc', 'bca', 'bowbow'])
+    return http.HttpResponse(json_response)
--- a/pytask/templates/base.html	Sun Jan 16 17:02:02 2011 +0530
+++ b/pytask/templates/base.html	Sun Jan 16 17:04:13 2011 +0530
@@ -1,8 +1,25 @@
 <html>
 <head>
     <title>{% block title %}PyTasks{% endblock %}</title>
-    {% block js_script %} {% endblock %}
+    {% comment %}Load site wide CSS here. Additional styling must be
+    loaded in the block that follows{% endcomment %}
     <link rel=stylesheet href="/pytask/static/css/base.css" type="text/css" >
+    <link type="text/css"
+        href="/pytask/static/css/jquery/ui/cupertino/jquery-ui-1.8.8.custom.css"
+        rel="stylesheet" /> 
+    {% block css %}
+    {% endblock %}
+
+    {% comment %}Load site wide javascript here. Additional scripts must
+    be loaded in the block that follows{% endcomment %}
+    <script type="text/javascript"
+        src="/pytask/static/jquery/jquery-1.4.4.min.js">
+    </script>
+    <script type="text/javascript"
+        src="/pytask/static/jquery/jquery-ui-1.8.8.custom.min.js">
+    </script>
+    {% block js %}
+    {% endblock %}
 </head>
 {% load user_tags %}
 <body>
--- a/pytask/templates/task/create.html	Sun Jan 16 17:02:02 2011 +0530
+++ b/pytask/templates/task/create.html	Sun Jan 16 17:04:13 2011 +0530
@@ -1,8 +1,22 @@
-{% extends 'base.html' %}
+{% extends "base.html" %}
+{% load form_helpers %}
+{% block css %}
+
+  <style>
+    .ui-autocomplete-loading {
+      background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
+    }
+  </style>
+{% endblock %}
+{% block js %}
+    <script type="text/javascript"
+        src="/pytask/static/js/create_task_form.js">
+    </script>
+    <script type="text/javascript">
+      create_form("{% url suggest_task_tags %}");
+    </script>
+{% endblock %}
+
 {% block content %}
-<form action="" method="post">
-{% csrf_token %}
-{{form.as_p}}
-<input type="submit" value="Create">
-</form>
+  {% as_div_form form "Create Task Form" csrf_token "" "Create" %}
 {% endblock %}
--- a/pytask/templates/task/create_textbook.html	Sun Jan 16 17:02:02 2011 +0530
+++ b/pytask/templates/task/create_textbook.html	Sun Jan 16 17:04:13 2011 +0530
@@ -1,8 +1,10 @@
 {% extends 'base.html' %}
+{% load form_helpers %}
+
 {% block content %}
-<form action="" method="post">
-{% csrf_token %}
-{{form.as_p}}
-<input type="submit" value="Create">
-</form>
+  <form action="" method="post">
+    {% csrf_token %}
+    {% as_div_form form %}
+    <input type="submit" value="Create">
+  </form>
 {% endblock %}
--- a/pytask/templates/task/edit.html	Sun Jan 16 17:02:02 2011 +0530
+++ b/pytask/templates/task/edit.html	Sun Jan 16 17:04:13 2011 +0530
@@ -1,8 +1,10 @@
 {% extends 'base.html' %}
+{% load form_helpers %}
+
 {% block content %}
-    <form action="" method="post">
+  <form action="" method="post">
     {% csrf_token %}
-    {{form.as_p}}
+    {% as_div_form form %}
     <input type="submit" value="Update">
-    </form>
+  </form>
 {% endblock %}