Beispiel #1
0
def index(request):

    # See http://stackoverflow.com/questions/2406537/django-formsets-make-first-required/4951032#4951032
    class RequiredFormSet(BaseFormSet):
        def __init__(self, *args, **kwargs):
            super(RequiredFormSet, self).__init__(*args, **kwargs)
            for form in self.forms:
                form.empty_permitted = False

                # si se fija aca le puse extra 3 para que me pinte 3 formularios de tareas al mismo tiempo
                # si se lo quito pues pone solo uno prueben

    TodoArticuloFormSet = formset_factory(TodoArticuloForm, extra=3, max_num=10, formset=RequiredFormSet)

    if request.method == "POST":

        todo_form = TodoForm(request.POST)
        todo_articulo_formset = TodoArticuloFormSet(request.POST, request.FILES)

        if todo_form.is_valid() and todo_articulo_formset.is_valid():
            todo_lista = todo_form.save()
            for form in todo_articulo_formset.forms:
                todo_item = form.save(commit=False)
                todo_item.fktodo = todo_lista
                todo_item.save()

            return HttpResponseRedirect("/")
    else:
        todo_form = TodoForm()
        todo_articulo_formset = TodoArticuloFormSet()

    todo = Todo.objects.all().order_by("id")  # Select * from Todo ;
    return render_to_response("index.html", locals(), RequestContext(request))
Beispiel #2
0
def todo_new(request):
    url = reverse('todo_new')
    if request.method == 'GET':
        form = TodoForm()
        context = {'form': form, 'url': url}
        return render(request, 'todo/todo_new.html',
                      context)
    elif request.method == 'POST':
        form = TodoForm(request.POST)
        if form.is_valid():
            messages.add_message(request, messages.INFO,
                                 'Successfully created todo')
            data = form.cleaned_data
            if data['due_date']:
                todo = Todo.objects.create(
                    name=data['name'],
                    is_completed=data['is_completed'],
                    due_date=data['due_date'],
                    notes=data['notes'],
                    user=request.user)
            else:
                todo = Todo.objects.create(
                    name=data['name'],
                    is_completed=data['is_completed'],
                    notes=data['notes'],
                    user=request.user)
            context = {'todo': todo}
            return render(request,
                          'todo/todo_detail.html',
                          context)
        else:
            context = {'form': form, 'url': url}
            return render(request,
                          'todo/todo_new.html',
                          context)
Beispiel #3
0
def todo_edit(request, pk):
    if request.method == 'GET':
        todo = get_object_or_404(Todo, pk=pk)
        url = reverse('todo_edit', args=(todo.id,))
        initial_data = {'id': todo.id,
                        'name': todo.name,
                        'notes': todo.notes,
                        'is_completed': todo.is_completed,
                        'due_date': todo.due_date}
        form = TodoForm(initial=initial_data)
        context = {'form': form, 'submit_url': url}
        return render(request,
                      'todo/todo_new.html',
                      context)
    elif request.method == 'POST':
        form = TodoForm(request.POST)
        todo = get_object_or_404(Todo, pk=pk)
        url = reverse('todo_edit', args=(todo.id,))
        if form.is_valid():
            data = form.cleaned_data
            todo.name = data['name']
            todo.is_completed = data['is_completed']
            todo.notes = data['notes']
            todo.due_date = data['due_date']
            todo.save()
            messages.add_message(request, messages.INFO,
                                 'Successfully updated todo')
            context = {'todo': todo}
            return redirect('todo_detail', todo.id)
        else:
            context = {'form': form,
                       'submit_url': url}
            return render(request,
                          'todo/todo_new.html',
                          context)
Beispiel #4
0
def add_or_update_task(request, pk=None):
    task = get_object_or_404(Todo, pk=pk) if pk else None
    form = TodoForm(request.user, request.POST or None, instance=task)
    if form.is_valid():
        form.save()
        return redirect(reverse('todo:home'))
    return render(request, 'todo/task_form.html', {'form': form})
