Esempio n. 1
0
 def get(self, *args, **kwargs):
     self.initialize_vars()
     self.asset = get_object_or_404(Asset.admin_objects,
                                    id=kwargs.get('asset_id'))
     if not self.asset.device_info:  # it isn't device asset
         raise Http404()
     mode = _get_mode(self.request)
     self.asset_form = EditDeviceForm(instance=self.asset, mode=mode)
     if self.asset.type in AssetType.BO.choices:
         self.office_info_form = OfficeForm(instance=self.asset.office_info)
     self.device_info_form = DeviceForm(
         instance=self.asset.device_info,
         mode=mode,
     )
     self.parts = Asset.objects.filter(part_info__device=self.asset)
     return super(EditDevice, self).get(*args, **kwargs)
Esempio n. 2
0
 def get(self, *args, **kwargs):
     self.initialize_vars()
     self.asset = get_object_or_404(
         Asset.admin_objects,
         id=kwargs.get('asset_id')
     )
     if not self.asset.device_info:  # it isn't device asset
         raise Http404()
     mode = _get_mode(self.request)
     self.asset_form = EditDeviceForm(instance=self.asset, mode=mode)
     if self.asset.type in AssetType.BO.choices:
         self.office_info_form = OfficeForm(instance=self.asset.office_info)
     self.device_info_form = DeviceForm(
         instance=self.asset.device_info,
         mode=mode
     )
     self.parts = Asset.objects.filter(part_info__device=self.asset)
     return super(EditDevice, self).get(*args, **kwargs)
Esempio n. 3
0
 def post(self, *args, **kwargs):
     self.initialize_vars()
     post_data = self.request.POST
     self.asset = get_object_or_404(Asset.admin_objects,
                                    id=kwargs.get('asset_id'))
     mode = _get_mode(self.request)
     self.asset_form = EditDeviceForm(
         post_data,
         instance=self.asset,
         mode=mode,
     )
     self.device_info_form = DeviceForm(
         post_data,
         mode=mode,
         instance=self.asset.device_info,
     )
     self.part_form = MoveAssetPartForm(post_data)
     if 'move_parts' in post_data.keys():
         destination_asset = post_data.get('new_asset')
         if not destination_asset or not Asset.objects.filter(
                 id=destination_asset, ):
             messages.error(
                 self.request,
                 _("Source device asset does not exist"),
             )
         elif kwargs.get('asset_id') == destination_asset:
             messages.error(
                 self.request,
                 _("You can't move parts to the same device"),
             )
         else:
             for part_id in post_data.get('part_ids'):
                 info_part = PartInfo.objects.get(asset=part_id)
                 info_part.device_id = destination_asset
                 info_part.save()
             messages.success(self.request, _("Selected parts was moved."))
     elif 'asset' in post_data.keys():
         if self.asset.type in AssetType.BO.choices:
             self.office_info_form = OfficeForm(
                 post_data,
                 self.request.FILES,
             )
         if all(
             (self.asset_form.is_valid(), self.device_info_form.is_valid(),
              self.asset.type not in AssetType.BO.choices
              or self.office_info_form.is_valid())):
             modifier_profile = self.request.user.get_profile()
             self.asset = _update_asset(modifier_profile, self.asset,
                                        self.asset_form.cleaned_data)
             if self.asset.type in AssetType.BO.choices:
                 self.asset = _update_office_info(
                     modifier_profile.user, self.asset,
                     self.office_info_form.cleaned_data)
             self.asset = _update_device_info(
                 modifier_profile.user, self.asset,
                 self.device_info_form.cleaned_data)
             if self.device_info_form.cleaned_data.get('create_stock'):
                 self.asset.create_stock_device()
             self.asset.save(user=self.request.user)
             messages.success(self.request, _("Assets edited."))
             cat = self.request.path.split('/')[2]
             return HttpResponseRedirect('/assets/%s/edit/device/%s/' %
                                         (cat, self.asset.id))
         else:
             messages.error(self.request, _("Please correct the errors."))
             messages.error(
                 self.request,
                 self.asset_form.non_field_errors(),
             )
     return super(EditDevice, self).get(*args, **kwargs)
