Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #173 from jbarrett/translation-system-improvements
Browse files Browse the repository at this point in the history
Initial translation system improvements
  • Loading branch information
jbarrett committed Jun 25, 2014
2 parents 8b57360 + 69d5ae1 commit 3f1e1c9
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 115 deletions.
5 changes: 5 additions & 0 deletions lib/DDGC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ sub _build_xslate {
mark_raw(substr($_[0],0,-2).'<i>'.substr($_[0],-2).'</i>')
},

hilight_token_placeholders => sub {
$_[0] =~ s{(%\d?\$?[s|d])}{<span class="hilight-token">$1</span>}g;
return mark_raw($_[0]);
},

# String::Truncate's elide function
truncate => \&elide,

Expand Down
6 changes: 3 additions & 3 deletions lib/DDGC/DB/Result/Screenshot/Token.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package DDGC::DB::Result::Screenshot::Token;
# ABSTRACT: A screenshot on a thread
# ABSTRACT: A screenshot on a token

use Moose;
use MooseX::NonMoose;
Expand All @@ -8,9 +8,9 @@ use DBIx::Class::Candy;
use DateTime::Format::RSS;
use namespace::autoclean;

table 'screenshot_thread';
table 'screenshot_token';

sub u { shift->thread->u(@_) }
sub u { shift->token->u(@_) }

