Beispiel #1
0
def update(request):
    # This function SHOULD take task id as an argument and get the corresponding record
    # from the database and then update it. Similar to add function, using forms in this
    # function can make it easier to validate and save form data.

    if request.method == 'POST':
        instance = Task.objects.get(id=request.POST['id'])
        form = TaskForm(request.POST or None, instance=instance)
        if form.is_valid():
            form.save()
            return index(request)
        else:
            form = TaskForm()
            context = {
                'form': form,
                'error': 'You stupid, stupid, waste of space'
            }
            return render(request, 'todo/update.html', context)

    else:
        task_id = request.GET.get('id')
        task = Task.objects.get(pk=task_id)
        data = {
            'task': task.task,
            'completed': task.completed,
            'created_at': task.created_at
        }
        form = TaskForm(initial=data)

        context = {'form': form, 'task_id': task_id}
        return render(request, 'todo/update.html', context)
Beispiel #2
0
def tasks_view(request):
#    request.session.clear()
    tasks_number = request.session.get('tasks_number', 0) #we are using a counter to store every new task in; also add new variable to mean the task_number counter
#    task_text = request.POST.get('add') now that the task_form has come in this is not needed
    initials = {
        'add': "Type your task" #instead of the form being in blank, now things are introduced, but if you press add, this text will be added as if it's a chore on todo list ; to make sure this doesn't happen go to forms and fix clean.self function. Here the initial is a variable of the dictionary that is the initial form, since it's outside of the main body of program, this is outside and not bound by so many restrictions
    }
    task_form = TaskForm(initial=initials)
    if request.POST:
        task_form = TaskForm(request.POST, initial=initials) #if a POST comes in make a form out of the TaskForm file
        if task_form.is_valid(): #if the form is valid, give the data, but the clean_data
            data = task_form.cleaned_data
            priority = data['priority']
            task_text = data['add']
            if task_text and task_text not in request.session.values():#if value already in dictionary doesn't show. This line is checking if value is in request session
#                while tasks_number in request.session:
                tasks_number += 1
                request.session[tasks_number] = task_text #in the dictionary request.session will be added the key tasks_number and the value will be task_text
                tasks_number += 1
                request.session['tasks_number'] = tasks_number
                return redirect("tasks") #after adding correctly a task, it asks the browser to redirect to a page, here it could be the page without the form filled up; the "tasks" refers to the name given to the url, now it is easier to call the url; can be any url. Remember that import is necessary 
    task_to_delete = request.GET.get('del') #the information on Post should be the text going into the form, the task to delete is the phrases so it should stay in GET
    if task_to_delete and int(task_to_delete) in request.session:
        request.session[int(task_to_delete)] = mark_safe('<s>'+request.session[int(task_to_delete)]+'</s>')
    return render_to_response('tasks.html', RequestContext(request, {
        'tasks_number': tasks_number,
        'tasks': request.session,
        'task_form': task_form
    }))
Beispiel #3
0
 def create(self, request):
     form = TaskForm(request.POST)
     if form.is_valid():
         form.instance.user = request.user
         task = form.save()
         return task
     return rc.BAD_REQUEST
Beispiel #4
0
def edit(request, pk):
    obj = get_object_or_404(Task, pk=pk)
    form = TaskForm(request.POST or None, instance=obj)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return redirect(reverse('todo:detail', kwargs={'pk': pk}))
    return render(request, 'todo/task_form.html', {'form': form, 'type': 'UPDATE'})
Beispiel #5
0
def TaskEdit(request, pk):
    item = Task.objects.get(id=pk)
    form = TaskForm(instance=item)
    if request.method == "POST":
        form = TaskForm(request.POST, instance=item)
        if form.is_valid():
            form.save()
            return redirect('/')
    return render(request, 'index.html', {'form': form})
Beispiel #6
0
def TodoApp(request):
    list = Task.objects.all()
    form = TaskForm()
    if request.method == "POST":
        form = TaskForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')
    return render(request, 'index.html', {'form': form, 'list': list})
