def policy(request, policy_id=False): if policy_id: if 'delete_rule' in request.POST: pr = PolicyRule.objects.get(id=request.POST.get('delete_rule')) pr.delete() if 'action' in request.POST: act_policy = Policy.objects.get(id=policy_id) permit = bool(int(request.POST.get('action'))) source = Zone.objects.get(id=request.POST.get('source')) destination = Zone.objects.get(id=request.POST.get('destination')) new_rule = PolicyRule(policy=act_policy, permit=permit, num=str(len(act_policy.policyrule_set.all())+1), source=source, destination=destination, description=request.POST.get('description')) new_rule.save() return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: zones = Zone.objects.all() plc = Policy.objects.get(id=policy_id) return render(request, 'policy.html', {'policy': plc, 'zones': zones}) else: if 'delete_policy' in request.POST: dp = Policy.objects.get(id=request.POST.get('delete_policy')) dp.delete() if 'policy_name' in request.POST: new_policy = Policy( name=request.POST.get('policy_name'), description=request.POST.get('description')) new_policy.save() return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: all_policy = Policy.objects.all() return render(request, 'all_policy.html', {'all_policy': all_policy})
def make_policy(): schedules = ["Annual", "Two-Pay", "Quarterly", "Monthly"] i_contacts = Contact.query.filter(Contact.role == "Named Insured").all() a_contacts = Contact.query.filter(Contact.role == "Agent").all() if request.method == "POST": #try: p_num = request.form['policy_num'] eff_date = request.form['date'] bill_schedule = request.form['bill_schedule'] premium = request.form['premium'] policy = Policy(p_num, eff_date, premium) policy.billing_schedule = bill_schedule insured = request.form['insured'] if insured > 0: # If a 'Named Insured' was selected, tie to policy policy.named_insured = insured agent = request.form['agent'] policy.agent = agent db.session.add(policy) db.session.commit() # make PolicyAccounting object to initialize invoices pa = PolicyAccounting(policy.id) return redirect(url_for('getinvoices', p_id=policy.id)) # except: # return render_template('create.html', schedules=schedules, insured_list=i_contacts, agent_list=a_contacts) else: return render_template('create.html', schedules=schedules, insured_list=i_contacts, agent_list=a_contacts)
def policies(tenant_id): if request.method == 'POST': for policy in request.json['policies']: keys = [] for k in policy['keys']: key = Key(uuid=k['uuid'], filename=k['filename'], mime_type=k['mime_type'], expiration=parse(k['expiration']), secret=k['secret'], owner=k['owner'], group=k['group'], cacheable=k['cacheable']) keys.append(key) policy = Policy(uuid=policy['uuid'], name=policy['name'], tenant_id=tenant_id, directory_name=policy['directory_name'], max_key_accesses=policy['max_key_accesses'], time_available_after_reboot=policy['time_available_after_reboot']) policy.keys.extend(keys) db_session.add(policy) db_session.commit() return Response(status=200) else: policy = Policy.query.filter_by(tenant_id=tenant_id).first() if policy is None: return Response('No policies defined for tenant', status=404) # Hack for Matt's agent p = [] p.append(policy.as_dict()) policies = {'policies': p } return jsonify(policies)
def test(rank, params, shared_p): torch.manual_seed(params.seed + rank) env = gym.make(params.env_name) num_inputs = env.observation_space.shape[0] num_outputs = env.action_space.shape[0] policy = Policy(num_inputs, num_outputs) state = env.reset() state = Variable(torch.Tensor(state).unsqueeze(0)) reward_sum = 0 done = True start_time = time.time() episode_length = 0 while True: episode_length += 1 policy.load_state_dict(shared_p.state_dict()) mu, _ = policy(state) action = mu.data env_action = action.squeeze().numpy() state, reward, done, _ = env.step(env_action) reward_sum += reward if done: print("Time {}, episode reward {}, episode length {}".format( time.strftime("%Hh %Mm %Ss", time.gmtime(time.time() - start_time)), reward_sum, episode_length)) reward_sum = 0 episode_length = 0 state = env.reset() time.sleep(60) state = Variable(torch.Tensor(state).unsqueeze(0))
def initialize(self): print('Build Environment for {}'.format(self.actor_name)) if self.env is None: self.env = Env(self.args, self.device, options=self.args.options, dummy=self.args.dummy, rank=self.rank) self.policy = Policy(self.env.action_dim).to(self.device) self.memory = Memory()
def save(self, data: Dict): try: action = data['action'] data['action'] = int(action) if '1' <= action <= '6' else action policy = Policy(data) policy.save() except ValueError as err: raise ValidationError( message=str(err) )
def load_models(args, state_size, n_drones, action_size): nets = [] for i in range(n_drones): model = Policy(state_size, n_drones, action_size, policy_type=args.policy) model.load_state_dict( torch.load( f"A2C_models/{args.policy}_policy/A2C_drone_{icm_model_name}{i}.bin" )) nets.append(model) return nets
def test_evaluate_cancel(self): policy = Policy('Test Policy', date(2015, 1, 1), 1200) policy.named_insured = self.test_insured.id policy.agent = self.test_agent.id self.policies.append(policy) db.session.add(policy) db.session.commit() # Get the policy from the database policy.billing_schedule = "Monthly" pa = PolicyAccounting(policy.id) # Make a payment self.payments.append(pa.make_payment(date_cursor=date(2015, 01, 01), amount=100))
def test_payment_contact_id_data(self): # Create and store a policy without a named insured policy = Policy('Test Policy', date(2015, 1, 1), 1200) policy.named_insured = None policy.agent = self.test_agent.id self.policies.append(policy) db.session.add(policy) db.session.commit() # Get the policy from the database policy.billing_schedule = "Annual" pa = PolicyAccounting(policy.id) # Try to create a payment without a contact id specified, no payment should be registered self.assertFalse(pa.make_payment(date_cursor=date(2015, 01, 01), amount=1200))
def create(**kw): kw['state'] = None now = utc_8_now() kw['create_time'] = now kw['update_time'] = now # 参数合法性检查. 如果不合法,直接报错. is_valid_kw(Policy, **kw) # 检查id,policy_name, 如果存在record, 不允许添加 sanity_check = Policy.find_by('id, policy_name', 'where id=? OR policy_name=?', *[kw['id'], kw['policy_name']]) if sanity_check: raise ValueError("Already exist record with id=[%s], policy_name=[%s]." % (sanity_check[0]['id'], sanity_check[0]['policy_name'])) policy = Policy(**kw) policy.insert()
def new_policy(policy_number, effective_date, annual_premium, billing_schedule, agent=None, named_insured=None): try: # makes a new policy object new_policy = Policy(policy_number, effective_date, annual_premium) new_policy.billing_schedule = billing_schedule # check if a named_insured was passed, if it was passed get the reference object from the db if named_insured: named_insured_ = Contact.query.filter_by( name=named_insured, role="Named Insured").first() # if there is not a contact with this name a new one is created if not named_insured_: contact = Contact(named_insured, 'Named Insured') named_insured_ = contact db.session.add(contact) db.session.commit() named_insured = named_insured_.id # check if a agent was passed, if it was passed get the reference object from the db if agent: agent_ = Contact.query.filter_by(name=agent, role="Agent").first() # if there is not a contact with this name a new one is created if not agent_: contact = Contact(agent, 'Agent') agent_ = contact db.session.add(contact) db.session.commit() agent = agent_.id new_policy.named_insured = named_insured new_policy.agent = agent db.session.add(new_policy) db.session.commit() except Exception as error: logging.error(error)
def create(**kw): kw['state'] = None now = utc_8_now() kw['create_time'] = now kw['update_time'] = now # 参数合法性检查. 如果不合法,直接报错. is_valid_kw(Policy, **kw) # 检查id,policy_name, 如果存在record, 不允许添加 sanity_check = Policy.find_by('id, policy_name', 'where id=? OR policy_name=?', *[kw['id'], kw['policy_name']]) if sanity_check: raise ValueError( "Already exist record with id=[%s], policy_name=[%s]." % (sanity_check[0]['id'], sanity_check[0]['policy_name'])) policy = Policy(**kw) policy.insert()
def init_training(opt): # Initialize losses losses = { 'adversarial': torch.nn.BCELoss(), 'pixelwise': torch.nn.L1Loss(), 'action': torch.nn.NLLLoss() } img_shape = (1, opt.img_size, opt.img_size) # Initialize models encoder = Encoder(img_shape, opt.latent_dim) decoder = Decoder(img_shape, opt.latent_dim) discriminator = Discriminator(opt.latent_dim) model = {'enc': encoder, 'dec': decoder, 'discr': discriminator} if opt.domain == 'source': pol = Policy(opt.latent_dim, ac_size=3) model['pol'] = pol if opt.use_dynamics: decoder_next = Decoder(img_shape, opt.latent_dim) model['dec_next'] = decoder_next if opt.domain == 'source': dyn = Dynamics(opt.latent_dim, ac_size=3, ac_embed_size=10) model['dyn'] = dyn # move to GPU if opt.cuda: for loss in losses.values(): loss.cuda() for network in model.values(): network.cuda() # Optimizers G_params = [] for name, network in model.items(): G_params += [network.parameters()] if name != 'discr' else [] G_params = itertools.chain(*G_params) optimizer_G = torch.optim.Adam(G_params, lr=opt.lr, betas=(opt.b1, opt.b2)) optimizer_D = torch.optim.Adam(model['discr'].parameters(), lr=opt.lr, betas=(opt.b1, opt.b2)) # metrics metrics_dict = { 'adv_losses': [], 'pix_losses': [], 'ac_losses': [], 'g_losses': [], 'd_losses': [], 'rf_z_sep_accs': [], 'pol_accs': [] } if opt.use_dynamics: metrics_dict['pix_next_losses'] = [] return model, losses, optimizer_G, optimizer_D, metrics_dict
def post_evaluate(models_path, sigma, n_post_episodes=5, add_noise=False): # print('----------------Post evaluation----------------') policy_path = models_path + "_policy" value_path = models_path + "_value" if args.use_parameter_noise: policy_post = PolicyLayerNorm(num_inputs, num_actions) value_post = Value(num_inputs) else: policy_post = Policy(num_inputs, num_actions) value_post = Value(num_inputs) # print('------------------') value_post.load_state_dict(torch.load(value_path)) policy_post.load_state_dict(torch.load(policy_path)) reward_post = 0 for i in range(n_post_episodes): state = env.reset() ##seeding # env.seed(i) # torch.manual_seed(i) # state = running_state(state) for t in range(1000): if args.use_parameter_noise and add_noise: action = select_action(policy_post, state, sigma, add_noise=True) else: action = select_action(policy_post, state) action = action.data[0].numpy() next_state, reward, done, _ = env.step(action) reward_post += reward # next_state = running_state(next_state) if done: break # state = running_state(next_state) state = next_state print('___Post evaluation reward___') print(reward_post / n_post_episodes) return reward_post / n_post_episodes
def make_policy(self, policy_number, effective_date, annual_premium): """ Create a new policy method. """ policy = Policy(policy_number, effective_date, annual_premium) db.session.add(policy) db.session.commit() return policy
def setUpClass(cls): cls.policy = Policy('Test Policy', date(2015, 1, 1), 1200) db.session.add(cls.policy) db.session.commit() cls.invoice_1 = Invoice(cls.policy.id, date(2015, 1, 1), date(2015, 1, 15), date(2015, 2, 1), 50) cls.invoice_2 = Invoice(cls.policy.id, date(2015, 3, 1), date(2015, 3, 1), date(2015, 2, 1), 20) db.session.add(cls.invoice_1) db.session.add(cls.invoice_2) db.session.commit()
def create_policy_four(): # get contact whose name and Role are John Doe and role respectively john_doe_agent = Contact.query.filter_by(name="John Doe", role="Agent").one() # get contact whose name and Role are Ryan Bucket and Name Insured respectively ryan_bucket = Contact.query.filter_by(name="Ryan Bucket", role="Named Insured").one() # create a policy instance for Policy Four with annual amount of $500 p4 = Policy('Policy Four', date(2015, 2, 1), 500) p4.billing_schedule = 'Two-Pay' # billing schedule p4.agent = john_doe_agent.id # agent p4.named_insured = ryan_bucket.id # named insured # save Policy Four to database db.session.add(p4) db.session.commit() # Use PolicyAccounting to create invoice(s) for Policy Four PolicyAccounting(p4.id) print "Policy Four Created and invoices are generated for it"
def setUp(self): self.policy = Policy("Test Policy", date(2015, 1, 1), 1200) self.policy.named_insured = self.test_insured.id self.policy.agent = self.test_agent.id self.policy.billing_schedule = "Quarterly" db.session.add(self.policy) db.session.commit() self.pa = PolicyAccounting(self.policy.id) self.payments = []
def setUpClass(cls): cls.test_agent = Contact('Test Agent', 'Agent') cls.test_insured = Contact('Test Insured', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy('Test Policy', date(2015, 1, 1), 1200) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id db.session.add(cls.policy) db.session.commit()
def setUpClass(cls): cls.test_agent = Contact("Test Agent", "Agent") cls.test_insured = Contact("Test Insured", "Named Insured") db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy("Test Policy", date(2015, 1, 1), 1200) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id db.session.add(cls.policy) db.session.commit()
def __init__(self, config): self.config = config # for universe-starter-agent self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) # build some graph nodes self.inputs_s = tf.placeholder( tf.float32, [None] + [config.n_actions, config.rollout_length] + config.state_dims) self.inputs_r = tf.placeholder( tf.float32, [None] + [config.n_actions, config.rollout_length]) self.X = tf.placeholder(tf.float32, [None] + config.state_dims) # instantiate the model free policy self.mf_policy = justCNN with tf.variable_scope('mf_policy', reuse=config.reuse): mf_feats = self.mf_policy(self.X) mf_feats = layers.flatten(mf_feats) # instantiate the rollout policy self.rollout_policy = Policy(natureCNN, config) with tf.variable_scope('rollout_policy', reuse=config.reuse): self.rp_logits, rp_pi, rp_actions, rp_vf = self.rollout_policy.forward( self.X) # instantiate the imagination core # we can only instantiate this once we have defined our rollout policy self.imagination_core = ImaginationCore(config, self.rollout_policy) # instantiate the encoder self.encoder = Encoder(justCNN, config) with tf.variable_scope('encoder', reuse=config.reuse): encodings = self.encoder.forward(self.inputs_s, self.inputs_r) aggregate = tf.reshape( encodings, shape=[-1, config.n_actions * config.hidden_dim]) # we can experiment with this next line of code # you can either concat, add, or multiply i2a_inputs = tf.concat([aggregate, mf_feats], -1) # instantiate the I2A policy self.i2a_policy = Policy(linear, config) with tf.variable_scope('i2a_policy', reuse=config.reuse): self.logits, self.pi, self.actions, self.vf = self.i2a_policy.forward( i2a_inputs)
def __init__(self): super(VAE, self).__init__() self.policy = Policy(state_size=8, action_size=0, latent_size=2, output_size=4, hidden_size=64, output_activation='sigmoid') self.posterior = Posterior(state_size=8, action_size=0, latent_size=2, hidden_size=64)
def setUpClass(cls): # print "Setting up TestReturnAccountBalance class..." cls.test_agent = Contact('Test Agent', 'Agent') cls.test_insured = Contact('Test Insured', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy('Test Policy', date(2015, 1, 1), 1200) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id db.session.add(cls.policy) db.session.commit()
def create_policy(): data = request.json try: curr_date = datetime.strptime(data['date'], "%Y-%m-%d") policy_name = data['policy_name'] existing_insured = data['existingInsured'] existing_agent = data['existingAgent'] insured = data['insured'] agent = data['agent'] billing_schedule = data['schedule'] premium = int(data['premium']) except: abort(400) if not existing_insured: new_insured = Contact(insured, 'Named Insured') db.session.add(new_insured) db.session.commit() insured = new_insured.id else: insured = insured['id'] if not existing_agent: new_agent = Contact(agent, 'Agent') db.session.add(new_agent) db.session.commit() agent = new_agent.id else: agent = agent['id'] new_policy = Policy(policy_name, curr_date, premium) new_policy.billing_schedule = billing_schedule new_policy.named_insured = insured new_policy.agent = agent db.session.add(new_policy) db.session.commit() return "All good"
def setUpClass(cls): print "Setting up TestBillingSchedules class..." cls.test_agent = Contact('Test Agent', 'Agent') cls.test_insured = Contact('Test Insured', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy('Test Policy', date(2015, 1, 1), 1200) db.session.add(cls.policy) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id db.session.commit() print "Class set up!\n"
def setUpClass(cls): cls.test_agent = Contact('Test Agent', 'Agent') cls.test_insured = Contact('Test Named Insured', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy('Test Policy', date(2018, 10, 1), 1870) db.session.add(cls.policy) db.session.commit() cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id cls.policy.billing_schedule = 'Monthly' db.session.commit()
def update(policy_id, **kw): # 参数合法性检查. 如果不合法,直接报错. is_valid_kw(Policy, is_update=True, **kw) # 获取想要的记录 p = Policy.get('id', policy_id) # 如果找不到这条记录,报错 if not p: raise AttributeError("Could not find in policy with [id]=[%s]. So could not update it, either." % policy_id) for key_name in kw: if key_name in p: p[key_name] = kw[key_name] now = utc_8_now() p['update_time'] = now p.update()
def setUpClass(cls): # print "Setting up TestChangeBillingSchedule class..." cls.test_agent = Contact('Test Joe Lee', 'Agent') cls.test_insured = Contact('Test Anna White', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy('Test Policy Two', date(2015, 2, 1), 1600) db.session.add(cls.policy) db.session.commit() cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id cls.policy.billing_schedule = 'Quarterly' db.session.commit()
def setUp(self): self.policy = Policy("Test Policy", date(2015, 1, 1), 1200) self.policy.named_insured = self.test_insured.id self.policy.agent = self.test_agent.id self.policy.billing_schedule = "Quarterly" db.session.add(self.policy) db.session.commit() self.pa = PolicyAccounting(self.policy.id) invoice = self.policy.invoices[0] self.payment = self.pa.make_payment( contact_id=self.policy.named_insured, date_cursor=invoice.bill_date, amount=invoice.amount_due, )
def update(policy_id, **kw): # 参数合法性检查. 如果不合法,直接报错. is_valid_kw(Policy, is_update=True, **kw) # 获取想要的记录 p = Policy.get('id', policy_id) # 如果找不到这条记录,报错 if not p: raise AttributeError( "Could not find in policy with [id]=[%s]. So could not update it, either." % policy_id) for key_name in kw: if key_name in p: p[key_name] = kw[key_name] now = utc_8_now() p['update_time'] = now p.update()
def setUpClass(cls): cls.test_agent = Contact("Test Agent", "Agent") cls.test_insured = Contact("Test Insured", "Named Insured") db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy("Test Policy", date(2015, 1, 1), 1200) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id cls.policy.billing_schedule = "Quarterly" db.session.add(cls.policy) db.session.commit() cls.pa = PolicyAccounting(cls.policy.id)
def test_render_show_policy(self): url = reverse('privacy.views.show_policy') response = self.client.get(url) eq_(response.status_code, 200) self.assert_all_embeds(response.content) ok_('Policy not found' in response.content) user = User.objects.create(username='******') policy = Policy.objects.create(text="Hi Mozilla", active=True) LogEntry.objects.create(content_type=Policy.contenttype(), user=user, object_id=policy.id, action_flag=CHANGE, change_message='activate') response = self.client.get(url) eq_(response.status_code, 200) self.assert_all_embeds(response.content)
def create_network(self, data): goals = data['goals'] policies = data['policies'] id_mapping = {} links = [] network = Network() for policy in policies: p = Policy(id=policy['id']) update_node_from_dict(p, policy) id_mapping[policy['id']] = p network.policies[p.id] = p for conn in policy['connections']: i = conn['id'] a = conn['from_id'] b = conn['to_id'] w = conn['weight'] links.append((i, a, b, w)) for goal in goals: g = Goal(id=goal['id']) update_node_from_dict(g, goal) id_mapping[goal['id']] = g network.goals[g.id] = g for conn in goal['connections']: i = conn['id'] a = conn['from_id'] b = conn['to_id'] w = conn['weight'] links.append((i, a, b, w)) for i, a, b, w in links: a = id_mapping[a] b = id_mapping[b] l = Edge(id=i) l.init(a, b, w) network.edges[l.id] = l network.rank() self.network = network
def setUpClass(cls): cls.test_agent = Contact('Test Agent', 'Agent') cls.test_insured = Contact('Test Insured', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy('Test Policy', date(2015, 2, 1), 1200) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id cls.policy.billing_schedule = "Quarterly" db.session.add(cls.policy) db.session.commit() cls.payments = Payment(cls.policy.id, cls.test_insured.id, 300, date(2015, 2, 1)) db.session.add(cls.payments) db.session.commit()
def setUpClass(cls): try: cls.test_agent = Contact('Test Agent', 'Agent') cls.test_insured = Contact('Test Insured', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.flush() cls.policy = Policy('Policy test', date(2015, 1, 5), 400) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id db.session.add(cls.policy) db.session.flush() db.session.commit() except Exception as error: print(error) db.session.rollback()
def test_render_show_policy(self): url = reverse('privacy.views.show_policy') response = self.client.get(url) eq_(response.status_code, 200) self.assert_all_embeds(response.content) ok_('Policy not found' in response.content) user = User.objects.create( username='******' ) policy = Policy.objects.create( text="Hi Mozilla", active=True ) LogEntry.objects.create( content_type=Policy.contenttype(), user=user, object_id=policy.id, action_flag=CHANGE, change_message='activate' ) response = self.client.get(url) eq_(response.status_code, 200) self.assert_all_embeds(response.content)
def insert_data(): #Contacts contacts = [] #Contact list to insert into db john_doe_agent = Contact('John Doe', 'Agent') contacts.append(john_doe_agent) john_doe_insured = Contact('John Doe', 'Named Insured') contacts.append(john_doe_insured) bob_smith = Contact('Bob Smith', 'Agent') contacts.append(bob_smith) anna_white = Contact('Anna White', 'Named Insured') contacts.append(anna_white) joe_lee = Contact('Joe Lee', 'Agent') contacts.append(joe_lee) ryan_bucket = Contact('Ryan Bucket', 'Named Insured') contacts.append(ryan_bucket) for contact in contacts: db.session.add(contact) db.session.commit() policies = [] #Policy list to insert into db p1 = Policy('Policy One', date(2015, 1, 1), 365) p1.billing_schedule = 'Annual' p1.agent = bob_smith.id policies.append(p1) p2 = Policy('Policy Three', date(2015, 2, 1), 1200) p2.billing_schedule = 'Monthly' p2.named_insured = anna_white.id p2.agent = joe_lee.id policies.append(p2) p3 = Policy('Policy Three', date(2015, 1, 1), 1200) p3.billing_schedule = 'Monthly' p3.named_insured = ryan_bucket.id p3.agent = john_doe_agent.id policies.append(p3) p4 = Policy('Policy Four', date(2015, 2, 1), 500) #creating policy 4 with given information p4.billing_schedule = 'Two-Pay' p4.name_insured = ryan_bucket.id p4.agent = john_doe_agent.id policies.append(p4) p5 = Policy('Policy Four', date(2015, 2, 1), 500) #Created another policy 4 for Bob Smith's client p5.bill_schedule = 'Two-Pay' p5.name_insured = john_doe_insured.id p5.agent = bob_smith.id policies.append(p5) for policy in policies: db.session.add(policy) db.session.commit() for policy in policies: PolicyAccounting(policy.id) payment_for_p2 = Payment(p2.id, anna_white.id, 400, date(2015, 2, 1)) db.session.add(payment_for_p2) db.session.delete(invoice[0]) #Delete old Quarterly invoice db.session.commit()
def find_by(cols, where, group_order_limit, *args): where = "%s %s" % (where, group_order_limit) return Policy.find_by(cols, where, *args)
def insert_data(): #Contacts contacts = [] john_doe_agent = Contact('John Doe', 'Agent') contacts.append(john_doe_agent) john_doe_insured = Contact('John Doe', 'Named Insured') contacts.append(john_doe_insured) bob_smith = Contact('Bob Smith', 'Agent') contacts.append(bob_smith) anna_white = Contact('Anna White', 'Named Insured') contacts.append(anna_white) joe_lee = Contact('Joe Lee', 'Agent') contacts.append(joe_lee) ryan_bucket = Contact('Ryan Bucket', 'Named Insured') contacts.append(ryan_bucket) for contact in contacts: db.session.add(contact) db.session.commit() policies = [] p1 = Policy('Policy One', date(2015, 1, 1), 365) p1.billing_schedule = 'Annual' p1.named_insured = john_doe_insured.id # newly added for problem 6 p1.agent = bob_smith.id policies.append(p1) p2 = Policy('Policy Two', date(2015, 2, 1), 1600) p2.billing_schedule = 'Quarterly' p2.named_insured = anna_white.id p2.agent = joe_lee.id policies.append(p2) p3 = Policy('Policy Three', date(2015, 1, 1), 1200) p3.billing_schedule = 'Monthly' p3.named_insured = ryan_bucket.id p3.agent = john_doe_agent.id policies.append(p3) # newly added for problem 5 p4 = Policy( 'Policy Four', date( 2015, 2, 1 ), 500 ) p4.billing_schedule = 'Two-Pay' p4.named_insured = ryan_bucket.id p4.agent = john_doe_agent.id policies.append(p4) for policy in policies: db.session.add(policy) db.session.commit() for policy in policies: PolicyAccounting(policy.id) payment_for_p2 = Payment(p2.id, anna_white.id, 400, date(2015, 2, 1)) db.session.add(payment_for_p2) db.session.commit()
def save(request): """ Insert/update a policy """ policyID = request.POST['policyId'] if not (policyID == 'None' or re.match(r'^\d+$', policyID)): raise ValueError type = request.POST['type'] if not re.match(r'^\d+$', type) and int(type) in range(len(Policy.types)): raise ValueError type = int(type) fileID = request.POST.get('file','') file = None if not fileID == '': if not re.match(r'^\d+$', fileID): raise ValueError try: file = File.objects.get(pk=fileID) except (File.DoesNotExist): messages.warning(request, _('No such file')) dirID = request.POST.get('dir','') dir = None if not dirID == '': if not re.match(r'^\d+$', dirID): raise ValueError try: dir = Directory.objects.get(pk=dirID) except (Directory.DoesNotExist): messages.warning(request, _('No such directory')) argument = '' ranges = request.POST.get('range', None) flip = int(request.POST.get('flip', 0)) if ranges != '': if not check_range(ranges): raise ValueError else: if flip: ranges = invert_range(ranges) argument = ranges fail = request.POST['fail'] if not re.match(r'^\d+$', fail) and int(fail) in range(len(Policy.action)): raise ValueError noresult = request.POST['noresult'] if not (re.match(r'^\d+$', noresult) and int(noresult) in range(len(Policy.action))): raise ValueError name = request.POST['name'] if not re.match(r'^[\S ]+$', name): raise ValueError if policyID == 'None': policy = Policy(name=name, type=type, fail=fail, noresult=noresult, file=file, dir=dir, argument=argument) else: policy = get_object_or_404(Policy, pk=policyID) policy.name = name policy.type = type policy.file = file policy.dir = dir policy.fail = fail policy.noresult = noresult policy.argument = argument type_name = Policy.types[policy.type] arg_func = Policy.argument_funcs[type_name] policy.argument = arg_func(policy) policy.save() messages.success(request, _('Policy saved!')) return redirect('/policies/%d' % policy.id)