source: server/trunk/web/lib/Sophie/Controller/Admin.pm @ 104

Last change on this file since 104 was 104, checked in by nanardon, 14 years ago
  • add admin/replace_path function
File size: 7.7 KB
Line 
1package Sophie::Controller::Admin;
2use Moose;
3use namespace::autoclean;
4use YAML qw/freeze thaw/;
5
6BEGIN {extends 'Catalyst::Controller'; }
7
8=head1 NAME
9
10Sophie::Controller::Admin - Catalyst Controller
11
12=head1 DESCRIPTION
13
14Catalyst Controller.
15
16=head1 METHODS
17
18=cut
19
20=head2 index
21
22=cut
23
24sub index :Path :Args(0) {
25    my ( $self, $c ) = @_;
26
27    $c->response->body('Matched Sophie::Controller::Admin in Admin.');
28}
29
30sub create :XMLRPC {
31    my ( $self, $c, $distribution, $version, $arch ) = @_;
32
33    my $rs = $c->model('Base')->resultset('Distribution');
34    my $rs_d = $rs->find_or_create({ name => $distribution}) or do {
35        $c->stash->{xmlrpc} = 'Erreur adding distrib';
36        return;
37    };
38
39    my $rs_r = $rs_d->Release->find_or_create({ version => $version, }) or do {
40        $c->stash->{xmlrpc} = 'Erreur adding release';
41        return;
42    };
43
44    my $rs_a = $rs_r->Arch->find_or_create({ arch => $arch }) or do {
45        $c->stash->{xmlrpc} = 'Erreur adding arch';
46        return;
47    };
48
49    $c->stash->{xmlrpc} = 'Ok';
50
51    $c->model('Base')->storage->dbh->commit;
52
53}
54
55sub add_media :XMLRPC {
56    my ( $self, $c, $distribspec, $mediaspec) = @_;
57
58    my $d = $c->model('Base')->resultset('Distribution')
59        ->search(name => $distribspec->{distribution})
60        ->search_related('Release', version => $distribspec->{release})
61        ->search_related('Arch', arch => $distribspec->{arch})->next;
62    if ($d) {
63        my $new = my $rs = $c->model('Base')->resultset('Medias')
64            ->update_or_create({
65                %{ $mediaspec },
66                Arch => $d,
67            },
68            { key => 'label' }
69        );
70        if ($new) {
71            $c->stash->{xmlrpc} = 'OK';
72            $c->model('Base')->storage->dbh->commit;
73        } else {
74            $c->stash->{xmlrpc} = 'Erreur adding media';
75        }
76    }
77}
78
79sub list_path :XMLRPC {
80    my ($self, $c, $distribution, $version, $arch, $media) = @_;
81   
82    if (ref $distribution) {
83        ($distribution, $version, $arch, $media) = 
84        (
85            $distribution->{distribution},
86            $distribution->{release},
87            $distribution->{arch},
88            $version,
89        );
90    }
91
92    $c->stash->{xmlrpc}  = [
93    $c->model('Base')->resultset('Distribution')
94        ->search($distribution ? (name => $distribution) : ())
95        ->search_related('Release', $version ? (version => $version) : ())
96        ->search_related('Arch', $arch ? (arch => $arch) : ())
97        ->search_related('Medias', $media ? (label => $media) : ())
98        ->search_related('MediasPaths')
99        ->search_related('Paths')->get_column('path')
100        ->all ];
101}
102
103sub media_path :XMLRPC {
104    my ( $self, $c, $distribution, $version, $arch, $label, $path ) = @_;
105
106    if (ref $distribution) {
107        ($distribution, $version, $arch, $label, $path) = 
108        (
109            $distribution->{distribution},
110            $distribution->{release},
111            $distribution->{arch},
112            $version,
113            $arch,
114        );
115    }
116
117    $path =~ s/\/*$//;
118    $path =~ s/\/+/\//g;
119
120    my $med = $c->model('Base')->resultset('Distribution')
121        ->search(name => $distribution)
122        ->search_related('Release', version => $version)
123        ->search_related('Arch', arch => $arch)
124        ->search_related('Medias', label => $label)->next or return;
125
126    my $rspath = $c->model('Base')->resultset('Paths')
127        ->find_or_create({ path => $path }) or do {
128    };
129    my $new = $c->model('Base')->resultset('MediasPaths')->new({
130            Medias => $med,
131            Paths =>  $rspath,
132        });
133    $new->insert;
134
135    $c->model('Base')->storage->dbh->commit;
136}
137
138sub media_remove_path :XMLRPC {
139    my ( $self, $c, $distribution, $version, $arch, $label, $path ) = @_;
140
141    if (ref $distribution) {
142        ($distribution, $version, $arch, $label, $path) = 
143        (
144            $distribution->{distribution},
145            $distribution->{release},
146            $distribution->{arch},
147            $version,
148            $arch,
149        );
150    }
151
152    $path =~ s/\/*$//;
153    $path =~ s/\/+/\//g;
154
155    my $med = $c->model('Base')->resultset('Distribution')
156        ->search(name => $distribution)
157        ->search_related('Release', version => $version)
158        ->search_related('Arch', arch => $arch)
159        ->search_related('Medias', label => $label)->find or return;
160
161    my $rspath = $c->model('Base')->resultset('Paths')
162        ->find({ path => $path }) or do {
163            return;
164    };
165    my $new = $c->model('Base')->resultset('MediasPaths')->search({
166            d_media => $med->d_media_key,
167            d_path =>  $rspath->d_path_key,
168        })->next->delete;
169
170    $c->model('Base')->storage->dbh->commit;
171}
172
173sub ls_local : XMLRPC {
174    my ($self, $c, $path) = @_;
175
176    $c->stash->{xmlrpc} = [ <$path*> ];
177}
178
179sub replace_path : XMLRPC {
180    my ($self, $c, $path, $newpath) = @_;
181
182    my $dpath = $c->model('Base::Paths')->find({
183        path => $path,
184    }) or do {
185        return $c->stash->{xmlrpc} = 'Path not found';
186    };
187
188    $newpath =~ s/\/*$//;
189
190    $dpath->update(
191        {
192            updated => undef,
193            path => $newpath,
194        }
195    ) and $c->model('Base')->storage->dbh->commit;
196    return $c->stash->{xmlrpc} = 'OK';
197}
198
199sub dump_distrib : XMLRPC {
200    my ($self, $c, $distribution, $version, $arch) = @_;
201   
202    if (!ref $distribution) {
203        $distribution = {
204            distribution => $distribution,
205            release => $version,
206            arch => $arch,
207        };
208    }
209
210    my $ref = {
211        distrib => $distribution,
212    };
213
214    $ref->{media} = $c->forward('/distrib/struct', [ $distribution ]);
215
216    foreach (@{ $ref->{media} || []}) {
217        warn $_->{label};
218        $ref->{path}{$_->{label}} = $c->forward('list_path', [ $distribution,
219                $_->{label} ]);
220    }
221
222    $c->stash->{xmlrpc} = freeze($ref);
223}
224
225sub clean_distrib : XMLRPC {
226    my ($self, $c, $distribution, $version, $arch) = @_;
227   
228    if (!ref $distribution) {
229        $distribution = {
230            distribution => $distribution,
231            release => $version,
232            arch => $arch,
233        };
234    }
235
236    my $rsdist = $c->model('Base')->resultset('Distribution')
237        ->search(name => $distribution->{distribution})
238        ->search_related('Release', version => $distribution->{release})
239        ->search_related('Arch', arch => $distribution->{arch})
240        ->search_related('Medias');
241
242    my $new = $c->model('Base')->resultset('MediasPaths')->search({
243            d_media => { IN => $rsdist->get_column('d_media_key')->as_query },
244        })->delete;
245
246    # $c->model('Base')->storage->dbh->rollback;
247   
248}
249
250sub load_distrib : XMLRPC {
251    my ( $self, $c, $dump ) = @_;
252
253    my $ref = thaw($dump);
254
255    warn keys %{$ref->{path}};
256
257    $c->forward('clean_distrib', [ $ref->{distrib} ]);
258
259    $c->forward('create', [ 
260            $ref->{distrib}{distribution},
261            $ref->{distrib}{release},
262            $ref->{distrib}{arch},
263        ]);
264
265    foreach my $media (@{ $ref->{media} || []}) {
266        $c->forward('add_media', [ $ref->{distrib}, $media ]);
267    }
268    foreach my $media (keys %{ $ref->{path} || [] }) {
269        foreach my $path (@{ $ref->{path}{$media} || [] }) {
270            $c->forward('media_path', [ $ref->{distrib}, $media, $path ]);
271        }
272    }
273
274    #$c->model('Base')->storage->dbh->rollback;
275}
276
277sub set_user_data : XMLRPC {
278    my ( $self, $c, $user, $dataname, $data ) = @_;
279    $c->forward('/user/set_user_data', [ $user, $dataname, $data ]);
280}
281
282sub update_user_data : XMLRPC {
283    my ( $self, $c, $user, $dataname, $data ) = @_;
284    $c->forward('/user/update_user_data', [ $user, $dataname, $data ]);
285}
286
287=head1 AUTHOR
288
289Olivier Thauvin
290
291=head1 LICENSE
292
293This library is free software. You can redistribute it and/or modify
294it under the same terms as Perl itself.
295
296=cut
297
298__PACKAGE__->meta->make_immutable;
299
3001;
Note: See TracBrowser for help on using the repository browser.