Esempio n. 1
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)
Esempio n. 2
0
def stopfuzzing(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "停止"
    if request.POST.get('post'):
        for obj in queryset:
            o = copy.deepcopy(obj)
            try:
                #os.kill(int(obj.runningpid), signal.SIGKILL)
                obj.actived = False
                kill_all_process(int(obj.runningpid))
                obj.runningpid = ""
            except Exception as e:
                print(e)

            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="停止",
                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)
Esempio n. 3
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
Esempio n. 4
0
def delete(request, queryset):
    model = queryset.model
    opts = model._meta
    action = sys._getframe().f_code.co_name
    action_name = "删除"

    modeladmin = admin.site._registry.get(model)
    # queryset = queryset.filter(actived=False)
    if not modeladmin.has_delete_permission(request):
        raise PermissionDenied
    using = router.db_for_write(modeladmin.model)

    deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
        queryset, request, modeladmin.admin_site)

    if request.POST.get('post') and not protected:
        if perms_needed:
            raise PermissionDenied
        if queryset.count():
            for obj in queryset:
                log_action(
                    user_id=request.user.pk,
                    content_type_id=get_content_type_for_model(obj, True).pk,
                    object_id=obj.pk,
                    action_flag="删除"
                )
            if not SOFT_DELELE:
                queryset.delete()
            else:
                queryset.update(deleted=True, actived=False)
        return None

    if len(queryset) == 1:
        objects_name = force_text(opts.verbose_name)
    else:
        objects_name = force_text(opts.verbose_name_plural)

    meta, menus = construct_model_meta(request, model, action_name)

    context = dict(
        objects_name=objects_name,
        deletable_objects=[deletable_objects],
        model_count=dict(model_count).items(),
        queryset=queryset,
        perms_lacking=perms_needed,
        protected=protected,
        opts=opts,
        meta=meta,
        action=action,
        action_name=action_name,
        menus=menus,
    )

    request.current_app = modeladmin.admin_site.name

    return TemplateResponse(request, 'base/base_confirmation.html', context)
Esempio n. 5
0
def startfuzzing(request, queryset):
    action = sys._getframe().f_code.co_name
    action_name = "开始"
    if not os.path.exists(settings.WORKING_ROOT):
        try:
            os.mkdir(settings.WORKING_ROOT)
            os.mkdir(os.path.join(settings.WORKING_ROOT, "fuzzer_in"))
            os.mkdir(os.path.join(settings.WORKING_ROOT, "fuzzer_out"))
        except:
            pass
    if request.POST.get('post'):
        for obj in queryset:
            o = copy.deepcopy(obj)
            obj.actived = True
            b_path = os.path.join(settings.BASE_DIR, obj.binary[1:])
            os.chmod(b_path,stat.S_IXUSR |stat.S_IRUSR |stat.S_IWUSR | stat.S_IWGRP | stat.S_IRGRP | stat.S_IXOTH | stat.S_IROTH | stat.S_IWOTH)
            cmd = [sys.executable, os.path.join(settings.BASE_DIR, "workingFuzzer.py"),
                   os.path.join(settings.WORKING_ROOT, "fuzzer_in"),
                   os.path.join(settings.WORKING_ROOT, "fuzzer_out", obj.name)]
            cmd += obj.cmdargs.split(" ")
            cmd += [b_path, "--", obj.targetargs]
            if not os.path.exists(os.path.join(settings.WORKING_ROOT, "fuzzer_out", obj.name)):
                try:
                    os.mkdir(os.path.join(settings.WORKING_ROOT, "fuzzer_out", obj.name))
                except:
                    pass
            #file_output = open(os.path.join(settings.WORKING_ROOT, "fuzzer_out", obj.name, "ui.log"), "w")
            file_output = SpooledFile(os.path.join(settings.WORKING_ROOT, "fuzzer_out", obj.name, "ui.log"), mode="w", max_size=100*1024)
            p = run_sh(cmd,stdin=subprocess.PIPE, stdout=file_output, stderr=subprocess.PIPE, cwd=settings.BASE_DIR)
            obj.runningpid = p.pid

            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="开始",
                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)
Esempio n. 6
0
 def form_valid(self, form):
     form.instance.creator = self.request.user
     if 'onidc' not in form.cleaned_data:
         form.instance.onidc = self.request.user.onidc
     response = super(NewModelView, self).form_valid(form)
     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="新增")
     # 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
Esempio n. 7
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)