taskapp/events/task.py
author nishanth
Wed, 24 Feb 2010 10:42:46 +0530
changeset 63 1fc027bf99ee
parent 55 ca2486e29178
child 74 7dc764854867
permissions -rw-r--r--
added events in task.py for adding subtask and dependencies
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
     1
from datetime import datetime
25
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
     2
from pytask.taskapp.models import Profile, Task, Comment, Credit, Claim
19
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
     3
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
     4
def publishTask(task):
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
     5
    """ set the task status to open """
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
     6
    
53
2c5062a93734 updated publishtask event .
nishanth
parents: 25
diff changeset
     7
    sub_tasks = task.subs.all()
2c5062a93734 updated publishtask event .
nishanth
parents: 25
diff changeset
     8
    dependencies = task.deps.all()
2c5062a93734 updated publishtask event .
nishanth
parents: 25
diff changeset
     9
    if sub_tasks or any(map(lambda t:t.status!="CM",dependencies)):
2c5062a93734 updated publishtask event .
nishanth
parents: 25
diff changeset
    10
        task.status = "LO"
2c5062a93734 updated publishtask event .
nishanth
parents: 25
diff changeset
    11
    else:
2c5062a93734 updated publishtask event .
nishanth
parents: 25
diff changeset
    12
        task.status = "OP"
19
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    13
    task.save()
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    14
    return task
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    15
54
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    16
def addSubTask(main_task, sub_task):
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    17
    """ add the task to subs attribute of the task and update its status.
63
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
    18
    sub task can be added only if a task is in UP/OP/LO state.
54
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    19
    """
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    20
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    21
    ## Shall modify after talking to pr about subtasks
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    22
    ## I think i might even remove the concept of subtasks
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    23
    main_task.subs.add(sub_task)
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    24
    sub_tasks = main_task.subs.all()
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    25
    if main_task.status == "OP":
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    26
        if any(map(lambda t:t.status!="CM",sub_tasks)):
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    27
            main_task.status = "LO"
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    28
        else:
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    29
            "CM"
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    30
    main_task.save()
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    31
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    32
def addDep(main_task, dependency):
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    33
    """ add the dependency task to deps attribute of the task.
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    34
    update the status of main_task accordingly.
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    35
    note that deps can be added only if task is in UP/OP/LO/CD state.
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    36
    And also if the task doesn't have any subs.
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    37
    """
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    38
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    39
    main_task.deps.add(dependency)
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    40
    deps = main_task.deps.all()
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    41
    if main_task.status in ["OP", "LO"]: 
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    42
        if all(map(lambda t:t.status=="CM",deps)):
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    43
            main_task.status = "OP"
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    44
        else:
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    45
            main_task.status = "LO"
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    46
    
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    47
    main_task.save()
943d682aefdd added the events addSubTask and addDep.
nishanth
parents: 53
diff changeset
    48
19
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    49
def addMentor(task,mentor):
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    50
    """ add the mentor to mentors list of the task """
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    51
    
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    52
    task.mentors.add(mentor)
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    53
    task.save()
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    54
    return task    
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    55
    
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    56
def createTask(title,desc,created_by,credits):
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    57
    """ creates a bare minimum task with title, description and credits.
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    58
    the creator of the task will be assigned as a mentor for the task.
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    59
    """
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    60
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    61
    try:
21
c28774fe7ffd implemented "add another mentor" functionality to a task.
nishanth
parents: 19
diff changeset
    62
        task = Task.objects.get(title__iexact=title)
19
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    63
        return None
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    64
    except Task.DoesNotExist:
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    65
        task = Task(title=title)
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    66
    task.desc = desc
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    67
    task.created_by = created_by
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    68
    task.credits = credits
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    69
    task.creation_datetime = datetime.now()
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    70
    task.save()
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    71
    return task
c52f7cde9861 added events/task.py and templates/error.html.
nishanth
parents:
diff changeset
    72
25
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    73
def addClaim(task, message, user):
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    74
    """ add claim data to the database if it does not exist 
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    75
    and also update the claimed users field of the task.
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    76
    """
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    77
    
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    78
    task.claimed_users.add(user)
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    79
    task.save()
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    80
    claim = Claim()
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    81
    claim.message = message
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    82
    claim.task = task
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    83
    claim.user = user
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    84
    claim.creation_datetime = datetime.now()
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    85
    claim.save()
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    86
    
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    87
def assignTask(task, user):
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    88
    """ check for the status of task and assign it to the particular user """
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    89
    
63
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
    90
    if task.status in ['OP', 'WR']:
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
    91
        task.assigned_users.add(user)
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
    92
        task.claimed_users.remove(user)
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
    93
        task.status = "WR"
25
c0e4fc8b8b5b added the functionality to assign a task to one of the claimed users.
nishanth
parents: 21
diff changeset
    94
    task.save()
55
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
    95
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
    96
def getTask(tid):
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
    97
    """ retreive the task from database.
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
    98
    if the task has deps or subs, update its status correspondingly.
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
    99
    """
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   100
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   101
    task = Task.objects.get(id=tid)
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   102
    deps = task.deps.all()
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   103
    subs = task.subs.all()
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   104
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   105
    if deps and task.status in ["OP", "LO"]:
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   106
        task.status = "OP" if all(map(lambda t:t.status=="CM",deps)) else "LO"
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   107
    if subs and task.status in ["OP", "LO", "CM"]:
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   108
        task.status = "CM" if all(map(lambda t:t.status=="CM",subs)) else "LO"
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   109
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   110
    task.save()
ca2486e29178 added a utility called getTask in task events and made changes in task views accordingly
nishanth
parents: 54
diff changeset
   111
    return task
63
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   112
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   113
def updateTask(task, title=None, desc=None, credits=None, tags_field=None):
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   114
    """ update the property accordingly.
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   115
    while updating title, check for uniqueness of title.
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   116
    return None if any error. 
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   117
    """
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   118
    
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   119
    if title:
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   120
        try:
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   121
            task.title = title
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   122
            task.save()
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   123
        except IntegrityError:
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   124
            return None
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   125
    if desc:task.desc = desc
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   126
    if credits:task.credits = credits
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   127
    if tags_field:task.tags_field = tags_field
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   128
    task.save()
1fc027bf99ee added events in task.py for adding subtask and dependencies
nishanth
parents: 55
diff changeset
   129
    return task