submit report works fine
authorNishanth Amuluru <nishanth@fossee.in>
Tue, 11 Jan 2011 17:40:14 +0530
changeset 383 4252da60a4ef
parent 382 daca865314e7
child 384 5bc5dc28fbde
submit report works fine
pytask/taskapp/forms.py
pytask/taskapp/models.py
pytask/taskapp/urls.py
pytask/taskapp/views.py
pytask/templates/task/submit_report.html
pytask/templates/task/view_report.html
--- a/pytask/taskapp/forms.py	Tue Jan 11 16:51:07 2011 +0530
+++ b/pytask/taskapp/forms.py	Tue Jan 11 17:40:14 2011 +0530
@@ -130,5 +130,5 @@
 
     class Meta:
         model = WorkReport
-        fields = ['remarks', 'attachment']
+        fields = ['data', 'summary', 'attachment']
 
--- a/pytask/taskapp/models.py	Tue Jan 11 16:51:07 2011 +0530
+++ b/pytask/taskapp/models.py	Tue Jan 11 17:40:14 2011 +0530
@@ -91,7 +91,7 @@
     approved_by = models.ForeignKey(User, null = True, blank = True,
                                     related_name = "approved_reports")
 
-    data = models.TextField()
+    data = models.TextField(verbose_name="Report")
     summary = models.CharField(max_length=100, verbose_name="Summary",
                                help_text="A one line summary")
     attachment = models.FileField(upload_to = UPLOADS_DIR)
--- a/pytask/taskapp/urls.py	Tue Jan 11 16:51:07 2011 +0530
+++ b/pytask/taskapp/urls.py	Tue Jan 11 17:40:14 2011 +0530
@@ -4,7 +4,7 @@
         select_user, edit_task, create_textbook, view_textbook, \
         browse_tasks, edit_textbook, approve_task, approved_task,\
         browse_textbooks, approve_textbook, approved_textbook, addreviewer,\
-        view_report, view_work
+        view_report, view_work, submit_report
 
 from pytask.views import under_construction
 
@@ -20,6 +20,7 @@
             (r'^addreviewer/tid=(\w+)$', addreviewer),
             (r'^view/work/tid=(\w+)$', view_work),
             (r'^view/report/rid=(\w+)$', view_report),
+            (r'^submit/report/tid=(\w+)$', submit_report),
             (r'^browse/$', browse_tasks),
 
             (r'^textbook/create/$', create_textbook),
--- a/pytask/taskapp/views.py	Tue Jan 11 16:51:07 2011 +0530
+++ b/pytask/taskapp/views.py	Tue Jan 11 17:40:14 2011 +0530
@@ -12,11 +12,12 @@
 from pytask.utils import make_key
 from pytask.views import show_msg
 
-from pytask.taskapp.models import Task, TaskComment, TaskClaim, TextBook
+from pytask.taskapp.models import Task, TaskComment, TaskClaim, TextBook, \
+        WorkReport
 from pytask.taskapp.forms import CreateTaskForm, EditTaskForm, \
                                  TaskCommentForm, ClaimTaskForm, \
                                  ChoiceForm, EditTaskForm, CreateTextbookForm,\
-                                 EditTextbookForm
+                                 EditTextbookForm, WorkReportForm
 from pytask.taskapp.utils import getTask, getTextBook
 from pytask.profile.utils import get_notification
 
@@ -335,8 +336,75 @@
 
 @login_required
 def view_report(request, rid):
-    pass
+
+    try:
+        report = WorkReport.objects.get(uniq_key=rid)
+    except WorkReport.DoesNotExist:
+        raise Http404
+
+    user = request.user
+    context = {"report": report,
+               "user": user,
+              }
+
+    if not user.is_authenticated():
+        return render_to_response("task/view_report.html", context)
+
+    profile = user.get_profile()
+
+    context.update({"profile": profile})
+    return render_to_response("task/view_report.html", context)
+
+@login_required
+def submit_report(request, tid):
+    """ Check if the work is in WR state and the user is in assigned_users.
+    """
+    
+    user = request.user
+    task = getTask(tid)
+    old_reports = task.reports.all()
+
+    if not task.status == "WR":
+        raise Http404
+
+    can_upload = True if user in task.selected_users.all() else False
 
+    context = {
+        'user': user,
+        'task': task,
+        'can_upload': can_upload,
+    }
+
+    context.update(csrf(request))
+
+    if request.method == "POST":
+        if not can_upload:
+            return show_msg(user, "You are not authorised to upload data to this task", task_url, "view the task")
+
+        form = WorkReportForm(request.POST, request.FILES)
+
+        if form.is_valid():
+            data = form.cleaned_data.copy()
+            data.update({"task":task,
+                         "revision": old_reports.count(),
+                         "uniq_key": make_key(WorkReport),
+                         "submitted_by": user,
+                         "submitted_at": datetime.now(),
+                        })
+            r = WorkReport(**data)
+            r.save()
+
+            report_url = "/task/view/report/rid=%s"%r.uniq_key
+            return redirect(report_url)
+
+        else:
+            context.update({"form":form})
+            return render_to_response('task/submit_report.html', context)
+
+    else:
+        form = WorkReportForm()
+        context.update({"form":form})
+        return render_to_response('task/submit_report.html', context)
 
 @login_required
 def create_textbook(request):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/task/submit_report.html	Tue Jan 11 17:40:14 2011 +0530
@@ -0,0 +1,12 @@
+{% extends 'base.html' %}
+{% block title %}
+    {{task.title}}
+{% endblock %}
+{% block content %}
+Submit a report: <br />
+<form action='' method=post enctype="multipart/form-data">
+	{% csrf_token %}
+{{ form.as_p }}
+<input type=submit value=submit />
+</form>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/task/view_report.html	Tue Jan 11 17:40:14 2011 +0530
@@ -0,0 +1,12 @@
+{% extends 'base.html' %}
+{% block content %}
+Report for {{report.task.title}} <br />
+
+<hr />
+Description:<br />
+{{ report.data|linebreaksbr }}
+<hr />
+Summary: {{ report.summary }} <br />
+<hr />
+Attachment: <a href="{{report.attachment.url}}">{{report.attachment.name}}</a> 
+{% endblock %}