def add_task(request, list_id): list_object = get_object_or_404(List, id=list_id) thedate = datetime.datetime.now() created_date = "%s/%s/%s" % (thedate.month, thedate.day, thedate.year) if request.POST: form = AddItemForm(list_object, request.POST, initial={ 'assigned_to': request.user.id, 'priority': 999, }) if form.is_valid(): new_task = form.save() # Send email alert only if Notify checkbox is checked AND assignee is not same as the submitter if "notify" in request.POST and new_task.assigned_to != request.user: send_notify_mail(request, new_task) messages.success( request, "Задачу \"{t}\" було успішно створено!".format( t=new_task.title)) return HttpResponseRedirect(request.path) else: form = AddItemForm(list_object, initial={ 'assigned_to': request.user.id, 'priority': 999, 'due_date': created_date, }) return render(request, 'todo/add_task.html', locals())
def view_list(request, list_id=0, list_slug=None, view_completed=0, view_category=0, category_id=0, category_slug=None): """ Display and manage items in a task list """ # Make sure the accessing user has permission to view this list. # Always authorize the "mine" view. Admins can view/edit all lists. if list_slug == "mine" or list_slug == "recent-add" or list_slug == "recent-complete": auth_ok = 1 else: list = get_object_or_404(List, slug=list_slug) listid = list.id # Check whether current user is a member of the group this list belongs to. if list.group in request.user.groups.all( ) or request.user.is_staff or list_slug == "mine": auth_ok = 1 # User is authorized for this view else: # User does not belong to the group this list is attached to messages.error( request, "You do not have permission to view/edit this list.") # First check for items in the mark_done POST array. If present, change # their status to complete. if request.POST.getlist('mark_done'): done_items = request.POST.getlist('mark_done') # Iterate through array of done items and update its representation in the model for thisitem in done_items: p = Item.objects.get(id=thisitem) p.completed = 1 p.completed_date = datetime.datetime.now() p.completed_by = request.user p.save() messages.success(request, "Item \"%s\" marked complete." % p.title) # Undo: Set completed items back to incomplete if request.POST.getlist('undo_completed_task'): undone_items = request.POST.getlist('undo_completed_task') for thisitem in undone_items: p = Item.objects.get(id=thisitem) p.completed = 0 p.save() messages.success( request, "Previously completed task \"%s\" marked incomplete." % p.title) # And delete any requested items if request.POST.getlist('del_task'): deleted_items = request.POST.getlist('del_task') for thisitem in deleted_items: p = Item.objects.get(id=thisitem) p.delete() messages.success(request, "Item \"%s\" deleted." % p.title) # And delete any *already completed* items if request.POST.getlist('del_completed_task'): deleted_items = request.POST.getlist('del_completed_task') for thisitem in deleted_items: p = Item.objects.get(id=thisitem) p.delete() messages.success( request, "Deleted previously completed item \"%s\"." % p.title) thedate = datetime.datetime.now() created_date = "%s-%s-%s" % (thedate.year, thedate.month, thedate.day) # Get list of items with this list ID, or filter on items assigned to me, or recently added/completed if list_slug == "mine": task_list = Item.objects.filter(assigned_to=request.user, completed=0) completed_list = Item.objects.filter( assigned_to=request.user, completed=1).order_by('-completed_date') elif list_slug == "recent-add": # We'll assume this only includes uncompleted items to avoid confusion. # Only show items in lists that are in groups that the current user is also in. task_list = Item.objects.filter( list__group__in=(request.user.groups.all()), completed=0).order_by('-created_date')[:50] # completed_list = Item.objects.filter(assigned_to=request.user, completed=1) elif list_slug == "recent-complete": # Only show items in lists that are in groups that the current user is also in. task_list = Item.objects.filter( list__group__in=request.user.groups.all(), completed=1).order_by('-completed_date')[:50] # completed_list = Item.objects.filter(assigned_to=request.user, completed=1) else: task_list = Item.objects.filter(list=list.id, completed=0) completed_list = Item.objects.filter( list=list.id, completed=1).order_by('-completed_date') category_list = Item.objects.filter(list=list.id, completed=0, category=category_id) if request.POST.getlist('add_task'): form = AddItemForm(list, request.POST, initial={ 'assigned_to': request.user.id, 'priority': 999, }) if form.is_valid(): # Save task first so we have a db object to play with new_task = form.save() # Send email alert only if the Notify checkbox is checked AND the assignee is not the same as the submittor # Email subect and body format are handled by templates if "notify" in request.POST: if new_task.assigned_to != request.user: # Send email email_subject = render_to_string( "todo/email/assigned_subject.txt", {'task': new_task}) email_body = render_to_string( "todo/email/assigned_body.txt", { 'task': new_task, 'site': current_site, }) try: send_mail(email_subject, email_body, new_task.created_by.email, [new_task.assigned_to.email], fail_silently=False) except: messages.error( request, "Task saved but mail not sent. Contact your administrator." ) messages.success( request, "New task \"%s\" has been added." % new_task.title) return HttpResponseRedirect(request.path) else: if list_slug != "mine" and list_slug != "recent-add" and list_slug != "recent-complete": # We don't allow adding a task on the "mine" view form = AddItemForm(list, initial={ 'assigned_to': request.user.id, 'priority': 999, }) if request.user.is_staff: can_del = 1 return render_to_response('todo/view_list.html', locals(), context_instance=RequestContext(request))
def view_list(request, list_id=0, list_slug=None, view_completed=False): """ Display and manage items in a list. """ # Make sure the accessing user has permission to view this list. # Always authorize the "mine" view. Admins can view/edit all lists. if list_slug == "mine" or list_slug == "recent-add" or list_slug == "recent-complete": auth_ok = True else: list = get_object_or_404(List, id=list_id) if list.group in request.user.groups.all( ) or request.user.is_staff or list_slug == "mine": auth_ok = True else: # User does not belong to the group this list is attached to messages.error( request, "У вас немає прав на перегляд даної категорії задач") # Process all possible list interactions on each submit mark_done(request, request.POST.getlist('mark_done')) del_tasks(request, request.POST.getlist('del_tasks')) undo_completed_task(request, request.POST.getlist('undo_completed_task')) thedate = datetime.datetime.now() created_date = "%s-%s-%s" % (thedate.year, thedate.month, thedate.day) # Get set of items with this list ID, or filter on items assigned to me, or recently added/completed if list_slug == "mine": task_list = Item.objects.filter(assigned_to=request.user, completed=False) completed_list = Item.objects.filter(assigned_to=request.user, completed=True) elif list_slug == "recent-add": # Only show items in lists that are in groups that the current user is also in. # Assume this only includes uncompleted items. task_list = Item.objects.filter( list__group__in=(request.user.groups.all()), completed=False).order_by('-created_date')[:50] elif list_slug == "recent-complete": # Only show items in lists that are in groups that the current user is also in. task_list = Item.objects.filter( list__group__in=request.user.groups.all(), completed=True).order_by('-completed_date')[:50] else: task_list = Item.objects.filter(list=list.id, completed=0) completed_list = Item.objects.filter(list=list.id, completed=1) if request.POST.getlist('add_task'): form = AddItemForm(list, request.POST, initial={ 'assigned_to': request.user.id, 'priority': 999, }) if form.is_valid(): new_task = form.save() # Send email alert only if Notify checkbox is checked AND assignee is not same as the submitter if "notify" in request.POST and new_task.assigned_to != request.user: send_notify_mail(request, new_task) messages.success( request, "Задачу \"{t}\" було успішно створено!".format( t=new_task.title)) return HttpResponseRedirect(request.path) else: # Don't allow adding new tasks on some views if list_slug != "mine" and list_slug != "recent-add" and list_slug != "recent-complete": form = AddItemForm(list, initial={ 'assigned_to': request.user.id, 'priority': 999, }) return render(request, 'todo/view_list.html', locals())
def view_list(request, group_pk=None, list_id=0, list_slug=None, view_completed=False): """ Display and manage items in a list. """ #this list is repeated a lot. lets re-factor somehow group = get_object_or_404(models.Group, pk=group_pk) familygroup = get_object_or_404(FamilyGroup, group_id=group_pk) #list = get_object_or_404(List, group_id=group_pk) event = familygroup.event members = models.User.objects.filter(groups=group) profile = request.user.userprofile # Make sure the accessing user has permission to view this list. # Always authorize the "mine" view. Admins can view/edit all lists. if list_slug == "mine" or list_slug == "recent-add" or list_slug == "recent-complete": auth_ok = True else: list = get_object_or_404(List, id=list_id) if list.group in request.user.groups.all( ) or request.user.is_staff or list_slug == "mine": auth_ok = True else: # User does not belong to the group this list is attached to messages.error( request, "You do not have permission to view/edit this list.") # Process all possible list interactions on each submit mark_done(request, request.POST.getlist('mark_done')) del_tasks(request, request.POST.getlist('del_tasks')) undo_completed_task(request, request.POST.getlist('undo_completed_task')) thedate = datetime.datetime.now() created_date = "%s-%s-%s" % (thedate.year, thedate.month, thedate.day) # Get set of items with this list ID, or filter on items assigned to me, or recently added/completed if list_slug == "mine": task_list = Item.objects.filter(assigned_to=request.user, completed=False) completed_list = Item.objects.filter(assigned_to=request.user, completed=True) elif list_slug == "recent-add": # Only show items in lists that are in groups that the current user is also in. # Assume this only includes uncompleted items. task_list = Item.objects.filter( list__group__in=(request.user.groups.all()), completed=False).order_by('-created_date')[:50] elif list_slug == "recent-complete": # Only show items in lists that are in groups that the current user is also in. task_list = Item.objects.filter( list__group__in=request.user.groups.all(), completed=True).order_by('-completed_date')[:50] else: task_list = Item.objects.filter(list=list.id, completed=0) completed_list = Item.objects.filter(list=list.id, completed=1) if request.POST.getlist('add_task'): print('got request') form = AddItemForm(list, request.POST, initial={ 'assigned_to': request.user.id, 'priority': 999, }) print('set form variable') print(form.errors) print(form.is_valid) if form.is_valid(): form.save() print('saved') new_task = form.save() # Send email alert only if Notify checkbox is checked AND assignee is not same as the submitter if "notify" in request.POST and new_task.assigned_to != request.user: send_notify_mail(request, new_task) messages.success( request, "New task \"{t}\" has been added.".format(t=new_task.title)) return HttpResponseRedirect(request.path) else: # Don't allow adding new tasks on some views if list_slug != "mine" and list_slug != "recent-add" and list_slug != "recent-complete": form = AddItemForm(list, initial={ 'assigned_to': request.user.id, 'priority': 999, }) return render(request, 'todo/view_list.html', locals())
def view_list(request, list_slug): """ Display and manage items in a task list """ # Make sure the accessing user has permission to view this list. # Always authorize the "mine" view. Admins can view/edit all lists. if list_slug == "mine": auth_ok = 1 else: list = get_object_or_404(List, slug=list_slug) listid = list.id # Check whether current user is a member of the group this list belongs to. if list.group in request.user.groups.all( ) or request.user.is_staff or list_slug == "mine": auth_ok = 1 # User is authorized for this view else: # User does not belong to the group this list is attached to request.user.message_set.create( message="You do not have permission to view/edit this list.") # First check for items in the mark_done POST array. If present, change # their status to complete. if request.POST.getlist('mark_done'): done_items = request.POST.getlist('mark_done') # Iterate through array of done items and update its representation in the model for thisitem in done_items: p = Item.objects.get(id=thisitem) p.completed = 1 p.completed_date = datetime.datetime.now() p.save() request.user.message_set.create(message="Item marked complete.") # Undo: Set completed items back to incomplete if request.POST.getlist('undo_completed_task'): undone_items = request.POST.getlist('undo_completed_task') for thisitem in undone_items: p = Item.objects.get(id=thisitem) p.completed = 0 p.save() request.user.message_set.create( message="Previously completed task marked incomplete.") # And delete any requested items if request.POST.getlist('del_task'): deleted_items = request.POST.getlist('del_task') for thisitem in deleted_items: p = Item.objects.get(id=thisitem) p.delete() request.user.message_set.create(message="Item deleted.") # And delete any *already completed* items if request.POST.getlist('del_completed_task'): deleted_items = request.POST.getlist('del_completed_task') for thisitem in deleted_items: p = Item.objects.get(id=thisitem) p.delete() request.user.message_set.create( message="Deleted previously completed item.") thedate = datetime.datetime.now() created_date = "%s-%s-%s" % (thedate.year, thedate.month, thedate.day) # Get list of items with this list ID, or filter on items assigned to me if list_slug == "mine": task_list = Item.objects.filter(assigned_to=request.user, completed=0) completed_list = Item.objects.filter(assigned_to=request.user, completed=1) # item = Item.objects.get(pk=1) # item.overdue_status() else: # list = get_object_or_404(List, slug=list_slug) # listid = list.id task_list = Item.objects.filter(list=list.id, completed=0) completed_list = Item.objects.filter(list=list.id, completed=1) if request.POST.getlist('add_task'): form = AddItemForm(request.user, request.POST, initial={ 'assigned_to': request.user.id, }) if form.is_valid(): form.save() # confirmation = "A new task has been added." request.user.message_set.create( message="A new task has been added.") return HttpResponseRedirect(request.path) else: form = AddItemForm(request.user, initial={ 'assigned_to': request.user.id, }) if request.user.is_staff: can_del = 1 return render_to_response('todo/view_list.html', locals(), context_instance=RequestContext(request))
def view_list(request, list_id=0, list_slug=None, view_completed=False): """ Display and manage items in a list. """ # Make sure the accessing user has permission to view this list. # Always authorize the "mine" view. Admins can view/edit all lists. if list_slug == "mine" or list_slug == "recent-add" or list_slug == "recent-complete": auth_ok = 1 else: list = get_object_or_404(List, slug=list_slug) listid = list.id if list.group in request.user.groups.all() or request.user.is_staff or list_slug == "mine": auth_ok = True # User is authorized for this view else: # User does not belong to the group this list is attached to messages.error(request, "You do not have permission to view/edit this list.") # Check for items in the mark_done POST array. If present, change status to complete. if request.POST.getlist('mark_done'): done_items = request.POST.getlist('mark_done') for item in done_items: i = Item.objects.get(id=item) i.completed = 1 i.completed_date = datetime.datetime.now() i.save() messages.success(request, "Item \"{i}\" marked complete.".format(i=i.title)) # Undo: Set completed items back to incomplete if request.POST.getlist('undo_completed_task'): undone_items = request.POST.getlist('undo_completed_task') for item in undone_items: i = Item.objects.get(id=item) i.completed = False i.save() messages.success(request, "Previously completed task \"{i}\" marked incomplete.".format(i=i.title)) # And delete any requested items if request.POST.getlist('del_task'): deleted_items = request.POST.getlist('del_task') for item in deleted_items: i = Item.objects.get(id=item) i.delete() messages.success(request, "Item \"{i}\" deleted.".format(i=i.title)) # Delete any already-completed items if request.POST.getlist('del_completed_task'): deleted_items = request.POST.getlist('del_completed_task') for item in deleted_items: i = Item.objects.get(id=item) i.delete() messages.success(request, "Deleted previously completed item \"{i}\".".format(i=i.title)) thedate = datetime.datetime.now() created_date = "%s-%s-%s" % (thedate.year, thedate.month, thedate.day) # Get list of items with this list ID, or filter on items assigned to me, or recently added/completed if list_slug == "mine": task_list = Item.objects.filter(assigned_to=request.user, completed=False) completed_list = Item.objects.filter(assigned_to=request.user, completed=True) elif list_slug == "recent-add": # Only show items in lists that are in groups that the current user is also in. # Assume this only includes uncompleted items to avoid confusion. task_list = Item.objects.filter( list__group__in=(request.user.groups.all()), completed=False).order_by('-created_date')[:50] elif list_slug == "recent-complete": # Only show items in lists that are in groups that the current user is also in. task_list = Item.objects.filter( list__group__in=request.user.groups.all(), completed=True).order_by('-completed_date')[:50] else: task_list = Item.objects.filter(list=list.id, completed=0) completed_list = Item.objects.filter(list=list.id, completed=1) if request.POST.getlist('add_task'): form = AddItemForm(list, request.POST, initial={ 'assigned_to': request.user.id, 'priority': 999, }) if form.is_valid(): new_task = form.save() # Send email alert only if Notify checkbox is checked AND assignee is not same as the submitter if "notify" in request.POST: if new_task.assigned_to != request.user: # Send email email_subject = render_to_string("todo/email/assigned_subject.txt", {'task': new_task}) email_body = render_to_string( "todo/email/assigned_body.txt", {'task': new_task, 'site': current_site, }) try: send_mail( email_subject, email_body, new_task.created_by.email, [new_task.assigned_to.email], fail_silently=False) except: messages.error(request, "Task saved but mail not sent. Contact your administrator.") messages.success(request, "New task \"{t}\" has been added.".format(t=new_task.title)) return HttpResponseRedirect(request.path) else: # Don't allow adding new tasks on some views if list_slug != "mine" and list_slug != "recent-add" and list_slug != "recent-complete": form = AddItemForm(list, initial={ 'assigned_to': request.user.id, 'priority': 999, }) return render(request, 'todo/view_list.html', locals())