Advertisement
mmornati

Untitled

Aug 19th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. '''
  2. Created on Aug 10, 2011
  3.  
  4. @author: mmornati
  5. '''
  6. import httplib2
  7. from django.http import HttpResponse
  8. from django.conf import settings
  9. from django.shortcuts import render_to_response
  10. from django.template.context import RequestContext
  11. from django.utils import simplejson as json
  12. import logging
  13. from webui.widgets.loading import registry
  14. from webui.restserver.utils import Operations
  15.  
  16. logger = logging.getLogger(__name__)
  17.  
  18. def callRestServer(filters, agent, action, args=None):
  19.     http = httplib2.Http()
  20.     url = settings.RUBY_REST_BASE_URL
  21.     url += filters + "/"
  22.     url += agent + "/"
  23.     url += action + "/"
  24.     if args:
  25.         url += args + "/"
  26.     logger.info('Calling RestServer on: ' + url)
  27.     response, content = http.request(url, "GET")
  28.     logger.info('Response: ' + str(response))
  29.     logger.info('Content: ' + str(content))
  30.     return response, content
  31.  
  32. def get(request, filters, agent, action, args=None):
  33.     response, content = callRestServer(filters, agent, action, args)
  34.     if response.status == 200:
  35.         return HttpResponse(content, mimetype="application/json")
  36.     return response
  37.  
  38. def getWithTemplate(request, template, filters, agent, action, args=None):
  39.     response, content = callRestServer(filters, agent, action, args)
  40.     if response.status == 200:
  41.         jsonObj = json.loads(content)
  42.         templatePath = 'ajax/' + template + '.html'
  43.         data = {
  44.                 'content': jsonObj
  45.         }
  46.         return render_to_response( templatePath, data,
  47.             context_instance = RequestContext( request ) )
  48.     return response
  49.  
  50.  
  51. def executeAction(request, action):
  52.     logger.info("Executing action " + action)
  53.     actions = Actions()
  54.     actionToExecute = getattr(actions, action)
  55.     actionToExecute()
  56.     return HttpResponse('')
  57.  
  58. class Actions(object):
  59.  
  60.     def refresh_dashboard(self):
  61.         logger.info("Getting all Widgets from database")
  62.         registry.reset_cache()
  63.         widgets_list = registry.get_widgets_dashboard()
  64.         for key, widgets in widgets_list.items():
  65.             for widget in widgets:
  66.                 retrieved = registry.get_widget(widget['name'])
  67.                 retrieved.db_reference = widget
  68.                
  69.     def refresh_server_inventory(self):
  70.         logger.info("Calling Refresh Inventory")
  71.         ops = Operations()
  72.         ops.server_basic_info()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement