Exemplo n.º 1
0
 def form_valid(self, form):
     form.instance.operator = self.request.user
     if 'onidc' not in form.cleaned_data:
         form.instance.onidc = self.request.user.onidc
     d1 = form.initial
     message = json.dumps(form.changed_data)
     response = super(EditModelView, self).form_valid(form)
     d2 = model_to_dict(construct_instance(form, self.object))
     diffs = diff_dict(make_dict(d1), make_dict(d2))
     content = json.dumps(diffs)
     log_action(user_id=self.request.user.pk,
                content_type_id=get_content_type_for_model(
                    self.object, True).pk,
                object_id=self.object.pk,
                action_flag="修改",
                message=message,
                content=content)
     if self.model_name == 'online':
         verify = Thread(target=device_post_save, args=(self.object.pk, ))
         verify.start()
     if self.request.is_ajax():
         data = {
             'message': "Successfully submitted form data.",
             'data': form.cleaned_data
         }
         return JsonResponse(data)
     else:
         return response
Exemplo n.º 2
0
def reoutbound(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "取消出库"
    queryset = queryset.filter(actived=False)
    if not queryset.exists():
        return "查无结果"

    if request.POST.get('post'):
        for obj in queryset:
            o = copy.deepcopy(obj)
            obj.actived = True
            obj.save()
            diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
            log_action(
                user_id=request.user.pk,
                content_type_id=get_content_type_for_model(obj, True).pk,
                object_id=obj.pk,
                action_flag=action_name,
                message=json.dumps(list(diffs.keys())),
                content=json.dumps(diffs)
            )
        return None

    context = construct_context(request, queryset, action, action_name)
    return TemplateResponse(request, 'base/base_confirmation.html', context)
Exemplo n.º 3
0
def renewal(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "机柜续期"
    queryset = queryset.filter(actived=True)
    if request.POST.get('post') and request.POST.getlist('items'):

        def construct_item(index):
            obj = queryset.get(pk=int(index))
            expiry_date = request.POST.get(f'expiry_date-{index}')
            return obj, expiry_date

        for item in request.POST.getlist('items'):
            obj, expiry_date = construct_item(item)
            try:
                expiry_date = timezone.datetime.strptime(
                    expiry_date, '%Y-%m-%d').date()
            except BaseException:
                expiry_date = None
            o = copy.deepcopy(obj)
            obj.operator = request.user
            obj.expiry_date = expiry_date
            obj.save()
            diffs = diff_dict(serialize_object(o), serialize_object(obj))
            log_action(user_id=request.user.pk,
                       content_type_id=get_content_type_for_model(obj,
                                                                  True).pk,
                       object_id=obj.pk,
                       action_flag=action_name,
                       message=json.dumps(list(diffs.keys())),
                       content=json.dumps(diffs))
        return None

    context = construct_context(request, queryset, action, action_name)
    return TemplateResponse(request, 'rack/renewal.html', context)
Exemplo n.º 4
0
def distribution(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "分配机柜"
    queryset = queryset.filter(actived=False)
    onidc_id = request.user.onidc.id
    options = Option.objects.filter(actived=True)
    clients = shared_queryset(Client.objects.filter(actived=True), onidc_id)
    status = shared_queryset(options.filter(flag='Rack-Status'), onidc_id)
    styles = shared_queryset(options.filter(flag='Rack-Style'), onidc_id)
    if request.POST.get('post') and request.POST.getlist('items'):

        def construct_item(index):
            obj = queryset.get(pk=int(index))
            try:
                client = int(request.POST.get('client-' + str(index)))
            except BaseException:
                client = 0
            status = int(request.POST.get(f'status-{index}'))
            style = int(request.POST.get(f'style-{index}'))
            expiry_date = request.POST.get(f'expiry_date-{index}')
            cpower = request.POST.get(f'cpower-{index}')
            comment = request.POST.get((f'comment-{index}'), None)
            return obj, client, status, style, expiry_date, cpower, comment

        for item in request.POST.getlist('items'):
            obj, client, status, style, expiry_date, cpower, _ = construct_item(
                item)
            try:
                expiry_date = timezone.datetime.strptime(
                    expiry_date, '%Y-%m-%d').date()
            except BaseException:
                expiry_date = None
            o = copy.deepcopy(obj)
            if client != 0:
                obj.client_id = client
            obj.status_id = status
            obj.style_id = style
            obj.cpower = cpower
            obj.expiry_date = expiry_date
            obj.actived = True
            obj.save()
            diffs = diff_dict(serialize_object(o), serialize_object(obj))
            log_action(user_id=request.user.pk,
                       content_type_id=get_content_type_for_model(obj,
                                                                  True).pk,
                       object_id=obj.pk,
                       action_flag=action_name,
                       message=json.dumps(list(diffs.keys())),
                       content=json.dumps(diffs))
        return None

    context = construct_context(request, queryset, action, action_name)
    _extra = dict(clients=clients, status=status, styles=styles)
    context.update(_extra)
    return TemplateResponse(request, 'rack/distribution.html', context)
Exemplo n.º 5
0
def release(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "释放机柜"
    rack_ids = [id for id in queryset.values_list('id', flat=True)]
    # fix: unknown your action: The QuerySet value
    if Online.objects.filter(rack_id__in=rack_ids).exists():
        mesg = "选择的机柜中仍有在线设备,无法释放"
        return mesg

    queryset = queryset.filter(actived=True)
    if request.POST.get('post'):
        for obj in queryset:
            o = copy.deepcopy(obj)
            if obj.client and obj.client.onlinenum() == 0:
                verb = f"客户 {force_text(obj.client)} 没有在线设备, 是否终止"
                log_action(user_id=request.user.pk,
                           content_type_id=get_content_type_for_model(
                               obj, True).pk,
                           object_id=obj.pk,
                           action_flag="系统通知",
                           message=verb,
                           content=verb)
            obj.actived = False
            obj.client = None
            obj.cpower = 0
            obj.style = None
            obj.status = None
            obj.expiry_date = None
            obj.operator = request.user
            obj.tags.clear()

            if obj.jnum() != 0:
                verb = f"机柜 {force_text(obj)} 还有跳线存在, 请回收"
                log_action(user_id=request.user.pk,
                           content_type_id=get_content_type_for_model(
                               obj, True).pk,
                           object_id=obj.pk,
                           action_flag="系统通知",
                           message=verb,
                           content=verb)

            obj.save()
            diffs = diff_dict(serialize_object(o), serialize_object(obj))
            log_action(user_id=request.user.pk,
                       content_type_id=get_content_type_for_model(obj,
                                                                  True).pk,
                       object_id=obj.pk,
                       action_flag=action_name,
                       message=json.dumps(list(diffs.keys())),
                       content=json.dumps(diffs),
                       related_client=get_related_client_name(o))
        return None
    context = construct_context(request, queryset, action, action_name)
    return TemplateResponse(request, 'base/base_confirmation.html', context)
Exemplo n.º 6
0
def removeup(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "取消下架"
    exclude = queryset.filter(rack__actived=False)
    if exclude.exists():
        mesg = "有设备所在机柜未使用, 无法取消下架"
        return mesg

    if request.POST.get('post'):
        for obj in queryset:
            o = copy.deepcopy(obj)
            obj.actived = True
            obj.status = 'online'
            obj.operator = request.user
            lastunits = copy.deepcopy(obj.units.all())
            lastpdus = copy.deepcopy(obj.pdus.all())
            ucan_recovery = False not in [u.actived for u in lastunits]
            pcan_recovery = False not in [p.actived for p in lastpdus]
            if ucan_recovery:
                obj.units.all().update(actived=False, operator=obj.operator)
            else:
                verb = f"无法恢复 {force_text(obj)} 的U位"
                log_action(user_id=request.user.pk,
                           content_type_id=get_content_type_for_model(
                               obj, True).pk,
                           object_id=obj.pk,
                           action_flag="系统通知",
                           message=verb,
                           content=verb)
                obj.units.clear()
            if pcan_recovery:
                obj.pdus.all().update(actived=False, operator=obj.operator)
            else:
                obj.pdus.clear()
            obj.save()
            diffs = diff_dict(serialize_object(o), serialize_object(obj))
            message = json.dumps(list(diffs.keys()))
            old_units = [force_text(u) for u in lastunits]
            old_pdus = [force_text(p) for p in lastpdus]
            diffs.update({'last_units': old_units, 'last_pdus': old_pdus})
            content = json.dumps(diffs)
            log_action(user_id=request.user.pk,
                       content_type_id=get_content_type_for_model(obj,
                                                                  True).pk,
                       object_id=obj.pk,
                       action_flag=action_name,
                       message=message,
                       content=content)
        return None
    context = construct_context(request, queryset, action, action_name)
    return TemplateResponse(request, 'base/base_confirmation.html', context)
Exemplo n.º 7
0
def release(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "释放机柜"
    if Online.objects.filter(rack=queryset).exists():
        mesg = "选择的机柜中仍有在线设备,无法释放"
        return mesg

    queryset = queryset.filter(actived=True)
    if request.POST.get('post'):
        for obj in queryset:
            o = copy.deepcopy(obj)
            if obj.client and obj.client.onlinenum() == 0:
                verb = "客户 {} 没有在线设备, 是否终止".format(force_text(obj.client))
                notify_user.send(
                    request.user,
                    recipient=request.user,
                    target=obj,
                    verb=verb,
                )

            obj.actived = False
            obj.client = None
            obj.cpower = 0
            obj.style = None
            obj.status = None
            obj.operator = request.user
            obj.tags.clear()

            if obj.jnum() != 0:
                verb = "机柜 {} 还有跳线存在, 请回收".format(force_text(obj))
                notify_user.send(
                    request.user,
                    recipient=request.user,
                    target=obj,
                    verb=verb,
                )

            obj.save()
            from idcops.lib.tasks import get_related_client_name
            diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
            log_action(user_id=request.user.pk,
                       content_type_id=get_content_type_for_model(obj,
                                                                  True).pk,
                       object_id=obj.pk,
                       action_flag=action_name,
                       message=json.dumps(list(diffs.keys())),
                       content=json.dumps(diffs),
                       related_client=get_related_client_name(o))
        return None
    context = construct_context(request, queryset, action, action_name)
    return TemplateResponse(request, 'base/base_confirmation.html', context)
Exemplo n.º 8
0
def cancel_reclaim(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "取消回收"
    if request.POST.get('post'):
        for obj in queryset:
            o = copy.deepcopy(obj)
            obj.actived = True
            obj.save()
            diffs = diff_dict(serialize_object(o), serialize_object(obj))
            log_action(user_id=request.user.pk,
                       content_type_id=get_content_type_for_model(obj,
                                                                  True).pk,
                       object_id=obj.pk,
                       action_flag=action_name,
                       message=json.dumps(list(diffs.keys())),
                       content=json.dumps(diffs))
        return None
    context = construct_context(request, queryset, action, action_name)
    return TemplateResponse(request, 'base/base_confirmation.html', context)
Exemplo n.º 9
0
def movedown(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "下架"
    if request.POST.get('post'):
        for obj in queryset:
            o = copy.deepcopy(obj)
            obj.actived = False
            obj.status = 'offline'
            obj.operator = request.user
            obj.units.all().update(actived=True, operator=obj.operator)
            obj.pdus.all().update(actived=True, operator=obj.operator)
            obj.save()
            diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
            log_action(user_id=request.user.pk,
                       content_type_id=get_content_type_for_model(obj,
                                                                  True).pk,
                       object_id=obj.pk,
                       action_flag=action_name,
                       message=json.dumps(list(diffs.keys())),
                       content=json.dumps(diffs))
        return None
    context = construct_context(request, queryset, action, action_name)
    return TemplateResponse(request, 'base/base_confirmation.html', context)
Exemplo n.º 10
0
def outbound(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "出库"
    queryset = queryset.filter(actived=True)
    if not queryset.exists():
        return "选择无结果"

    total = queryset.aggregate(Sum('amount'))
    if request.POST.get('post') and request.POST.getlist('items'):

        def construct_item(index):
            obj = queryset.get(pk=int(index))
            out_amount = int(request.POST.get('count-' + str(index)))
            out_serials = request.POST.getlist('sn-' + str(index))
            copy_needed = True
            if int(out_amount) == obj.amount:
                copy_needed = False
            comment = request.POST.get(('comment-' + index), None)
            return obj, copy_needed, out_serials, out_amount, comment

        for item in request.POST.getlist('items'):
            obj, _copy, out_serials, out_amount, comment = construct_item(item)
            o = copy.deepcopy(obj)
            if _copy:
                hold = [
                    s for s in obj.serials.split(',') if s not in out_serials
                ]
                obj.amount -= out_amount
                obj.serials = ','.join(hold)
                new_obj = copy.deepcopy(obj)
                new_obj.pk = None
                new_obj.amount = out_amount
                new_obj.serials = ','.join(out_serials)
                new_obj.actived = False
                new_obj.creator = request.user
                new_obj.created = timezone.datetime.now()
                new_obj.operator = None
                new_obj.parent = obj
                new_obj.save()
                comment_obj = new_obj
            else:
                obj.actived = False
                obj.operator = request.user
                comment_obj = obj
            obj.save()
            if comment:
                Comment.objects.create(object_repr=comment_obj,
                                       content=comment,
                                       creator=request.user,
                                       onidc=obj.onidc)
            diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
            log_action(user_id=request.user.pk,
                       content_type_id=get_content_type_for_model(obj,
                                                                  True).pk,
                       object_id=comment_obj.pk,
                       action_flag=action_name,
                       message=json.dumps(list(diffs.keys())),
                       content=json.dumps(diffs))
        return None
    context = construct_context(request, queryset, action, action_name)
    _extra = dict(total=total)
    context.update(_extra)
    return TemplateResponse(request, 'base/items_out.html', context)