Ejemplo n.º 1
0
def test_create_and_get_item(app):
    text = 'Yoooloooo'
    item = TodoItem(text=text)
    item.save()
    item_fetched = TodoItem.select().where(
        TodoItem.id==item.id
    ).get()

    assert item.id is not None
    assert item.id == item_fetched.id
Ejemplo n.º 2
0
def todo(request, project_name):
    """Allows to create a new todolist and todoitems.
    Actions available here:
    Add a todolist: Owner Participant
    Add a todoitem: Owner Participant
    """
    project = get_project(request, project_name)
    access = get_access(project, request.user)
    if request.GET.get('includecomplete', 0):
        lists = TodoList.objects.filter(user=request.user, project=project)
    else:
        lists = TodoList.objects.filter(user=request.user, project=project, is_complete_attr=False)
    addlistform = bforms.AddTodoListForm()
    if request.method == 'POST':
        if request.POST.has_key('addlist'):
            addlistform = bforms.AddTodoListForm(project, request.user, request.POST)
            if addlistform.is_valid():
                addlistform.save()
                return HttpResponseRedirect('.')
        elif request.POST.has_key('additem'):
            id = int(request.POST['id'])
            list = TodoList.objects.get(id=id)
            text_id = '%s-text' % list.id
            if request.POST[text_id]:
                item = TodoItem(list=list, text=request.POST[text_id])
                item.save()
        elif request.POST.has_key('listmarkdone'):
            id = int(request.POST['id'])
            list = TodoList.objects.get(id=id)
            list.is_complete = True
            list.save()
            return HttpResponseRedirect('.')
        elif request.POST.has_key('itemmarkdone'):
            id = int(request.POST['id'])
            todoitem = TodoItem.objects.get(id=id)
            todoitem.is_complete = True
            todoitem.save()
            return HttpResponseRedirect('.')

    if request.method == 'GET':
        addlistform = bforms.AddTodoListForm()

    if request.GET.get('csv', ''):
        response, writer = reponse_for_cvs(project=project)
        writer.writerow(('Todo Lists',))
        writer.writerow(TodoList.as_csv_header())
        lists = TodoList.objects.filter(user=request.user, project=project)
        for list in lists:
            writer.writerow(list.as_csv())
        for list in lists:
            for item in list.todoitem_set.all():
                writer.writerow(item.as_csv())
        return response
    payload = {'project': project, 'lists': lists, 'addlistform': addlistform}
    return render(request, 'project/todo.html', payload)
Ejemplo n.º 3
0
def additem(request, label, priority):
    '''Add the item specified in the POST request with the given label
    and optional priority.'''
    if label is None:
        return {'error': 'Must provide a label for a new item'}
    kw = {'label': label, 'user': request.user}
    if priority is not None:
        kw['priority'] = priority
    item = TodoItem(**kw)
    item.save()
    return {'id': item.pk}
Ejemplo n.º 4
0
def todos(request, list_id):
	if request.method == 'OPTIONS':
		resp = HttpResponse()
		resp['Access-Control-Max-Age'] = '3600'
		return resp

	if request.method == 'HEAD':
		last_cursor = TodoItem.get_last_cursor(list_id)
		resp = HttpResponse()
		resp['Link'] = _changes_link(list_id, last_cursor.cur)
		return resp
	elif request.method == 'GET':
		stream = request.GET.get('stream')
		after = request.GET.get('after_change')
		wait = request.META.get('HTTP_WAIT')
		if stream:
			stream = (stream == 'true')
		if wait:
			wait = int(wait)

		if stream:
			set_hold_stream(request, Channel('todos-%s' % list_id))
			return HttpResponse(content_type='text/plain')
		else:
			if after:
				try:
					items, last_cursor = TodoItem.get_after(list_id, after)
				except TodoItem.DoesNotExist:
					return HttpResponseNotFound()
			else:
				items, last_cursor = TodoItem.get_all(list_id)
			resp = _list_response(list_id, items)
			resp['Link'] = _changes_link(list_id, last_cursor.cur)
			if len(items) == 0 and wait:
				set_hold_longpoll(request, Channel('todos-%s' % list_id, prev_id=last_cursor.cur), timeout=wait)
			return resp
	elif request.method == 'POST':
		params = json.loads(request.body)
		i = TodoItem(list_id=list_id)
		if 'text' in params:
			i.text = params['text']
		if 'completed' in params:
			if not isinstance(params['completed'], bool):
				return HttpResponseBadRequest('completed must be a bool\n', content_type='text/plain')
			i.completed = params['completed']
		try:
			cursor = i.save()
		except TodoItem.LimitError:
			return HttpResponseForbidden('limit reached\n', content_type='text/plain')
		if cursor.cur != cursor.prev:
			_publish_item(list_id, i, cursor)
		resp = _item_response(i, status=201)
		resp['Location'] = reverse('todos-item', args=[list_id, i.id])
		return resp
	else:
		return HttpResponseNotAllowed(['HEAD', 'GET', 'POST'])
Ejemplo n.º 5
0
    def post(self):
        json_data = request.get_json(force=True)

        if not json_data:
            return {'message': 'No valid data provided'}, 400

        # Deserialize input
        data, errors = todo_item_schema.load(json_data)
        if errors:
            return errors, 422

        todo_item = TodoItem.objects(title=data['title']).first()
        if todo_item:
            return {
                'message': 'A todo item with the same title already exists'
            }, 400

        todo_item = TodoItem(title=json_data['title'])
        todo_item.save()

        # Serialize and dump input
        result = todo_item_schema.dump(todo_item).data

        return {"status": 'success', 'data': result}, 201
Ejemplo n.º 6
0
 def post(self):
     text = request.form.get('text')
     item = TodoItem(text=text)
     item.save()
     return self.render_todo()
Ejemplo n.º 7
0
def todos(request, list_id):
    if request.method == 'OPTIONS':
        resp = HttpResponse()
        resp['Access-Control-Max-Age'] = '3600'
        return resp

    if request.method == 'HEAD':
        last_cursor = TodoItem.get_last_cursor(list_id)
        resp = HttpResponse()
        resp['Link'] = _changes_link(list_id, last_cursor.cur)
        return resp
    elif request.method == 'GET':
        stream = request.GET.get('stream')
        after = request.GET.get('after_change')
        wait = request.META.get('HTTP_WAIT')
        if stream:
            stream = (stream == 'true')
        if wait:
            wait = int(wait)

        if stream:
            set_hold_stream(request, Channel('todos-%s' % list_id))
            return HttpResponse(content_type='text/plain')
        else:
            if after:
                try:
                    items, last_cursor = TodoItem.get_after(list_id, after)
                except TodoItem.DoesNotExist:
                    return HttpResponseNotFound()
            else:
                items, last_cursor = TodoItem.get_all(list_id)
            resp = _list_response(list_id, items)
            resp['Link'] = _changes_link(list_id, last_cursor.cur)
            if len(items) == 0 and wait:
                set_hold_longpoll(request,
                                  Channel('todos-%s' % list_id,
                                          prev_id=last_cursor.cur),
                                  timeout=wait)
            return resp
    elif request.method == 'POST':
        params = json.loads(request.body)
        i = TodoItem(list_id=list_id)
        if 'text' in params:
            i.text = params['text']
        if 'completed' in params:
            if not isinstance(params['completed'], bool):
                return HttpResponseBadRequest('completed must be a bool\n',
                                              content_type='text/plain')
            i.completed = params['completed']
        try:
            cursor = i.save()
        except TodoItem.LimitError:
            return HttpResponseForbidden('limit reached\n',
                                         content_type='text/plain')
        if cursor.cur != cursor.prev:
            _publish_item(list_id, i, cursor)
        resp = _item_response(i, status=201)
        resp['Location'] = reverse('todos-item', args=[list_id, i.id])
        return resp
    else:
        return HttpResponseNotAllowed(['HEAD', 'GET', 'POST'])