[mb-commits] branch, nes-aliases, created. Allow creating, viewing, and deleting aliases.
MusicBrainz Git Server <[email protected]> Fri, 18 Jan 2013 15:36:00 +0000
| Newsgroups | gmane.comp.audio.musicbrainz.cvs |
|---|---|
| Message-ID | <E1TwDz6-00046S-MD@wiley> |
The branch, nes-aliases has been created
at 0dedc091f6c5adb543968692b5d342a62904266a (commit)
- Log -----------------------------------------------------------------
commit 0dedc091f6c5adb543968692b5d342a62904266a
Author: Oliver Charles <[email protected]>
Date: Fri Jan 18 12:54:37 2013 +0000
Allow creating, viewing, and deleting aliases.
diff --git a/lib/MusicBrainz/Server/Controller/Role/Alias.pm b/lib/MusicBrainz/Server/Controller/Role/Alias.pm
index 2df415f..25258e9 100644
--- a/lib/MusicBrainz/Server/Controller/Role/Alias.pm
+++ b/lib/MusicBrainz/Server/Controller/Role/Alias.pm
@@ -3,120 +3,130 @@ use Moose::Role -traits => 'MooseX::MethodAttributes::Role::Meta::Role';
requires 'load';
-use MusicBrainz::Server::Constants qw(
- $EDIT_ARTIST_ADD_ALIAS $EDIT_ARTIST_DELETE_ALIAS $EDIT_ARTIST_EDIT_ALIAS
- $EDIT_LABEL_ADD_ALIAS $EDIT_LABEL_DELETE_ALIAS $EDIT_LABEL_EDIT_ALIAS
- $EDIT_WORK_ADD_ALIAS $EDIT_WORK_DELETE_ALIAS $EDIT_WORK_EDIT_ALIAS
-);
-
-my %model_to_edit_type = (
- add => {
- Artist => $EDIT_ARTIST_ADD_ALIAS,
- Label => $EDIT_LABEL_ADD_ALIAS,
- Work => $EDIT_WORK_ADD_ALIAS,
- },
- delete => {
- Artist => $EDIT_ARTIST_DELETE_ALIAS,
- Label => $EDIT_LABEL_DELETE_ALIAS,
- Work => $EDIT_WORK_DELETE_ALIAS,
- },
- edit => {
- Artist => $EDIT_ARTIST_EDIT_ALIAS,
- Label => $EDIT_LABEL_EDIT_ALIAS,
- Work => $EDIT_WORK_EDIT_ALIAS,
- }
-);
+use MusicBrainz::Server::Entity::Alias;
+use MusicBrainz::Server::NES::Controller::Utils qw( create_update );
my %model_to_search_hint_type_id = (
Artist => 3,
Label => 2,
- Work => 2
+ 'NES::Work' => 2
);
+sub alias_type_model {
+ my ($c, $parent) = @_;
+ my %type_model = (
+ 'NES::Work' => 'Work'
+ );
+ return $c->model($type_model{$parent})->alias_type;
+}
+
sub aliases : Chained('load') PathPart('aliases')
{
my ($self, $c) = @_;
- my $entity = $c->stash->{$self->{entity_name}};
- my $m = $c->model($self->{model});
- my $aliases = $m->alias->find_by_entity_id($entity->id);
- $m->alias_type->load(@$aliases);
+ my $entity = $c->stash->{entity};
+ my $m = $self->{model};
+
+ my $aliases = $c->model($m)->get_aliases($entity);
+ alias_type_model($c, $m)->load(@$aliases);
+
$c->stash(
aliases => $aliases,
);
}
-sub alias : Chained('load') PathPart('alias') CaptureArgs(1)
+sub alias : Chained('load') PathPart('alias') CaptureArgs(0)
{
- my ($self, $c, $alias_id) = @_;
- my $alias = $c->model($self->{model})->alias->get_by_id($alias_id)
- or $c->detach('/error_404');
- $c->stash( alias => $alias );
+ my ($self, $c) = @_;
+
+ my $qp = $c->req->query_params;
+
+ my $all_aliases = $c->model( $self->{model} )->get_aliases($c->stash->{entity});
+ my ($alias) = grep {
+ $_->name eq $qp->{name}
+ } @$all_aliases or $c->detach('/error_404');
+
+ $c->stash(
+ alias => $alias,
+ all_aliases => $all_aliases
+ );
}
sub add_alias : Chained('load') PathPart('add-alias') RequireAuth Edit
{
my ($self, $c) = @_;
- my $type = $self->{entity_name};
- my $entity = $c->stash->{ $type };
- my $alias_model = $c->model( $self->{model} )->alias;
- $self->edit_action($c,
- form => 'Alias',
- form_args => {
- parent_id => $entity->id,
- alias_model => $alias_model,
- search_hint_type_id => $model_to_search_hint_type_id{ $self->{model} }
- },
- type => $model_to_edit_type{add}->{ $self->{model} },
- edit_args => {
- entity => $entity
- },
- item => {
- name => $entity->name,
- id => $entity->id
- },
- on_creation => sub { $self->_redir_to_aliases($c) }
+ my $entity = $c->stash->{entity};
+
+ create_update(
+ $self, $c,
+ form => $self->_build_alias_form($c),
+ build_tree => sub {
+ my ($values, $revision) = @_;
+
+ my $aliases = $c->model($self->{model})->get_aliases($revision);
+ $self->{tree_entity}->new(
+ aliases => [
+ @$aliases,
+ MusicBrainz::Server::Entity::Alias->new($values)
+ ]
+ );
+ }
);
}
sub delete_alias : Chained('alias') PathPart('delete') RequireAuth Edit
{
my ($self, $c) = @_;
+
+ my $entity = $c->stash->{entity};
my $alias = $c->stash->{alias};
- $self->edit_action($c,
- form => 'Confirm',
- type => $model_to_edit_type{delete}->{ $self->{model} },
- edit_args => {
- alias => $alias,
- entity => $c->stash->{ $self->{entity_name} }
- },
- on_creation => sub { $self->_redir_to_aliases($c) }
+
+ create_update(
+ $self, $c,
+ form => $c->form(
+ form => 'Confirm',
+ init_object => { revision_id => $entity->revision_id }
+ ),
+ build_tree => sub {
+ my ($values, $revision) = @_;
+
+ return $self->{tree_entity}->new(
+ aliases => [ _aliases_without($c->stash->{all_aliases}, $alias) ]
+ );
+ }
);
+
+ # on_creation => sub { $self->_redir_to_aliases($c) }
}
sub edit_alias : Chained('alias') PathPart('edit') RequireAuth Edit
{
my ($self, $c) = @_;
+
my $alias = $c->stash->{alias};
- my $type = $self->{entity_name};
- my $entity = $c->stash->{ $type };
- my $alias_model = $c->model( $self->{model} )->alias;
- $self->edit_action($c,
- form => 'Alias',
- form_args => {
- parent_id => $entity->id,
- alias_model => $alias_model,
- id => $alias->id,
- search_hint_type_id => $model_to_search_hint_type_id{ $self->{model} }
- },
- item => $alias,
- type => $model_to_edit_type{edit}->{ $self->{model} },
- edit_args => {
- alias => $alias,
- entity => $c->stash->{ $self->{entity_name} }
- },
- on_creation => sub { $self->_redir_to_aliases($c) }
+ my $entity = $c->stash->{entity};
+
+ create_update(
+ $self, $c,
+ form => $self->_build_alias_form($c, $alias),
+ build_tree => sub {
+ my ($values, $revision) = @_;
+
+ return $self->{tree_entity}->new(
+ aliases => [
+ _aliases_without($c->stash->{all_aliases}, $alias),
+ MusicBrainz::Server::Entity::Alias->new($values),
+ ]
+ );
+ }
);
+
+ # on_creation => sub { $self->_redir_to_aliases($c) }
+}
+
+sub _aliases_without {
+ my ($aliases, $alias) = @_;
+ return grep { $_ != $alias } @$aliases;
}
sub _redir_to_aliases
@@ -127,5 +137,20 @@ sub _redir_to_aliases
$c->response->redirect($c->uri_for($action, [ $entity->gid ]));
}
+sub _build_alias_form {
+ my ($self, $c, $alias) = @_;
+ my $model_name = $self->{model};
+
+ $c->form(
+ form => 'Alias',
+ search_hint_type_id => $model_to_search_hint_type_id{ $model_name },
+ type_model => alias_type_model($c, $model_name),
+ init_object => {
+ %{ $alias // {} },
+ revision_id => $c->stash->{entity}->revision_id
+ }
+ )
+}
+
no Moose::Role;
1;
diff --git a/lib/MusicBrainz/Server/Controller/Work.pm b/lib/MusicBrainz/Server/Controller/Work.pm
index e50d49e..9a2a193 100644
--- a/lib/MusicBrainz/Server/Controller/Work.pm
+++ b/lib/MusicBrainz/Server/Controller/Work.pm
@@ -20,11 +20,11 @@ with 'MusicBrainz::Server::Controller::Role::Load' => {
entity_name => 'work',
};
+with 'MusicBrainz::Server::Controller::Role::Alias';
with 'MusicBrainz::Server::Controller::Role::Details';
with 'MusicBrainz::Server::Controller::Role::EditListing';
with 'MusicBrainz::Server::Controller::Role::Rating';
with 'MusicBrainz::Server::Controller::Role::Tag';
-# with 'MusicBrainz::Server::Controller::Role::Alias';
# with 'MusicBrainz::Server::Controller::Role::Annotation';
# with 'MusicBrainz::Server::Controller::Role::Relationship';
# with 'MusicBrainz::Server::Controller::Role::Cleanup';
@@ -62,7 +62,7 @@ sub show : PathPart('') Chained('load')
# NES - originally:
# for my $action (qw( relationships aliases tags details )) {
-for my $action (qw( tags details )) {
+for my $action (qw( aliases tags details )) {
after $action => sub {
my ($self, $c) = @_;
my $work = $c->stash->{work};
diff --git a/lib/MusicBrainz/Server/Data/NES/Work.pm b/lib/MusicBrainz/Server/Data/NES/Work.pm
index 3165b1b..4d3fc50 100644
--- a/lib/MusicBrainz/Server/Data/NES/Work.pm
+++ b/lib/MusicBrainz/Server/Data/NES/Work.pm
@@ -10,6 +10,8 @@ with 'MusicBrainz::Server::Data::Role::NES';
sub create {
my ($self, $edit, $editor, $tree) = @_;
+ $tree->aliases([]) unless $tree->aliases_set;
+
my $response = $self->request('/work/create', {
edit => $edit->id,
editor => $editor->id,
@@ -25,7 +27,7 @@ sub update {
die 'Need a base revision' unless $base_revision;
my $final_tree = do {
- if( $tree->work_set && $tree->iswcs_set ) {
+ if( $tree->work_set && $tree->aliases_set && $tree->iswcs_set ) {
$tree
}
else {
@@ -34,6 +36,9 @@ sub update {
$original_tree->work($tree->work)
if ($tree->work_set);
+ $original_tree->aliases($tree->aliases)
+ if ($tree->aliases_set);
+
$original_tree->iswcs($tree->iswcs)
if ($tree->iswcs_set);
@@ -57,6 +62,7 @@ sub view_tree {
return MusicBrainz::Server::Entity::Tree::Work->new(
work => $revision,
iswcs => $self->get_iswcs($revision),
+ aliases => $self->get_aliases($revision)
);
}
@@ -75,6 +81,18 @@ sub _work_tree {
},
iswcs => [
map +{ iswc => $_ }, @{ $tree->iswcs }
+ ],
+ aliases => [
+ map +{
+ name => $_->name,
+ 'sort-name' => $_->sort_name,
+ 'begin-date' => partial_date_to_hash($_->begin_date),
+ 'end-date' => partial_date_to_hash($_->end_date),
+ ended => $_->ended,
+ 'primary-for-locale' => boolean($_->primary_for_locale),
+ type => $_->type_id,
+ locale => $_->locale
+ }, @{ $tree->aliases }
]
);
}
@@ -112,6 +130,27 @@ sub tags {
$self->c->model('Work')->tags;
}
+sub get_aliases {
+ my ($self, $work) = @_;
+ my $response = $self->request('/work/view-aliases', {
+ revision => $work->revision_id
+ });
+ return [
+ map {
+ MusicBrainz::Server::Entity::Alias->new(
+ name => $_->{name},
+ sort_name => $_->{'sort-name'},
+ locale => $_->{locale},
+ type_id => $_->{type},
+ begin_date => MusicBrainz::Server::Entity::PartialDate->new($_->{begin_date}),
+ end_date => MusicBrainz::Server::Entity::PartialDate->new($_->{end_date}),
+ ended => $_->{ended},
+ primary_for_locale => $_->{'primary-for-locale'}
+ )
+ } @$response
+ ]
+}
+
sub get_iswcs {
my ($self, $revision) = @_;
warn "Unimplemented";
diff --git a/lib/MusicBrainz/Server/Entity/Alias.pm b/lib/MusicBrainz/Server/Entity/Alias.pm
index 36467df..1d29dec 100644
--- a/lib/MusicBrainz/Server/Entity/Alias.pm
+++ b/lib/MusicBrainz/Server/Entity/Alias.pm
@@ -18,12 +18,12 @@ has 'sort_name' => (
has 'locale' => (
is => 'rw',
- isa => 'Str',
+ isa => 'Maybe[Str]',
);
has 'type_id' => (
is => 'rw',
- isa => 'Int',
+ isa => 'Maybe[Int]',
);
has 'type' => (
diff --git a/lib/MusicBrainz/Server/Entity/Tree/Work.pm b/lib/MusicBrainz/Server/Entity/Tree/Work.pm
index 2e637b4..acf63f0 100644
--- a/lib/MusicBrainz/Server/Entity/Tree/Work.pm
+++ b/lib/MusicBrainz/Server/Entity/Tree/Work.pm
@@ -6,6 +6,11 @@ has work => (
predicate => 'work_set',
);
+has aliases => (
+ is => 'rw',
+ predicate => 'aliases_set',
+);
+
has iswcs => (
is => 'rw',
predicate => 'iswcs_set',
diff --git a/lib/MusicBrainz/Server/Form/Alias.pm b/lib/MusicBrainz/Server/Form/Alias.pm
index c918b56..16ffbfc 100644
--- a/lib/MusicBrainz/Server/Form/Alias.pm
+++ b/lib/MusicBrainz/Server/Form/Alias.pm
@@ -43,14 +43,8 @@ has 'id' => (
is => 'rw',
);
-has 'parent_id' => (
- isa => 'Int',
- is => 'ro',
- required => 1,
-);
-
-has 'alias_model' => (
- isa => 'MusicBrainz::Server::Data::Alias',
+has 'type_model' => (
+ isa => 'MusicBrainz::Server::Data::AliasType',
is => 'ro',
required => 1
);
@@ -61,6 +55,10 @@ has search_hint_type_id => (
required => 1
);
+has_field revision_id => (
+ type => 'Integer',
+);
+
sub edit_field_names {
qw( name locale sort_name period.begin_date period.end_date
type_id primary_for_locale )
@@ -94,7 +92,7 @@ sub options_locale {
sub options_type_id {
my $self = shift;
- $self->_select_all($self->alias_model->parent->alias_type);
+ $self->_select_all($self->type_model);
}
sub validate_primary_for_locale {
@@ -115,13 +113,13 @@ after validate => sub {
$sort_name_field->validate_field;
}
- if ($self->alias_model->exists({ name => $self->field('name')->value,
- locale => $self->field('locale')->value,
- type_id => $self->field('type_id')->value,
- not_id => $self->init_object ? $self->init_object->{id} : undef,
- })) {
- $self->field('name')->add_error('This alias already exists');
- }
+ # if ($self->alias_model->exists({ name => $self->field('name')->value,
+ # locale => $self->field('locale')->value,
+ # type_id => $self->field('type_id')->value,
+ # not_id => $self->init_object ? $self->init_object->{id} : undef,
+ # })) {
+ # $self->field('name')->add_error('This alias already exists');
+ # }
};
1;
diff --git a/lib/MusicBrainz/Server/Form/Confirm.pm b/lib/MusicBrainz/Server/Form/Confirm.pm
index 19bd985..e7e6b90 100644
--- a/lib/MusicBrainz/Server/Form/Confirm.pm
+++ b/lib/MusicBrainz/Server/Form/Confirm.pm
@@ -5,6 +5,11 @@ extends 'MusicBrainz::Server::Form';
with 'MusicBrainz::Server::Form::Role::Edit';
has '+name' => ( default => 'confirm' );
+has_field 'revision_id' => (
+ type => 'Integer',
+ required => 1
+);
+
sub edit_field_names { () }
1;
diff --git a/lib/MusicBrainz/Server/NES/Controller/Utils.pm b/lib/MusicBrainz/Server/NES/Controller/Utils.pm
index ff5ff27..2baa211 100644
--- a/lib/MusicBrainz/Server/NES/Controller/Utils.pm
+++ b/lib/MusicBrainz/Server/NES/Controller/Utils.pm
@@ -2,6 +2,7 @@ package MusicBrainz::Server::NES::Controller::Utils;
use strict;
use warnings;
+use Scalar::Util qw( blessed );
use Sub::Exporter -setup => {
exports => [qw( create_edit create_update )]
};
@@ -10,20 +11,19 @@ sub create_edit {
my ($controller, $c, %opts) = @_;
my $form = do {
- if (my $build_form = $opts{build_form}) {
- $build_form->()
+ my $f = $opts{form};
+ if (blessed($f)) {
+ $f;
}
else {
- my $form = do {
- my %args = (
- ctx => $c,
- );
+ my %args = (
+ ctx => $c,
+ );
- $args{init_object} = $opts{subject}
- if defined $opts{subject};
+ $args{init_object} = $opts{subject}
+ if defined $opts{subject};
- $c->form(form => $opts{form}, %args);
- }
+ $c->form(form => $f, %args);
}
};
@@ -43,7 +43,6 @@ sub create_edit {
);
}
-
# NES:
# my $privs = $c->user->privileges;
# if ($c->user->is_auto_editor &&
diff --git a/root/alias/delete.tt b/root/alias/delete.tt
index 1fd05ae..61bfe11 100644
--- a/root/alias/delete.tt
+++ b/root/alias/delete.tt
@@ -12,5 +12,7 @@
<form action="[% c.req.uri %]" method="post">
[% INCLUDE 'forms/edit-note.tt' %]
+ [% USE r = FormRenderer(form) %]
+ [% r.hidden(form.field('revision_id')) %]
[% enter_edit() %]
</form>
diff --git a/root/alias/edit_form.tt b/root/alias/edit_form.tt
index b95c158..d27091d 100644
--- a/root/alias/edit_form.tt
+++ b/root/alias/edit_form.tt
@@ -20,6 +20,7 @@
[% form_row_checkbox(r, 'primary_for_locale', l('This is the primary alias for this locale')) %]
</span>
[% form_row_select(r, 'type_id', l('Type:')) %]
+ [% r.hidden(form.field('revision_id')) %]
</fieldset>
<fieldset>
diff --git a/root/components/aliases.tt b/root/components/aliases.tt
index 7f5c6e6..f5faf12 100644
--- a/root/components/aliases.tt
+++ b/root/components/aliases.tt
@@ -45,10 +45,11 @@
</td>
[% IF c.user_exists %]
<td>
- <a href="[% c.uri_for_action(edit_path, [ entity.gid, alias.id ]) %]">
+ [% key = { name => alias.name } %]
+ <a href="[% c.uri_for_action(edit_path, [ entity.gid ], key) %]">
[%- l('Edit') -%]
</a> |
- <a href="[% c.uri_for_action(delete_path, [ entity.gid, alias.id ]) %]">
+ <a href="[% c.uri_for_action(delete_path, [ entity.gid ], key) %]">
[%- l('Remove') -%]
</a>
</td>
-----------------------------------------------------------------------
hooks/post-receive
--
mb_server