Beispiel #5
0
def todoedit(request, pk):
    todo = get_object_or_404(Todo, pk=pk)
    todoform = TodoForm(request.POST or None, instance=todo)
    if todoform.is_valid():
        todoform.save()
        return redirect('todolist')
    return render(request, 'todoadd.html', {'form': todoform})
Beispiel #6
0
def addTodo(request):
    form = TodoForm(request.POST)

    if form.is_valid():
        new_todo = Todo(text=request.POST['text'])
        new_todo.save()
    return redirect('index')
Beispiel #7
0
def index(request):

	# See http://stackoverflow.com/questions/2406537/django-formsets-make-first-required/4951032#4951032
	class RequiredFormSet(BaseFormSet):
		def __init__(self, *args, **kwargs):
			super(RequiredFormSet, self).__init__(*args, **kwargs)
			for form in self.forms:
				form.empty_permitted = False

	#si se fija aca le puse extra 3 para que me pinte 3 formularios de tareas al mismo tiempo
	#si se lo quito pues pone solo uno prueben 
	TodoArticuloFormSet = formset_factory(TodoArticuloForm, extra=3, max_num=10, formset=RequiredFormSet)
	

	if request.method == 'POST':

		todo_form = TodoForm(request.POST)
		todo_articulo_formset = TodoArticuloFormSet(request.POST, request.FILES)

		if todo_form.is_valid() and todo_articulo_formset.is_valid():
			todo_lista = todo_form.save()
			for form in todo_articulo_formset.forms:
				todo_item = form.save(commit=False)
				todo_item.fktodo = todo_lista
				todo_item.save()

			return HttpResponseRedirect('/')
	else:
		todo_form = TodoForm()
		todo_articulo_formset = TodoArticuloFormSet()

	todo = Todo.objects.all().order_by('id') #Select * from Todo ;
	return  render_to_response('index.html', locals(),
								RequestContext(request))
Beispiel #8
0
def todo_create(request):
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('todo_list')
    form = TodoForm()
    return render(request, 'todo/edit.html', {'form': form})
Beispiel #9
0
    def test_form_is_valid(self):
        obj = Todo.objects.create(text='Do this', owner=self.user)
        data = {'text': obj.text}
        form = TodoForm(data=data)

        self.assertTrue(form.is_valid())
        self.assertEqual(form.cleaned_data.get('text'), obj.text)
        self.assertNotEqual(form.cleaned_data.get('text'), 'Sleep')
Beispiel #10
0
def add(request):
    form = TodoForm(request.POST or None)
    if form.is_valid():
        form.save()
        return http.HttpResponseRedirect(reverse('todo.list'))

    return render_to_response('add.html', {'form': form},
                              context_instance=RequestContext(request))
Beispiel #11
0
def add(request):
    form = TodoForm(request.POST or None)
    if form.is_valid():
        form.save()
        return http.HttpResponseRedirect(reverse('todo.list'))
    
    return render_to_response('add.html', {'form':form},
                              context_instance=RequestContext(request))
Beispiel #12
0
 def test_valid(self):
     """バリデーションOK"""
     params = {
         'title': 'title-1',
         'expiration_date': self.today,
     }
     form = TodoForm(params)
     self.assertTrue(form.is_valid())
Beispiel #13
0
def todoadd(request):
    todoform=TodoForm(request.POST or None)
    if todoform.is_valid():
        todo = todoform.save()
        todo.user = request.user
        todo.save()
        return redirect('todolist')
    return render(request,'todoadd.html',{'form':todoform})
Beispiel #14
0
def edit(request, pk):
    instance = get_object_or_404(Todo, pk=pk)
    form = TodoForm(request.POST or None, instance=instance)
    if form.is_valid():
        form.save()
        return http.HttpResponseRedirect(reverse('todo.list'))

    return render_to_response('edit.html', {'form': form},
                              context_instance=RequestContext(request))
