美文网首页django
Django test inlineformset_factor

Django test inlineformset_factor

作者: swotpp | 来源:发表于2018-07-07 15:07 被阅读0次

    测试更换备件时记录备件挂载表

    在备件更换的工单中,系统会将备件的好件pn和好件sn记录到备件挂载表(class SpareMount)里。

    该功能是在 class SpareExchangeView(View) 里实现的。


    说明

    • inlineformset_factory: 前缀是 spareexchange_set-, 可以在生成的html中找到,复制过来即可。
    • ForeignKey: 需要使用关联表的 pk(id)
      • 如下面的 exchange_type_obj.pk

    urls.py

    from django.contrib.auth.decorators import login_required
    from spare import views as spare_views
    
    url(r'^spare_exchange/(?P<pk>[0-9]+)/$',
        login_required(spare_views.SpareExchangeView.as_view()),
        name='spare_exchange',
    ),
    

    test代码示例:

    "spare/tests/test_views.py"

    from spare import models as spare_models
    from call.tests.base import ViewTest # <-- 初始化一些数据 def setUp()
    
    class SpareTest(ViewTest):
        def test_SpareMount_goodSpare_in_other_device_fail(self):
    
            '''测试工程师备件交换操作时,好件已经在备件挂载表中,则不让继续做备件交换'''
    
            # 生成一条备件挂载记录,这个好件之前就存在99999设备上,下面在备件交换时不会更改此挂载内容
            spare_models.SpareMount.objects.create(
                devSn='999999',
                pn='aaaaaa',
                sn='a11111',)
    
            exchange_type_obj = spare_models.ExchangeType.objects.first()
            # print('exchange_type_obj:', exchange_type_obj)
    
            data = {
                'spareexchange_set-TOTAL_FORMS': '1',
                'spareexchange_set-INITIAL_FORMS': '0',
                'spareexchange_set-MAX_NUM_FORMS': '',
                'spareexchange_set-0-pn_bad': 'aaaaaa',
                'spareexchange_set-0-sn_bad': 'a00000',
                'spareexchange_set-0-pn_good': 'aaaaaa',
                'spareexchange_set-0-sn_good': 'a11111',
                # 'spareexchange_set-0-badTime': timezone.now(),
                # 'spareexchange_set-0-goodTime': timezone.now(),
                'spareexchange_set-0-exchangeType': exchange_type_obj.pk,
            }
            # args=[] Call表的第一条工单要符合已经开始,但未关闭。否则无法生成备件挂载到设备序列号记录
            not_close_call_obj = call_models.Call.objects.get(closeTime=None)
            # print('not_close_call_obj:', not_close_call_obj)
            response = self.client.post(reverse('spare_exchange', args=[not_close_call_obj.pk]), data)
    
            self.assertContains(response, u'该备件已经安装于设备序列号')
    

    views代码示例:

    "spare/views.py"

    from django.views.generic import View
    from django.forms import inlineformset_factory, TextInput
    from django.utils import timezone
    from call import models as call_models
    from spare import models as spare_models
    from spare import forms as spare_forms
    
    class SpareExchangeView(View):
        def post(self, request, pk, *args, **kwargs):
            call = call_models.Call.objects.get(pk=pk)
            SpareExchangeFormSet = inlineformset_factory(
                call_models.Call,
                spare_models.SpareExchange,
                extra=0 if call.closeTime else 1,
                fields=("badTime",
                        "goodTime",
                        "pn_bad",
                        "sn_bad",
                        "pn_good",
                        "sn_good",
                        "exchangeType",
                       ),
                widgets={
                    'badTime': TextInput(attrs={'type': 'hidden'}),
                    'goodTime': TextInput(attrs={'type': 'hidden'}),
                },
                formset=spare_forms.CustomSpareInlineFormSet
            )
    
            # print("SpareExchangeFormSet:", SpareExchangeFormSet)
    
            formset = SpareExchangeFormSet(
                request.POST,
                request.FILES,
                instance=call,
            )
    
            if call.engineer == request.user:
                if call.startTime and not call.closeTime:
                    # formset.is_valid()
                    # print(formset.errors)
                    if formset.is_valid():
                        instances = formset.save(commit=False)
                        for instance in instances:
                            << create SpareExchange records >> 省略部分创建备件交换记录代码
                            << create SpareMount >> 创建备件挂载记录,如下
                            # 找到就更新,没找到就创建
                            # 不需要删除原来的设备序列号关联,因为找的是pn和sn,如果有直接就更新了,这样不错。
                            spare_mount_obj, created = spare_models.SpareMount.objects.update_or_create(
                                # devSn=call.sn,
                                pn=instance.pn_good,
                                sn=instance.sn_good,
                                defaults={
                                    'devSn': call.sn,
                                    'engineer': call.engineer,
                                    'logTime': timezone.now(),
                                    'call': call,
                                },
                            )
                        return redirect(reverse(
                            'call_engineer_detail',
                            kwargs={'pk': pk}
                        ))
    

    相关文章

      网友评论

        本文标题:Django test inlineformset_factor

        本文链接:https://www.haomeiwen.com/subject/pqwwuftx.html