def post(self, request): if request.is_ajax(): # import pdb # pdb.set_trace() # from datetime import datetime # todo = Todo(due=datetime.today()) form = AddTodoForm(request.POST) # import pdb if form.is_valid(): # print request.POST['title'] # from datetime import datetime # td = Todo.objects.create(title=request.POST['title']) td = form.save() # td.save() response_data = {'status': 'success', 'data': { 'id': td.id, 'title': td.title, 'due': td.due, }, } else: response_data = {'status': 'error', 'errors': form.errors} return JsonResponse(response_data) else: data = { 'errors': 'AJAX not used', } return JsonResponse(data)
def post(self, request, todo_id): try: todo = Todo.objects.get(pk=todo_id) except Todo.DoesNotExist: return JsonResponse({'status': 'error', 'errors': 'Todo does not exist'}) if request.is_ajax(): form = AddTodoForm(request.POST, instance=todo) if form.is_valid(): from datetime import datetime id = form.cleaned_data['id'] todo = Todo.objects.get(pk=id) todo.title = form.cleaned_data['title'] todo.save() response_data = {'status': 'success', 'data':{'id': todo.pk, 'title': todo.title, } } else: response_data = {'status': 'error', 'errors': form.errors} return JsonResponse(form.errors) else: data = { 'errors': 'AJAX not used', } return HttpResponse(JsonResponse(data))
def home(): form = AddTodoForm() todos = current_user.todos # current_user is an instance of User today = date.today() day = today.day if 4 <= day <= 20 or 24 <= day <= 30: suffix = "th" else: suffix = ["st", "nd", "rd"][day % 10 - 1] literal_date = today.strftime("%A, %B %d{} %Y".format(suffix)) return render_template("list_of_todos.html", todos=todos, date=literal_date, form=form)
def dashboard(): form = AddTodoForm() current_todos = Todo.query.filter_by(created_by=g.user.user_id, status='current') completed_todos = Todo.query.filter_by(created_by=g.user.user_id, status='completed') if request.method == 'POST': if 'submit' in request.form.keys(): name = request.form['submit'] todo = Todo.query.filter_by(name=name, created_by=g.user.user_id).first() print(name) todo.status = 'completed' db.session.add(todo) db.session.commit() return redirect(url_for('dashboard')) name = request.form['name'] todo = Todo(name=name, status='current') todo.created_by = g.user.user_id db.session.add(todo) db.session.commit() return redirect(url_for('dashboard')) return render_template('dashboard.html', form=form, current_todos=current_todos, completed_todos=completed_todos)