Beispiel #1
0
class BulkEditAssetForm(ModelForm):
    class Meta:
        model = Asset
        fields = (
            'type', 'model', 'device_info', 'invoice_no', 'order_no',
            'sn', 'barcode', 'price', 'support_price',
            'support_period', 'support_type', 'support_void_reporting',
            'provider', 'source', 'status', 'request_date',
            'delivery_date', 'invoice_date', 'production_use_date',
            'provider_order_date', 'production_year',
        )
        widgets = {
            'request_date': DateWidget(),
            'delivery_date': DateWidget(),
            'invoice_date': DateWidget(),
            'production_use_date': DateWidget(),
            'provider_order_date': DateWidget(),
            'device_info': HiddenInput(),
        }
    barcode = BarcodeField(max_length=200, required=False)
    source = ChoiceField(
        choices=AssetSource(),
    )
    model = AutoCompleteSelectField(
        LOOKUPS['asset_model'],
        required=True,
        plugin_options=dict(
            add_link='/admin/ralph_assets/assetmodel/add/?name=',
        )
    )

    def __init__(self, *args, **kwargs):
        super(BulkEditAssetForm, self).__init__(*args, **kwargs)
        fillable_fields = [
            'type', 'model', 'device_info', 'invoice_no', 'order_no',
            'request_date', 'delivery_date', 'invoice_date',
            'production_use_date', 'provider_order_date',
            'provider_order_date', 'support_period', 'support_type',
            'provider', 'source', 'status', 'production_year',
        ]
        for field_name in self.fields:
            if field_name in fillable_fields:
                classes = "span12 fillable"
            elif field_name == 'support_void_reporting':
                classes = ""
            else:
                classes = "span12"
            self.fields[field_name].widget.attrs = {'class': classes}
        group_type = AssetType.from_id(self.instance.type).group.name
        if group_type == 'DC':
            del self.fields['type']
        elif group_type == 'BO':
            self.fields['type'].choices = [('', '---------')] + [
                (choice.id, choice.name) for choice in AssetType.BO.choices
            ]
Beispiel #2
0
def get_asset(device_id):
    try:
        asset = Asset.objects.get(device_info__ralph_device_id=device_id)
    except Asset.DoesNotExist:
        return
    manufacturer_name = ''
    if asset.model.manufacturer:
        manufacturer_name = asset.model.manufacturer.name
    try:
        asset_source = AssetSource.from_id(asset.source).raw
    except ValueError:
        asset_source = None
    return {
        'asset_id': asset.id,
        'model': asset.model.name,
        'manufacturer': manufacturer_name,
        'source': asset_source,
        'invoice_no': asset.invoice_no,
        'order_no': asset.order_no,
        'invoice_date': asset.invoice_date,
        'sn': asset.sn,
        'barcode': asset.barcode,
        'price': asset.price,
        'support_price': asset.support_price,
        'support_period': asset.support_period,
        'support_type': asset.support_type,
        'support_void_reporting': asset.support_void_reporting,
        'provider': asset.provider,
        'status': AssetStatus.from_id(asset.status).raw,
        'remarks': asset.remarks,
        'niw': asset.niw,
        'warehouse': asset.warehouse.name,
        'request_date': asset.request_date,
        'delivery_date': asset.delivery_date,
        'production_use_date': asset.production_use_date,
        'provider_order_date': asset.provider_order_date,
        'category': asset.model.category.name,
        'slots': asset.slots,
        'price': asset.price,
        'deprecation_rate': asset.deprecation_rate,
        'is_deprecated': asset.is_deprecated(),
        'size': asset.device_info.size,
        'u_level': asset.device_info.u_level,
        'u_height': asset.device_info.u_height,
        'rack': asset.device_info.rack,
        'required_support': asset.required_support,
        'supports': get_asset_supports(asset),
        'url': asset.url,
    }
Beispiel #3
0
def _create_asset_dict(asset):
    manufacturer_name = ''
    if asset.model.manufacturer:
        manufacturer_name = asset.model.manufacturer.name
    try:
        asset_source = AssetSource.from_id(asset.source).raw
    except ValueError:
        asset_source = None
    return {
        'asset_id': asset.id,
        'device_id': asset.device_info.ralph_device_id,
        'model': asset.model.name,
        'manufacturer': manufacturer_name,
        'source': asset_source,
        'invoice_no': asset.invoice_no,
        'order_no': asset.order_no,
        'invoice_date': asset.invoice_date,
        'sn': asset.sn,
        'barcode': asset.barcode,
        'price': asset.price,
        'support_price': asset.support_price,
        'support_period': asset.support_period,
        'support_type': asset.support_type,
        'support_void_reporting': asset.support_void_reporting,
        'provider': asset.provider,
        'status': AssetStatus.from_id(asset.status).raw,
        'remarks': asset.remarks,
        'niw': asset.niw,
        'warehouse': asset.warehouse.name,
        'request_date': asset.request_date,
        'delivery_date': asset.delivery_date,
        'production_use_date': asset.production_use_date,
        'provider_order_date': asset.provider_order_date,
        'category': asset.model.category.name,
        'slots': asset.slots,
        'price': asset.price,
        'deprecation_rate': asset.deprecation_rate,
        'is_deprecated': asset.is_deprecated(),
        'size': asset.device_info.size,
        'u_level': asset.device_info.u_level,
        'u_height': asset.device_info.u_height,
        'rack': asset.device_info.rack,
        'required_support': asset.required_support,
        'supports': get_asset_supports(asset),
        'url': asset.url,
    }
