def edit_device(request, id=None): context = { 'page_title': u'Dispositivos', 'edit_name': 'device', 'has_back': False, 'features': get_user_features(request) } t = get_template('edit.html') device = None form = DeviceForm() try: if request.method == 'POST': form = DeviceForm(request.POST) if form.is_valid(): cd = form.cleaned_data device = _save_device(cd) messages.success(request, 'Dispositivo salvo com sucesso.') form = DeviceForm(initial={}) elif id: device = Device.objects.get(id=id) initial = device.__dict__ initial['category'] = device.category.id form = DeviceForm(initial=initial) except Exception as e: log.error(e) messages.error( request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.' ) context = _set_device_form_context(device, form, context) return render_to_response('edit.html', context, context_instance=RequestContext(request))
def post(request): if "protocol" not in request.POST: return HttpResponseBadRequest( content=simplejson.dumps({"errors": ["protocol"]}), content_type="application/json", ) protocol = get_object_or_404(Protocol, name=request.POST["protocol"]) form = DeviceForm(request.POST) if form.is_valid(): exec "from %s import validate_address" % protocol.validate_address_module obj = form.save(commit=False) obj.protocol = protocol try: validate_address(obj.did) obj.save() except ValueError: return HttpResponseBadRequest( content=simplejson.dumps({"errors": ["did"]}), content_type="application/json", ) except IntegrityError: return HttpResponse( status=409, content=simplejson.dumps({"conflicting": ["did", "protocol"]}), content_type="application/json", ) response = redirect(reverse('device_by_id', kwargs={"protocol": obj.protocol, "did": obj.id})) response.content_type = "application/json" return response return HttpResponseBadRequest( content=simplejson.dumps({"errors": [x for x in form.errors]}), content_type="application/json",)
def newdevice(request): if request.POST: form = DeviceForm(request.POST) if form.is_valid(): form.save() messages.success(request, 'Device added!') return HttpResponseRedirect('/devices/') else: form = DeviceForm() return render(request, 'newdevice.html', locals())
def post(self, request, **kwargs): """ Creates a new device or updates an existing one to `is_active=True`. Expects two non-options POST parameters: `token` and `service`. """ devices = DeviceToken.objects.filter(token=request.POST.get('token'), service__id=int(request.POST.get('service', 0))) if devices.exists(): device = devices.get() device.is_active = True device.save() return JSONResponse(device) form = DeviceForm(request.POST) if form.is_valid(): device = form.save(commit=False) device.is_active = True device.save() return JSONResponse(device, status=201) return JSONResponse(form.errors, status=400)
def post(self, request, **kwargs): """ Creates a new device or updates an existing one to `is_active=True`. Expects two non-options POST parameters: `token` and `service`. """ devices = DeviceToken.objects.filter( token=request.POST.get("token"), service__id=int(request.POST.get("service", 0)) ) if devices.exists(): device = devices.get() device.is_active = True device.save() return JSONResponse(device) form = DeviceForm(request.POST) if form.is_valid(): device = form.save(commit=False) device.is_active = True device.save() return JSONResponse(device, status=201) return JSONResponse(form.errors, status=400)
def editdevice(request, device_id): device = get_object_or_404(Device, pk=device_id) history = Device.history.filter(id=device_id) t = "Edit" tickets = Ticket.objects.filter(UniqueID=device_id) if request.POST: form = DeviceForm(request.POST, instance=device) if form.is_valid(): form.save() return HttpResponseRedirect('/device/edit/%s' % device_id) else: form = DeviceForm(instance=device) return render_to_response("editdevice.html", { 'form': form, 't': t, 'device': device, 'history': history, 'tickets': tickets, }, context_instance=RequestContext(request))
def add_edit_item(request, model_name, add_edit, edit_id): """Add/Edit page for the inventory""" if add_edit == "add": edit_id = None if _checkModel(model_name): directory = "asset_inventory/add_edit_Pages/" # Adding or Editing a Job if model_name == "Job": templateFile = directory + "Job.html" post_data = request.POST or None try: Job_Instance = Job.objects.get(pk=edit_id) except Job.DoesNotExist: Job_Instance = None if add_edit == "edit": return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Job", "add", "new"])) Job_Form = JobForm(post_data, instance=Job_Instance) if Job_Form.is_valid(): Job_Occurance = Job_Form.save() return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Job"])) return render_to_response(templateFile, {"form": Job_Form}, context_instance=RequestContext(request)) # Adding or Editing a Location elif model_name == "Location": templateFile = directory + "Location.html" post_data = request.POST or None try: Location_Instance = Location.objects.get(pk=edit_id) except Location.DoesNotExist: Location_Instance = None if add_edit == "edit": return HttpResponseRedirect( reverse("asset_inventory_add_edit_item", args=["Location", "add", "new"]) ) Location_Form = LocationForm(post_data, instance=Location_Instance) if Location_Form.is_valid(): Location_Occurance = Location_Form.save() return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Location"])) return render_to_response(templateFile, {"form": Location_Form}, context_instance=RequestContext(request)) # Adding or Editing a Person elif model_name == "Person": templateFile = directory + "Person.html" post_data = request.POST or None try: Person_Instance = Person.objects.get(pk=edit_id) except Person.DoesNotExist: Person_Instance = None if add_edit == "edit": return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Person", "add", "new"])) Person_Form = PersonForm(post_data, instance=Person_Instance) if Person_Form.is_valid(): Person_Occurance = Person_Form.save() return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Person"])) return render_to_response(templateFile, {"form": Person_Form}, context_instance=RequestContext(request)) # Adding or Editing a Purchase elif model_name == "Purchase": templateFile = directory + "Purchase.html" post_data = request.POST or None try: Purchase_Instance = Purchase.objects.get(pk=edit_id) except Purchase.DoesNotExist: Purchase_Instance = None if add_edit == "edit": return HttpResponseRedirect( reverse("asset_inventory_add_edit_item", args=["Purchase", "add", "new"]) ) Purchase_Form = PurchaseForm(post_data, instance=Purchase_Instance) if Purchase_Form.is_valid(): Purchase_Occurance = Purchase_Form.save() return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Purchase"])) return render_to_response(templateFile, {"form": Purchase_Form}, context_instance=RequestContext(request)) # Adding or Editing a Status elif model_name == "Status": templateFile = directory + "Status.html" post_data = request.POST or None try: Status_Instance = Status.objects.get(pk=edit_id) except Status.DoesNotExist: Status_Instance = None if add_edit == "edit": return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Status", "add", "new"])) Status_Form = StatusForm(post_data, instance=Status_Instance) if Status_Form.is_valid(): Status_Occurance = Status_Form.save() return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Status"])) return render_to_response(templateFile, {"form": Status_Form}, context_instance=RequestContext(request)) # Adding or Editing a Esign elif model_name == "Esign": templateFile = directory + "Esign.html" post_data = request.POST or None try: Esign_Instance = Esign.objects.get(pk=edit_id) except Esign.DoesNotExist: Esign_Instance = None if add_edit == "edit": return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Esign", "add", "new"])) Esign_Form = EsignForm(post_data, instance=Esign_Instance) if Esign_Form.is_valid(): Esign_Occurance = Esign_Form.save() return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Esign"])) return render_to_response(templateFile, {"form": Esign_Form}, context_instance=RequestContext(request)) # Adding or Editing a Device elif model_name == "Device": templateFile = directory + "Device.html" post_data = request.POST or None try: Device_Instance = Device.objects.get(pk=edit_id) except Device.DoesNotExist: Device_Instance = None if add_edit == "edit": return HttpResponseRedirect(reverse("asset_inventory_add_edit_item", args=["Device", "add", "new"])) Device_Form = DeviceForm(post_data, instance=Device_Instance) if Device_Form.is_valid(): Device_Occurance = Device_Form.save() return HttpResponseRedirect(reverse("asset_inventory_list_model", args=["Device"])) return render_to_response(templateFile, {"form": Device_Form}, context_instance=RequestContext(request)) # This is a catch all response, if some unforseen error manages # to occur with the calling of add/edit pages else: render_to_response(reverse("homepage"))