Exemplo n.º 1
0
def newHw(request):
    if request.POST:
        txt_hwid = request.POST.get('txt_hwid').strip()
        txt_psw = request.POST.get('txt_sn').strip()
        txt_title = request.POST.get('txt_title').strip()
        hwtype_radio = request.POST.get('hwtype')
        import re
        reg = re.compile(r'^[\w\s]+$')
        if reg.search(txt_hwid) is None:
            messages.info(
                request,
                'Please use only alphanumeric symbols and whitespaces for hwid.'
            )
            return render(request, 'hardware/newhw.html')
        if reg.search(txt_psw) is None:
            messages.info(
                request,
                'Please use only alphanumeric symbols and whitespaces for serial number.'
            )
            return render(request, 'hardware/newhw.html')
        if reg.search(txt_title) is None:
            messages.info(
                request,
                'Please use only alphanumeric symbols and whitespaces for title.'
            )
            return render(request, 'hardware/newhw.html')

        from django.db import IntegrityError
        if hwtype_radio is not None:
            if hwtype_radio == '0':
                from appliances.models import Appliance, ApplianceChore
                try:
                    a = Appliance()
                    a.hwid = txt_hwid
                    a.password = txt_psw
                    a.name = txt_title
                    a.generatePad()
                    a.save()

                    ac = ApplianceChore()
                    ac.appliance = a
                    ac.name = a.name + " Chore"
                    ac.save()

                except IntegrityError:
                    messages.error(request,
                                   'Appliance with this id already exists.')
                    return render(request, 'hardware/newhw.html')
                else:
                    messages.info(request,
                                  a.getName() + ' successfully registered.')
                    return redirect('hardware:hwlist')

            elif hwtype_radio == '1':
                from dishes.models import ItemTracker
                try:
                    h = ItemTracker()
                    h.hwid = txt_hwid
                    h.password = txt_psw
                    h.name = txt_title
                    h.generatePad()
                    h.save()
                except IntegrityError:
                    messages.error(
                        request, 'Item tracker with this id already exists.')
                    return render(request, 'hardware/newhw.html')
                else:
                    messages.info(request,
                                  h.getName() + ' successfully registered.')
                    return redirect('hardware:hwlist')

        else:
            messages.info(request, 'Please select hardware type')
            return render(request, 'hardware/newhw.html')

    return render(request, 'hardware/newhw.html')