fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-06-25 12:35 pm

[dw-free] replace ljmood.pl with DW::Mood.pm

[commit: http://hg.dwscoalition.org/dw-free/rev/c7431ec0bc18]

http://bugs.dwscoalition.org/show_bug.cgi?id=2690

Refactoring. Make a new method $u->create_moodtheme; call it in the
appropriate places.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/DW/Mood.pm
  • cgi-bin/LJ/Console/Command/MoodthemeCreate.pm
  • htdocs/manage/moodthemes.bml
  • t/console-moodthemes.t
--------------------------------------------------------------------------------
diff -r 92e2e6b2f18e -r c7431ec0bc18 cgi-bin/DW/Mood.pm
--- a/cgi-bin/DW/Mood.pm	Fri Jun 25 19:05:56 2010 +0800
+++ b/cgi-bin/DW/Mood.pm	Fri Jun 25 20:41:23 2010 +0800
@@ -264,4 +264,32 @@ sub clear_cache {
 }
 
 
+# END package DW::Mood;
+
+package LJ::User;
+
+# user method for creating new mood theme
+# args: theme name, description, errorref
+# returns: id of new theme or undef on failure
+sub create_moodtheme {
+    my ( $u, $name, $desc, $err ) = @_;
+    my $errsub = sub { $$err = $_[0] if ref $err; return undef };
+
+    return $errsub->( LJ::Lang::ml( "/manage/moodthemes.bml.error.cantcreatethemes" ) )
+        unless $u->can_create_moodthemes;
+    return $errsub->( LJ::Lang::ml( "/manage/moodthemes.bml.error.nonamegiven" ) )
+        unless $name;
+    $desc ||= '';
+
+    my $dbh = LJ::get_db_writer() or
+        return $errsub->( LJ::Lang::ml( "error.nodb" ) );
+    my $sth = $dbh->prepare( "INSERT INTO moodthemes " .
+        "(ownerid, name, des, is_public) VALUES (?, ?, ?, 'N')" );
+    $sth->execute( $u->id, $name, $desc ) or
+        return $errsub->( LJ::Lang::ml( "error.dberror" ) . $dbh->errstr );
+
+    return $dbh->{mysql_insertid};
+}
+
+
 1;
diff -r 92e2e6b2f18e -r c7431ec0bc18 cgi-bin/LJ/Console/Command/MoodthemeCreate.pm
--- a/cgi-bin/LJ/Console/Command/MoodthemeCreate.pm	Fri Jun 25 19:05:56 2010 +0800
+++ b/cgi-bin/LJ/Console/Command/MoodthemeCreate.pm	Fri Jun 25 20:41:23 2010 +0800
@@ -37,14 +37,10 @@ sub execute {
         unless $name && $desc && scalar(@args) == 0;
 
     my $remote = LJ::get_remote();
-    return $self->error("Sorry, your account type doesn't let you create new mood themes")
-        unless $remote->can_create_moodthemes;
+    my $err;
+    my $mtid = $remote->create_moodtheme( $name, $desc, \$err )
+        or return $self->error( $err );
 
-    my $dbh = LJ::get_db_writer();
-    my $sth = $dbh->prepare("INSERT INTO moodthemes (ownerid, name, des, is_public) VALUES (?, ?, ?, 'N')");
-    $sth->execute($remote->id, $name, $desc);
-
-    my $mtid = $dbh->{'mysql_insertid'};
     return $self->print("Success. Your new mood theme ID is $mtid");
 }
 
diff -r 92e2e6b2f18e -r c7431ec0bc18 htdocs/manage/moodthemes.bml
--- a/htdocs/manage/moodthemes.bml	Fri Jun 25 19:05:56 2010 +0800
+++ b/htdocs/manage/moodthemes.bml	Fri Jun 25 20:41:23 2010 +0800
@@ -310,10 +310,9 @@ body<=
             if ($POST{'isnew'} == 1) {
                 return LJ::bad_input($ML{'.error.nonamegiven'})
                     unless LJ::trim($POST{'name'});
-                my $dbh = LJ::get_db_writer();
-                my $i = $dbh->do("INSERT INTO moodthemes (ownerid, name, is_public) VALUES (?, ?, 'N')", undef, $u->{'userid'}, $POST{'name'});
-                return "<?h1 $ML{'Error'} h1?><?p $ML{'.error.cantcreatetheme'} p?>" unless $i;
-                $themeid = $dbh->{'mysql_insertid'};
+                my $err;
+                $themeid = $u->create_moodtheme( $POST{name}, '', \$err )
+                    or return "<?h1 $ML{'Error'} h1?><?p $err p?>";
                 $info->{'name'} = $POST{'name'};
             }
 
diff -r 92e2e6b2f18e -r c7431ec0bc18 t/console-moodthemes.t
--- a/t/console-moodthemes.t	Fri Jun 25 19:05:56 2010 +0800
+++ b/t/console-moodthemes.t	Fri Jun 25 20:41:23 2010 +0800
@@ -26,8 +26,8 @@ LJ::set_remote($u);
 LJ::set_remote($u);
 ok($run->("moodtheme_list") =~ "Your themes", "Got logged-in stuff.");
 
-is($run->("moodtheme_create blahblah \"my stuff\""),
-   "error: Sorry, your account type doesn't let you create new mood themes");
+is($run->( "moodtheme_create blahblah \"my stuff\""), "error: " .
+   LJ::Lang::ml( '/manage/moodthemes.bml.error.cantcreatethemes' ) );
 local $LJ::T_HAS_ALL_CAPS = 1;
 
 my $resp = $run->("moodtheme_create blahblah \"my stuff\"");
--------------------------------------------------------------------------------