taskapp/events/task.py
branchbuildout
changeset 227 3c8f3b0e5b00
parent 214 679c7e237052
child 228 81994e525e69
equal deleted inserted replaced
214:679c7e237052 227:3c8f3b0e5b00
     1 from datetime import datetime
       
     2 from pytask.taskapp.models import Profile, Task, Comment, Map
       
     3 from pytask.taskapp.utilities.task import getTask
       
     4 from pytask.taskapp.utilities.request import create_request
       
     5 from pytask.taskapp.utilities.helper import get_key
       
     6 from pytask.taskapp.utilities.notification import create_notification
       
     7 
       
     8 def publishTask(task, rem_mentors=True, rem_comments=True):
       
     9     """ set the task status to open """
       
    10 
       
    11  #   if task.sub_type == 'D':
       
    12      #      deps, subs = task.map_subs.all(), []
       
    13      #else:
       
    14          #   subs, deps = task.map_subs.all(), []
       
    15    
       
    16     task = getTask(task.id)
       
    17     if task.subs or any(map(lambda t:t.status!="CM",task.deps)):
       
    18         task.status = "LO"
       
    19     else:
       
    20         task.status = "OP"
       
    21 
       
    22     if rem_mentors:
       
    23         task.mentors.clear()
       
    24         task.mentors.add(task.created_by)
       
    25 
       
    26     if rem_comments:
       
    27         task.comment_set.update(is_deleted=True)
       
    28         task.comment_set.update(deleted_by=task.created_by)
       
    29 
       
    30     task.published_datetime = datetime.now()
       
    31     task.save()
       
    32 
       
    33     pending_requests = task.request_task.filter(is_valid=True, is_replied=False)
       
    34     pending_requests.update(is_valid=False)
       
    35 
       
    36     return task
       
    37 
       
    38 def addSubTask(main_task, sub_task):
       
    39     """ add the task to subs attribute of the task and update its status.
       
    40     sub task can be added only if a task is in UP/OP/LO state.
       
    41     """
       
    42 
       
    43     ## Shall modify after talking to pr about subtasks
       
    44     ## I think i might even remove the concept of subtasks
       
    45 
       
    46     main_task.sub_type = "S"
       
    47     main_task.save()
       
    48 
       
    49     try:
       
    50         mapobj = Map.objects.get(main=main_task)
       
    51     except Map.DoesNotExist:
       
    52         mapobj = Map()
       
    53         mapobj.main = main_task
       
    54         mapobj.save()
       
    55     mapobj.subs.add(sub_task)
       
    56     mapobj.save()
       
    57 
       
    58     sub_tasks = getTask(main_task.id).subs
       
    59     if main_task.status == "OP":
       
    60         if any(map(lambda t:t.status!="CM",sub_tasks)):
       
    61             main_task.status = "LO"
       
    62         else:
       
    63             "CM"
       
    64     main_task.save()
       
    65 
       
    66 def addDep(main_task, dependency):
       
    67     """ add the dependency task to deps attribute of the task.
       
    68     update the status of main_task accordingly.
       
    69     note that deps can be added only if task is in UP/OP/LO state.
       
    70     And also if the task doesn't have any subs.
       
    71     """
       
    72 
       
    73     main_task.sub_type = "D"
       
    74     main_task.save()
       
    75 
       
    76     try:
       
    77         mapobj = Map.objects.get(main=main_task)
       
    78     except Map.DoesNotExist:
       
    79         mapobj = Map()
       
    80         mapobj.main = main_task
       
    81         mapobj.save()
       
    82 
       
    83     mapobj.subs.add(dependency)
       
    84     mapobj.save()
       
    85 
       
    86     deps = getTask(main_task.id).deps
       
    87 
       
    88     if main_task.status in ["OP", "LO"]: 
       
    89         if all(map(lambda t:t.status=="CM",deps)):
       
    90             main_task.status = "OP"
       
    91         else:
       
    92             main_task.status = "LO"
       
    93     
       
    94     main_task.save()
       
    95 
       
    96 def reqMentor(task, mentor, req_by):
       
    97     """ create a request object with role as MT.
       
    98     """
       
    99 
       
   100     create_request(sent_by=req_by, role="MT", sent_to=mentor, task=task) 
       
   101 
       
   102 def addMentor(task,mentor):
       
   103     """ add the mentor to mentors list of the task """
       
   104     
       
   105     task.mentors.add(mentor)
       
   106     task.save()
       
   107     return task     
       
   108 
       
   109 def createTask(title,desc,created_by,credits):
       
   110     """ creates a bare minimum task with title, description and credits.
       
   111     the creator of the task will be assigned as a mentor for the task.
       
   112     """
       
   113 
       
   114     while True:
       
   115         id = get_key()
       
   116         try:
       
   117             task = Task.objects.get(id__iexact=id)
       
   118             continue
       
   119         except Task.DoesNotExist:
       
   120             break
       
   121 
       
   122     try:
       
   123         task = Task.objects.exclude(status="DL").get(title__iexact=title)
       
   124         return None
       
   125     except:
       
   126         task = Task(title=title)
       
   127 
       
   128     task.id = id 
       
   129     task.desc = desc
       
   130     task.created_by = created_by
       
   131     task.credits = credits
       
   132     task.creation_datetime = datetime.now()
       
   133     task.published_datetime = datetime.now()
       
   134     task.save()
       
   135     return task
       
   136 
       
   137 def addClaim(task, message, user):
       
   138     """ add claim data to the database if it does not exist 
       
   139     and also update the claimed users field of the task.
       
   140     """
       
   141     
       
   142     task.claimed_users.add(user)
       
   143     task.save()
       
   144 
       
   145     pending_reqs = user.request_sent_to.filter(is_replied=False, is_valid=True, role="MT", task=task).all()
       
   146     for req in pending_reqs:
       
   147         req.is_valid = False
       
   148         req.save()
       
   149         user_url = '<a href="/user/view/uid=%s">%s</a>'%(user.id, user.username)
       
   150         reason = "User has claimed the task and hence cannot be a mentor and this request was made invalid."
       
   151         create_notification("MT", req.sent_by, user, task=task, reply=False, remarks=reason, requested_by=req.sent_by)
       
   152 
       
   153     for a_mentor in task.mentors.all():
       
   154         create_notification("CL", a_mentor, user, task=task, remarks=message)
       
   155     
       
   156 def assignTask(task, added_user, assigned_by):
       
   157     """ check for the status of task and assign it to the particular user """
       
   158     
       
   159     if task.status in ['OP', 'WR']:
       
   160         task.assigned_users.add(added_user)
       
   161         task.claimed_users.remove(added_user)
       
   162         task.status = "WR"
       
   163     task.save()
       
   164 
       
   165     create_notification("AU", added_user, assigned_by, task=task)
       
   166 
       
   167 
       
   168 def updateTask(task, title=None, desc=None, credits=None, tags_field=None):
       
   169     """ update the property accordingly.
       
   170     while updating title, check for uniqueness of title.
       
   171     return None if any error. 
       
   172     """
       
   173     
       
   174     if title:
       
   175         try:
       
   176             task.title = title
       
   177             task.save()
       
   178         except Task.IntegrityError:
       
   179             return None
       
   180     if desc:task.desc = desc
       
   181     if credits:task.credits = credits
       
   182     if tags_field:task.tags_field = tags_field
       
   183     task.save()
       
   184     return task
       
   185 
       
   186 def removeTask(main_task, sub_task):
       
   187     """ get the corresponding map object and remove the sub_task.
       
   188     """
       
   189 
       
   190     mapobj = Map.objects.get(main=main_task)
       
   191     mapobj.subs.remove(sub_task)
       
   192     mapobj.save()
       
   193 
       
   194 def removeUser(main_task, rem_user, removed_by, reason=None):
       
   195     """ right now, just remove the user from the list of assigned_users.
       
   196     """
       
   197 
       
   198     main_task.assigned_users.remove(rem_user)
       
   199     main_task.save()
       
   200 
       
   201     ## TODiscuss : when a user is kicked off, his pending requests for pynts is made invalid
       
   202     rem_user.request_receiving_user.filter(task=main_task,role="PY",is_valid=True,is_replied=False).update(is_valid=False)
       
   203 
       
   204     create_notification("RU", rem_user, removed_by, task=main_task, remarks=reason)
       
   205     ## TODO : create notification to the victim
       
   206 
       
   207 def assignCredits(task, given_by, given_to, points):
       
   208     """ make a proper request object.
       
   209     """
       
   210     
       
   211     create_request(sent_by=given_by, role="PY", task=task, receiving_user=given_to, pynts=points ) 
       
   212 
       
   213 def completeTask(task, marked_by):
       
   214     """ set the status of task as completed.
       
   215     We dont have to inform parent tasks.
       
   216     That part is taken care by getTask method.
       
   217     """
       
   218 
       
   219     task.status = "CM"
       
   220     task.save()
       
   221 
       
   222     pending_requests = task.request_task.filter(is_replied=False)
       
   223     pending_requests.update(is_valid=False)
       
   224 
       
   225     ## generate notification appropriately using marked_by
       
   226     ## we also have to mark unread requests as invalid
       
   227 
       
   228     for a_user in task.assigned_users.all():
       
   229         create_notification(role="CM", sent_to=a_user, sent_from=marked_by, task=task)
       
   230 
       
   231     for a_user in task.claimed_users.all():
       
   232         create_notification(role="CM", sent_to=a_user, sent_from=marked_by, task=task)
       
   233 
       
   234     for a_mentor in task.mentors.all():
       
   235         create_notification(role="CM", sent_to=a_mentor, sent_from=marked_by, task=task)
       
   236 
       
   237 def closeTask(task, closed_by, reason=None):
       
   238     """ set the status of task as CD.
       
   239     generate notifications accordingly.
       
   240     """
       
   241 
       
   242     task.status = "CD"
       
   243     task.save()
       
   244 
       
   245     pending_requests = task.request_task.filter(is_replied=False)
       
   246     pending_requests.update(is_valid=False)
       
   247 
       
   248     ## generate notifications here
       
   249 
       
   250     for a_user in task.assigned_users.all():
       
   251         create_notification(role="CD", sent_to=a_user, sent_from=closed_by, task=task, remarks=reason)
       
   252 
       
   253     for a_user in task.claimed_users.all():
       
   254         create_notification(role="CD", sent_to=a_user, sent_from=closed_by, task=task, remarks=reason)
       
   255 
       
   256     for a_mentor in task.mentors.all():
       
   257         create_notification(role="CD", sent_to=a_mentor, sent_from=closed_by, task=task, remarks=reason)
       
   258 
       
   259 def deleteTask(task, deleted_by, reason=None):
       
   260     """ set the task status as DL
       
   261     notify all its other viewers about the deleting of task.
       
   262     """
       
   263 
       
   264     task.status = "DL"
       
   265     task.save()
       
   266 
       
   267     pending_requests = task.request_task.filter(is_replied=False,is_valid=True)
       
   268     pending_requests.update(is_valid=False)
       
   269 
       
   270     for a_mentor in task.mentors.exclude(id=deleted_by.id):
       
   271         create_notification("DL", sent_to=a_mentor, sent_from=deleted_by, task=task, remarks=reason)