Beispiel #7
0
def update_task(request, pk):
    task = get_object_or_404(Task, id=pk)
    form = TaskForm(instance=task)
    if request.method == "POST":
        form = TaskForm(request.POST, instance=task)
        if form.is_valid():
            form.save()
        return redirect(reverse("list"))
    context = {"form": form}
    return render(request, "update.html", context)
Beispiel #8
0
 def update(self, request, task_id):
     try:
         task = Task.objects.get(pk=task_id, user=request.user)
     except Task.DoesNotExist:
         return rc.NOT_FOUND
     form = TaskForm(request.PUT, instance=task)
     if form.is_valid():
         task = form.save()
         return task
     return rc.BAD_REQUEST
Beispiel #9
0
def add(request):
    if request.method == 'POST':
        form = TaskForm(request.POST)
        if form.is_valid():
            update_or_create_task(form, None)
            messages.success(request, 'ToDo successful created!')
            return redirect('index')
    else:
        form = TaskForm()
    return render(request, 'add.html', { 'form': form })
Beispiel #10
0
def todo(request):
    if request.method == 'POST':
        form = TaskForm(request.POST or None)
        if form.is_valid():
            form.save()
        messages.success(request,("New task added!!"))
        return redirect('todo')
    else:
        all_tasks = TaskList.objects.all
        return render(request,'todo.html',{'all_task' : all_tasks})
Beispiel #11
0
def create_task_method(request):
    form = TaskForm(
        request.POST or None
    )  # jeśli metoda POST to renderuj ten formularz a jeśli nie ma danych to renderuj pusty formularz
    if form.is_valid():
        form.save(commit=True)
        # form = BookForm()  # odświeża formularz, po zapisaniu będą puste pola. To już niepotrzebne, bo po wysłaniu przenosi na inną stronę
        return redirect("/todo/task/new/added/")
    context = {"form": form}
    return render(request, "todo/tasks/createtask.html", context)
Beispiel #12
0
def edit_task(request, task_id):
    if request.method == "POST":
        task = TaskList.objects.get(pk=task_id)
        form = TaskForm(request.POST or None, instance=task)
        if form.is_valid():
            form.save()
            messages.success(request, 'Task edited')
        return redirect('tasks')
    else:
        task_obj = TaskList.objects.get(pk=task_id)
        return render(request, 'todo/edit.html', {'task_obj': task_obj})
Beispiel #13
0
def edit(request, todo_id):
    todo = get_object_or_404(Todo, id=todo_id)
    if request.method == 'POST':
        form = TaskForm(request.POST)
        if form.is_valid():
            update_or_create_task(form, todo_id)
            messages.success(request, 'ToDo #%s successful updated!' % todo_id)
            return redirect('index')
    else:
        form = TaskForm(initial={ 'task': todo.task, 'progress': todo.progress, 'deadline_date': todo.deadline_date })
    return render(request, 'edit.html', { 'todo': todo, 'form': form })
