def post(self, id): data = request.get_json() now = datetime.datetime.now() bucketlist = Bucketlist.query.filter_by(id=id).first() bucketlist_item = BucketlistItem.query.filter_by( item_name=data.get('item_name')).first() if not bucketlist_item: new_item = BucketlistItem(item_name=data.get('item_name'), bucketlist_id=bucketlist.id, date_created=now, date_modified=now, done=False, complete_by=data.get('complete_by')) new_item.save() response = jsonify({ 'status': "Success", 'message': "Bucketlist Item Created" }) response.status_code = 201 else: response = jsonify({ 'status': "Fail", 'message': "Bucketlist Item Already Exists" }) response.status_code = 409 # 409 means there is a conflict with db as two of the same resource exists return make_response(response)
def post(self, request, **kwargs): bucketlistid = self.kwargs.get('id') itemname = request.POST.get('itemname', '') done = request.POST.get('done') done = True if done else False if itemname: items = BucketlistItem( name=itemname, done=done, bucketlist_id=bucketlistid) items.save() msg = "Item succesfully added." messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect( reverse_lazy('view', kwargs={'id': bucketlistid})) else: msg = "field should not be submitted empty." messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect(reverse_lazy('view', kwargs={ 'id': bucketlistid}))
def post(self, request, id): '''create a bucketlist item''' # get the bucketlist object bucketlist = get_object_or_404(Bucketlist, pk=id) # validate form form = CreateBucketlistForm(request.POST) if form.is_valid(): item_name = form.cleaned_data['name'] # create and save bucketlist item item = BucketlistItem(name=item_name, bucketlist=bucketlist) item.save() # create success message messages.add_message(request, messages.INFO, 'Bucketlist item created') else: # send back form errors as messages for error in form.errors: messages.add_message(request, messages.ERROR, form.errors[error]) return HttpResponseRedirect(reverse('frontend:bucketlist_items', kwargs={'id': id}))
def post(self, request, **kwargs): bucketitem_name = request.POST.get('name') if not bucketitem_name: messages.error(request, 'You attempted to enter an unnamed bucketitem') # returns error if trying to add an empty item return render(request, 'bucketlist/errors.html') bucketitem = BucketlistItem( name=bucketitem_name, bucketlist=BucketList.objects.get(pk=kwargs['bucketlistid'])) bucketitem.save() messages.success(request, 'Successfully added an item!') return redirect('/bucketlist', context_instance=RequestContext(request))
def post(self, request, **kwargs): bucketitem_name = request.POST.get('name') if not bucketitem_name: messages.error( request, 'You attempted to enter an unnamed bucketitem') # returns error if trying to add an empty item return render(request, 'bucketlist/errors.html') bucketitem = BucketlistItem( name=bucketitem_name, bucketlist=BucketList.objects.get( pk=kwargs['bucketlistid'])) bucketitem.save() messages.success( request, 'Successfully added an item!') return redirect( '/bucketlist', context_instance=RequestContext(request) )
def post(self, request, **kwargs): bucketlistid = self.kwargs.get('id') itemname = request.POST.get('itemname', '') done = request.POST.get('done') done = True if done else False if itemname: items = BucketlistItem(name=itemname, done=done, bucketlist_id=bucketlistid) items.save() msg = "Item succesfully added." messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect( reverse_lazy('view', kwargs={'id': bucketlistid})) else: msg = "field should not be submitted empty." messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect( reverse_lazy('view', kwargs={'id': bucketlistid}))