def get_device(user, uid, user_agent, undelete=True): """ Loads or creates the device indicated by user, uid. If the device has been deleted and undelete=True, it is undeleted. """ store_ua = user.profile.settings.get_wksetting(STORE_UA) # list of fields to update -- empty list = no update update_fields = [] try: with transaction.atomic(): client = Client(id=uuid.uuid1(), user=user, uid=uid) client.clean_fields() client.clean() client.save() except IntegrityError: client = Client.objects.get(user=user, uid=uid) if client.deleted and undelete: client.deleted = False update_fields.append('deleted') if store_ua and user_agent and client.user_agent != user_agent: client.user_agent = user_agent update_fields.append('user_agent') if update_fields: client.save(update_fields=update_fields) return client
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")) try: device = Client() device.user = request.user device.id = uuid.uuid1() device.name = device_form.cleaned_data["name"] device.type = device_form.cleaned_data["type"] device.uid = device_form.cleaned_data["uid"].replace(" ", "-") device.full_clean() device.save() messages.success(request, _("Device saved")) except ValidationError as e: messages.error(request, "; ".join(e.messages)) return HttpResponseRedirect(reverse("devices")) except IntegrityError: 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 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')) try: device = Client() device.user = request.user device.id = uuid.uuid1() device.name = device_form.cleaned_data['name'] device.type = device_form.cleaned_data['type'] device.uid = device_form.cleaned_data['uid'].replace(' ', '-') device.full_clean() device.save() messages.success(request, _('Device saved')) except ValidationError as e: messages.error(request, '; '.join(e.messages)) return HttpResponseRedirect(reverse('devices')) except IntegrityError: 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 get_device(user, uid, user_agent, undelete=True): """ Loads or creates the device indicated by user, uid. If the device has been deleted and undelete=True, it is undeleted. """ store_ua = user.profile.settings.get_wksetting(STORE_UA) # list of fields to update -- empty list = no update update_fields = [] with transaction.atomic(): try: client = Client(id=uuid.uuid1(), user=user, uid=uid) client.full_clean() client.save() except IntegrityError: client = Client.objects.get(user=user, uid=uid) if client.deleted and undelete: client.deleted = False update_fields.append('deleted') if store_ua and user_agent and client.user_agent != user_agent: client.user_agent = user_agent update_fields.append('user_agent') if update_fields: client.save(update_fields=update_fields) return client