Beispiel #4
0
def get_asset(device_id):
    try:
        asset = Asset.objects.get(device_info__ralph_device_id=device_id)
    except Asset.DoesNotExist:
        return
    manufacturer_name = ''
    if asset.model.manufacturer:
        manufacturer_name = asset.model.manufacturer.name
    return {
        'asset_id': asset.id,
        'model': asset.model.name,
        'manufacturer': manufacturer_name,
        'source': AssetSource.from_id(asset.source).raw,
        'invoice_no': asset.invoice_no,
        'order_no': asset.order_no,
        'invoice_date': asset.invoice_date,
        'sn': asset.sn,
        'barcode': asset.barcode,
        'price': asset.price,
        'support_price': asset.support_price,
        'support_period': asset.support_period,
        'support_type': asset.support_type,
        'support_void_reporting': asset.support_void_reporting,
        'provider': asset.provider,
        'status': AssetStatus.from_id(asset.status).raw,
        'remarks': asset.remarks,
        'niw': asset.niw,
        'warehouse': asset.warehouse.name,
        'request_date': asset.request_date,
        'delivery_date': asset.delivery_date,
        'production_use_date': asset.production_use_date,
        'provider_order_date': asset.provider_order_date,
        'category': asset.category.name,
        'slots': asset.slots,
        'price': asset.price,
        'deprecation_rate': asset.deprecation_rate,
        'is_deprecated': asset.is_deprecated(),
        'size': asset.device_info.size,
        'u_level': asset.device_info.u_level,
        'u_height': asset.device_info.u_height,
        'rack': asset.device_info.rack,
    }
class SearchAssetForm(Form):
    """returns search asset form for DC and BO.

    :param mode: one of `dc` for DataCenter or `bo` for Back Office
    :returns Form
    """
    model = AutoCompleteField(
        LOOKUPS['asset_model'],
        required=False,
        help_text=None,
    )
    manufacturer = AutoCompleteField(
        LOOKUPS['asset_manufacturer'],
        required=False,
        help_text=None,
    )
    invoice_no = CharField(required=False)
    order_no = CharField(required=False)
    provider = CharField(required=False, label='Provider')
    status = ChoiceField(required=False,
                         choices=[('', '----')] + AssetStatus(),
                         label='Status')
    part_info = ChoiceField(required=False,
                            choices=[('', '----'), ('device', 'Device'),
                                     ('part', 'Part')],
                            label='Asset type')
    category = TreeNodeChoiceField(
        required=False,
        queryset=AssetCategory.tree.all(),
        level_indicator='|---',
        empty_label="---",
    )
    source = ChoiceField(
        required=False,
        choices=[('', '----')] + AssetSource(),
    )
    niw = CharField(required=False, label='Niw')
    sn = CharField(required=False, label='SN')
    barcode = CharField(required=False, label='Barcode')
    ralph_device_id = IntegerField(
        required=False,
        label='Ralph device id',
    )
    request_date_from = DateField(
        required=False,
        widget=DateWidget(attrs={
            'placeholder': 'Start YYYY-MM-DD',
            'data-collapsed': True,
        }),
        label="Request date",
    )
    request_date_to = DateField(required=False,
                                widget=DateWidget(
                                    attrs={
                                        'class': 'end-date-field ',
                                        'placeholder': 'End YYYY-MM-DD',
                                        'data-collapsed': True,
                                    }),
                                label='')
    provider_order_date_from = DateField(
        required=False,
        widget=DateWidget(attrs={
            'placeholder': 'Start YYYY-MM-DD',
            'data-collapsed': True,
        }),
        label="Provider order date",
    )
    provider_order_date_to = DateField(required=False,
                                       widget=DateWidget(
                                           attrs={
                                               'class': 'end-date-field ',
                                               'placeholder': 'End YYYY-MM-DD',
                                               'data-collapsed': True,
                                           }),
                                       label='')
    delivery_date_from = DateField(
        required=False,
        widget=DateWidget(attrs={
            'placeholder': 'Start YYYY-MM-DD',
            'data-collapsed': True,
        }),
        label="Delivery date",
    )
    delivery_date_to = DateField(required=False,
                                 widget=DateWidget(
                                     attrs={
                                         'class': 'end-date-field ',
                                         'placeholder': 'End YYYY-MM-DD',
                                         'data-collapsed': True,
                                     }),
                                 label='')
    deprecation_rate = ChoiceField(required=False,
                                   choices=[
                                       ('', '----'),
                                       ('null', 'None'),
                                       ('48>', '48 <'),
                                       ('48', '24 < * <= 48'),
                                       ('24', '12 < * <= 24'),
                                       ('12', '6 < * <= 12'),
                                       ('6', '* <= 6'),
                                       ('deprecated', 'Deprecated'),
                                   ],
                                   label='Deprecation')
    invoice_date_from = DateField(
        required=False,
        widget=DateWidget(attrs={
            'placeholder': 'Start YYYY-MM-DD',
            'data-collapsed': True,
        }),
        label="Invoice date",
    )
    invoice_date_to = DateField(required=False,
                                widget=DateWidget(
                                    attrs={
                                        'class': 'end-date-field ',
                                        'placeholder': 'End YYYY-MM-DD',
                                        'data-collapsed': True,
                                    }),
                                label='')

    production_use_date_from = DateField(
        required=False,
        widget=DateWidget(attrs={
            'placeholder': 'Start YYYY-MM-DD',
            'data-collapsed': True,
        }),
        label="Production use date",
    )
    production_use_date_to = DateField(required=False,
                                       widget=DateWidget(
                                           attrs={
                                               'class': 'end-date-field ',
                                               'placeholder': 'End YYYY-MM-DD',
                                               'data-collapsed': True,
                                           }),
                                       label='')
    unlinked = BooleanField(required=False, label="Is unlinked")
    deleted = BooleanField(required=False, label="Include deleted")

    def __init__(self, *args, **kwargs):
        # Ajax sources are different for DC/BO, use mode for distinguish
        mode = kwargs.get('mode')
        if mode:
            del kwargs['mode']
        super(SearchAssetForm, self).__init__(*args, **kwargs)
        category = self.fields['category'].queryset
        if mode == 'dc':
            self.fields['category'].queryset = category.filter(
                type=AssetCategoryType.data_center)
        elif mode == 'back_office':
            self.fields['category'].queryset = category.filter(
                type=AssetCategoryType.back_office)
