sunnyman's (sunnyman) wrote in changelog,
sunnyman's
sunnyman
changelog

[ljcom] r12578: LJSUP-13467: Selfpromo/CommPromo refacto...

Committer: vtroitsky
LJSUP-13467: Selfpromo/CommPromo refactoring required for Journal Promo
U   trunk/cgi-bin/LJ/Pay/SelfPromo.pm
Modified: trunk/cgi-bin/LJ/Pay/SelfPromo.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/SelfPromo.pm	2012-08-29 12:04:01 UTC (rev 12577)
+++ trunk/cgi-bin/LJ/Pay/SelfPromo.pm	2012-08-29 12:45:29 UTC (rev 12578)
@@ -2,6 +2,8 @@
 use strict;
 use warnings;
 
+use base qw(LJ::Promo::Processor);
+
 use LJ::Pay::Wallet;
 use LJ::Pay::Wallet::Error qw(:codes);
 use LJ::Pay::PromotionSlot qw(:classes);
@@ -10,6 +12,7 @@
 use LJ::Pay::SelfPromo::History;
 use LJ::Event::PromoBuyout;
 
+
 #use LJ::Pay::SelfPromo::Lock;
 #use LJ::Pay::SelfPromo::Error;
 
@@ -55,33 +58,14 @@
     return $slot->config->{'min_cost'};  # return minimal price 
 }
   
-=item is_object_eligible
-    Check that specified object is eligible to promote:
-    As object may be considered: entries and  users (entries and  communities)
-    Have to be overriden in childs
-    REQ: accept PromotedObject objects
-    PROCESSING:
-        2. call $promoted->is_eligible($promoter)
-        3. Ensure that object isn't currently promoted in all promos!!!
-        
+=item _is_object_eligible 
+    Specific checks for that available for current processor
+    Ensure that object is not promoting at the moment in all promos!!!!
 =cut
-sub is_object_eligible {
+sub _is_object_eligible {
     my ($class, $slot, $promoted_object, $promoter, $reason_ref) = @_;
     my $type = $promoted_object->type;
-  
-    unless ($type) {
-        $$reason_ref = 'unknown_object';
-    }
 
-    unless ($slot->type eq $type) {
-        $$reason_ref = 'mismatch_type';
-        return;
-    }
-
-    return unless $promoted_object->is_eligible($promoter, $reason_ref, slot => $slot);
-
-    # TODO: Ensure that object is not promoting at the moment in all promos!!!!
-
     if ($type eq PROMO_OBJECT_TYPE_ENTRY()) {
         my $cslot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_COMMERCIAL, type => PROMO_OBJECT_TYPE_ENTRY());
         my $cpromoted_object = $cslot->current_promoted_object();
@@ -93,10 +77,9 @@
             }
         }
     }
-
     return 1;
-}
 
+}
 =item buyout
     Function to make a bid: core logic is implemented here
     TODO: remove specific logic to subfunctions
