added the functionality to request a user to be AD MG DV.
authornishanth
Sun, 28 Feb 2010 13:25:24 +0530
changeset 141 2489392ffb56
parent 140 7bdcbb4f2c27
child 142 bd65e2c9a7b4
added the functionality to request a user to be AD MG DV.
taskapp/forms/user.py
taskapp/utilities/notification.py
taskapp/views/task.py
taskapp/views/user.py
templates/index.html
templates/user/changerole.html
urls.py
--- a/taskapp/forms/user.py	Sun Feb 28 10:43:36 2010 +0530
+++ b/taskapp/forms/user.py	Sun Feb 28 13:25:24 2010 +0530
@@ -2,11 +2,10 @@
 
 from django import forms
 from pytask.taskapp.models import GENDER_CHOICES, Profile
-from django.forms import ModelForm
 from registration.forms import RegistrationFormUniqueEmail
 from registration.models import RegistrationProfile
 
-class UserProfileEditForm(ModelForm):
+class UserProfileEditForm(forms.ModelForm):
     """Form used to edit the profile of a user"""
     
     class Meta:
@@ -26,3 +25,11 @@
         new_profile.save()
         
         return new_user
+
+def UserChoiceForm(choices, instance=None):
+    """ take a list of users and return a choice form.
+    """
+
+    class myForm(forms.Form):
+        user = forms.ChoiceField(choices, required=True)
+    return myForm(instance) if instance else myForm()
--- a/taskapp/utilities/notification.py	Sun Feb 28 10:43:36 2010 +0530
+++ b/taskapp/utilities/notification.py	Sun Feb 28 13:25:24 2010 +0530
@@ -71,14 +71,14 @@
     elif role in ["DV", "MG", "AD"]:
 
         accepting_user = sent_from
-        user_url = "/user/view/uid=%s"%(accepting_user.id) ## i mean the user who has accepted it
-        requested_by_url = "/user/view/uid=%s"%(requested_by.id)
+        user_url = '<a href="/user/view/uid=%s">%s</a>'%(accepting_user.id, accepting_user.username) ## i mean the user who has accepted it
+        requested_by_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
         role_rights = dict(RIGHTS_CHOICES)[role]
         role_learn_url = "/about/%s"%role_rights.lower()
 
         if reply:
             notification.sub = "New %s for the site"%role_rights
-            notification.message = "%s has accepted request made by %s asking him to act as a %s for the website.<br />"(user_url, requested_by_url, role_rights)
+            notification.message = "%s has accepted request made by %s asking him to act as a %s for the website.<br />"%(user_url, requested_by_url, role_rights)
         else:
             notification.sub = "Rejected your request to act as %s"%role_rights
             notification.message = "%s has rejected your request asking him to act as a %s.<br />"%(new_mentor_url, task_url)