column id => {
data_type => 'bigint',
Expand Down
6 changes: 6 additions & 0 deletions lib/DDGC/DB/Result/Token.pm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ column updated => {
set_on_update => 1,
};

column retired => {
data_type => 'tinyint',
is_nullable => 0,
default_value => 0,
};

belongs_to 'token_domain', 'DDGC::DB::Result::Token::Domain', 'token_domain_id';

has_many 'token_languages', 'DDGC::DB::Result::Token::Language', 'token_id';
Expand Down
11 changes: 8 additions & 3 deletions lib/DDGC/DB/Result/Token/Domain/Language.pm
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,14 @@ sub search_tokens {

sub untranslated_tokens {
my ( $self, $page, $pagesize ) = @_;
$self->_get_token_languages(1, $page, $pagesize, {
'token_language_translations.id' => undef,
},{
$self->_get_token_languages(1, $page, $pagesize,
{ 'token_language.id' => {
-not_in => $self->result_source->schema->resultset('Token::Language::Translation')->search({
check_result => '1',
})->get_column('token_language_id')->as_query,
},
},
{
join => 'token_language_translations',
});
}
Expand Down
6 changes: 6 additions & 0 deletions lib/DDGC/DB/Result/Token/Language.pm
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ column updated => {
set_on_update => 1,
};

column fuzzy => {
data_type => 'tinyint',
is_nullable => 0,
default_value => 0,
};

belongs_to 'token', 'DDGC::DB::Result::Token', 'token_id', {
on_delete => 'cascade',
};
Expand Down
13 changes: 11 additions & 2 deletions lib/DDGC/DB/ResultSet/Token/Language.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ sub untranslated {
my ( $self, $token_domain_id, $language_id, $scalar_ignore_ids ) = @_;
my @ignore_ids = $scalar_ignore_ids ? @{$scalar_ignore_ids} : ();
$self->search({
'token_language_translations.id' => undef,
'token_domain_id' => $token_domain_id,
'language_id' => $language_id,
($self->me.'id') => { -not_in => \@ignore_ids },
-and => [
'me.id' => { -not_in => \@ignore_ids },
-or => [
'me.id' => { -not_in =>
$self->ddgc->rs('Token::Language::Translation')->search({
check_result => '1',
},)->get_column('token_language_id')->as_query,
},
fuzzy => 1,
],
],
},{
join => [ {
token_language_translations => 'token_language_translation_votes'
Expand Down
7 changes: 2 additions & 5 deletions lib/DDGC/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extends 'Catalyst';
use DDGC::Config;
use Class::Load ':all';
use Digest::MD5 qw( md5_hex );
use Data::UUID;

use DDGC::Web::Wizard::Unvoted;
use DDGC::Web::Wizard::Untranslated;
Expand Down Expand Up @@ -118,11 +119,7 @@ sub d {
sub ddgc { shift->d(@_) }

sub next_form_id {
my ( $c ) = @_;
my $last_id = $c->session->{last_form_id} || int(rand(1_000_000));
my $next_id = $last_id + int(rand(1_000));
$c->session->{last_form_id} = $next_id;
return $next_id;
Data::UUID->new->create_str;
}

sub set_new_action_token {
Expand Down
67 changes: 16 additions & 51 deletions lib/DDGC/Web/Controller/Forum/My.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ sub newthread : Chained('base') Args(1) {
$c->req->params->{content},
forum => $c->stash->{forum_index},
title => $c->req->params->{title},
defined $c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots}
? ( screenshot_ids => $c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots} )
defined $c->session->{thread_forms}->{$c->stash->{form_id}}->{screenshots}
? ( screenshot_ids => $c->session->{thread_forms}->{$c->stash->{form_id}}->{screenshots} )
: (),
);
$c->response->redirect($c->chained_uri(@{$thread->u}));
Expand All @@ -56,55 +56,20 @@ sub newthread : Chained('base') Args(1) {

sub thread_form {
my ( $self, $c ) = @_;
$c->stash->{thread_form_id} = $c->req->param('thread_form_id') || $c->next_form_id;
$c->stash->{form_id} = $c->req->param('form_id') || $c->next_form_id;
$c->session->{thread_forms} = {} unless defined $c->session->{thread_forms};
$c->session->{thread_forms}->{$c->stash->{thread_form_id}} = {}
unless defined $c->session->{thread_forms}->{$c->stash->{thread_form_id}};
if ($c->req->param('screenshot')) {
my $upload = $c->req->uploads->{screenshot};
my $media = $c->d->rs('Media')->create_via_file($c->user->db, $upload->tempname,{
upload_filename => $upload->filename,
content_type => $upload->type
});
my $screenshot = $c->d->rs('Screenshot')->create({
media_id => $media->id,
});
$c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots} = []
unless defined $c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots};
push @{$c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots}},
$screenshot->id;
$c->stash->{x} = {
screenshot_id => $screenshot->id,
media_url => $media->url,
};
$c->forward('View::JSON');
} elsif ($c->req->param('delete_screenshot')) {
my $delete_id = $c->req->param('delete_screenshot');
$c->stash->{x} = {
screenshot_id => $delete_id
};
my $screenshot = $c->d->rs('Screenshot')->find($delete_id);
$screenshot->media->delete
if $screenshot->media->users_id == $c->user->id || $c->user->admin;
my @old_ids = @{$c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots}};
my @new_ids;
for (@old_ids) {
push @new_ids, $_ unless $_ == $delete_id;
}
$c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots} = [@new_ids];
$c->forward('View::JSON');
} else {
my @screenshot_ids;
if (defined $c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots}) {
@screenshot_ids = @{$c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots}};
} elsif (defined $c->stash->{thread}) {
@screenshot_ids = $c->stash->{thread}->sorted_screenshots->ids;
$c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots} = [@screenshot_ids];
}
$c->stash->{screenshots} = $c->d->rs('Screenshot')->search({
id => { -in => [@screenshot_ids] },
});
$c->session->{thread_forms}->{$c->stash->{form_id}} = {}
unless defined $c->session->{thread_forms}->{$c->stash->{form_id}};
my @screenshot_ids;
if (defined $c->session->{thread_forms}->{$c->stash->{form_id}}->{screenshots}) {
@screenshot_ids = @{$c->session->{thread_forms}->{$c->stash->{form_id}}->{screenshots}};
} elsif (defined $c->stash->{thread}) {
@screenshot_ids = $c->stash->{thread}->sorted_screenshots->ids;
$c->session->{thread_forms}->{$c->stash->{form_id}}->{screenshots} = [@screenshot_ids];
}
$c->stash->{screenshots} = $c->d->rs('Screenshot')->search({
id => { -in => [@screenshot_ids] },
});
}

sub thread : Chained('base') CaptureArgs(1) {
Expand Down Expand Up @@ -143,8 +108,8 @@ sub edit : Chained('thread') Args(0) {
$c->stash->{thread}->comment->content($c->req->params->{content});
$c->stash->{thread}->comment->update;
my @screenshot_ids;
if (defined $c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots}) {
@screenshot_ids = @{$c->session->{thread_forms}->{$c->stash->{thread_form_id}}->{screenshots}};
if (defined $c->session->{thread_forms}->{$c->stash->{form_id}}->{screenshots}) {
@screenshot_ids = @{$c->session->{thread_forms}->{$c->stash->{form_id}}->{screenshots}};
}
$c->stash->{thread}->screenshot_threads->search_rs({
screenshot_id => { -not_in => [@screenshot_ids] },
Expand Down
56 changes: 56 additions & 0 deletions lib/DDGC/Web/Controller/Screenshot.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package DDGC::Web::Controller::Screenshot;
# ABSTRACT: Screenshot management

use Moose;
BEGIN { extends 'Catalyst::Controller'; }

sub base : Chained('/base') PathPart('screenshot') CaptureArgs(0) {
my ( $self, $c ) = @_;
if (!$c->user) {
$c->response->redirect($c->chained_uri('My','login'));
return $c->detach;
}
}

sub manage : Chained('base') Args(1) {
my ( $self, $c, $form_id ) = @_;
if ($c->req->param('screenshot')) {
my $upload = $c->req->uploads->{screenshot};
my $media = $c->d->rs('Media')->create_via_file($c->user->db, $upload->tempname,{
upload_filename => $upload->filename,
content_type => $upload->type
});
my $screenshot = $c->d->rs('Screenshot')->create({
media_id => $media->id,
});
$c->session->{thread_forms}->{$form_id}->{screenshots} = [] unless defined $c->session->{thread_forms}->{$form_id}->{screenshots};
push @{$c->session->{thread_forms}->{$form_id}->{screenshots}}, $screenshot->id;
$c->stash->{x} = {
screenshot_id => $screenshot->id,
media_url => $media->url,
};
$c->forward('View::JSON');
}
elsif ($c->req->param('delete_screenshot')) {
my $delete_id = $c->req->param('delete_screenshot');
$c->stash->{x} = {
screenshot_id => $delete_id,
};
my $screenshot = $c->d->rs('Screenshot')->find($delete_id);
if ($screenshot->media->users_id == $c->user->id || $c->user->admin) {
$screenshot->media->delete;
}
else {
$c->res->code(403);
$c->stash->{x} = {
error => 'Forbidden',
};
return $c->forward('View::JSON');
}
@{$c->session->{thread_forms}->{$form_id}->{screenshots}} = grep { $_ != $delete_id } @{$c->session->{thread_forms}->{$form_id}->{screenshots}};
$c->forward('View::JSON');
}
}

no Moose;
__PACKAGE__->meta->make_immutable;
10 changes: 8 additions & 2 deletions lib/DDGC/Web/Controller/Translate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,14 @@ sub domain :Chained('logged_in') :PathPart('') :CaptureArgs(1) {
$c->stash->{token_domain_languages_rs} = $token_domain_language_rs->search({},{
'+columns' => {
token_languages_undone_count => $c->d->rs('Token::Language')->search({
'token_language_translations.id' => undef,
'undone_count.token_domain_language_id' => { -ident => 'me.id' },
-and => [
'undone_count.id' => { -not_in =>
$c->ddgc->rs('Token::Language::Translation')->search({
check_result => '1',
},)->get_column('token_language_id')->as_query,
},
'undone_count.token_domain_language_id' => { -ident => 'me.id' },
],
},{
join => 'token_language_translations', alias => 'undone_count'
})->count_rs->as_query,
Expand Down
6 changes: 5 additions & 1 deletion root/static/css/translate.css
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@
font-size: 16px;
}
}

span.hilight-token {
color: #990000;
}

@media only screen and (max-width: 40em) {
.translate-overview .half.progress { width: 100%; clear: both; }
Expand All @@ -235,4 +239,4 @@
}
@media only screen and (max-width: 30em) {
.translate-comments.content-box .row { padding-left: 8px; }
}
}
48 changes: 2 additions & 46 deletions templates/forum/my/thread_form.tx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<: } :>
<form action="<: if $thread.id { $u('Forum::My','edit',$thread.id) } else { $u('Forum::My','newthread',$forum_index) } :>" method="POST">
<input type="hidden" name="action_token" value="<: $action_token :>">
<input type="hidden" name="thread_form_id" value="<: $thread_form_id :>"/>
<input type="hidden" name="form_id" value="<: $form_id :>"/>
<div class="input-wrap"><input autocomplete="off" value="<: $thread.title || $c.req.params.title :>" type="text" name="title" placeholder="Title" autofocus></input></div>
<div id="suggestions" class="group" style="white-space:nowrap"></div>
<div class="input-wrap"><textarea name="content" class="js-bbcode" placeholder="Body" rows="15"><: $thread.comment.content || $c.req.params.content :></textarea></div>
Expand Down Expand Up @@ -60,50 +60,6 @@
}

$('input[name="title"]').keypress(suggest);
$(function(){

new Dropzone("#screenshots", {
url: "<: if $thread.id { $u('Forum::My','edit',$thread.id, { thread_form_id => $thread_form_id }) } else { $u('Forum::My','newthread',$forum_index, { thread_form_id => $thread_form_id }) } :>",
paramName: 'screenshot',
maxFilesize: '2',
addRemoveLinks: true,
acceptedFiles: 'image/*',
removedfile: function(file){
if (file.screenshot_id) {
$.post(
"<: if $thread.id { $u('Forum::My','edit',$thread.id, { thread_form_id => $thread_form_id }) } else { $u('Forum::My','newthread',$forum_index, { thread_form_id => $thread_form_id }) } :>",
{ delete_screenshot: file.screenshot_id },
function(){
file.previewElement.remove();
}
).fail(function(){
alert('Delete failed');
});
} else {
file.previewElement.remove();
}
},
init: function() {

<: if $screenshots { :>
<: for results($screenshots) -> $screenshot { :>
var mockFile<: $screenshot.id :> = {
name: "<: $screenshot.upload_filename :>",
screenshot_id: <: $screenshot.id :>,
media_url: "<: $screenshot.media.url :>",
};
this.options.addedfile.call(this, mockFile<: $screenshot.id :>);
this.options.thumbnail.call(this, mockFile<: $screenshot.id :>, '<: $screenshot.media.url_thumbnail :>');
<: } :>
<: } :>

this.on("success", function(file, response) {
file.screenshot_id = response.screenshot_id;
file.media_url = response.media_url;
});
}
});

});

</script>
<: i('screenshot') :>
Loading

0 comments on commit 3f1e1c9

Please sign in to comment.