@@ -142,7 +125,7 @@
 
         my $min_price = $class->buyout_cost($slot, 'require_db' => 1 );
         unless ( $price >= $min_price ) {
-            LJ::Pay::SelfPromo->debug_msg(
+            $class->debug_msg($slot,
                 "not enough money offered, need at least $min_price");
 
             $class->raise_error( ERROR_PRICE_INSUFFICIENT, $min_price );
@@ -150,7 +133,7 @@
 
         my $wallet_balance = int LJ::Pay::Wallet->get_user_balance($promoter);
         unless ( $wallet_balance >= $price ) {
-            LJ::Pay::SelfPromo->debug_msg(
+            $class->debug_msg($slot,
                 "promoter doesn't actually have that much " .
                 "(has $wallet_balance, tried to buy out for $price)"
             );
@@ -166,7 +149,7 @@
     my $current_promotion;
     do {
 
-        LJ::Pay::SelfPromo->debug_msg("creating the cart");
+        $class->debug_msg($slot, "creating the cart");
 
         # calculate how much money goes where for this item:
         # a part of it goes to the system, a part of it goes to the
@@ -206,14 +189,14 @@
             }
         );
 
-        LJ::Pay::SelfPromo->debug_msg("cart created, setting the method");
+        $class->debug_msg($slot, "cart created, setting the method");
 
         # Take the money from the promoter
         $cart->set_method( LJ::Pay::Method::Wallet->code );
 
-        LJ::Pay::SelfPromo->debug_msg("paying for the cart");
+        $class->debug_msg($slot, "paying for the cart");
         LJ::Pay::Wallet->pay_for_cart( $cart->get_payid );
-        LJ::Pay::SelfPromo->debug_msg("paid successfully");
+        $class->debug_msg($slot, "paid successfully");
         # TODO: Catch exceptions from wallet operations!!!
  
     };
@@ -230,12 +213,12 @@
     #   2. 
  
     do {
-        $class->debug_msg("delivering the cart/items");
-        $class->debug_msg( "payid=" . $cart->get_payid );
+        $class->debug_msg($slot, "delivering the cart/items");
+        $class->debug_msg($slot,  "payid=" . $cart->get_payid );
 
         my ($item) = $cart->get_items;
 
-        LJ::Pay::SelfPromo->debug_msg( "piid=" . $item->get_piid );
+        $class->debug_msg($slot,  "piid=" . $item->get_piid );
 
         $cart->update( 'used'   => 'Y' );
         $item->update( 'status' => 'pend' );
@@ -244,25 +227,25 @@
 
         $class->_deliver_item($slot, $item, $promoter, $current_promotion, $promoted_object);
 
-        LJ::Pay::SelfPromo->debug_msg(
+        $class->debug_msg($slot,
             "cart and item updated to be ready for delivery");
 
         local $SIG{'__WARN__'} = sub {
             my @messages = @_;
-            $class->debug_msg( "delivery warning: ", @messages );
+            $class->debug_msg($slot, "delivery warning: ", @messages );
             warn @messages;
         };
 
         # Mark all delivered by calling deliver method
         my $deliver_res = $item->deliver( $cart, time, sub { } );
         unless ($deliver_res) {
-            $class->debug_msg("delivery failed");
+            $class->debug_msg($slot, "delivery failed");
             die "delivering item failed, see error log for details";
         }
         $cart->mark_complete;
     };
 
-    LJ::Pay::SelfPromo->debug_msg("all done");
+    $class->debug_msg($slot, "all done");
 
     # Update history for current slot
     # TODO: Move History into slot
@@ -279,8 +262,8 @@
 sub _deliver_item {
     my ($class, $slot, $item, $rcpt_u, $current_promotion, $promoted_object) = @_;
 
-    LJ::Pay::SelfPromo->debug_msg("delivery of a selfpromo item requested");
-    LJ::Pay::SelfPromo->debug_msg( "piid=" . $item->get_piid );
+    $class->debug_msg($slot, "delivery of a selfpromo item requested");
+    $class->debug_msg($slot, "piid=" . $item->get_piid );
 
     # this should be temporary until we l10n the shop
     my $lang = ($rcpt_u && $rcpt_u->prop('browselang')) ? $rcpt_u->prop('browselang') : $LJ::DEFAULT_LANG;
@@ -295,7 +278,7 @@
 
     my $type = $item->get_prop('selfpromo_type'); # action type
 
-    LJ::Pay::SelfPromo->debug_msg("type = $type, object_type = $object_type, class = $class");
+    $class->debug_msg($slot, "type = $type, object_type = $object_type, class = $class");
     # if there's an entry being promoted, deactivate it, and
     # then handle any refund if need be
     #
@@ -303,7 +286,7 @@
     # also only cancel the current promotion
     my $object_url;
     if ( $current_promotion ) {
-        LJ::Pay::SelfPromo->debug_msg("current PROMO received");
+        $class->debug_msg($slot, "current PROMO received");
         $object_url = $current_promotion->object_url;
         my $object = $current_promotion->promoted_object;
         $current_promotion->deactivate(
@@ -315,7 +298,7 @@
             my $refund_to = LJ::load_userid($refund_userid);
             my $refund = $item->get_prop('selfpromo_refund');
             if ($refund) {
-                LJ::Pay::SelfPromo->debug_msg("crediting them");
+                $class->debug_msg($slot, "crediting them");
 
                 # credit them:
                 LJ::Pay::Wallet->try_add( $refund_to, $refund );
@@ -329,7 +312,7 @@
                     'time_end' => time,
                 );
 
-                LJ::Pay::SelfPromo->debug_msg("successfully credited");
+                $class->debug_msg($slot, "successfully credited");
             }
 
             my $reason_map = {
@@ -360,7 +343,7 @@
                     'notify_owner' => 1,
                 );
             }
-            LJ::Pay::SelfPromo->debug_msg("refund successful");
+            $class->debug_msg($slot, "refund successful");
         }
     }
 
@@ -371,7 +354,7 @@
         my $remainder_to = LJ::load_userid($remainder_to_userid);
 
         if ($remainder_to) {
-            LJ::Pay::SelfPromo->debug_msg(
+            $class->debug_msg($slot, 
                 "sending the remainder of tokens to " .
                 $remainder_to->username );
 
@@ -418,7 +401,7 @@
 
         $email_subject = $ml->("selfpromo.$object_type.notification.activate.subject", { region => $region_str });
  
-        LJ::Pay::SelfPromo->debug_msg("generating the emails");
+        $class->debug_msg($slot, "generating the emails");
         # Need to send second email if object owner and recipient are different
         unless ( LJ::u_equals($owner, $rcpt_u) ) {
             $email_body = $ml->(
@@ -477,7 +460,7 @@
         );
     }
 
-    LJ::Pay::SelfPromo->debug_msg("delivery successful");
+    $class->debug_msg($slot, "delivery successful");
 }
 
 =item admin_cancel_promo
@@ -779,6 +762,7 @@
 # Check that current promo can still be active
 # deactivate it: depending on cases
 # Called from worker 
+# TODO: (Refactoring) move Promotion specific part into Promotion object
 sub check_current_promotion {
     my ($class, $slot) = @_;
 
@@ -976,8 +960,9 @@
 # * object
 sub create_shop_cart {
     my ( $class, $opts ) = @_;
+    my $slot = $opts->{'slot'};
 
-    LJ::Pay::SelfPromo->debug_msg(
+    $class->debug_msg($slot,
         "create_shop_cart called: " . LJ::compact_dumper($opts) );
 
     my $cart = LJ::Pay::Payment::new_cart( $opts->{'cart_owner'} );
@@ -998,7 +983,7 @@
     ($it) = $cart->get_items;
 
     # set payitem props as appropriate
-    $it->set_prop( 'selfpromo'                => $opts->{'slot'}->type );   # type of item promoted
+    $it->set_prop( 'selfpromo'                => $slot->type );   # type of item promoted
     $it->set_prop( 'selfpromo_type'           => $opts->{'type'} );         # type of the cart operation
     $it->set_prop( 'selfpromo_profit'         => $opts->{'profit'} );
     $it->set_prop( 'selfpromo_refund'         => $opts->{'refund'} );
@@ -1007,7 +992,7 @@
     $it->set_prop( 'selfpromo_remainder'      => $opts->{'remainder'} );
     $it->set_prop( 
                  'selfpromo_remainder_userid' => $opts->{'remainder_userid'} );
-    $it->set_prop( 'selfpromo_country'        => $opts->{'slot'}->region );
+    $it->set_prop( 'selfpromo_country'        => $slot->region );
 
     # Add object specific properties, if object exists (in case of non refunding cart)
     if ( my $object = $opts->{'object'} ) {
@@ -1020,7 +1005,7 @@
         }
     }
 
-    LJ::Pay::SelfPromo->debug_msg(
+    $class->debug_msg($slot,
         "create_shop_cart successful, " .
         "payid=" . $cart->get_payid . ", " .
         "piid=" . $it->get_piid
@@ -1131,121 +1116,121 @@
 
 
 # TODO: Candidate to move it to PromotionSlot
-sub was_promoted {
-    my ($class, $slot, $object) = @_;
-    return undef unless ($object->type eq PROMO_OBJECT_TYPE_ENTRY);
-    return LJ::MemCache::get_or_set(
-        $slot->was_promoted_key($object->ownerid, $object->ditemid), sub {
-            my $res = LJ::Pay::Promotion->find_promotions({ slot => $slot, ditemid => $object->ditemid, posterid => $object->ownerid });
-            return @$res? 1 : 0;
-        }, 7200
-    );
-}
+#sub was_promoted {
+#    my ($class, $slot, $object) = @_;
+#    return undef unless ($object->type eq PROMO_OBJECT_TYPE_ENTRY);
+#    return LJ::MemCache::get_or_set(
+#        $slot->was_promoted_key($object->ownerid, $object->ditemid), sub {
+#            my $res = LJ::Pay::Promotion->find_promotions({ slot => $slot, ditemid => $object->ditemid, posterid => $object->ownerid });
+#            return @$res? 1 : 0;
+#        }, 7200
+#    );
+#}
 
-sub raise_error {
-    my ( $class, @args ) = @_;
-    LJ::Pay::SelfPromo::Error->raise(@args);
-}
+#sub raise_error {
+#    my ( $class, @args ) = @_;
+#    LJ::Pay::SelfPromo::Error->raise(@args);
+#}
 
-sub lock {
-    my ($class, $slot) = @_;
-    return LJ::Pay::SelfPromo::Lock->new($slot ? $slot->slot_id : $class->class());
-}
+#sub lock {
+#    my ($class, $slot) = @_;
+#    return LJ::Pay::SelfPromo::Lock->new($slot ? $slot->slot_id : $class->class());
+#}
 
-sub lock_taken {
-    my ($class, $slot) = @_;
-    return LJ::Pay::SelfPromo::Lock->taken($slot ? $slot->slot_id : $class->class());
-}
+#sub lock_taken {
+#    my ($class, $slot) = @_;
+#    return LJ::Pay::SelfPromo::Lock->taken($slot ? $slot->slot_id : $class->class());
+#}
 
-sub lock_taken_elsewhere {
-    my ($class, $slot) = @_;
-    return LJ::Pay::SelfPromo::Lock->taken_elsewhere($slot ? $slot->slot_id : $class->class());
-}
+#sub lock_taken_elsewhere {
+#    my ($class, $slot) = @_;
+#    return LJ::Pay::SelfPromo::Lock->taken_elsewhere($slot ? $slot->slot_id : $class->class());
+#}
 
-sub debug_msg {
-    my ( $class, @args ) = @_;
+#sub debug_msg {
+#    my ( $class, @args ) = @_;
+#
+#    return unless LJ::is_enabled('selfpromo_debug');
+#
+#    my $hostname = Sys::Hostname::hostname();
+#    my $time     = localtime;
 
-    return unless LJ::is_enabled('selfpromo_debug');
+#     my $msg = join( '',
+#        "[shop/selfpromo, pid=$$, host=$hostname, time=$time] ", @args );
 
-    my $hostname = Sys::Hostname::hostname();
-    my $time     = localtime;
+#    my ( $package, $filename, $line, $sub ) = caller(1);
+#    warn "$msg in sub ${package}::${sub} at $filename line $line\n";
+#}
 
-    my $msg = join( '',
-        "[shop/selfpromo, pid=$$, host=$hostname, time=$time] ", @args );
+#package LJ::Pay::SelfPromo::Error;
+#
+#sub raise {
+#    my ( $class, $type, $details ) = @_;
+#    die bless { 'type' => $type, 'details' => $details }, $class;
+#}
 
-    my ( $package, $filename, $line, $sub ) = caller(1);
-    warn "$msg in sub ${package}::${sub} at $filename line $line\n";
-}
+#sub type    { shift->{'type'} }
+#sub details { shift->{'details'} }
 
-package LJ::Pay::SelfPromo::Error;
+#package LJ::Pay::SelfPromo::Lock;
 
-sub raise {
-    my ( $class, $type, $details ) = @_;
-    die bless { 'type' => $type, 'details' => $details }, $class;
-}
+#my $locked;
 
-sub type    { shift->{'type'} }
-sub details { shift->{'details'} }
+#sub new {
+#    my ($class, @names) = @_;
 
-package LJ::Pay::SelfPromo::Lock;
+#    LJ::Pay::SelfPromo->debug_msg("trying to acquire lock");
 
-my $locked;
+#    my $name = join('_', 'selfpromo', @names);
 
-sub new {
-    my ($class, @names) = @_;
+#    if ( $LJ::REQ_GLOBAL{$name} ) {
+#        LJ::Pay::SelfPromo->debug_msg("... already got it, skipping");
+#        return;
+#    }
 
-    LJ::Pay::SelfPromo->debug_msg("trying to acquire lock");
+#    my $lock_result = LJ::get_lock( LJ::get_db_writer(), 'global', $name );
 
-    my $name = join('_', 'selfpromo', @names);
+#    unless ($lock_result) {
+#        LJ::Pay::SelfPromo->debug_msg(
+#            "LJ::get_lock returned false, throwing an exception" );
+#        die "Can't get lock";
+#    }
 
-    if ( $LJ::REQ_GLOBAL{$name} ) {
-        LJ::Pay::SelfPromo->debug_msg("... already got it, skipping");
-        return;
-    }
+#    LJ::Pay::SelfPromo->debug_msg("locked successfully");
 
-    my $lock_result = LJ::get_lock( LJ::get_db_writer(), 'global', $name );
+#    $LJ::REQ_GLOBAL{$name} = 1;
+#    $locked = $name;
+#    return bless {}, $class;
+#}
 
-    unless ($lock_result) {
-        LJ::Pay::SelfPromo->debug_msg(
-            "LJ::get_lock returned false, throwing an exception" );
-        die "Can't get lock";
-    }
+#sub DESTROY {
+#    my ($self) = @_;
 
-    LJ::Pay::SelfPromo->debug_msg("locked successfully");
+#    LJ::Pay::SelfPromo->debug_msg("releasing the lock");
 
-    $LJ::REQ_GLOBAL{$name} = 1;
-    $locked = $name;
-    return bless {}, $class;
-}
-
-sub DESTROY {
-    my ($self) = @_;
-
-    LJ::Pay::SelfPromo->debug_msg("releasing the lock");
-
     ## warning: this code may be called during exception "stack unwind" step.
     ## if original exception ($@) is not saved, it will be lost.
-    local $@;
+#    local $@;
 
-    LJ::release_lock( LJ::get_db_writer(), 'global', $locked ) unless $locked eq '';
-    $LJ::REQ_GLOBAL{$locked} = 0;
-    $locked = '';
-}
+#    LJ::release_lock( LJ::get_db_writer(), 'global', $locked ) unless $locked eq '';
+#    $LJ::REQ_GLOBAL{$locked} = 0;
+#    $locked = '';
+#}
 
-sub taken {
-    my ($class, @names) = @_;
-    my $name = join('_', 'selfpromo', @names);
-    my $result = LJ::lock_taken( LJ::get_db_writer(), 'global', $name );
-    LJ::Pay::SelfPromo->debug_msg("lock taken => $result");
-    return $result;
-}
+#sub taken {
+#    my ($class, @names) = @_;
+#    my $name = join('_', 'selfpromo', @names);
+#    my $result = LJ::lock_taken( LJ::get_db_writer(), 'global', $name );
+#    LJ::Pay::SelfPromo->debug_msg("lock taken => $result");
+#    return $result;
+#}
 
-sub taken_elsewhere {
-    my ($class, @names) = @_;
-    my $result = ! $locked && $class->taken(@names);
-    LJ::Pay::SelfPromo->debug_msg("lock taken elsewhere => $result");
-    return $result;
-}
+#sub taken_elsewhere {
+#    my ($class, @names) = @_;
+#    my $result = ! $locked && $class->taken(@names);
+#    LJ::Pay::SelfPromo->debug_msg("lock taken elsewhere => $result");
+#    return $result;
+#}
 
 1;
 

Tags: ljcom, pm, sunnyman, vtroitsky
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments