Skip to content

Commit

Permalink
Merge branch 'master' into beta
Browse files Browse the repository at this point in the history
* master:
  MBS-12311: Allow adding annotations to genres (#2492)
  Add basic genre create/edit tests
  Remove useless Area::Create _insert_hash
  MBS-10165: Use edit system for genre editing
  MBS-10165: Use edit system for genre adding
  MBS-10165: Use edit system for genre deletion
  MBS-10165: Basic preparations for genre edits
  Support genres in formatEntityTypeName
  • Loading branch information
reosarevok committed Jun 14, 2022
2 parents 56f21ed + df414ca commit b64c968
Show file tree
Hide file tree
Showing 38 changed files with 766 additions and 94 deletions.
5 changes: 4 additions & 1 deletion entities.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,14 @@
"model": "Gender"
},
"genre": {
"annotations": {
"edit_type": 164
},
"cache": {
"id": 333
},
"disambiguation": true,
"edit_table": false,
"edit_table": true,
"mbid": {
"indexable": true,
"multiple": false
Expand Down
6 changes: 6 additions & 0 deletions lib/MusicBrainz/Server/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ Readonly our $EDIT_EVENT_ADD_ALIAS => 155;
Readonly our $EDIT_EVENT_DELETE_ALIAS => 156;
Readonly our $EDIT_EVENT_EDIT_ALIAS => 157;

Readonly our $EDIT_GENRE_CREATE => 160;
Readonly our $EDIT_GENRE_EDIT => 161;
Readonly our $EDIT_GENRE_DELETE => 162;
# 163 reserved for EDIT_GENRE_MERGE if ever implemented
Readonly our $EDIT_GENRE_ADD_ANNOTATION => 164;

Readonly our $EDIT_WIKIDOC_CHANGE => 120;

Readonly our $EDIT_URL_EDIT => 101;
Expand Down
92 changes: 27 additions & 65 deletions lib/MusicBrainz/Server/Controller/Genre.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ with 'MusicBrainz::Server::Controller::Role::Load' => {
entity_name => 'genre',
};
with 'MusicBrainz::Server::Controller::Role::LoadWithRowID';
with 'MusicBrainz::Server::Controller::Role::Annotation';
with 'MusicBrainz::Server::Controller::Role::Details';
with 'MusicBrainz::Server::Controller::Role::EditListing';

use MusicBrainz::Server::Constants qw(
$EDIT_GENRE_CREATE
$EDIT_GENRE_EDIT
$EDIT_GENRE_DELETE
);

sub base : Chained('/') PathPart('genre') CaptureArgs(0) { }

Expand Down Expand Up @@ -44,74 +52,28 @@ sub _redirect_to_genre {
$c->response->redirect($c->uri_for_action($self->action_for('show'), [ $gid ]));
}

sub create : Local RequireAuth(relationship_editor) Edit {
my ($self, $c) = @_;

my $form = $c->form( form => 'Genre' );

if ($c->form_posted_and_valid($form)) {
my %insert = $self->_form_to_hash($form);
my $genre = $c->model('MB')->with_transaction(sub {
$c->model('Genre')->insert(\%insert);
});

$self->_redirect_to_genre($c, $genre->{gid});
}

$c->stash(
component_path => 'genre/CreateGenre',
component_props => {form => $form->TO_JSON},
current_view => 'Node',
);
}

sub edit : Chained('load') RequireAuth(relationship_editor) {
my ($self, $c) = @_;

my $genre = $c->stash->{genre};

my $form = $c->form( form => 'Genre', init_object => $genre );

if ($c->form_posted_and_valid($form)) {
my %update = $self->_form_to_hash($form);

$c->model('MB')->with_transaction(sub {
$c->model('Genre')->update($genre->{id}, \%update);
});
$self->_redirect_to_genre($c, $genre->{gid});
}

my %props = (
form => $form->TO_JSON,
genre => $genre->TO_JSON,
);

$c->stash(
component_path => 'genre/EditGenre',
component_props => \%props,
current_view => 'Node',
);
}

sub delete : Chained('load') RequireAuth(relationship_editor) {
my ($self, $c) = @_;

my $genre = $c->stash->{genre};
with 'MusicBrainz::Server::Controller::Role::Create' => {
form => 'Genre',
edit_type => $EDIT_GENRE_CREATE,
};

if ($c->form_posted) {
$c->model('MB')->with_transaction(sub {
$c->model('Genre')->delete($genre->{id});
});
with 'MusicBrainz::Server::Controller::Role::Edit' => {
form => 'Genre',
edit_type => $EDIT_GENRE_EDIT,
};

$c->response->redirect($c->uri_for_action('genre/list'));
}
with 'MusicBrainz::Server::Controller::Role::Delete' => {
edit_type => $EDIT_GENRE_DELETE,
};

$c->stash(
component_path => 'genre/DeleteGenre',
component_props => {genre => $genre->TO_JSON},
current_view => 'Node',
);
}
for my $method (qw( create edit delete edit_annotation )) {
before $method => sub {
my ($self, $c) = @_;
if (!$c->user->is_relationship_editor) {
$c->detach('/error_403');
}
};
};

sub list : Path('/genres') Args(0) {
my ($self, $c) = @_;
Expand Down
12 changes: 12 additions & 0 deletions lib/MusicBrainz/Server/Controller/Role/Create.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package MusicBrainz::Server::Controller::Role::Create;
use MooseX::MethodAttributes::Role;
use MooseX::Role::Parameterized;
use MusicBrainz::Server::Data::Utils qw( model_to_type );
use aliased 'MusicBrainz::Server::WebService::JSONSerializer';

parameter 'form' => (
Expand Down Expand Up @@ -58,6 +59,17 @@ role {
my $model = $self->config->{model};
my $entity;

if ($model eq 'Genre') {
my $type = model_to_type($model);
my $form = $c->form( form => $params->form );

$c->stash(
component_path => $type . '/Create' . $model,
component_props => {form => $form->TO_JSON},
current_view => 'Node',
);
}

$self->edit_action($c,
form => $params->form,
type => $params->edit_type,
Expand Down
16 changes: 14 additions & 2 deletions lib/MusicBrainz/Server/Controller/Role/Delete.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package MusicBrainz::Server::Controller::Role::Delete;
use MooseX::MethodAttributes::Role;
use MooseX::Role::Parameterized;
use MusicBrainz::Server::ControllerUtils::Delete qw( cancel_or_action );
use MusicBrainz::Server::Data::Utils qw( model_to_type );

parameter 'edit_type' => (
isa => 'Int',
Expand Down Expand Up @@ -38,12 +39,23 @@ role {
my ($self, $c) = @_;
my $entity_name = $self->{entity_name};
my $edit_entity = $c->stash->{ $entity_name };
if ($self->{model} eq 'Area') {
my $model = $self->{model};
if ($model eq 'Area') {
$c->stash(
is_release_country_area => $c->model('Area')->is_release_country_area($edit_entity->id)
);
}
if ($c->model($self->{model})->can_delete($edit_entity->id)) {

if ($model eq 'Genre') {
my $type = model_to_type($model);
$c->stash(
component_path => $type . '/Delete' . $model,
component_props => {entity => $edit_entity->TO_JSON},
current_view => 'Node',
);
}

if ($c->model($model)->can_delete($edit_entity->id)) {
$c->stash( can_delete => 1 );
# find a corresponding add edit and cancel instead, if applicable (MBS-1397)
my $create_edit_type = $self->{create_edit_type};
Expand Down
22 changes: 21 additions & 1 deletion lib/MusicBrainz/Server/Controller/Role/Edit.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package MusicBrainz::Server::Controller::Role::Edit;
use MooseX::MethodAttributes::Role;
use MooseX::Role::Parameterized;
use MusicBrainz::Server::Data::Utils qw( model_to_type );

parameter 'form' => (
isa => 'Str',
Expand Down Expand Up @@ -33,8 +34,27 @@ role {

my $entity_name = $self->{entity_name};
my $edit_entity = $c->stash->{ $entity_name };
my $model = $self->{model};

$c->stash->{template} = 'entity/edit.tt';
if ($model eq 'Genre') {
my $type = model_to_type($model);

my $form = $c->form(
form => $params->form,
init_object => $edit_entity,
);

$c->stash(
component_path => $type . '/Edit' . $model,
component_props => {
entity => $edit_entity->TO_JSON,
form => $form->TO_JSON,
},
current_view => 'Node',
);
} else {
$c->stash->{template} = 'entity/edit.tt';
}

return $self->edit_action($c,
form => $params->form,
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Data/Editor.pm
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ sub added_entities_counts {
WHEN type = ? THEN 'area'
WHEN type = ? THEN 'cover_art'
WHEN type = ? THEN 'event'
WHEN type = ? THEN 'genre'
WHEN type = ? THEN 'instrument'
WHEN type = ? THEN 'label'
WHEN type = ? THEN 'place'
Expand All @@ -690,7 +691,7 @@ sub added_entities_counts {
GROUP BY type};
my @params = ($EDIT_ARTIST_CREATE, $EDIT_RELEASE_CREATE,
$EDIT_HISTORIC_ADD_RELEASE, $EDIT_AREA_CREATE, $EDIT_RELEASE_ADD_COVER_ART,
$EDIT_EVENT_CREATE, $EDIT_INSTRUMENT_CREATE, $EDIT_LABEL_CREATE,
$EDIT_EVENT_CREATE, $EDIT_GENRE_CREATE, $EDIT_INSTRUMENT_CREATE, $EDIT_LABEL_CREATE,
$EDIT_PLACE_CREATE, $EDIT_RECORDING_CREATE, $EDIT_RELEASEGROUP_CREATE,
$EDIT_SERIES_CREATE, $EDIT_WORK_CREATE, $STATUS_APPLIED);
my $rows = $self->sql->select_list_of_lists($query, @params, $editor_id);
Expand Down
4 changes: 4 additions & 0 deletions lib/MusicBrainz/Server/Data/Genre.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use MusicBrainz::Server::Data::Utils qw(
use MusicBrainz::Server::Entity::Genre;

extends 'MusicBrainz::Server::Data::CoreEntity';
with 'MusicBrainz::Server::Data::Role::Annotation' => { type => 'genre' };
with 'MusicBrainz::Server::Data::Role::Alias' => { type => 'genre' };
with 'MusicBrainz::Server::Data::Role::CoreEntityCache';
with 'MusicBrainz::Server::Data::Role::Editable' => { table => 'genre' };
with 'MusicBrainz::Server::Data::Role::LinksToEdit' => { table => 'genre' };
with 'MusicBrainz::Server::Data::Role::SelectAll';
with 'MusicBrainz::Server::Data::Role::DeleteAndLog' => { type => 'genre' };

Expand Down Expand Up @@ -52,6 +55,7 @@ sub can_delete { 1 }
sub delete {
my ($self, $genre_id) = @_;

$self->annotation->delete($genre_id);
$self->delete_returning_gids($genre_id);
return;
}
Expand Down
6 changes: 0 additions & 6 deletions lib/MusicBrainz/Server/Edit/Area/Create.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ sub build_display_data
};
}

sub _insert_hash
{
my ($self, $data) = @_;
return $data;
};

sub edit_template_react { 'AddArea' };

__PACKAGE__->meta->make_immutable;
Expand Down
19 changes: 19 additions & 0 deletions lib/MusicBrainz/Server/Edit/Genre.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package MusicBrainz::Server::Edit::Genre;
use Moose::Role;
use namespace::autoclean;

use MusicBrainz::Server::Translation qw ( l );

sub edit_category { l('Genre') }

1;

=head1 COPYRIGHT AND LICENSE
Copyright (C) 2022 MetaBrainz Foundation
This file is part of MusicBrainz, the open internet music database,
and is licensed under the GPL version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl-2.0.txt
=cut
18 changes: 18 additions & 0 deletions lib/MusicBrainz/Server/Edit/Genre/AddAnnotation.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package MusicBrainz::Server::Edit::Genre::AddAnnotation;
use Moose;

use MusicBrainz::Server::Constants qw( $EDIT_GENRE_ADD_ANNOTATION );
use MusicBrainz::Server::Translation qw( N_l );

extends 'MusicBrainz::Server::Edit';

with 'MusicBrainz::Server::Edit::Annotation::Edit' => {
model => 'Genre',
edit_name => N_l('Add genre annotation'),
edit_type => $EDIT_GENRE_ADD_ANNOTATION,
};

__PACKAGE__->meta->make_immutable;
no Moose;

1;
71 changes: 71 additions & 0 deletions lib/MusicBrainz/Server/Edit/Genre/Create.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package MusicBrainz::Server::Edit::Genre::Create;
use Moose;

use MusicBrainz::Server::Constants qw( $EDIT_GENRE_CREATE );
use MusicBrainz::Server::Edit::Types qw( Nullable );
use MusicBrainz::Server::Entity::Util::JSON qw( to_json_object );
use MusicBrainz::Server::Translation qw( N_l );
use aliased 'MusicBrainz::Server::Entity::PartialDate';
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose qw( Str );
use MooseX::Types::Structured qw( Dict Optional );

use aliased 'MusicBrainz::Server::Entity::Genre';

extends 'MusicBrainz::Server::Edit::Generic::Create';
with 'MusicBrainz::Server::Edit::Role::Preview';
with 'MusicBrainz::Server::Edit::Genre';
with 'MusicBrainz::Server::Edit::Role::AlwaysAutoEdit';

sub edit_name { N_l('Add genre') }
sub edit_type { $EDIT_GENRE_CREATE }
sub _create_model { 'Genre' }
sub genre_id { shift->entity_id }


has '+data' => (
isa => Dict[
name => Str,
gid => Optional[Str],
comment => Nullable[Str],
]
);

sub foreign_keys
{
my $self = shift;
return {
Genre => [ $self->entity_id ],
};
}

sub build_display_data
{
my ($self, $loaded) = @_;

return {
name => $self->data->{name},
comment => $self->data->{comment},
genre => to_json_object((defined($self->entity_id) &&
$loaded->{Genre}{ $self->entity_id }) ||
Genre->new( name => $self->data->{name} )
),
};
}

sub edit_template_react { 'AddGenre' };

__PACKAGE__->meta->make_immutable;
no Moose;

1;

=head1 COPYRIGHT AND LICENSE
Copyright (C) 2022 MetaBrainz Foundation
This file is part of MusicBrainz, the open internet music database,
and is licensed under the GPL version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl-2.0.txt
=cut
Loading

0 comments on commit b64c968

Please sign in to comment.