Skip to content

Commit

Permalink
Merge branch 'release/0.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Sep 11, 2011
2 parents 7834a4e + cb70b0e commit 6681644
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ logs
/db
/share

SETUP.bat
SETUP.bat
Dwimmer-*
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
v0.11 2011.09.10

Release of the first interesting version.

5 changes: 4 additions & 1 deletion MANIFEST.SKIP
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ MYMETA\.(?:yaml|json)$
\bsessions
.gitignore
\bdb\b
\bshare\b

# need to include the share directory as well as apparently
# Module::Install is only installing stuff listed in the MANIFEST file.
# \bshare\b
22 changes: 21 additions & 1 deletion lib/Dwimmer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Dancer ':syntax';

use 5.008005;

our $VERSION = '0.01';
our $VERSION = '0.11';

use Dwimmer::DB;
use Dwimmer::Tools qw(_get_db _get_site);
Expand Down Expand Up @@ -45,3 +45,23 @@ get qr{^/([a-zA-Z0-9]\w*)?$} => \&route_index;


true;

=head1 NAME
Dwimmer - A platform to develop things
=head1 COPYRIGHT
(c) 2011 Gabor Szabo
=head1 LICENSE
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl 5 itself.
=cut

# Copyright 2011 Gabor Szabo
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.
24 changes: 22 additions & 2 deletions lib/Dwimmer/Admin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Dancer ':syntax';

use 5.008005;

our $VERSION = '0.01';
our $VERSION = '0.11';

use Data::Dumper qw(Dumper);
use Email::Valid ();
Expand All @@ -12,7 +12,7 @@ use String::Random ();
use Template ();

use Dwimmer::DB;
use Dwimmer::Tools qw(sha1_base64 _get_db _get_site save_page);
use Dwimmer::Tools qw(sha1_base64 _get_db _get_site save_page create_site);