Beispiel #15
0
def edit(request, pk):
    instance = get_object_or_404(Todo, pk=pk)
    form = TodoForm(request.POST or None, instance=instance)
    if form.is_valid():
        form.save()
        return http.HttpResponseRedirect(reverse('todo.list'))
    
    return render_to_response('edit.html', {'form':form},
                              context_instance=RequestContext(request))
def create_todo(request):
    if request.method == 'GET':
        form = TodoForm(user=request.user)
    elif request.method == 'POST':
        form = TodoForm(request.POST, user=request.user)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')
    return render(request, "todo_form.html", {'form': form})
Beispiel #17
0
def create_todo(request):
    form = TodoForm(request.POST)

    if form.is_valid():
        todo = TodoList(name=form.cleaned_data['name'], description=form.cleaned_data['description'], is_done=False)
        todo.save()
        return redirect('todos index')

    return home(request, form)
Beispiel #18
0
    def post(self, request, pk):
        task = get_object_or_404(self.model, pk=pk)
        form = TodoForm(request.POST, instance=blog)
        if not form.is_valid():
            ctx = {'task': task.name, 'desc': task.desc}
            return render(request, self.template, ctx)

        form.save()
        return redirect(self.success_url)
Beispiel #19
0
    def test_valid_if_expiration_date_is_not_specified(self):
        """バリデーションOK

        期限日が指定されていない場合
        """
        params = {
            'title': 'title-1',
        }
        form = TodoForm(params)
        self.assertTrue(form.is_valid())
Beispiel #20
0
def todo_edit(request, pk):
    todo = List.objects.get(user=request.user, pk=pk)
    if request.method == "POST":
        form = TodoForm(request.POST, instance=todo)
        if form.is_valid():
            form.save()
            return redirect('todo_view', todo.pk)
    else:
        form = TodoForm(instance=todo)
    return render(request, 'todo/edit.html', {'menu': 'list', 'form': form, 'pk': pk})
Beispiel #21
0
def todo_create(request):
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            todo = form.save(commit=False)
            todo.user = request.user
            todo.save()
            return redirect('todo_view', todo.pk)
    else:
        form = TodoForm()
    return render(request, 'todo/edit.html', {'menu': 'list', 'form': form})
Beispiel #22
0
    def test_invalid_if_title_is_empty(self):
        """バリデーションNG

        タイトルが空文字の場合
        """
        params = {
            'title': '',
            'expiration_date': self.today,
        }
        form = TodoForm(params)
        self.assertFalse(form.is_valid())
Beispiel #23
0
def todo_add(request):
    if request.method == 'POST':
        form = TodoForm(request.POST, request.FILES)
        if form.is_valid():
            todo = form.save()
            return redirect('todo_detail', pk=todo.pk)
    form = TodoForm()
    context = {
        'form': form,
    }
    return render(request, 'todo/todo_create.html', context)
Beispiel #24
0
def todoView(request):
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            item = form.save(commit=False)
            item.createUser = request.user.username
            item.save()

    items = Todo.objects.filter(createUser=request.user.username).reverse()
    form = TodoForm()
    return render(request, 'todo.html', {'items': items, 'form': form})
Beispiel #25
0
def todo_edit(request, pk):
    task = Todo.objects.get(pk=pk)
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            task.description = request.POST.get('description')
            task.status = request.POST.get('status')
            task.save()
            return redirect('todo_list')
    form = TodoForm(instance=task)
    return render(request, 'todo/edit.html', {'form': form})
Beispiel #26
0
def add(request):
    if request.user.is_authenticated:
        user = request.user
    form = TodoForm(request.POST)
    if form.is_valid():
        todo = form.save(commit=False)
        todo.user = user
        todo.save()
        return redirect('home')
    else:
        return render(request, 'todo/home.html', context={'form': form})
Beispiel #27
0
def new_todo(request):
    if not request.method == 'POST':
        return redirect('home')
    todo_form = TodoForm(request.POST)
    if todo_form.is_valid():
        todo_form.save()
        messages.add_message(request, messages.SUCCESS,
                             'Item has been added.')
    else:
        messages.add_message(request, messages.ERROR,
                             'You need to enter a name.')
    return redirect('home')
Beispiel #28
0
def update(request, id):
    if request.method == 'POST':
        todo = get_object_or_404(Todo, pk=id)
        todoForm = TodoForm(request.POST,instance=todo)
        if todoForm.is_valid():
            todoForm.save()

    context = {
        'message': 'Update article ' + str(id),
        'todo': todo,
    }
    return HttpResponseRedirect('/todo/',context)
Beispiel #29
0
def edit_todo(req, pk):
    todo = TodoList.objects.get(pk=pk)
    if req.method == 'GET':
        form = TodoForm(initial=todo.__dict__)
        return home(req, form, 'edit todo', pk=pk)
    else:
        form = TodoForm(req.POST)
        if form.is_valid():
            todo.name = form.cleaned_data['name']
            todo.description = form.cleaned_data['description']
            todo.save()
        return home(req, form)
Beispiel #30
0
def AddTodo(request):
    if not request.user.is_authenticated():
        return redirect('accounts:login')
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            todo = form.save(commit=False)
            todo.user = request.user
            todo.save()
            return IndexView(request)
    else:
        form = TodoForm()
    return render(request, 'addtodo.html', {'form': form})
Beispiel #31
0
def add_todo(request):
    """Add a new todo. Should be invoked via POST method."""
    if request.method == 'POST':
        form = TodoForm(request.POST)
        if form.is_valid():
            # should log the action
            form.save()
            messages.success(request,
                             f"Added \"{request.POST['description']}\"")
            return redirect('todo:index')
    else:  # GET /add/ should be handled by the main index
        return redirect('todo:index')
    return render(request, 'todo/add.html', {'form': form})
Beispiel #32
0
def add_todo(request):
	form = TodoForm()
	if request.method == 'POST':
		print("Post")
		form = TodoForm(request.POST)
		# Have we been provided with a valid form?
		if form.is_valid():
			print(form.is_valid())
			# Save the new item to the database.
			form.save(commit=True)
			print("balh")
			# Now that the category is saved
			# We could give a confirmation message
			# But since the most recent category added is on the index page
			# Then we can direct the user back to the index page.
			return index(request)
		else:
			# The supplied form contained errors -
			# just print them to the terminal.
			print(form.errors)
			# Will handle the bad form, new form, or no form supplied cases.
		# Render the form with error messages (if any).
	return render(request, 'todo/add_todo.html', {'form': form})
Beispiel #33
0
def addItem(request):
    t = Todo()
    t.text = request.POST.get('todo_text', '')
    t.save()
    
    if request.method == 'POST':
        form = TodoForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')
        #Item.objects.create(text=request.POST['item_text'])
        #return redirect('/')

    else:
        return render(request, 'todo/create.html', {'form': form})
Beispiel #34
0
def create_todo(request):
    args = {}
    args.update(csrf(request))
    args['user'] = request.user
    if request.method == 'POST':
        form = TodoForm(request.POST)
        if form.is_valid():
            todo = form.save(commit=False)
            todo.user = request.user
            todo.save()
            return HttpResponseRedirect('/todo/all')
    else:
        form = TodoForm()
    args['form'] = form
    return render_to_response('create_todo.html', args)
Beispiel #35
0
def edit_todo(request: HttpRequest, todo_id: int = 0):
    context = {'todo_lists': TodoList.objects.filter(user=request.user).all()}
    todo = get_object_or_404(Todo, pk=todo_id)
    if todo.todo_list.user != request.user:
        raise Http404
    if request.method == 'POST':
        form = TodoForm(request.POST, instance=todo)
        if not form.is_valid():
            context['form'] = form
            return render(request, 'edit_todo.html', context)
        todo = form.save()
        return redirect('view_list', todo.todo_list.id)
    form = TodoForm(instance=todo)
    context['form'] = form
    return render(request, 'edit_todo.html', context)