Beispiel #14
0
def add_task(request):
    domain, is_created = Domain.objects.get_or_create(name=request.subdomain,slug=request.subdomain)
    default_list_name = request.subdomain + "_default"
    if request.method == 'POST' and 'add_task' in request.POST:
        form = TaskForm(domain, default_list_name, request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')
    else:
        form = TaskForm(domain, default_list_name)
        return render_to_response('add_task.html', locals(), context_instance=RequestContext(request))
Beispiel #15
0
def add(request):
    form = TaskForm(request.POST or None)

    if request.method == 'POST':
        if form.is_valid():
            obj = form.save()
            return redirect(reverse('todo:detail', kwargs={'pk': obj.pk}))
        else:
            return render(request, 'todo/task_form.html', {'form': form, 'type': 'add'})

    return render(request, 'todo/task_form.html', {'form': form, 'type': 'ADD'})
Beispiel #16
0
def add_task(request):
    if request.POST:
        form = TaskForm(request.user, request.POST)
        if form.is_valid():
            form = form.save(commit=False)
            form.assignor = request.user
            form.save()
            return redirect(
                reverse("todo:profile", args=(request.user.profile.id, )))
    else:
        form = TaskForm(request.user)
    return render(request, "todo/task.html", {"form": form})
Beispiel #17
0
def index(request):
    tasks = Task.objects.all()
    form = TaskForm()
    if request.method == 'POST':
        print(request.POST)
        form = TaskForm(request.POST)
        if form.is_valid():
            print(form)
            form.save()
        return redirect("/")
    context = {"tasks": tasks, "form": form}
    return render(request, "index.html", context)
Beispiel #18
0
def task_update(request, id):
    """ Update a task
    """
    template_name = 'task_update.html'
    task = get_object_or_404(Task, pk=id)

    print "method %r" % request.method
    if request.method == 'POST':
        form = TaskForm(request.POST or None, **{'request':request})
        print "form valid %r" % form.is_valid()
        if form.is_valid():
            start_date = form.data.get('start_date')
            if start_date:
                start_date = datetime.datetime.strptime(start_date, '%d/%M/%Y')
            else:
                start_date = None
            due_date = form.data.get('due_date')
            if due_date:
                due_date = datetime.datetime.strptime(due_date, '%d/%m/%Y')
            else:
                due_date = None

            completed = form.data.get('completed')
            if not completed:
                completed = False

            task.title = form.data.get('title')
            task.start_date = start_date
            task.due_date = due_date
            task.completed = completed
            task.todo_list_id = form.data.get('todo_list')
            task.priority = form.data.get('priority')

            task.save()
            messages.success(request,
                             "Task updated !")
            return HttpResponseRedirect(reverse('todo.views.task_list'))
        else:
            message_error(form, request)
    return HttpResponseRedirect(reverse('todo.views.task_list'))
Beispiel #19
0
def task_edit_view(request, task_id):
    task = get_object_or_404(Task, pk=task_id, user=request.user)
    if request.method == 'POST':
        form = TaskForm(request.POST, instance=task)
        if form.is_valid():
            form.save()
            return redirect(task_list_view)
    else:
        form = TaskForm(instance=task)
    return {
        'form': form,
        'action': 'edit',
    }
Beispiel #20
0
def task_add_view(request):
    if request.method == 'POST':
        form = TaskForm(request.POST)
        if form.is_valid():
            form.instance.user = request.user
            form.save()
            return redirect(task_list_view)
    else:
        form = TaskForm()
    return {
        'form': form,
        'action': 'add',
    }
Beispiel #21
0
def task_update(request, id):
    """ Update a task
    """
    template_name = 'task_update.html'
    task = get_object_or_404(Task, pk=id)

    print "method %r" % request.method
    if request.method == 'POST':
        form = TaskForm(request.POST or None, **{'request': request})
        print "form valid %r" % form.is_valid()
        if form.is_valid():
            start_date = form.data.get('start_date')
            if start_date:
                start_date = datetime.datetime.strptime(start_date, '%d/%M/%Y')
            else:
                start_date = None
            due_date = form.data.get('due_date')
            if due_date:
                due_date = datetime.datetime.strptime(due_date, '%d/%m/%Y')
            else:
                due_date = None

            completed = form.data.get('completed')
            if not completed:
                completed = False

            task.title = form.data.get('title')
            task.start_date = start_date
            task.due_date = due_date
            task.completed = completed
            task.todo_list_id = form.data.get('todo_list')
            task.priority = form.data.get('priority')

            task.save()
            messages.success(request, "Task updated !")
            return HttpResponseRedirect(reverse('todo.views.task_list'))
        else:
            message_error(form, request)
    return HttpResponseRedirect(reverse('todo.views.task_list'))
Beispiel #22
0
def edit_task(request, task_id):
    task = Task.objects.get(id=task_id)

    if request.POST:
        form = TaskForm(request.user, request.POST, instance=task)
        if form.is_valid():
            form = form.save(commit=False)
            form.assignor = request.user
            form.assignee = task.assignee
            form.save()
            return redirect(reverse("todo:taskboard"))
    else:
        form = TaskForm(request.user, instance=task)
    return render(request, "todo/task.html", {"form": form})
Beispiel #23
0
def add(request):
    colors = Color.objects.all()
    form = TaskForm(request.POST or None)
    form_title = "Create Task"
    user_id = request.session['user_id']
    if form.is_valid():
        form.save()
        return redirect('/tasks')
    return render(
        request, 'task-form.html', {
            'form': form,
            'colors': colors,
            'form_title': form_title,
            'user_id': user_id
        })
Beispiel #24
0
def update(request, id):
    colors = Color.objects.all()
    task = Task.objects.get(id=id)
    form = TaskForm(request.POST or None, instance=task)
    form_title = "Edit Task"
    user_id = request.session['user_id']
    if form.is_valid():
        form.save()
        return redirect('/tasks')
    return render(
        request, 'task-form.html', {
            'form': form,
            'task': task,
            'colors': colors,
            'form_title': form_title,
            'user_id': user_id
        })
Beispiel #25
0
def tasks_view(request):
    #    request.session.clear()
    tasks_number = request.session.get(
        'tasks_number', 0
    )  #we are using a counter to store every new task in; also add new variable to mean the task_number counter
    #    task_text = request.POST.get('add') now that the task_form has come in this is not needed
    initials = {
        'add':
        "Type your task"  #instead of the form being in blank, now things are introduced, but if you press add, this text will be added as if it's a chore on todo list ; to make sure this doesn't happen go to forms and fix clean.self function. Here the initial is a variable of the dictionary that is the initial form, since it's outside of the main body of program, this is outside and not bound by so many restrictions
    }
    task_form = TaskForm(initial=initials)
    if request.POST:
        task_form = TaskForm(
            request.POST, initial=initials
        )  #if a POST comes in make a form out of the TaskForm file
        if task_form.is_valid(
        ):  #if the form is valid, give the data, but the clean_data
            data = task_form.cleaned_data
            priority = data['priority']
            task_text = data['add']
            if task_text and task_text not in request.session.values(
            ):  #if value already in dictionary doesn't show. This line is checking if value is in request session
                #                while tasks_number in request.session:
                tasks_number += 1
                request.session[
                    tasks_number] = task_text  #in the dictionary request.session will be added the key tasks_number and the value will be task_text
                tasks_number += 1
                request.session['tasks_number'] = tasks_number
                return redirect(
                    "tasks"
                )  #after adding correctly a task, it asks the browser to redirect to a page, here it could be the page without the form filled up; the "tasks" refers to the name given to the url, now it is easier to call the url; can be any url. Remember that import is necessary
    task_to_delete = request.GET.get(
        'del'
    )  #the information on Post should be the text going into the form, the task to delete is the phrases so it should stay in GET
    if task_to_delete and int(task_to_delete) in request.session:
        request.session[int(task_to_delete)] = mark_safe(
            '<s>' + request.session[int(task_to_delete)] + '</s>')
    return render_to_response(
        'tasks.html',
        RequestContext(
            request, {
                'tasks_number': tasks_number,
                'tasks': request.session,
                'task_form': task_form
            }))
Beispiel #26
0
def tasks(request):
    if request.method == "POST":
        form = TaskForm(request.POST or None)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.owner = request.user  # owner es el campo definido como ForeignKey.
            instance.save()
            messages.success(request, 'New Task added.')
        return redirect('tasks')
    else:
        # all_tasks = TaskList.objects.all()
        all_tasks = TaskList.objects.filter(owner=request.user)
        paginator = Paginator(
            all_tasks, 5,
            orphans=1)  # Orphans. Ver referencia en Paginator.Django.
        page = request.GET.get('pg')
        all_tasks = paginator.get_page(page)
        return render(request, 'todo/tasks.html', {'all_tasks': all_tasks})
Beispiel #27
0
def add(request):
    # This function SHOULD be executed when the user enters a new task on the index page.
    # This function can also be used to save the data into the database. Towards that, using forms
    # as explained above can make it easier to validate and save form data.
    if request.method == 'POST':
        form = TaskForm(request.POST)

        if form.is_valid():
            form.save()
            return index(request)

        else:
            return render(request, 'todo/update.html')

    else:
        form = TaskForm()
        context = {'form': form}
        return render(request, 'todo/update.html', context)
Beispiel #28
0
def task_create(request):
    """ Create new task view
    """
    if request.method == 'POST':
        # If the form has been submitted...
        form = TaskForm(request.POST, **{'request': request})
        # A form bound to the POST data
        print "Process the form"
        if form.is_valid():
            print("Form is valid")
            # All validation rules pass
            # Process the data in form.cleaned_data
            start_date = form.data.get('start_date')
            print start_date
            if start_date:
                start_date = datetime.datetime.strptime(start_date, '%m/%d/%Y')
            else:
                start_date = None
            due_date = form.data.get('due_date')
            if due_date:
                due_date = datetime.datetime.strptime(due_date, '%m/%d/%Y')
            else:
                due_date = None

            completed = form.data.get('completed')
            if not completed:
                completed = False

            t = Task(title=form.data.get('title'),
                     start_date=start_date,
                     due_date=due_date,
                     completed=completed,
                     todo_list_id=form.data.get('todo_list'),
                     priority=form.data.get('priority'))
            messages.success(request, 'New task successfully created')
            t.save()
            return HttpResponseRedirect(reverse('todo.views.task_list'))
        print "Form is invalid"
        message_error(form, request)
    else:
        # An unbound form
        form = TaskForm(**{'request': request})
    return render_to_response('new_task.html', {'form': form},
                              context_instance=RequestContext(request))
Beispiel #29
0
def task_create(request):
    """ Create new task view
    """
    if request.method == 'POST':
        # If the form has been submitted...
        form = TaskForm(request.POST, **{'request':request})
        # A form bound to the POST data
        print "Process the form"
        if form.is_valid():
            print("Form is valid")
            # All validation rules pass
            # Process the data in form.cleaned_data
            start_date = form.data.get('start_date')
            print start_date
            if start_date:
                start_date = datetime.datetime.strptime(start_date, '%m/%d/%Y')
            else:
                start_date = None
            due_date = form.data.get('due_date')
            if due_date:
                due_date = datetime.datetime.strptime(due_date, '%m/%d/%Y')
            else:
                due_date = None

            completed = form.data.get('completed')
            if not completed:
                completed = False

            t = Task(title=form.data.get('title'), start_date=start_date,
                     due_date=due_date, completed=completed,
                     todo_list_id=form.data.get('todo_list'),
                     priority=form.data.get('priority'))
            messages.success(request, 'New task successfully created')
            t.save()
            return HttpResponseRedirect(reverse('todo.views.task_list'))
        print "Form is invalid"
        message_error(form, request)
    else:
        # An unbound form
        form = TaskForm(**{'request':request})
    return render_to_response('new_task.html', {'form': form},
                              context_instance=RequestContext(request))
Beispiel #30
0
def adding(request):
    if request.method == 'POST':
        task_form = TaskForm(data=request.POST)
        if task_form.is_valid():
            title = task_form.cleaned_data['title']
            task = task_form.cleaned_data['task']
            user = task_form.cleaned_data['user']
            priority = task_form.cleaned_data['priority']
            category = task_form.cleaned_data['category']
            due_time = task_form.cleaned_data['due_time']
            Task.objects.create(
                title=title,
                task=task,
                user=user,
                priority=priority,
                category=category,
                due_time=due_time)
            return HttpResponseRedirect(reverse('tasks'))
    else:
        task_form = TaskForm()
        return render(request, 'todo/adding.html', {'task_form' : task_form})