<s> from server_manager . models import Server <EOL> from server_manager . loader import backend_loader <EOL> from django . shortcuts import render_to_response <EOL> from django . utils import simplejson <EOL> from django . template import RequestContext , Template <EOL> from django . http import HttpResponse , Http404 <EOL> from django . contrib import admin <EOL> from django . contrib . sites . models import Site <EOL> def dt2str ( dt , request ) : <EOL> t = Template ( '<STR_LIT>' ) <EOL> return t . render ( RequestContext ( request , { '<STR_LIT>' : dt } ) ) <EOL> class SiteWrapper ( object ) : <EOL> def __init__ ( self , site , backend , request ) : <EOL> self . site = site <EOL> self . backend = backend <EOL> self . request = request <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . site , attr ) <EOL> def can_restart ( self ) : <EOL> return self . backend . can_restart ( self . site . pk , self . request ) <EOL> def can_stop ( self ) : <EOL> return self . backend . can_stop ( self . site . pk , self . request ) <EOL> def can_start ( self ) : <EOL> return self . backend . can_start ( self . site . pk , self . request ) <EOL> def get_uptime ( self ) : <EOL> return self . backend . get_uptime ( self . site . pk , self . request ) <EOL> class ServerRestarterAdmin ( admin . ModelAdmin ) : <EOL> def changelist_view ( self , request , extra_context = None ) : <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> raise Http404 <EOL> if request . GET . get ( '<STR_LIT>' ) : <EOL> return self . ajax_call ( request ) <EOL> sites = [ ] <EOL> for site in Site . objects . all ( ) : <EOL> sites . append ( SiteWrapper ( site , self . backend , request ) ) <EOL> data = { <EOL> '<STR_LIT>' : sites , <EOL> '<STR_LIT>' : self . media , <EOL> } <EOL> return render_to_response ( '<STR_LIT>' , data , RequestContext ( request ) ) <EOL> def ajax_call ( self , request ) : <EOL> site_id = request . POST . get ( '<STR_LIT>' , None ) <EOL> if not site_id : <EOL> return self . ajax_response ( False ) <EOL> try : <EOL> site = Site . objects . get ( pk = site_id ) <EOL> except Site . DoesNotExist : <EOL> return self . ajax_response ( False ) <EOL> action = request . POST . get ( '<STR_LIT:action>' , None ) <EOL> if action == '<STR_LIT>' : <EOL> return self . ajax_response ( self . backend . restart ( site . pk , request ) ) <EOL> elif action == "<STR_LIT:start>" : <EOL> return self . ajax_response ( self . backend . start ( site . pk , request ) ) <EOL> elif action == "<STR_LIT>" : <EOL> return self . ajax_response ( self . backend . stop ( site . pk , request ) ) <EOL> elif action == '<STR_LIT>' : <EOL> uptime = self . backend . get_uptime ( site . pk , request ) <EOL> if uptime is None : <EOL> return self . ajax_response ( False ) <EOL> return self . ajax_response ( dt2str ( uptime , request ) ) <EOL> return self . ajax_response ( False ) <EOL> def ajax_response ( self , data ) : <EOL> return HttpResponse ( simplejson . dumps ( data ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ property <EOL> def backend ( self ) : <EOL> return backend_loader <EOL> admin . site . register ( Server , ServerRestarterAdmin ) </s>
