def createtask(self, request, **kwargs): self.is_authenticated(request) # check if the id provided is correct or not data = json.loads(request.body) print("createtask method of class TaskResource") try: list_id = List.objects.get(id=data['listid']) except List.DoesNotExist: return self.create_response( request, { 'error-message': 'The task you are trying to add cannot be added as list with provided id doesnot exist.' }) # check if the user have authority or not if list_id.author != request.user: # # check if list is shared with user try: valid_2 = Share.objects.get(listid=list_id, user=request.user) except Share.DoesNotExist: return self.create_response( request, {'error-message': "You don't have access to this list."}) q = Task(listid=list_id, task=data['task_name']) q.save() return self.create_response( request, { 'status': '200', 'message': 'task added to the list successfully' })
def db_with_some_tasks(app): task1 = Task(id=1, description='First Task') task2 = Task(id=2, description='Second Task') task3 = Task(id=3, description='Third Task') task4 = Task(id=4, description='Fourth Task', active=False) db.session.add_all([task1, task2, task3, task4]) db.session.commit()
def main_page(request): if request.method == 'POST': print(request.POST) form = NewTaskForm(request.POST) if form.is_valid(): task = Task( title = form.cleaned_data['title'], description = form.cleaned_data['description'], end_date = form.cleaned_data['end_date'] ) task.save() else: form = NewTaskForm() tasks = Task.objects.all() return render(request, 'main_page.html', {'form': form, 'tasks': tasks})
def test_task_model(self): task = Task(description='Desc') db.session.add(task) db.session.commit() assert task.description == 'Desc' assert task.active is True assert repr(task) == '<Task Desc>'
class TestTaskViews(TestCase): def setUp(self): self.task = Task(content='test') self.task.save() def test_home(self): result = self.client.get('') self.assertEqual(result.status_code, 200) def test_create_task(self): result = self.client.get('/create/') self.assertEqual(result.status_code, 200) def test_edit_task(self): result = self.client.get(f'/edit/{self.task.id}/') self.assertEquals(result.status_code, 200) def test_remove_task(self): result = self.client.get(f'/remove/{self.task.id}/') self.assertEquals(result.status_code, 302)
def index(): # form input if request.method == 'POST': if not current_user.is_authenticated: flash('Please Login.') return redirect(url_for('index')) title = request.form.get('title') create_time = datetime.datetime.now() user_id = current_user.get_id() # check if valid if not title or len(title) > 20: flash('Invalid input.') return redirect(url_for('index')) task = Task(title=title, create_time=create_time, user_id=user_id) db.session.add(task) db.session.commit() flash('Task created.') return redirect(url_for('index')) tasks = Task.query.filter(Task.user_id == current_user.get_id()) return render_template('index.html', tasks=tasks)
def setUp(self): self.task = Task(content='test') self.task.save()
def large_db(app): tasks = [ Task(id=i, description=f'Task {i:02d}', active=(i < 50)) for i in range(100) ] db.session.add_all(tasks) db.session.commit()