class BaseEditAssetForm(ModelForm):
    class Meta:
        model = Asset
        fields = (
            'niw',
            'sn',
            'type',
            'category',
            'model',
            'status',
            'warehouse',
            'source',
            'invoice_no',
            'order_no',
            'price',
            'support_price',
            'support_type',
            'support_period',
            'support_void_reporting',
            'provider',
            'remarks',
            'sn',
            'barcode',
            'request_date',
            'provider_order_date',
            'delivery_date',
            'invoice_date',
            'production_use_date',
            'deleted',
            'deprecation_rate',
            'slots',
        )
        widgets = {
            'request_date': DateWidget(),
            'delivery_date': DateWidget(),
            'invoice_date': DateWidget(),
            'production_use_date': DateWidget(),
            'provider_order_date': DateWidget(),
            'remarks': Textarea(attrs={'rows': 3}),
            'support_type': Textarea(attrs={'rows': 5}),
            'sn': Textarea(attrs={
                'rows': 1,
                'readonly': '1'
            }),
            'barcode': Textarea(attrs={'rows': 1}),
        }

    model = AutoCompleteSelectField(
        LOOKUPS['asset_model'],
        required=True,
        plugin_options=dict(
            add_link='/admin/ralph_assets/assetmodel/add/?name=', ))
    warehouse = AutoCompleteSelectField(
        LOOKUPS['asset_warehouse'],
        required=True,
        plugin_options=dict(
            add_link='/admin/ralph_assets/warehouse/add/?name=', ))
    category = TreeNodeChoiceField(
        queryset=AssetCategory.tree.all(),
        level_indicator='|---',
        empty_label="---",
    )
    source = ChoiceField(choices=AssetSource(), )

    def __init__(self, *args, **kwargs):
        mode = kwargs.get('mode')
        if mode:
            del kwargs['mode']
        super(BaseEditAssetForm, self).__init__(*args, **kwargs)
        category = self.fields['category'].queryset
        if mode == "dc":
            self.fields['type'].choices = [(c.id, c.desc)
                                           for c in AssetType.DC.choices]
            self.fields['category'].queryset = category.filter(
                type=AssetCategoryType.data_center)
        elif mode == "back_office":
            self.fields['type'].choices = [(c.id, c.desc)
                                           for c in AssetType.BO.choices]
            self.fields['category'].queryset = category.filter(
                type=AssetCategoryType.back_office)

    def clean_sn(self):
        return self.instance.sn

    def clean_category(self):
        data = self.cleaned_data["category"]
        if not data.parent:
            raise ValidationError(
                _("Category must be selected from the subcategory"))
        return data

    def clean(self):
        if self.instance.deleted:
            raise ValidationError(_("Cannot edit deleted asset"))
        return self.cleaned_data