def device(request, username, device_uid): d = get_device(request.user, device_uid, request.META.get('HTTP_USER_AGENT', '')) try: data = parse_request_body(request) except (JSONDecodeError, UnicodeDecodeError, ValueError) as e: msg = ('Could not decode device update POST data for ' + 'user %s: %s') % (username, request.body.decode('ascii', errors='replace')) logger.warn(msg, exc_info=True) return HttpResponseBadRequest(msg) if 'caption' in data: if not data['caption']: return HttpResponseBadRequest('caption must not be empty') d.name = data['caption'] if 'type' in data: if not valid_devicetype(data['type']): return HttpResponseBadRequest('invalid device type %s' % data['type']) d.type = data['type'] set_device(request.user, d) return HttpResponse()
def create(request): device_form = DeviceForm(request.POST) if not device_form.is_valid(): messages.error(request, _('Please fill out all fields.')) return HttpResponseRedirect(reverse('devices')) device = Device() device.name = device_form.cleaned_data['name'] device.type = device_form.cleaned_data['type'] device.uid = device_form.cleaned_data['uid'].replace(' ', '-') try: set_device(request.user, device) messages.success(request, _('Device saved')) except DeviceUIDException as e: messages.error(request, _(unicode(e))) return HttpResponseRedirect(reverse('devices')) except Unauthorized: messages.error(request, _("You can't use the same Device " "ID for two devices.")) return HttpResponseRedirect(reverse('devices')) return HttpResponseRedirect(reverse('device-edit', args=[device.uid]))
def update(request, device): device_form = DeviceForm(request.POST) uid = device.uid if device_form.is_valid(): device.name = device_form.cleaned_data['name'] device.type = device_form.cleaned_data['type'] device.uid = device_form.cleaned_data['uid'].replace(' ', '-') try: set_device(request.user, device) messages.success(request, _('Device updated')) uid = device.uid # accept the new UID after rest has succeeded except DeviceUIDException as e: messages.error(request, _(str(e))) except Unauthorized as u: messages.error(request, _("You can't use the same Device " "ID for two devices.")) return HttpResponseRedirect(reverse('device-edit', args=[uid]))