def setUp(self): self.flow = Flow() self.flow.name = "Test" self.flow.description = "A test flow" #self.flow.expires = timezone.now() + datetime.timedelta(days=1) self.flow.save() self.flow.destination = '1.2.3.4/32' self.flow.source = '4.3.2.1/23' self.flow.protocol_set.create(protocol=12) self.flow.protocol_set.create(protocol=123) self.flow.port_set.create(port_number=23) self.flow.port_set.create(port_number=234, direction="source-port") self.flow.packetlength_set.create(packet_length=23) self.flow.packetlength_set.create(packet_length='>432') self.flow.dscp_set.create(dscp='<2') self.flow.icmp_set.create(icmp_type=3, icmp_code=0) self.flow.icmp_set.create(icmp_type=1) self.flow.tcpflag_set.create(tcp_flag=2) self.flow.fragment_set.create(fragment=8) self.flow.active = True self.flow.save()
def demand_create(request): if request.method == 'GET': form = DemandCreateForm() return render(request, 'flow_create.html', {'form': form}) elif request.method == 'POST': form = DemandCreateForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] remark = form.cleaned_data['remark'] executor = form.cleaned_data['executor'] start_dt = form.cleaned_data['start_dt'] end_dt = form.cleaned_data['end_dt'] u = Employee.objects.get(user=request.user) obj = Flow(flow_type='demand', title=title, initiator=u.user, remark=remark, state='start', executor=executor, start_dt=start_dt, end_dt=end_dt) obj.save() obj_node = FlowNode(operator=u.user, operate='start', remark=remark, flowid=obj, executor=executor) obj_node.save() return redirect('flow_submit')
class TaskTest(TestCase): def setUp(self): self.flow = Flow() self.flow.name = "Test" self.flow.description = "A test flow" #self.flow.expires = timezone.now() + datetime.timedelta(days=1) self.flow.save() self.flow.destination = '1.2.3.4/32' self.flow.source = '4.3.2.1/23' self.flow.protocol_set.create(protocol=12) self.flow.protocol_set.create(protocol=123) self.flow.port_set.create(port_number=23) self.flow.port_set.create(port_number=234, direction="source-port") self.flow.packetlength_set.create(packet_length=23) self.flow.packetlength_set.create(packet_length='>432') self.flow.dscp_set.create(dscp='<2') self.flow.icmp_set.create(icmp_type=3, icmp_code=0) self.flow.icmp_set.create(icmp_type=1) self.flow.tcpflag_set.create(tcp_flag=2) self.flow.fragment_set.create(fragment=8) self.flow.active = True self.flow.save() def test_announce(self): task = tasks.announce.delay(self.flow) self.assertTrue(task.successful()) def test_withdraw(self): task = tasks.withdraw.delay(self.flow, self.flow.match, self.flow.then) self.assertTrue(task.successful()) def test_delete(self): task = tasks.withdraw.delay(self.flow, self.flow.match, self.flow.then) self.assertTrue(task.successful()) def test_expire(self): task = tasks.withdraw.delay(self.flow, self.flow.match, self.flow.then) self.assertTrue(task.successful())
class FlowTest(TestCase): def setUp(self): self.flow = Flow() self.flow.name = "Test" self.flow.description = "A test flow" #self.flow.expires = timezone.now() + datetime.timedelta(days=1) self.flow.save() self.flow.destination = '1.2.3.4/32' self.flow.source = '4.3.2.1/23' self.flow.protocol_set.create(protocol=12) self.flow.protocol_set.create(protocol=123) self.flow.port_set.create(port_number=23) self.flow.port_set.create(port_number=234, direction="source-port") self.flow.packetlength_set.create(packet_length=23) self.flow.packetlength_set.create(packet_length='>432') self.flow.dscp_set.create(dscp='<2') self.flow.icmp_set.create(icmp_type=3, icmp_code=0) self.flow.icmp_set.create(icmp_type=1) self.flow.tcpflag_set.create(tcp_flag=2) self.flow.fragment_set.create(fragment=8) self.flow.active = True self.flow.save() def test_init(self): self.assertEqual(self.flow.status, 5) # INACTIVE def test_has_expired(self): self.assertFalse(self.flow.has_expired()) self.flow.expires = timezone.now() - datetime.timedelta(hours=1) self.assertTrue(self.flow.has_expired()) def test_match(self): match = self.flow.match() self.assertEqual(match['source'], '4.3.2.1/23') self.assertEqual(match['destination'], '1.2.3.4/32') self.assertItemsEqual(match['protocol'], [12, 123]) self.assertItemsEqual(match['port'], ['23']) self.assertItemsEqual(match['source-port'], ['234']) self.assertItemsEqual(match['destination-port'], []) self.assertItemsEqual(match['packet-length'], ['23', '>432']) self.assertItemsEqual(match['dscp'], ['<2']) self.assertItemsEqual(match['icmp-type'], [1, 3]) self.assertItemsEqual(match['icmp-code'], [0]) self.assertItemsEqual(match['tcp-flags'], [2]) self.assertItemsEqual(match['fragment'], [8]) def test_then(self): then = self.flow.then() self.assertEquals(then, 'accept')
def flow_create(request): if request.method == 'GET': form = FlowForm() return render(request, 'flow_create.html', {'form': form}) elif request.method == 'POST': form = FlowForm(request.POST) if form.is_valid(): flow_type = form.cleaned_data['flow_type'] remark = form.cleaned_data['remark'] u = Employee.objects.get(user=request.user) obj = Flow(flow_type=flow_type, initiator=u.user, remark=remark, state='start', executor=u.leader) obj.save() obj_node = FlowNode(operator=u.user, operate='start', remark=remark, flowid=obj, executor=u.leader) obj_node.save() return redirect('flow_submit')
product = Product.objects.get(name='Venezuelan Beaver Cheese') product.number_sold += 1 product.save() from django.contrib.auth.models import User from flow.models import Flow,FlowNode,Employee # 发起人信息从request.user中获取,通过i获取employee对象 u=Employee.objects.get(user=request.user) <Employee: Employee object> u.user <User: rencs> u.leader <User: wangbin> # 分别保存工作流和工作流节点两个对象 obj=Flow(flow_type='work',initiator=u.user,remark='test',state='init',executor=u.leader) obj.save() obj_node=FlowNode(operator=u.user,operate='init',remark='test',flowid=obj,executor=u.leader) obj_node.save() # 以上是用户创建表单时候的初始化,用户和用户leader数据从request.user取 # 以下是用户leader的审批,工作流id从url中传入,obj=Flow.objects.get(id=obj.id) u2=Employee.objects.get(user=request.user) # 从url中获取当前审批工作流的id,获取工作流对象,leader审批后新增一条工作流节点信息。并更新当前工作流对象。 obj=Flow.objects.get(id=obj.id) obj_node2=FlowNode(operator=u2.user,operate='ok',remark='test',flowid=obj,executor=u2.leader) obj_node2.save() obj.state='doing' obj.executor=u2.leader obj.save()