Implement Create/Show/Update/Delete of Contracts

Use sippeering contracts to update peering groups.
agranig/1_0_subfix
Gerhard Jungwirth 12 years ago
parent 25e75e4420
commit bb0d585418

@ -43,6 +43,8 @@ my $builder = Local::Module::Build->new(
'Template' => 0,
'warnings' => 0,
'Text::CSV_XS' => 0,
'DBIx::Class::ResultSet::RecursiveUpdate' => 0.30,
#patch to set foreign key instead of relation, install from github until its available on cpan
},
test_requires => {
'Catalyst::Test' => 0,

@ -1,44 +1,24 @@
package NGCP::Panel::Controller::Contract;
use Moose;
use Sipwise::Base;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }
use NGCP::Panel::Form::Contract;
use NGCP::Panel::Utils;
=head1 NAME
NGCP::Panel::Controller::Contract - Catalyst Controller
=head1 DESCRIPTION
Catalyst Controller.
=head1 METHODS
=cut
sub list :Chained('/') :PathPart('contract') :CaptureArgs(0) {
sub contract_list :Chained('/') :PathPart('contract') :CaptureArgs(0) {
my ($self, $c) = @_;
my $contracts = [
{id => 1, contact => 1, billing_profile => 1, status => 'active'},
{id => 2, contact => 2, billing_profile => 2, status => 'pending'},
{id => 3, contact => 3, billing_profile => 3, status => 'active'},
{id => 4, contact => 4, billing_profile => 4, status => 'terminated'},
{id => 5, contact => 5, billing_profile => 5, status => 'locked'},
{id => 6, contact => 6, billing_profile => 6, status => 'active'},
];
$c->stash(contracts => $contracts);
$c->stash(ajax_uri => $c->uri_for_action("/contract/ajax"));
$c->stash(template => 'contract/list.tt');
NGCP::Panel::Utils::check_redirect_chain(c => $c);
}
sub root :Chained('list') :PathPart('') :Args(0) {
sub root :Chained('contract_list') :PathPart('') :Args(0) {
my ($self, $c) = @_;
}
sub create :Chained('list') :PathPart('create') :Args(0) {
sub create :Chained('contract_list') :PathPart('create') :Args(0) {
my ($self, $c) = @_;
my $form = NGCP::Panel::Form::Contract->new;
@ -46,18 +26,20 @@ sub create :Chained('list') :PathPart('create') :Args(0) {
posted => ($c->request->method eq 'POST'),
params => $c->request->params,
action => $c->uri_for('create'),
item => $c->model('billing')->resultset('billing_mappings')->new_result({}),
);
return if NGCP::Panel::Utils::check_form_buttons(
c => $c, form => $form, fields => [qw/contact.create/],
back_uri => $c->uri_for('create')
);
if($form->validated) {
$c->flash(messages => [{type => 'success', text => 'Contract successfully created!'}]);
if($c->stash->{close_target}) {
$c->response->redirect($c->stash->{close_target});
return;
}
$c->flash(messages => [{type => 'success', text => 'Contract successfully created!'}]);
$c->response->redirect($c->stash->{close_target});
$c->response->redirect($c->uri_for_action('/contract/root'));
return;
}
@ -65,25 +47,34 @@ sub create :Chained('list') :PathPart('create') :Args(0) {
$c->stash(form => $form);
}
sub search :Chained('list') :PathPart('search') Args(0) {
my ($self, $c) = @_;
$c->flash(messages => [{type => 'info', text => 'Contract search not implemented!'}]);
$c->response->redirect($c->uri_for());
}
sub base :Chained('/contract/list') :PathPart('') :CaptureArgs(1) {
sub base :Chained('contract_list') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $contract_id) = @_;
unless($contract_id && $contract_id =~ /^\d+$/) {
unless($contract_id && $contract_id->is_integer) {
$c->flash(messages => [{type => 'error', text => 'Invalid contract id detected!'}]);
$c->response->redirect($c->uri_for());
$c->detach;
return;
}
# TODO: fetch details of contract from model
my @rfilter = grep { $_->{id} == $contract_id } @{ $c->stash->{contracts} };
$c->stash(contract => shift @rfilter);
my $res = $c->model('billing')->resultset('contracts')
->search(undef, {
'join' => 'billing_mappings',
'+select' => 'billing_mappings.billing_profile_id',
'+as' => 'billing_profile',
})
->find($contract_id);
unless(defined($res)) {
$c->flash(messages => [{type => 'error', text => 'Contract does not exist!'}]);
$c->response->redirect($c->uri_for());
$c->detach;
return;
}
$c->stash(contract => {$res->get_inflated_columns});
$c->stash(contract_result => $res);
return;
}
sub edit :Chained('base') :PathPart('edit') :Args(0) {
@ -92,8 +83,9 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) {
my $posted = ($c->request->method eq 'POST');
my $form = NGCP::Panel::Form::Contract->new;
$form->process(
posted => 1,
params => $posted ? $c->request->params : $c->stash->{contract},
posted => $posted,
params => $c->req->params,
item => $c->stash->{contract_result}->billing_mappings->first,
action => $c->uri_for($c->stash->{contract}->{id}, 'edit'),
);
return if NGCP::Panel::Utils::check_form_buttons(
@ -113,23 +105,122 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) {
sub delete :Chained('base') :PathPart('delete') :Args(0) {
my ($self, $c) = @_;
# $c->model('Provisioning')->contract($c->stash->{contract}->{id})->delete;
$c->flash(messages => [{type => 'info', text => 'Contract delete not implemented!'}]);
try {
$c->stash->{contract_result}->delete;
$c->flash(messages => [{type => 'success', text => 'Contract successfully deleted!'}]);
} catch (DBIx::Class::Exception $e) {
$c->flash(messages => [{type => 'error', text => 'Delete failed.'}]);
$c->log->info("Delete failed: " . $e);
};
$c->response->redirect($c->uri_for());
}
sub ajax :Chained('list') :PathPart('ajax') :Args(0) {
sub ajax :Chained('contract_list') :PathPart('ajax') :Args(0) {
my ($self, $c) = @_;
my $contracts = $c->stash->{contracts};
my $rs = $c->model('billing')->resultset('contracts')
->search(undef, {
'join' => 'billing_mappings',
'+select' => 'billing_mappings.billing_profile_id',
'+as' => 'billing_profile',
});
$c->forward( "/ajax_process", [$contracts,
["id","contact","billing_profile","status"],
[1,2,3]]);
$c->forward( "/ajax_process_resultset", [$rs,
["id","contact_id","billing_profile","status"],
[]]);
$c->detach( $c->view("JSON") );
}
sub peering_list :Chained('contract_list') :PathPart('peering') :CaptureArgs(0) {
my ($self, $c) = @_;
$c->stash(ajax_uri => $c->uri_for_action("/contract/peering_ajax"));
}
sub peering_root :Chained('peering_list') :PathPart('') :Args(0) {
}
sub peering_ajax :Chained('peering_list') :PathPart('ajax') :Args(0) {
my ($self, $c) = @_;
my $rs = $c->model('billing')->resultset('contracts')
->search({
'product.class' => 'sippeering',
}, {
'join' => {'billing_mappings' => 'product'},
'+select' => 'billing_mappings.billing_profile_id',
'+as' => 'billing_profile',
});
$c->forward( "/ajax_process_resultset", [$rs,
["id","contact_id","billing_profile","status"],
[]]);
$c->detach( $c->view("JSON") );
}
<<<<<<< HEAD
=======
__PACKAGE__->meta->make_immutable;
1;
=head1 NAME
NGCP::Panel::Controller::Contract - Catalyst Controller
=head1 DESCRIPTION
View and edit Contracts. Optionally filter them by only peering contracts.
=head1 METHODS
=head2 contract_list
Basis for contracts.
=head2 root
Display contracts through F<contract/list.tt> template.
=head2 create
Show modal dialog to create a new contract.
=head2 base
Capture id of existing contract. Used for L</edit> and L</delete>. Stash "contract" and "contract_result".
=head2 edit
Show modal dialog to edit a contract.
=head2 delete
Delete a contract.
=head2 ajax
Get contracts from the database and output them as JSON.
The output format is meant for parsing with datatables.
The selected rows should be billing.billing_mappings JOIN billing.contracts with only one billing_mapping per contract (the one that fits best with the time).
=head2 peering_list
Basis for peering_contracts.
=head2 peering_root
Display contracts through F<contract/list.tt> template. Use L</peering_ajax> as data source.
=head2 peering_ajax
Similar to L</ajax>. Only select contracts, where billing.product is of class "sippeering".
>>>>>>> Implement Create/Show/Update/Delete of Contracts
=head1 AUTHOR
Andreas Granig,,,
@ -141,8 +232,4 @@ it under the same terms as Perl itself.
=cut
__PACKAGE__->meta->make_immutable;
1;
# vim: set tabstop=4 expandtab:

@ -2,10 +2,23 @@ package NGCP::Panel::Field::BillingProfile;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field::Compound';
has_field 'billing_profile' => (
type => '+NGCP::Panel::Field::BillingProfileSelect',
has_field 'id' => (
type => '+NGCP::Panel::Field::DataTable',
label => 'Billing Profile',
do_label => 0,
do_wrapper => 0,
required => 1,
template => 'share/templates/helpers/datatables_field.tt',
ajax_src => '/billing/ajax',
table_titles => ['#', 'Profile'],
table_fields => ['id', 'name'],
);
has_field 'create' => (
type => 'Button',
label => 'or',
value => 'Create Contract',
element_class => [qw/btn btn-tertiary/],
);
1;

@ -1,21 +0,0 @@
package NGCP::Panel::Field::BillingProfileSelect;
use Moose;
extends 'HTML::FormHandler::Field::Select';
sub build_options {
my ($self) = @_;
return [
{ label => 'Select...', value => '' },
{ label => '1', value => 1 },
{ label => '2', value => 2 },
{ label => '3', value => 3 },
{ label => '4', value => 4 },
{ label => '5', value => 5 },
{ label => '6', value => 6 },
];
}
1;
# vim: set tabstop=4 expandtab:

@ -9,7 +9,7 @@ has_field 'id' => (
do_wrapper => 0,
required => 1,
template => 'share/templates/helpers/datatables_field.tt',
ajax_src => '/contract/ajax',
ajax_src => '/contract/peering/ajax',
table_titles => ['#', 'Contact #', 'Billing Profile #', 'Status'],
table_fields => ['id', 'contact.id', 'billing_profile.id', 'status'],
);

@ -1,7 +1,7 @@
package NGCP::Panel::Form::Contract;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
extends 'HTML::FormHandler::Model::DBIC';
use Moose::Util::TypeConstraints;
use HTML::FormHandler::Widget::Block::Bootstrap;
@ -11,10 +11,15 @@ sub build_render_list {[qw/submitid fields actions/]}
sub build_form_element_class { [qw/form-horizontal/] }
has_field 'submitid' => (
type => 'Hidden'
type => 'Hidden',
noupdate => 1,
);
has_field 'contact' => (
has_field 'contract' => (
type => 'Compound',
);
has_field 'contract.contact' => (
type => '+NGCP::Panel::Field::Contact',
label => 'Contact',
not_nullable => 1,
@ -26,8 +31,8 @@ has_field 'billing_profile' => (
);
has_field 'status' => (
type => '+NGCP::Panel::Field::ContractStatus',
has_field 'contract.status' => (
type => '+NGCP::Panel::Field::ContractStatusSelect',
not_nullable => 1,
);
@ -41,7 +46,7 @@ has_field 'save' => (
has_block 'fields' => (
tag => 'div',
class => [qw/modal-body/],
render_list => [qw/contact billing_profile status/],
render_list => [qw/contract.contact billing_profile contract.status/],
);
has_block 'actions' => (

@ -10,7 +10,7 @@
helper.create_flag = create_flag;
helper.edit_flag = edit_flag;
helper.form_object = form;
helper.ajax_uri = c.uri_for( c.controller.action_for('ajax') );
helper.ajax_uri = ajax_uri;
helper.base_uri = c.uri_for( c.controller.action_for("") );
PROCESS 'helpers/datatables.tt';

Loading…
Cancel
Save