Committer: vtroitsky
LJSUP-12005: Update console command Selfpromo. Some refactoring were made.U trunk/cgi-bin/LJ/Console/Command/SelfPromo.pm
Modified: trunk/cgi-bin/LJ/Console/Command/SelfPromo.pm =================================================================== --- trunk/cgi-bin/LJ/Console/Command/SelfPromo.pm 2012-05-03 15:44:44 UTC (rev 11881) +++ trunk/cgi-bin/LJ/Console/Command/SelfPromo.pm 2012-05-03 17:02:31 UTC (rev 11882) @@ -4,8 +4,8 @@ use base qw( LJ::Console::Command ); -use LJ::Pay::SelfPromo; -use LJ::Pay::PromotionSlot qw(:classes); +use LJ::Pay::PromotionSlot qw(:classes :regions); +use LJ::Pay::PromotedObject qw(:types); sub cmd {'selfpromo'} @@ -14,7 +14,7 @@ } sub usage { - return '<cmd> <object> [norefund] <reason>'; + return '<cmd> <object> [region] [norefund] <reason>'; } sub args_desc { @@ -25,6 +25,12 @@ 'For "cancel", "ban" and "unban", either the entry URL or ' . 'the username to cancel promotion or to ban or unban.', + 'region' => + 'For "cancel" only, an optional flag that specifies ' . + 'region of the promo. Available regions are: [' . + join(',', LJ::Pay::PromotionSlot->get_all_regions()) . ']. ' . + 'Default region: '. PROMO_REGION_DEFAULT(), + 'norefund' => 'For "cancel" only, an optional flag that specifies ' . 'not to issue token refund to the promotion author.', @@ -87,7 +93,7 @@ my $type; my $object_orig = $object; - $object = LJ::Pay::PromotedObject->new_from_url($object); + $object = LJ::Pay::PromotedObject->new_from_name($object); unless ($object) { if ( $object_orig =~ m{^http://} ) { return $self->error("No entry found at $object_orig"); @@ -99,10 +105,17 @@ } $type = $object->type; + my $country = PROMO_REGION_DEFAULT(); + my ($norefund, $reason) = 0; if (@args_remainder) { my ($arg) = @args_remainder; + if ( $arg =~ /^ua$/ ) { + $country = $arg; + shift @args_remainder; + } + if ( $arg eq 'norefund' ) { $norefund = 1; shift @args_remainder; @@ -115,35 +128,29 @@ return $self->error("Insufficient arguments"); } - my $country = 'cyr'; # TODO - if ( $cmd eq 'cancel' ) { - if ( $type eq 'user' ) { - return $self->error('"cancel" requires personal journal or community') unless ($object->is_community || $object->is_personal); - $type = $object->is_community ? 'community' : 'journal'; - } + #if ( $type =~ /^journal|community$/ ) { + # return $self->error('"cancel" requires personal journal or community') unless ($object->is_community || $object->is_personal); + # $type = $object->is_community ? 'community' : 'journal'; + #} my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $type, country => $country); $slot->check_current_promotion; my $current_promotion = $slot->current_promotion; - # use Data::Dumper; - # return $self->error("Type: $type". Dumper($current_promo)); - unless ($current_promotion) { - return $self->error("No $type currently promoted"); + return $self->error("No $type currently promoted in $country promo"); } - unless ($current_promotion->is_promoting($object)) - { + unless ($current_promotion->is_promoting($object)) { return $self->error( - $object_orig . " is not the $type currently promoted" ); + $object_orig . " is not the $type currently promoted in $country promo" ); } $self->_cancel_object( 'slot' => $slot, 'type' => $type, 'object' => $object, 'refund' => !$norefund, 'reason' => $reason ); - $self->info( $object_orig . ' cancelled successfully' ); + $self->info( "$object_orig cancelled successfully from $country promo" ); return 1; } @@ -151,20 +158,21 @@ if ( $type ne PROMO_OBJECT_TYPE_ENTRY() ) { my $u = $object->object; # LJ::User + # check all current promotions - foreach my $ptype qw(entry journal community) { - my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $ptype, country => $country); - $slot->check_current_promotion; - my $current_promo = $slot->current_promotion(); + foreach my $cc (LJ::Pay::PromotionSlot->get_all_regions()) { + foreach my $ptype qw(entry journal community) { + my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $ptype, country => $cc); + $slot->check_current_promotion; + my $current_promo = $slot->current_promotion(); - if ( $current_promo && ( $current_promo->owner->equals($u) || $current_promo->journal->equals($u) || $current_promo->promoter->equals($u)) ) - { - $self->info('The ban will affect the currently promoted '. $ptype . ', so cancelling that.'); - my $current_object = $current_promo->object(); - $self->_cancel_object( 'slot' => $slot, 'type' => $ptype, 'object' => $current_object, 'reason' => $reason ); + if ( $current_promo && ( $current_promo->is_owner($u) || $current_promo->is_promoter($u) || $current_promo->is_promoting($u)) ) { + $self->info("The ban will affect the currently promoted $ptype in $cc promo, so cancelling that."); + my $current_object = $current_promo->promoted_object(); + $self->_cancel_object( 'slot' => $slot, 'type' => $ptype, 'object' => $current_object, 'reason' => $reason ); + } } } - $u->set_prop( 'selfpromo_banned' => 1 ); LJ::statushistory_add( $u, $admin, 'selfpromo', 'admin banned user. Reason: '. $reason ); @@ -174,16 +182,19 @@ if ( $type eq PROMO_OBJECT_TYPE_ENTRY() ) { my $entry = $object->object; - my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $type); - $slot->check_current_promotion; - my $current_promo = $slot->current_promotion(); + # check all regions + foreach my $cc (LJ::Pay::PromotionSlot->get_all_regions()) { + + my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $type, country => $cc); - if ( $current_promo && $current_promo->is_promoting($object) ) - { - $self->info('The ban will affect the currently promoted ' - . 'entry, so cancelling that.'); - $self->_cancel_object( 'slot' => $slot, 'type' => $type, 'object' => $object, 'reason' => $reason ); + $slot->check_current_promotion; + my $current_promo = $slot->current_promotion(); + + if ( $current_promo && $current_promo->is_promoting($object) ) { + $self->info("The ban will affect the currently promoted entry in $country promo, so cancelling that."); + $self->_cancel_object( 'slot' => $slot, 'type' => $type, 'object' => $object, 'reason' => $reason ); + } } $entry->set_prop( 'selfpromo_banned' => 1 );