Permalink
Browse files

Add basic staff API for searching etc

  • Loading branch information...
1 parent 178ff51 commit 1f60bd4bbe83ae7d5c92838196612d45d8af05f2 @abh committed Apr 9, 2012
View
2 README
@@ -52,6 +52,8 @@ $CBROOT/bin/database_update ntppool
$CBROOT/bin/database_update combust
$CBROOT/bin/database_update ntppool
+make templates
+
$CBROOT/bin/httpd
If any modules are missing, the easiest way to install them is with
@@ -0,0 +1,18 @@
+<ul>
+{{#users}}
+ <li>{{username}} {{email}} {{name}} ({{id}})<br>
+ <ol>
+ {{#servers}}
+ <li><a href="/scores/{{ip}}">
+ {{{ip_display}}}
+ </a> ({{score}}, {{netspeed_human}})
+ {{#deletion_on}}<span class="badge badge-warning">{{deletion_on}}</span>{{/deletion_on}}
+ {{#zones}}
+ {{.}}
+ {{/zones}}
+ </li>
+
+ {{/servers}}
+ </ol>
+ </li>
+{{/users}}
View
@@ -0,0 +1,23 @@
+[% page.js = BLOCK %]
+[% page.js %]
+[% PROCESS tpl/admin_js.html %]
+[% END %]
+
+<link rel="stylesheet" type="text/css" href="[% combust.static_url("cdn/libs/bootstrap/2/css/bootstrap.min.css") %]">
+
+
+[% PROCESS tpl/manage/navigation.html %]
+
+<div id="admin" class="block">
+ <h3>Manage NTP Pool</h3>
+
+ <form id="search_form" class="well form-search">
+ <input class="input-large search-query" id="q" type="text" name="q">
+ <button type="submit" class="btn">Search</button>
+ </form>
+
+ <div id="users">
+
+ </div>
+
+</div>
Submodule cdn updated from 3b7c3f to b76153
@@ -0,0 +1,62 @@
+/* Copyright 2012 Ask Bjørn Hansen, Develooper LLC */
+/*jshint jquery:true browser:true */
+/*global _:true, templates:true */
+
+if(!1 in window)window.console={log:function(){}};
+else if(!console)var console={log:function(){}};
+
+if (!NP) { var NP = {}; }
+
+(function ($) {
+ "use strict";
+ $(document).ready(function() {
+ $('#search_form').submit(function(e) {
+ e.preventDefault();
+ var q = $(this).find('input:first').val();
+ $('#users').html("Loading ...");
+ $.post('/api/staff/search',
+ { q: q },
+ function(r) {
+ var reg = new RegExp(q, 'gi');
+ _.each(r.users, function(user) {
+ _.each(user.servers, function(server) {
+ server.ip_display = server.ip;
+ server.ip_display = server.ip_display.replace(reg,
+ function(str) {
+ console.log("STR", str);
+ return '<b>' + str + '</b>';
+ }
+ );
+ });
+ });
+ $('#users').html( templates.users.render( r ));
+ }, 'json'
+ );
+ });
+
+ var zone_list = $('#zone_list');
+
+ if (zone_list) {
+ zone_list.editable('/api/staff/server_zones', {
+ submit_data: {
+ auth_token: NP.token,
+ server: zone_list.data('server-ip')
+ },
+ data: zone_list.data('zones'),
+ indicator: 'Saving...',
+ cancel: 'Cancel',
+ submit: 'Save',
+ callback: function(zones,editable) {
+ console.log(zones);
+ return "bah!";
+ }
+ });
+
+ $('#server_edit_zones').click(function() {
+ console.log("button click");
+ zone_list.click();
+ $('#server_edit_zones').hide();
+ });
+ }
+ });
+})(jQuery);
@@ -0,0 +1,5 @@
+<script src="[% combust.static_url("/cdn/libs/hogan/2/hogan.min.js") %]" type="text/javascript"></script>
+<script src="[% combust.static_url("/cdn/libs/underscore/1.3/underscore-min.js") %]" type="text/javascript"></script>
+<script src="[% combust.static_url("/cdn/libs/jquery-plugins/jeditable/1.7/jquery.jeditable.min.js") %]" type="text/javascript"></script>
+<script src="[% combust.static_url("/js/admin-templates.js") %]" type="text/javascript"></script>
+<script src="[% combust.static_url("/js/admin.js") %]" type="text/javascript"></script>
View
@@ -0,0 +1,8 @@
+package NTPPool::API;
+use strict;
+use base qw(Combust::API);
+
+__PACKAGE__->setup_api
+ ('staff' => 'Staff');
+
+1;
View
@@ -0,0 +1,5 @@
+package NTPPool::API::Base;
+use strict;
+use base qw(Combust::API::Base);
+
+1;
View
@@ -0,0 +1,104 @@
+package NTPPool::API::Staff;
+use strict;
+use base qw(NTPPool::API::Base);
+use NP::Model;
+
+sub search {
+ my $self = shift;
+ return { error => 'No access' } unless $self->user && $self->user->is_staff;
+ my $q = $self->_required_param('q');
+
+ my $ip = Net::IP->new($q);
+
+ my $result = {
+ users => [],
+ servers => [],
+ };
+
+ my @users;
+
+ local $Rose::DB::Object::Debug = $Rose::DB::Object::Manager::Debug = 1;
+
+ warn "getting servers";
+
+ my $servers = NP::Model->server->get_servers
+ (
+ query => [ or => [ ( $ip ? (ip => $ip->ip) : () ),
+ hostname => { like => $q . '%' },
+ ]
+ ],
+ require_objects => [ 'user' ]
+ );
+ if ($servers) {
+ warn "got servdrs!", Data::Dump::pp($servers);
+
+ push @users, $_->user for @$servers;
+ }
+
+ warn "getting users";
+
+ my $users = NP::Model->user->get_users
+ (
+ query => [ or => [ username => { like => $q . '%' },
+ email => { like => '%' . $q . '%' },
+ ]
+ ],
+ multi_many_ok => 1,
+ with_objects => [ 'servers_all.zones' ]
+ );
+ push @users, @$users;
+ warn "USERS: ", join ", ", map { $_->username } @users;
+
+ if (@users) {
+ $result->{users} = [];
+ for my $user (@users) {
+ my $data = $user->get_data_hash;
+ push @{$result->{users}}, {
+ id => $data->{id},
+ name => $data->{name},
+ username => $data->{username},
+ email => $data->{email},
+ servers => [
+ map {
+ my $s = $_;
+ my $d = $s->get_data_hash;
+ { id => $d->{id},
+ ip => $d->{ip},
+ score => $s->score,
+ netspeed => $d->{netspeed},
+ netspeed_human => $s->netspeed_human,
+ created_on => $d->{created_on},
+ deletion_on => $d->{deletion_on},
+ hostname => $d->{hostname},
+ in_pool => $d->{in_pool},
+ zones => [map { $_->name } $s->zones]
+ }
+ } sort {
+ if ($a->deletion_on or $b->deletion_on) {
+ unless ($a->deletion_on and $b->deletion_on) {
+ return 1 if $a->deletion_on;
+ return -1;
+ }
+ return $b->deletion_on <=> $a->deletion_on;
+ }
+ return $b->created_on <=> $a->created_on;
+ }
+ $user->servers_all
+ ]
+ };
+ }
+ use Data::Dump qw(pp);
+ #pp($result);
+ }
+
+ # search users by username and email
+
+ return $result;
+}
+
+sub server_zones {
+ my $self = shift;
+ return {};
+}
+
+1;
View
@@ -0,0 +1,14 @@
+package NTPPool::Control::API;
+use strict;
+use base qw(Combust::Control::API NTPPool::Control::Manage);
+use NTPPool::API;
+
+sub post_process {
+ my $self = shift;
+ $self->request->header_out('Cache-Control' => 'private');
+ return $self->SUPER::post_process(@_);
+}
+
+
+
+1;

0 comments on commit 1f60bd4

Please sign in to comment.