def add(): if 'username' not in session: return redirect('/') if request.method == 'GET': return render_template('add.html') elif request.method == 'POST': form = forms.TaskForm(request.form) if not form.validate(): return render_template('error.html', message='Invalid task'), 400 user = db.get(session['username']) if len(user['tasks']) >= 5: return render_template('error.html', message='Maximum task limit reached!'), 400 task = { 'title': form.title.data, 'content': form.content.data, 'priority': form.priority.data, 'id': len(user['tasks']) } user['tasks'].append(task) db.put(session['username'], user) return redirect('/tasks')
def editTask(user_id, task_id): task = models.Todo.get(id=task_id) form = forms.TaskForm(obj=task) if form.validate_on_submit(): try: if form.title.data: updateTitle = models.Todo.update(title=form.title.data).where( models.Todo.id == task_id) updateTitle.execute() if form.date.data: updateDate = models.Todo.update(date=form.date.data).where( models.Todo.id == task_id) updateDate.execute() if form.content.data: updateContent = models.Todo.update( content=form.content.data).where(models.Todo.id == task_id) updateContent.execute() if form.title.data is not models.Todo.select('priority').where( models.Todo.id == task_id): updatePriority = models.Todo.update( priority=form.priority.data).where( models.Todo.id == task_id) updatePriority.execute() todo = models.Todo.get() return redirect(url_for('main', user_id=user_id)) except AttributeError: raise ValueError('There is some wrong field here!') return render_template('edit_task.html', form=form)
def put(self, request, task_id=None): task = get_object_or_404(Task, pk=task_id) request.PUT['hub'] = task.hub.pk request.PUT['state'] = request.PUT.get('state', Task.STATE_NEW) form = forms.TaskForm(request.PUT, instance=task, request=request) if form.is_valid(): T = form.save() self.res.write(T.as_json(request_user=request.user)) else: self.res.write(json.dumps(form.errors)) self.res.status_code = form.status_code return self.res
def newTask(user_id): form = forms.TaskForm() if form.validate_on_submit(): try: flash("You've added a new task!") models.Todo.create_task(title=form.title.data, content=form.content.data, priority=form.priority.data, userid=user_id, is_done=False) todo = models.Todo.get() return redirect(url_for('main', todo=todo, user_id=user_id)) except AttributeError: raise ValueError('There is some wrong field here!') return render_template('new_task.html', form=form)
def post(self, request, task_id=None, image=None): if image: return self.image_upload(request, task_id) request.JSON['state'] = request.JSON.get('state', Task.STATE_NEW) form = forms.TaskForm(request.JSON, request=request) if form.is_valid(): T = form.save(commit=False) T.owner = request.user.profile T.save() self.res.write(T.as_json(request_user=request.user)) else: self.res.write(json.dumps(form.errors)) self.res.status_code = form.status_code return self.res
def image_upload(self, request, task_id): res = HttpResponse() # Handle image upload task = get_object_or_404(Task, pk=task_id) request.POST['state'] = task.state request.POST['hub'] = task.hub_id form = forms.TaskForm(request.POST, request.FILES, instance=task, request=request) if form.is_valid(): T = form.save() res.write(json.dumps({'image': T.image.name})) res.status_code = 201 else: res.write(json.dumps({'error': 'Cannot upload image'})) res.status_code = 400 return res
def newTask(user_id): form = forms.TaskForm(request.form) if request.method == 'POST' and form.validate(): title = form.title.data, content = form.content.data, priority = form.priority.data, date = form.date.data, date_time = form.date_time.data, userid = user_id, is_done = False # Create cursor cur = mysql.connection.cursor() # Execute query cur.execute("INSERT INTO todo(title, content, priority, date, date_time, people, is_done) VALUES(%s, %s, %s, %s, %s, %s, %s)", (title, content, priority, date, date_time, userid, is_done)) # Commit to DB mysql.connection.commit() # Close connection cur.close() return redirect(url_for('mainr', user_id=user_id)) return render_template('new_task.html', form=form)