sub include_session {
Expand Down Expand Up @@ -48,6 +48,11 @@ sub render_response {
sub get_page_data {
my ($site, $path, $revision) = @_;

# make it easy to deploy in CGI environment.
if ($path eq '/index' or $path eq '/index.html') {
$path = '/';
}

my $db = _get_db();
my $cpage = $db->resultset('Page')->find( {siteid => $site->id, filename => $path} );
return if not $cpage;
Expand Down Expand Up @@ -292,6 +297,21 @@ get '/get_pages.json' => sub {
};


post '/create_site.json' => sub {
my %args;
foreach my $field ( qw(name) ) {
$args{$field} = params->{$field} || '';
trim($args{$field});
}

return to_json {error => 'missing_name' } if not $args{name};

create_site($args{name}, $args{name}, session->{userid});

return to_json { success => 1 };
};


###### helper methods


Expand Down
9 changes: 8 additions & 1 deletion lib/Dwimmer/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ has host => (is => 'ro', isa => 'Str', required => 1);
has mech => (is => 'rw', isa => 'WWW::Mechanize', default => sub { WWW::Mechanize->new } );


our $VERSION = '0.01';
our $VERSION = '0.11';

sub login {
my ($self, $username, $password) = @_;
Expand Down Expand Up @@ -90,5 +90,12 @@ sub get_history {
return from_json $m->content;
}

sub create_site {
my ($self, %args) = @_;
my $m = $self->mech;
$m->post( $self->host . "/_dwimmer/create_site.json", \%args );
return from_json $m->content;
}

1;

43 changes: 41 additions & 2 deletions lib/Dwimmer/Tools.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use YAML;

use Dwimmer::DB;

our @EXPORT_OK = qw(sha1_base64 _get_db _get_site save_page);
our $VERSION = '0.11';

our @EXPORT_OK = qw(sha1_base64 _get_db _get_site save_page create_site);

our $dbfile;

Expand All @@ -29,13 +31,28 @@ sub _get_site {

# based on hostname?
my $host = request->host;
if ($host =~ /^([\w-]+)\./) {

# development and testing:
if ($host =~ /^localhost:\d+$/) {
$host = 'www';
}

if (params->{_dwimmer}) {
$host = params->{_dwimmer};
}
if ($host =~ /^([\w.-]+)/) {
$site_name = $1;
}

my $db = _get_db();
my $site = $db->resultset('Site')->find( { name => $site_name } );

# for now, let's default to www if the site isn't in the database
if (not $site) {
$site_name = 'www';
$site = $db->resultset('Site')->find( { name => $site_name } );
}

return ($site_name, $site);
}

Expand Down Expand Up @@ -88,4 +105,26 @@ sub save_page {
return to_json { success => 1 };
};

sub create_site {
my ($hostname, $title, $ownerid) = @_;

my $time = time;

my $db = _get_db();

my $site = $db->resultset('Site')->create({
name => $hostname, owner => $ownerid, creation_ts => $time
});

save_page($site, {
create => 1,
editor_title => 'Welcome to your Dwimmer installation',
editor_body => "<h1>Welcome to $title</h1>",
author => $ownerid,
filename => '/',
});

return;
}

1;
2 changes: 1 addition & 1 deletion public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ font-size: 10px;
z-index:100;
color:#735005;
background-color:#F4A83D;
border-bottom:3px solid #222;
border-bottom:0px solid #222;
/* margin-bottom: 10px; */
padding: 0px 0;
}
Expand Down
25 changes: 24 additions & 1 deletion public/javascripts/dwimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ var original_content; // to make editor cancellation quick
$('#guest_bar').hide();
$('#manage-bar').hide();
$('#manage-bar > div').hide();
$('#admin').height(0);

$.getJSON('/_dwimmer/session.json', function(resp) {
if (resp["logged_in"] == 1) {
$('#admin').height("35px");
$('#logged_in_bar').show();
$("#logged-in").html(resp["username"]);
username = resp["username"];
userid = resp["userid"];
} else {
} else if (window.location.href.indexOf('?_dwimmer') > 0) {
$('#admin').height("35px");
$('#guest_bar').show();
}
});
Expand Down Expand Up @@ -119,6 +122,26 @@ var original_content; // to make editor cancellation quick
return false;
});

$(".create_site").click(function(){
manage_bar();
original_content = $('#content').html();
$('#admin-create-site').show();

return false;
});
$("#create-site-form").submit(function() {
var url = "/_dwimmer/create_site.json";
$.post(url, $(this).serialize(), function(resp) {
if (resp["success"] == 1) {
alert('added');
} else {
alert(resp["error"]);
}
}, 'json');
return false;
});


$(".list_pages").click(function(){
manage_bar();
$.getJSON('/_dwimmer/get_pages.json', function(resp) {
Expand Down
53 changes: 53 additions & 0 deletions t/014_new_site.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use strict;
use warnings;

use t::lib::Dwimmer::Test qw(start $admin_mail @users);

use Cwd qw(abs_path);
use Data::Dumper qw(Dumper);

my $password = 'dwimmer';

my $run = start($password);

eval "use Test::More";
eval "use Test::Deep";
require Test::WWW::Mechanize;
plan(skip_all => 'Unsupported OS') if not $run;

my $url = "http://localhost:$ENV{DWIMMER_PORT}";

plan(tests => 4);


use Dwimmer::Client;
my $user = Dwimmer::Client->new( host => $url );
#$user->register

my $admin = Dwimmer::Client->new( host => $url );
is_deeply($admin->login( 'admin', $password ), {
success => 1,
username => 'admin',
userid => 1,
logged_in => 1,
}, 'login success');

# just to make sure we don't have the same default page
# is_deeply($admin->save_page(
# body => 'New text [link] here',
# title => 'New main title',
# filename => '/',
# ), { success => 1 }, 'save_page');

is_deeply($admin->create_site( name => 'foobar' ), {
success => 1,
}, 'create_site foobar');

# fetch main page of new site
#$admin->switch_host( fake => 'foobar' );
#$admin->get_page('/');

my $w = Test::WWW::Mechanize->new;
$w->get_ok("$url/?_dwimmer=foobar"); # faking hostname
$w->content_like( qr{Welcome to foobar}, 'content ok' ) or diag($w->content);

8 changes: 8 additions & 0 deletions views/layouts/main.tt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<li><a class="add_page" href="">add a new page</a></li>
<li><a class="list_pages" href="">list pages</a></li>
<li><a class="show_history" href="">show history</a></li>
<li><a class="create_site" href="">create site</a></li>
</ul>
</div>
</li>
Expand Down Expand Up @@ -96,6 +97,13 @@ Set verified bit <input type="radio" name="verify" value="verified" />
</form>
</div>

<div id="admin-create-site">
<form id="create-site-form" action="/_dwimmer/create_site.json" method="POST">
Hostname: <input name="name" size="30" />
<input type="submit" id="create-site" value="Create Site" />
</form>
</div>

</div>

<div id="content">
Expand Down

0 comments on commit 6681644

Please sign in to comment.