def update(self, request, categoryID): if not request.user.is_authenticated(): return rc.FORBIDDEN try: category = Category.objects.get(id=categoryID) except Category.DoesNotExist: return HttpResponse("Oops, an error has occurred, please try again later", status=500) itemID = request.PUT.get('id') try: todoItem = TodoItem.objects.get(id=itemID) except TodoItem.DoesNotExist: return HttpResponse("Oops, an error has occurred, please try again later",status=500) name = request.PUT.get('itemName') description = request.PUT.get('description', '') completed = request.PUT.get('completed') if not all([name, completed]): return HttpResponse("Oops, an error has occurred, please try again later",status=500) todoItem.name = name todoItem.description = description todoItem.category = category todoItem.completed = util.strToBool(completed) todoItem.save() return HttpResponse(simplejson.dumps(util.toDict(todoItem), cls=DateTimeAwareJSONEncoder, indent=4))
def userItemService(request): """ A JSON provider that supplies all the todo items for this user """ if not request.user.is_authenticated(): return HttpResponse(status=401) todoItems = TodoItem.objects.filter(category__user=request.user) response = simplejson.dumps([toDict(item) for item in todoItems], cls=DateTimeAwareJSONEncoder, indent=4) return HttpResponse(response)
def home(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/login/') categories = Category.objects.filter(user=request.user) responseDict = {'categories':simplejson.dumps([toDict(category) for category in categories], cls=DateTimeAwareJSONEncoder, indent=4)} return render_to_response('home.html', responseDict, context_instance=RequestContext(request))
def newCategory(request): if not request.user.is_authenticated(): return HttpResponse(status=401) name = request.POST.get('name') if not name: return HttpResponse("Please provide a name for your category", status=500) try: category = Category.objects.get(name=name, user=request.user) return HttpResponse("Please provide a name for your category", status=500) except Category.DoesNotExist: pass category = Category.objects.create(name=name, user=request.user, when_created=datetime.datetime.utcnow()) allCategories = Category.objects.filter(user=request.user) responseDict = {} responseDict['newCategory'] = simplejson.dumps(toDict(category), cls=DateTimeAwareJSONEncoder, indent=4) responseDict['allCategories'] = simplejson.dumps([toDict(category) for category in allCategories], cls=DateTimeAwareJSONEncoder, indent=4) return HttpResponse(response)
def read(self, request, categoryID): if not request.user.is_authenticated(): return rc.FORBIDDEN try: category = Category.objects.get(id=categoryID) except Category.DoesNotExist: return HttpResponse("Oops, an error has occurred, please try again later", status=500) todoItems = TodoItem.objects.filter(category=category) response = simplejson.dumps([util.toDict(item) for item in todoItems], cls=DateTimeAwareJSONEncoder, indent=4) return HttpResponse(response)
def create(self, request, categoryID): now = datetime.datetime.utcnow() itemName = request.POST.get('itemName') if not all([itemName, categoryID]): return HttpResponse("Please provide a category and a name for the new item", status=500) try: category = Category.objects.get(id=categoryID) except Category.DoesNotExist: return HttpResponse("Oops, an error has occurred, please try again later", status=500) todoItem = TodoItem.objects.create(name=itemName, category=category, when_created=now, last_updated=now) return HttpResponse(simplejson.dumps(util.toDict(todoItem),cls=DateTimeAwareJSONEncoder, indent=4), status=201)