Esempio n. 4
0
class EditDevice(Base):
    template_name = 'assets/edit_device.html'

    def initialize_vars(self):
        self.parts = []
        self.office_info_form = None
        self.asset = None

    def get_context_data(self, **kwargs):
        ret = super(EditDevice, self).get_context_data(**kwargs)
        status_history = AssetHistoryChange.objects.all().filter(
            asset=kwargs.get('asset_id'),
            field_name__exact='status').order_by('-date')
        ret.update({
            'asset_form': self.asset_form,
            'device_info_form': self.device_info_form,
            'office_info_form': self.office_info_form,
            'part_form': MoveAssetPartForm(),
            'form_id': 'edit_device_asset_form',
            'edit_mode': True,
            'status_history': status_history,
            'parts': self.parts,
            'asset': self.asset,
            'history_link': self.get_history_link(),
        })
        return ret

    def get(self, *args, **kwargs):
        self.initialize_vars()
        self.asset = get_object_or_404(Asset.admin_objects,
                                       id=kwargs.get('asset_id'))
        if not self.asset.device_info:  # it isn't device asset
            raise Http404()
        mode = _get_mode(self.request)
        self.asset_form = EditDeviceForm(instance=self.asset, mode=mode)
        if self.asset.type in AssetType.BO.choices:
            self.office_info_form = OfficeForm(instance=self.asset.office_info)
        self.device_info_form = DeviceForm(
            instance=self.asset.device_info,
            mode=mode,
        )
        self.parts = Asset.objects.filter(part_info__device=self.asset)
        return super(EditDevice, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        self.initialize_vars()
        post_data = self.request.POST
        self.asset = get_object_or_404(Asset.admin_objects,
                                       id=kwargs.get('asset_id'))
        mode = _get_mode(self.request)
        self.asset_form = EditDeviceForm(
            post_data,
            instance=self.asset,
            mode=mode,
        )
        self.device_info_form = DeviceForm(
            post_data,
            mode=mode,
            instance=self.asset.device_info,
        )
        self.part_form = MoveAssetPartForm(post_data)
        if 'move_parts' in post_data.keys():
            destination_asset = post_data.get('new_asset')
            if not destination_asset or not Asset.objects.filter(
                    id=destination_asset, ):
                messages.error(
                    self.request,
                    _("Source device asset does not exist"),
                )
            elif kwargs.get('asset_id') == destination_asset:
                messages.error(
                    self.request,
                    _("You can't move parts to the same device"),
                )
            else:
                for part_id in post_data.get('part_ids'):
                    info_part = PartInfo.objects.get(asset=part_id)
                    info_part.device_id = destination_asset
                    info_part.save()
                messages.success(self.request, _("Selected parts was moved."))
        elif 'asset' in post_data.keys():
            if self.asset.type in AssetType.BO.choices:
                self.office_info_form = OfficeForm(
                    post_data,
                    self.request.FILES,
                )
            if all(
                (self.asset_form.is_valid(), self.device_info_form.is_valid(),
                 self.asset.type not in AssetType.BO.choices
                 or self.office_info_form.is_valid())):
                modifier_profile = self.request.user.get_profile()
                self.asset = _update_asset(modifier_profile, self.asset,
                                           self.asset_form.cleaned_data)
                if self.asset.type in AssetType.BO.choices:
                    self.asset = _update_office_info(
                        modifier_profile.user, self.asset,
                        self.office_info_form.cleaned_data)
                self.asset = _update_device_info(
                    modifier_profile.user, self.asset,
                    self.device_info_form.cleaned_data)
                if self.device_info_form.cleaned_data.get('create_stock'):
                    self.asset.create_stock_device()
                self.asset.save(user=self.request.user)
                messages.success(self.request, _("Assets edited."))
                cat = self.request.path.split('/')[2]
                return HttpResponseRedirect('/assets/%s/edit/device/%s/' %
                                            (cat, self.asset.id))
            else:
                messages.error(self.request, _("Please correct the errors."))
                messages.error(
                    self.request,
                    self.asset_form.non_field_errors(),
                )
        return super(EditDevice, self).get(*args, **kwargs)

    def get_history_link(self):
        mode = _get_mode(self.request)
        asset_id = self.asset.id
        if mode == 'dc':
            url = reverse('dc_device_history', args=[
                asset_id,
            ])
        elif mode == 'back_office':
            url = reverse('back_office_device_history', args=[
                asset_id,
            ])
        return url
Esempio n. 5
0
 def post(self, *args, **kwargs):
     self.initialize_vars()
     post_data = self.request.POST
     self.asset = get_object_or_404(
         Asset.admin_objects,
         id=kwargs.get('asset_id')
     )
     mode = _get_mode(self.request)
     self.asset_form = EditDeviceForm(
         post_data,
         instance=self.asset,
         mode=mode,
     )
     self.device_info_form = DeviceForm(
         post_data,
         mode=mode,
         instance=self.asset.device_info,
     )
     self.part_form = MoveAssetPartForm(post_data)
     if 'move_parts' in post_data.keys():
         destination_asset = post_data.get('new_asset')
         if not destination_asset or not Asset.objects.filter(
             id=destination_asset,
         ):
             messages.error(
                 self.request,
                 _("Source device asset does not exist"),
             )
         elif kwargs.get('asset_id') == destination_asset:
             messages.error(
                 self.request,
                 _("You can't move parts to the same device"),
             )
         else:
             for part_id in post_data.get('part_ids'):
                 info_part = PartInfo.objects.get(asset=part_id)
                 info_part.device_id = destination_asset
                 info_part.save()
             messages.success(self.request, _("Selected parts was moved."))
     elif 'asset' in post_data.keys():
         if self.asset.type in AssetType.BO.choices:
             self.office_info_form = OfficeForm(
                 post_data, self.request.FILES,
             )
         if all((
             self.asset_form.is_valid(),
             self.device_info_form.is_valid(),
             self.asset.type not in AssetType.BO.choices or
             self.office_info_form.is_valid()
         )):
             modifier_profile = self.request.user.get_profile()
             self.asset = _update_asset(
                 modifier_profile, self.asset, self.asset_form.cleaned_data
             )
             if self.asset.type in AssetType.BO.choices:
                 self.asset = _update_office_info(
                     modifier_profile.user, self.asset,
                     self.office_info_form.cleaned_data
                 )
             self.asset = _update_device_info(
                 modifier_profile.user, self.asset,
                 self.device_info_form.cleaned_data
             )
             self.asset.save(user=self.request.user)
             messages.success(self.request, _("Assets edited."))
             cat = self.request.path.split('/')[2]
             return HttpResponseRedirect(
                 '/assets/%s/edit/device/%s/' % (cat, self.asset.id)
             )
         else:
             messages.error(self.request, _("Please correct the errors."))
             messages.error(
                 self.request, self.asset_form.non_field_errors(),
             )
     return super(EditDevice, self).get(*args, **kwargs)
Esempio n. 6
0
class EditDevice(Base):
    template_name = 'assets/edit_device.html'

    def initialize_vars(self):
        self.parts = []
        self.office_info_form = None
        self.asset = None

    def get_context_data(self, **kwargs):
        ret = super(EditDevice, self).get_context_data(**kwargs)
        status_history = AssetHistoryChange.objects.all().filter(
            asset=kwargs.get('asset_id'), field_name__exact='status'
        ).order_by('-date')
        ret.update({
            'asset_form': self.asset_form,
            'device_info_form': self.device_info_form,
            'office_info_form': self.office_info_form,
            'part_form': MoveAssetPartForm(),
            'form_id': 'edit_device_asset_form',
            'edit_mode': True,
            'status_history': status_history,
            'parts': self.parts,
            'asset': self.asset,
            'history_link': self.get_history_link(),
        })
        return ret

    def get(self, *args, **kwargs):
        self.initialize_vars()
        self.asset = get_object_or_404(
            Asset.admin_objects,
            id=kwargs.get('asset_id')
        )
        if not self.asset.device_info:  # it isn't device asset
            raise Http404()
        mode = _get_mode(self.request)
        self.asset_form = EditDeviceForm(instance=self.asset, mode=mode)
        if self.asset.type in AssetType.BO.choices:
            self.office_info_form = OfficeForm(instance=self.asset.office_info)
        self.device_info_form = DeviceForm(
            instance=self.asset.device_info,
            mode=mode
        )
        self.parts = Asset.objects.filter(part_info__device=self.asset)
        return super(EditDevice, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        self.initialize_vars()
        post_data = self.request.POST
        self.asset = get_object_or_404(
            Asset.admin_objects,
            id=kwargs.get('asset_id')
        )
        mode = _get_mode(self.request)
        self.asset_form = EditDeviceForm(
            post_data,
            instance=self.asset,
            mode=mode,
        )
        self.device_info_form = DeviceForm(
            post_data,
            mode=mode,
            instance=self.asset.device_info,
        )
        self.part_form = MoveAssetPartForm(post_data)
        if 'move_parts' in post_data.keys():
            destination_asset = post_data.get('new_asset')
            if not destination_asset or not Asset.objects.filter(
                id=destination_asset,
            ):
                messages.error(
                    self.request,
                    _("Source device asset does not exist"),
                )
            elif kwargs.get('asset_id') == destination_asset:
                messages.error(
                    self.request,
                    _("You can't move parts to the same device"),
                )
            else:
                for part_id in post_data.get('part_ids'):
                    info_part = PartInfo.objects.get(asset=part_id)
                    info_part.device_id = destination_asset
                    info_part.save()
                messages.success(self.request, _("Selected parts was moved."))
        elif 'asset' in post_data.keys():
            if self.asset.type in AssetType.BO.choices:
                self.office_info_form = OfficeForm(
                    post_data, self.request.FILES,
                )
            if all((
                self.asset_form.is_valid(),
                self.device_info_form.is_valid(),
                self.asset.type not in AssetType.BO.choices or
                self.office_info_form.is_valid()
            )):
                modifier_profile = self.request.user.get_profile()
                self.asset = _update_asset(
                    modifier_profile, self.asset, self.asset_form.cleaned_data
                )
                if self.asset.type in AssetType.BO.choices:
                    self.asset = _update_office_info(
                        modifier_profile.user, self.asset,
                        self.office_info_form.cleaned_data
                    )
                self.asset = _update_device_info(
                    modifier_profile.user, self.asset,
                    self.device_info_form.cleaned_data
                )
                self.asset.save(user=self.request.user)
                messages.success(self.request, _("Assets edited."))
                cat = self.request.path.split('/')[2]
                return HttpResponseRedirect(
                    '/assets/%s/edit/device/%s/' % (cat, self.asset.id)
                )
            else:
                messages.error(self.request, _("Please correct the errors."))
                messages.error(
                    self.request, self.asset_form.non_field_errors(),
                )
        return super(EditDevice, self).get(*args, **kwargs)

    def get_history_link(self):
        mode = _get_mode(self.request)
        asset_id = self.asset.id
        if mode == 'dc':
            url = reverse('dc_device_history', args=[asset_id, ])
        elif mode == 'back_office':
            url = reverse('back_office_device_history', args=[asset_id, ])
        return url