implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
authornishanth
Thu, 04 Feb 2010 22:27:47 +0530
changeset 18 a39549bd5b08
parent 17 aa45fec40e7e
child 19 c52f7cde9861
implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
taskapp/forms/task.py
taskapp/models.py
taskapp/views/task.py
taskapp/views/user.py
templates/task/create.html
urls.py
--- a/taskapp/forms/task.py	Thu Feb 04 22:21:44 2010 +0530
+++ b/taskapp/forms/task.py	Thu Feb 04 22:27:47 2010 +0530
@@ -0,0 +1,8 @@
+from django import forms
+from pytask.taskapp.models import Task
+
+class TaskCreateForm(forms.ModelForm):
+    class Meta:
+        model = Task
+        fields = ['title', 'desc', 'tags', 'credits']
+    publish = forms.BooleanField(required=False)
--- a/taskapp/models.py	Thu Feb 04 22:21:44 2010 +0530
+++ b/taskapp/models.py	Thu Feb 04 22:27:47 2010 +0530
@@ -9,6 +9,7 @@
 	("CT", "Contributor"),)
 
 STATUS_CHOICES = (
+    ("UP", "Unpublished"),
 	("OP", "Open"),
     ("LO", "Locked"),
 	("CL", "Claimed"),
@@ -47,14 +48,14 @@
     
     title = models.CharField(max_length = 200, unique = True)
     desc = models.TextField()
-    status = models.CharField(max_length = 2, choices = STATUS_CHOICES)
+    status = models.CharField(max_length = 2, choices = STATUS_CHOICES, default = "UP")
     tags = models.CharField(max_length = 200, blank = True)
     
     subs = models.ManyToManyField('self', blank = True, related_name = "%(class)s_parents")
     deps = models.ManyToManyField('self', blank = True, related_name = "%(class)s_deps")
     
     credits = models.PositiveSmallIntegerField()
-    progress = models.PositiveSmallIntegerField()
+    progress = models.PositiveSmallIntegerField(default = 0)
         
     mentors = models.ManyToManyField(User, related_name = "%(class)s_mentors")
     created_by = models.ForeignKey(User, related_name = "%(class)s_created_by")
--- a/taskapp/views/task.py	Thu Feb 04 22:21:44 2010 +0530
+++ b/taskapp/views/task.py	Thu Feb 04 22:27:47 2010 +0530
@@ -2,7 +2,11 @@
 
 from django.http import HttpResponse
 from django.shortcuts import render_to_response, redirect
+
 from pytask.taskapp.models import Task, Comment
+from pytask.taskapp.forms.task import TaskCreateForm
+from pytask.taskapp.events.task import createTask, addMentor, publishTask
+from pytask.taskapp.views.user import show_msg
 
 def browse_tasks(request):
     """ display all the tasks """
@@ -50,3 +54,57 @@
             return render_to_response('task/view.html', context)
     else:
         return render_to_response('task/view.html', context)
+        
+def create_task(request):
+    """ check for rights and create a task if applicable.
+    if user cannot create a task, redirect to homepage.
+    """
+    
+    user = request.user
+    is_guest = True if not user.is_authenticated() else False
+    
+    if not is_guest:
+        user_profile = user.get_profile()
+        can_create_task = False if user_profile.rights == "CT" else True
+        if can_create_task:
+            if request.method == "POST":
+                form = TaskCreateForm(request.POST)
+                if form.is_valid():
+                    data = form.cleaned_data
+                    title = data['title']
+                    desc = data['desc']
+                    credits = data['credits']
+                    publish = data['publish']
+                    task = createTask(title,desc,user,credits)
+                    
+                    if not task:
+                        error_msg = "Another task with the same title exists"
+                        return render_to_response('task/create.html',{'form':form, 'error_msg':error_msg})
+                    
+                    addMentor(task, user)
+                    if publish: publishTask(task)    
+                    task_url = '/task/view/tid=%s'%task.id
+                    return redirect(task_url)
+                else:
+                    return render_to_response('task/create.html',{'form':form})
+            else:
+                form = TaskCreateForm()
+                return render_to_response('task/create.html',{'form':form})
+        else:
+            return show_msg('You are not authorised to create a task.')
+    else:
+        return show_msg('You are not authorised to create a task.')
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
--- a/taskapp/views/user.py	Thu Feb 04 22:21:44 2010 +0530
+++ b/taskapp/views/user.py	Thu Feb 04 22:27:47 2010 +0530
@@ -6,10 +6,10 @@
 from django.contrib.auth import login, logout, authenticate
 from django.contrib.auth.models import User
 
-def redirect_to_homepage(request):
+def show_msg(error_msg):
     """ simply redirect to homepage """
     
-    return redirect('/')
+    return render_to_response('error.html',{'error_msg':error_msg})
 
 def homepage(request):
     """ check for authentication and display accordingly. """
@@ -81,7 +81,7 @@
                 login(request, user)
                 return redirect('/')# Redirect to a success page.
             else:
-                return HttpResponse('username is not active, please contact the administrator')# Return a 'disabled account' error message
+                return show_msg('username is not active, please contact the administrator')# Return a 'disabled account' error message
         else:
             errors = ['Please check your username and password']
             form = LoginForm()
@@ -93,4 +93,4 @@
 
 def user_logout(request):
     logout(request)
-    return HttpResponse('You have logged off successfully!!!')
+    return show_msg('You have logged off successfully!!!')
--- a/templates/task/create.html	Thu Feb 04 22:21:44 2010 +0530
+++ b/templates/task/create.html	Thu Feb 04 22:27:47 2010 +0530
@@ -1,2 +1,10 @@
 {% extends 'base.html' %}
+{% block content %}
+    {% if error_msg %}
+    {{ error_msg }}<br />
+    {% endif %}
+    <form action="" method="post">
+    {{form.as_p}}
+    <input type="submit" value="Submit">
+    </form>
 {% endblock %}
--- a/urls.py	Thu Feb 04 22:21:44 2010 +0530
+++ b/urls.py	Thu Feb 04 22:27:47 2010 +0530
@@ -4,8 +4,8 @@
 from django.contrib import admin
 admin.autodiscover()
 
-from pytask.taskapp.views.user import redirect_to_homepage, homepage, register, user_login, user_logout
-from pytask.taskapp.views.task import browse_tasks, view_task
+from pytask.taskapp.views.user import homepage, register, user_login, user_logout
+from pytask.taskapp.views.task import browse_tasks, view_task, create_task
 
 urlpatterns = patterns('',
     # Example:
@@ -17,7 +17,8 @@
     
     (r'^$', homepage),
     (r'^task/browse/$', browse_tasks),
-    (r'^task/view/tid=(\d+)', view_task),
+    (r'^task/view/tid=(\d+)$', view_task),
+    (r'^task/create/$', create_task),
     
     (r'^admin/', include(admin.site.urls)),