--- a/taskapp/views/task.py	Sun Feb 28 10:43:36 2010 +0530
+++ b/taskapp/views/task.py	Sun Feb 28 13:25:24 2010 +0530
@@ -16,7 +16,7 @@
 def browse_tasks(request):
     """ display all the tasks """
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task_list = Task.objects.exclude(status="UP").exclude(status="DL").order_by('published_datetime').reverse()
     
     context = {'user':user,
@@ -30,7 +30,7 @@
 
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
 
     is_guest = True if not user.is_authenticated() else False
@@ -58,7 +58,7 @@
     
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
 
     if task.status == "DL":
@@ -110,7 +110,7 @@
     if user cannot create a task, redirect to homepage.
     """
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     is_guest = True if not user.is_authenticated() else False
     
     if not is_guest:
@@ -152,7 +152,7 @@
     
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
     errors = []
     
@@ -192,7 +192,7 @@
     
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
 
     deps, subs = task.deps, task.subs
@@ -252,7 +252,7 @@
 
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
 
     is_guest = True if not user.is_authenticated() else False
@@ -295,7 +295,7 @@
     
     errors = []
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
     claims = Claim.objects.filter(task=task)
 
@@ -339,7 +339,7 @@
     
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
     
     is_guest = True if not user.is_authenticated() else False
@@ -384,7 +384,7 @@
     
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
     
     is_guest = True if not user.is_authenticated() else False
@@ -423,7 +423,7 @@
     
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
 
     is_guest = True if not user.is_authenticated() else False
@@ -472,6 +472,8 @@
     """
     
     task = Task.objects.get(id=tid) 
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
+
 
 def complete_task(request, tid):
 
@@ -481,7 +483,7 @@
 
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
     
     is_guest = True if not user.is_authenticated() else False
@@ -522,7 +524,7 @@
 
     task_url = "/task/view/tid=%s"%tid
     
-    user = get_user(request.user)
+    user = get_user(request.user) if request.user.is_authenticated() else request.user
     task = getTask(tid)
     
     is_guest = True if not user.is_authenticated() else False
--- a/taskapp/views/user.py	Sun Feb 28 10:43:36 2010 +0530
+++ b/taskapp/views/user.py	Sun Feb 28 13:25:24 2010 +0530
@@ -10,9 +10,9 @@
 from pytask.taskapp.events.user import createUser, updateProfile
 from pytask.taskapp.events.request import reply_to_request
 
-from pytask.taskapp.forms.user import UserProfileEditForm
+from pytask.taskapp.forms.user import UserProfileEditForm, UserChoiceForm
 
-from pytask.taskapp.utilities.request import get_request
+from pytask.taskapp.utilities.request import get_request, create_request
 from pytask.taskapp.utilities.notification import get_notification
 from pytask.taskapp.utilities.user import get_user
 
@@ -29,7 +29,7 @@
 def homepage(request):
     """ check for authentication and display accordingly. """
    
-    user = get_user(request.user)
+    user = request.user
     is_guest = False
     is_mentor = False
     can_create_task = False
@@ -47,6 +47,7 @@
         return render_to_response('index.html', {'user':user, 'is_guest':is_guest, 'task_list':task_list})
         
     else:
+        user = get_user(request.user)
         user_profile = user.get_profile()
         is_mentor = True if user.task_mentors.all() else False
         can_create_task = False if user_profile.rights == u"CT" else True
@@ -265,4 +266,60 @@
             return redirect(notifications_url)
     else:
         return show_msg(user, 'This is wrong', notification_url, "view the notification")
-   
+ 
+@login_required
+def change_rights(request, role):
+    """ check if the current user has privileges to do this.
+    """
+    
+    user = get_user(request.user)
+    role = role.upper()
+    user_profile = user.get_profile()
+    user_rights = user_profile.rights
+
+    user_can_view = True if user_rights == "AD" or ( user_rights == "MG" and role in ["mg", "dv"] ) else False
+
+    if user_can_view:
+        if role == "DV":
+            choices = [ (_.user.id,_.user.username ) for _ in Profile.objects.filter(rights="CT",user__is_active=True) ]
+        elif role == "MG":
+            choices = [ (_.user.id,_.user.username ) for _ in Profile.objects.exclude(rights="MG",user__is_active=True).exclude(rights="AD") ]
+        elif role == "AD":
+            choices = [ (_.user.id,_.user.username ) for _ in Profile.objects.exclude(rights="AD",user__is_active=True) ]
+
+        form = UserChoiceForm(choices)
+
+        context = {
+            'user':user,
+            'form':form,
+        }
+
+        if request.method=="POST":
+            data = request.POST
+            form = UserChoiceForm(choices, data)
+            if form.is_valid():
+                user_to_change = User.objects.get(id=form.cleaned_data['user'])
+                create_request(sent_by=user, role=role, sent_to=user_to_change)
+                return show_msg(user, "A request has been sent", "/", "return to home page")
+            else:
+                raise Http404
+        else:
+            return render_to_response('user/changerole.html', context)
+    else:
+        raise Http404
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
--- a/templates/index.html	Sun Feb 28 10:43:36 2010 +0530
+++ b/templates/index.html	Sun Feb 28 13:25:24 2010 +0530
@@ -65,9 +65,9 @@
         {% endfor %}
     {% endif %}
     <br /><br />
-    {% ifnotequal user.get_profile.rights "CT" %}
+    {% if can_create_task %}
         <a href="/task/create/">Create a task</a><br />
-    {% endifnotequal %}
+    {% endif %}
     {% ifequal user.get_profile.rights "MG" %}
         <a href="/user/make/dv/">Request another to be a Developer</a>
         <a href="/user/make/mg/">Request another user to act as manager</a><br />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/user/changerole.html	Sun Feb 28 13:25:24 2010 +0530
@@ -0,0 +1,11 @@
+{% extends 'base.html' %}
+{% block title %}
+    {{task.title}}
+{% endblock %}
+{% block content %}
+        <a href="/">Click here</a> to return to home page.<br />
+        <form action="" method="post">
+        {{form.as_p}}
+        <input type="submit" value="Submit">
+        </form>
+{% endblock %}
--- a/urls.py	Sun Feb 28 10:43:36 2010 +0530
+++ b/urls.py	Sun Feb 28 13:25:24 2010 +0530
@@ -54,6 +54,7 @@
     (r'^user/notifications/$', userViews.browse_notifications),
     (r'^user/notifications/nid=(\d+)/$', userViews.view_notification),
     (r'^user/notifications/nid=(\d+)/(\w+)/$', userViews.edit_notification),
+    (r'^user/make/(\w+)/$', userViews.change_rights),
 
     (r'^about/(\w+)/$', userViews.learn_more),