Beispiel #36
0
def create_todo(request: HttpRequest, list_id: int = 0):
    context = {'todo_lists': TodoList.objects.filter(user=request.user).all()}
    todo_list = get_object_or_404(TodoList, pk=list_id)
    if request.method == 'POST':
        form = TodoForm(request.POST)
        if not form.is_valid():
            context['form'] = form
            return render(request, 'create_todo.html', context)
        todo = form.save(commit=False)
        todo.todo_list = todo_list
        todo.save()
        return redirect('view_list', todo_list.id)
    form = TodoForm()
    context['form'] = form
    return render(request, 'create_todo.html', context)
Beispiel #37
0
def ManageAddView(request, username=''):

    user = User.objects.get(username=username)
    form = TodoForm(request.POST or None)
    if form.is_valid():
        thisTodo = form.cleaned_data['todo']
        thisPriority = form.cleaned_data['priority']
        thisUser = User.objects.get(username=username)

        todo = Todo(user=thisUser, todo=thisTodo, priority=thisPriority, status=1)
        todo.save()

        return HttpResponseRedirect(URL_TODO_MANAGE(user.username))

    return HttpResponseRedirect(URL_TODO_MANAGE(user.username))
Beispiel #38
0
    def post(self, request, pk):
        form = TodoForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            status = form.cleaned_data['status']
            todo = self.query_set(pk=pk)
            todo.title = title
            todo.content = content
            todo.status = status
            todo.save()
            return HttpResponseRedirect(
                reverse(viewname='todo_detail', kwargs={'pk': pk}))

        return HttpResponseRedirect(reverse(viewname='edit', kwargs={'pk':
                                                                     pk}))
Beispiel #39
0
def edit_task(request, task_id):
    if request.method == "POST":
        task = TodoList.objects.get(pk=task_id)
        form = TodoForm(request.POST or None, instance=task)
        if form.is_valid():
            form.save()
        messages.success(request, ("Task Edited!"))
        return redirect('addTodo')
    else:
        form = TodoForm()
        # for checking status of task
        task_obj = TodoList.objects.get(pk=task_id)
        return render(request, 'edit.html', {
            'task_obj': task_obj,
            'form': form
        })
Beispiel #40
0
def ManageUpdateView(request, username='', todoid=''):
 
    todo = Todo.objects.get(id=todoid)
    user = User.objects.get(username=username)

    form = TodoForm(request.POST or None)


    if form.is_valid():
        todo.todo = form.cleaned_data['todo']
        todo.priority = form.cleaned_data['priority']
        todo.save()
        return HttpResponseRedirect(URL_TODO_MANAGE(user.username))
    else:
        template_name = 'todo/manager_update.html'
        template_value = {'form': form, 'todo': todo, 'user': user}
        return render_to_response(template_name, template_value, context_instance=RequestContext(request))
Beispiel #41
0
 def test_form_invalid(self):
     todo = TodoForm()
     self.assertEqual(todo.is_valid(),False)
Beispiel #42
0
 def test_form(self):
     todo = TodoForm({"activity":"esto es un texto"})
     self.assertEqual(todo.is_valid(), True)
Beispiel #43
0
 def _post(self, request):
     form = TodoForm(request.POST, instance=Todo())
     if form.is_valid():
         # get data from form
         # form.cleaned_data.get('title')
         form.save()
Beispiel #44
0
 def _update(self, data):
     todo = Todo.get_todos(data.get('id'))
     data.update({'completed': json.loads(data.get('completed'))})
     form = TodoForm(data, instance=todo)
     if form.is_valid():
         form.save()