Skip to content

Commit

Permalink
一旦作業内容全部突っ込む
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishibayashi Takuji committed Jan 20, 2012
1 parent 3890a0e commit c57f7a9
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 91 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ META.yml
MYMETA.json
MYMETA.yml
_build/
Build*
!Build.PL
58 changes: 0 additions & 58 deletions Build

This file was deleted.

2 changes: 1 addition & 1 deletion app.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use DBI;
}
my $db_config = Xpost->config->{DBI} || die "Missing configuration for DBI";
builder {
enable 'Log::Minimal';
enable 'Log::Minimal', autodump => 1;
enable 'Plack::Middleware::ReverseProxy';
enable 'Plack::Middleware::Static',
path => qr{^(?:/static/)},
Expand Down
5 changes: 5 additions & 0 deletions config/validator.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
return +{
param => {
email => 'メールアドレス',
username => 'ユーザーID',
password => 'パスワード',
password2 => 'パスワード(確認)',
},
function => {
unique => "その[_1]は既に登録されています",
Expand All @@ -13,6 +16,7 @@
email => "[_1]がEメールアドレスとして正しくありません",
date => "[_1]が日付として正しくありません",
not_sp => "[_1]は半角スペースを含むことができません",
ascii => "[_1]は半角英数字で入力してください",
int => "[_1]は整数で入力してください",
uint => "[_1]は正の整数で入力してください",
decimal => "[_1]は実数で入力してください",
Expand All @@ -24,6 +28,7 @@
http_url => "[_1]がURLとして正しくありません",
email => "[_1]がメールアドレスとして正しくありません",
email_loose => "[_1]がメールアドレスとして正しくありません",
length => "[_1]の長さが正しくありません",
},
message => {
'file.not_null' => ' please upload the file.',
Expand Down
10 changes: 10 additions & 0 deletions lib/Xpost.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package Xpost {

our $VERSION = '0.01';

use DateTime::TimeZone;

# initialize database
sub setup_schema {
my $self = shift;
Expand All @@ -13,10 +15,13 @@ package Xpost {
my $fname = lc("sql/${driver_name}.sql");
open my $fh, '<:encoding(UTF-8)', $fname or die "$fname: $!";
my $source = do { local $/; <$fh> };
require DBIx::QueryLog;
DBIx::QueryLog->disable;
for my $stmt (split /;/, $source) {
next unless $stmt =~ /\S/;
$dbh->do($stmt) or die $dbh->errstr();
}
DBIx::QueryLog->enable;
}

use Xpost::DB;
Expand All @@ -28,5 +33,10 @@ package Xpost {
return $self->{db};
}

sub time_zone {
my $self = shift;
$self->{time_zone} ||= DateTime::TimeZone->new(name => 'Asia/Tokyo');
}

}
1;
7 changes: 4 additions & 3 deletions lib/Xpost/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package Xpost::DB {
use Carp ();
use Data::GUID::URLSafe;
use Data::Validator;
use DateTime;
use Digest::MurmurHash qw/murmur_hash/;
use Scope::Container::DBI;
use Time::Piece::Plus;
use Class::Method::Modifiers;

use Xpost;
Expand All @@ -29,14 +29,15 @@ package Xpost::DB {
return Scope::Container::DBI->connect(shift->conf->{DBI});
}


#各メソッドへのtrigger
#TODO 共通化すべし
before 'insert', 'fast_insert' => sub {
my ($self, $table_name, $row_data) = @_;
my $table = $self->schema->get_table($table_name);
if ($table) {
my @columns = @{$table->columns};
my $now = localtime;
my $now = DateTime->now(time_zone => Xpost->context->time_zone);

#GUID
if(grep /^guid$/, @columns) {
Expand Down Expand Up @@ -65,7 +66,7 @@ package Xpost::DB {
my $table = $self->schema->get_table($table_name);
if ($table) {
my $columns = $table->columns;
my $now = localtime;
my $now = DateTime->now(time_zone => Xpost->context->time_zone);
my @datetime_columns = grep /^updated_(at|on)$/, @$columns;

#updated_at
Expand Down
26 changes: 15 additions & 11 deletions lib/Xpost/DB/Schema.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package Xpost::DB::Schema;
use Time::Piece::Plus;
use DateTime::Format::MySQL;

use Teng::Schema::Declare;
table {
Expand All @@ -16,12 +16,13 @@ table {
inflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return Time::Piece::Plus->parse_mysql_datetime(str => $value);
return DateTime::Format::MySQL->parse_datetime($value)->set_time_zone('Asia/Tokyo');
};
deflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return ref $value ? $value->mysql_datetime : $value;
return $value unless ref $value;
return DateTime::Format::MySQL->format_datetime($value);
};

};
Expand Down Expand Up @@ -54,12 +55,13 @@ table {
inflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return Time::Piece::Plus->parse_mysql_datetime(str => $value);
return DateTime::Format::MySQL->parse_datetime($value)->set_time_zone('Asia/Tokyo');
};
deflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return ref $value ? $value->mysql_datetime : $value;
return $value unless ref $value;
return DateTime::Format::MySQL->format_datetime($value);
};

};
Expand Down Expand Up @@ -89,19 +91,20 @@ table {
inflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return Time::Piece::Plus->parse_mysql_datetime(str => $value);
return DateTime::Format::MySQL->parse_datetime($value)->set_time_zone('Asia/Tokyo');
};
deflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return ref $value ? $value->mysql_datetime : $value;
return $value unless ref $value;
return DateTime::Format::MySQL->format_datetime($value);
};

};

table {
name 'user';
pk 'created_at','id';
pk 'email','id';
columns (
{name => 'profile', type => 12},
{name => 'email_hash', type => 4},
Expand All @@ -110,21 +113,22 @@ table {
{name => 'full_name', type => 12},
{name => 'username', type => 12},
{name => 'email', type => 12},
{name => 'password', type => 12},
{name => 'created_at', type => 11},
{name => 'password', type => 12},
{name => 'password_hash', type => 4},
{name => 'id', type => 4},
);
row_class 'Xpost::Model::User';
inflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return Time::Piece::Plus->parse_mysql_datetime(str => $value);
return DateTime::Format::MySQL->parse_datetime($value)->set_time_zone('Asia/Tokyo');
};
deflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return ref $value ? $value->mysql_datetime : $value;
return $value unless ref $value;
return DateTime::Format::MySQL->format_datetime($value);
};

};
Expand Down
2 changes: 0 additions & 2 deletions lib/Xpost/Model.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ package Xpost::Model {
my ($class, $args) = @_;
my $table = $class->db->schema->get_table($class->table_name);
return unless $table;
debugf(ddf($table));
my @hash_coluns = grep /_hash$/, @{$table->columns};
return unless @hash_coluns;
for my $hash_column (@hash_coluns) {
my ($column) = ($hash_column =~ /^(.+)_hash$/);
warnf($column);
next unless exists $args->{$column};
$args->{$hash_column} = $class->make_hash($args->{$column});
}
Expand Down
28 changes: 22 additions & 6 deletions lib/Xpost/Model/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,47 @@ use warnings;
package Xpost::Model::User {

use String::Random;
use DBIx::QueryLog;

use parent qw/Xpost::Model/;

our $UNATH_PREFIX = '__UNAUTH__';

sub register_email {
state $validator = Data::Validator->new(
email => {isa => 'Str'}
)->with(qw/Method/);
my ($class, $args) = $validator->validate(@_);
my $random_uname = $class->create_initial_username;
$class->fast_insert({%$args, username => $UNATH_PREFIX.$random_uname});
$class->fast_insert({%$args, username => $random_uname});
return $random_uname;
}

sub create_initial_username {
my $class = shift;
state $rand = String::Random->new;
my $random_uname = $rand->randregex('[a-zA-Z0-9]{16}');
my $full_random_uname = $UNATH_PREFIX . $random_uname;
if ($class->db->count('user' => 'id', {username_hash => $class->make_hash($full_random_uname), username => $full_random_uname})) {
my $random_uname = $rand->randregex('[a-zA-Z0-9]{32}');
if ($class->db->count('user' => 'id', {username => $random_uname})) {
$random_uname = $class->create_initial_username;
}
return $random_uname;
}

sub fetch_unauthorized_user {
state $validator = Data::Validator->new(
username => {isa => 'Str'}
)->with(qw/Method/);
my ($class, $args) = $validator->validate(@_);
$class->single({%$args, status => 'unauthorized'});
}

sub register {
state $validator = Data::Validator->new(
username => {isa => 'Str'},
password => {isa => 'Str'},
)->with(qw/Method AllowExtra/);
my ($self, $args) = $validator->validate(@_);
$self->update({%$args, status => 'registered'});
return $self->refetch;
}

}
1;
14 changes: 8 additions & 6 deletions lib/Xpost/Script/SchemaDumper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sub data_section { Data::Section::Simple->new(__PACKAGE__)->get_data_section }

sub run {
my ($class, $c) = @_;
my $dbi = Xpost::DB->get_dbi(type => 'master');
my $dbi = Xpost::DB->get_dbh(type => 'master');
my $schema = Teng::Schema::Dumper->dump(
dbh => $dbi,
namespace => 'Xpost::DB',
Expand Down Expand Up @@ -94,24 +94,26 @@ __DATA__
inflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return Time::Piece::Plus->parse_mysql_datetime(str => $value);
return DateTime::Format::MySQL->parse_datetime($value)->set_time_zone('Asia/Tokyo');
};
deflate qr{_at$} => sub {
my $value = shift;
return unless $value;
return ref $value ? $value->mysql_datetime : $value;
return $value unless ref $value;
return DateTime::Format::MySQL->format_datetime($value);
};
: }
:if $date_exists {
inflate qr{_on$} => sub {
my $value = shift;
return unless $value;
return Time::Piece::Plus->parse_mysql_date(str => $value);
return DateTime::Format::MySQL->parse_date($value)->set_time_zone('Asia/Tokyo');
};
deflate qr{_on$} => sub {
my $value = shift;
return unless $value;
return ref $value ? $value->mysql_date : $value;
return $value unless ref $value;
return DateTime::Format::MySQL->format_date($value);
};
: }
Expand All @@ -126,4 +128,4 @@ use parent qw/Xpost::Model/;
1;
@@ header
use Time::Piece::Plus;
use DateTime::Format::MySQL;
1 change: 1 addition & 0 deletions lib/Xpost/Util/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package Xpost::Util::Template {
number
radio
check
password
/;

no Mouse::Util::TypeConstraints;
Expand Down
5 changes: 4 additions & 1 deletion lib/Xpost/Validator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ package Xpost::Validator {
$self->SUPER::check(@_);
return if $self->has_error;
$self->{valid_params} = {};
$self->{valid_params}->{$_} = 1 for (keys %args);
for my $key (keys %args) {
next if ref $key;
$self->{valid_params}->{$key} = 1;
}
}


Expand Down
Loading

0 comments on commit c57f7a9

Please sign in to comment.