Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. From 659677d234dced2f0f7f012bfda434a0689cab13 Mon Sep 17 00:00:00 2001
  2. From: Kenny Meyer <knny.myer@gmail.com>
  3. Date: Wed, 5 Jan 2011 17:04:19 -0300
  4. Subject: [PATCH] Implement pagination for cluster detail -> virtual machines
  5.  
  6. ---
  7. ganeti/templates/virtual_machine/inner_table.html | 138 +++++++++++++++++++++
  8. ganeti/templates/virtual_machine/list.html | 4 +-
  9. ganeti/templates/virtual_machine/table.html | 83 +------------
  10. ganeti/urls.py | 3 +
  11. ganeti/views/cluster.py | 16 ++-
  12. ganeti/views/virtual_machine.py | 46 ++++++-
  13. 6 files changed, 195 insertions(+), 95 deletions(-)
  14. create mode 100644 ganeti/templates/virtual_machine/inner_table.html
  15.  
  16. diff --git a/ganeti/templates/virtual_machine/inner_table.html b/ganeti/templates/virtual_machine/inner_table.html
  17. new file mode 100644
  18. index 0000000..c52b9c1
  19. --- /dev/null
  20. +++ b/ganeti/templates/virtual_machine/inner_table.html
  21. @@ -0,0 +1,138 @@
  22. +{% load webmgr_tags %}
  23. +
  24. +{% block head %}
  25. +<style>
  26. + td.actions div.delete {
  27. + float:none;
  28. + }
  29. + #content td.actions a, #content td.actions {
  30. + padding:5px 0 0 0;
  31. + }
  32. +</style>
  33. +
  34. +<script type="text/javascript" src="{{MEDIA_URL}}/js/jquery.ajax.delete.js"></script>
  35. +<script type="text/javascript" src="{{MEDIA_URL}}/js/jquery.tablesorter.min.js"></script>
  36. +<script type="text/javascript">
  37. +$(document).ready(function() {
  38. + $("#vmlist").tablesorter();
  39. +});
  40. +
  41. +function ajax_get_update() {
  42. + $.get(url, function(results){
  43. + var table = $("table", results);
  44. + var span = $("span.step-links", results);
  45. +
  46. + // update vmlist with the return value
  47. + $('#vmlist').html(table);
  48. + $('.step-links').html(span);
  49. + }, "html");
  50. +}
  51. +
  52. +$(document).ready( function() {
  53. + $('.step-links #prev').click(function(e) {
  54. + e.preventDefault();
  55. + url = ($('.step-links #prev')[0].href);
  56. + ajax_get_update();
  57. + });
  58. + $('.step-links #next').click(function(e) {
  59. + e.preventDefault();
  60. + url = ($('.step-links #next')[0].href);
  61. + ajax_get_update();
  62. + });
  63. +});
  64. +
  65. +$(document).ajaxStop( function() {
  66. + $('.step-links #prev').click( function(e) {
  67. + e.preventDefault();
  68. + url = ($('.step-links #prev')[0].href);
  69. + ajax_get_update();
  70. + });
  71. + $('.step-links #next').click( function(e) {
  72. + e.preventDefault();
  73. + url = ($('.step-links #next')[0].href);
  74. + ajax_get_update();
  75. + });
  76. +});
  77. +</script>
  78. +{% endblock %}
  79. +
  80. +{% if vms.object_list %}
  81. +<table id="vmlist" class="sorted">
  82. +<thead>
  83. + <tr>
  84. + <th class="status"></th>
  85. + <th>Name</th>
  86. + {% if not cluster %}
  87. + <th>Cluster</th>
  88. + {% endif %}
  89. + <th>Node</th>
  90. + <th>OS</th>
  91. + <th>RAM</th>
  92. + <th>Disk Space</th>
  93. + <th>vCPUs</th>
  94. + </tr>
  95. +</thead>
  96. +<tbody id="vms">
  97. + {% for vm in vms.object_list %}
  98. + {% with vm.info as info %}
  99. + <tr>
  100. +
  101. + <td class="status">
  102. + {% if vm.error %}
  103. + <div class="icon_error" title="Ganeti API Error: {{vm.error}}, last status was {{ info.status|render_instance_status }}"></div>
  104. + {% else %}
  105. + {% if info.admin_state %}
  106. + {% if info.oper_state %}
  107. + <div class="icon_running" title="running"></div>
  108. + {% else %}
  109. + <div class="icon_error" title="{{ info.status|render_instance_status }}"></div>
  110. + {% endif %}
  111. + {% else %}
  112. + {% if info.oper_state %}vm
  113. + <div class="icon_error" title="{{ info.status|render_instance_status }}"></div>
  114. + {% else %}
  115. + <div class="icon_stopped" title="stopped"></div>
  116. + {% endif %}
  117. + {% endif %}
  118. + {% endif %}
  119. + </td>
  120. +
  121. + <td class="name">
  122. + <a href="{% url instance-detail vm.cluster.slug vm.hostname %}">
  123. + {{ vm.hostname }}
  124. + </a>
  125. + </td>
  126. + {% if not cluster %}
  127. + <td>{{ vm.cluster|abbreviate_fqdn }}</td>
  128. + {% endif %}
  129. + <td>{{ info.pnode|abbreviate_fqdn }}</td>
  130. + <td>{{ vm.operating_system|render_os }}</td>
  131. + <td>{{ vm.ram|render_storage }}</td>
  132. + <td>{{ vm.disk_size|render_storage }}</td>
  133. + <td>{{ vm.virtual_cpus }}</td>
  134. + {% endwith %}
  135. + {% empty %}
  136. + <tr class="none"><td colspan="100%">No Virtual Machines</td></tr>
  137. + {% endfor %}
  138. +</tbody>
  139. +</table>
  140. +<div class="pagination">
  141. + <span class="step-links">
  142. + {% if vms.has_previous %}
  143. + <a id="prev" href="{% url virtualmachine-table %}?page={{ vms.previous_page_number }}">previous</a>
  144. + {% else %}
  145. + <span style="visibility:hidden;">previous</span>
  146. + {% endif %}
  147. +
  148. + <span class="current">
  149. + Page {{ vms.number }} of {{ vms.paginator.num_pages }}.
  150. + </span>
  151. +
  152. + {% if vms.has_next %}
  153. + <a id="next" href="{% url virtualmachine-table %}?page={{ vms.next_page_number }}">next</a>
  154. + {% else %}
  155. + <span style="visibility:hidden;">next</span>
  156. + {% endif %}
  157. + </span>
  158. +</div>
  159. +{% endif %}
  160. diff --git a/ganeti/templates/virtual_machine/list.html b/ganeti/templates/virtual_machine/list.html
  161. index 23be7ba..ff76b77 100644
  162. --- a/ganeti/templates/virtual_machine/list.html
  163. +++ b/ganeti/templates/virtual_machine/list.html
  164. @@ -3,6 +3,6 @@
  165. {% block title %}Virtual Machines{% endblock %}
  166.  
  167. {% block content %}
  168. -<h1>Virtual Machines </h1>
  169. +<h1>Virtual Machines</h1>
  170. {% include "virtual_machine/table.html" %}
  171. -{% endblock %}
  172. \ No newline at end of file
  173. +{% endblock %}
  174. diff --git a/ganeti/templates/virtual_machine/table.html b/ganeti/templates/virtual_machine/table.html
  175. index 9393424..e7cf251 100644
  176. --- a/ganeti/templates/virtual_machine/table.html
  177. +++ b/ganeti/templates/virtual_machine/table.html
  178. @@ -1,25 +1,3 @@
  179. -{% load webmgr_tags %}
  180. -
  181. -{% block head %}
  182. -
  183. -<style>
  184. - td.actions div.delete {
  185. - float:none;
  186. - }
  187. - #content td.actions a, #content td.actions {
  188. - padding:5px 0 0 0;
  189. - }
  190. -</style>
  191. -
  192. -<script type="text/javascript" src="{{MEDIA_URL}}/js/jquery.ajax.delete.js"></script>
  193. -<script type="text/javascript" src="{{MEDIA_URL}}/js/jquery.tablesorter.min.js"></script>
  194. -<script type="text/javascript">
  195. - $(document).ready(function() {
  196. - $("#vmlist").tablesorter();
  197. - });
  198. -</script>
  199. -{% endblock %}
  200. -
  201. {% if cluster %}
  202. <a class="button add" href="{% url instance-create cluster.slug %}">Add Virtual Machine</a>
  203. {% else %}
  204. @@ -27,62 +5,5 @@
  205. <a class="button add" href="{% url instance-create %}">Add Virtual Machine</a>
  206. {% endif %}
  207. {% endif %}
  208. -<table id="vmlist" class="sorted">
  209. -<thead>
  210. - <tr>
  211. - <th class="status"></th>
  212. - <th>Name</th>
  213. - {% if not cluster %}
  214. - <th>Cluster</th>
  215. - {% endif %}
  216. - <th>Node</th>
  217. - <th>OS</th>
  218. - <th>RAM</th>
  219. - <th>Disk Space</th>
  220. - <th>vCPUs</th>
  221. - </tr>
  222. -</thead>
  223. -<tbody id="vms">
  224. - {% for vm in vms %}
  225. - {% with vm.info as info %}
  226. - <tr>
  227. -
  228. - <td class="status">
  229. - {% if vm.error %}
  230. - <div class="icon_error" title="Ganeti API Error: {{vm.error}}, last status was {{ info.status|render_instance_status }}"></div>
  231. - {% else %}
  232. - {% if info.admin_state %}
  233. - {% if info.oper_state %}
  234. - <div class="icon_running" title="running"></div>
  235. - {% else %}
  236. - <div class="icon_error" title="{{ info.status|render_instance_status }}"></div>
  237. - {% endif %}
  238. - {% else %}
  239. - {% if info.oper_state %}
  240. - <div class="icon_error" title="{{ info.status|render_instance_status }}"></div>
  241. - {% else %}
  242. - <div class="icon_stopped" title="stopped"></div>
  243. - {% endif %}
  244. - {% endif %}
  245. - {% endif %}
  246. - </td>
  247. -
  248. - <td class="name">
  249. - <a href="{% url instance-detail vm.cluster.slug vm.hostname %}">
  250. - {{ vm.hostname }}
  251. - </a>
  252. - </td>
  253. - {% if not cluster %}
  254. - <td>{{ vm.cluster|abbreviate_fqdn }}</td>
  255. - {% endif %}
  256. - <td>{{ info.pnode|abbreviate_fqdn }}</td>
  257. - <td>{{ vm.operating_system|render_os }}</td>
  258. - <td>{{ vm.ram|render_storage }}</td>
  259. - <td>{{ vm.disk_size|render_storage }}</td>
  260. - <td>{{ vm.virtual_cpus }}</td>
  261. - {% endwith %}
  262. - {% empty %}
  263. - <tr class="none"><td colspan="100%">No Virtual Machines</td></tr>
  264. - {% endfor %}
  265. -</tbody>
  266. -</table>
  267. +
  268. +{% include "virtual_machine/inner_table.html" %}
  269. diff --git a/ganeti/urls.py b/ganeti/urls.py
  270. index 7f30409..a517762 100644
  271. --- a/ganeti/urls.py
  272. +++ b/ganeti/urls.py
  273. @@ -91,6 +91,9 @@ urlpatterns += patterns('ganeti.views.virtual_machine',
  274.  
  275. # SSH Keys
  276. url(r'^%s/keys/(?P<api_key>\w+)/?$' % vm_prefix, "ssh_keys", name="instance-keys"),
  277. +
  278. + # Table
  279. + url(r'^vms/table/$', 'vm_table', name="virtualmachine-table"),
  280. )
  281.  
  282.  
  283. diff --git a/ganeti/views/cluster.py b/ganeti/views/cluster.py
  284. index 5067162..0e670c8 100644
  285. --- a/ganeti/views/cluster.py
  286. +++ b/ganeti/views/cluster.py
  287. @@ -26,6 +26,7 @@ from django import forms
  288. from django.contrib.auth import authenticate, login, logout
  289. from django.contrib.auth.decorators import login_required, user_passes_test
  290. from django.core.urlresolvers import reverse
  291. +from django.core.paginator import Paginator, InvalidPage, EmptyPage
  292. from django.http import HttpResponse, HttpResponseRedirect
  293. from django.shortcuts import get_object_or_404, render_to_response
  294. from django.template import RequestContext
  295. @@ -39,7 +40,7 @@ from logs.models import LogItem
  296. log_action = LogItem.objects.log_action
  297.  
  298. from ganeti.models import *
  299. -from ganeti.views import render_403, render_404
  300. +from ganeti.views import render_403, render_404, virtual_machine
  301. from util.portforwarder import forward_port
  302.  
  303. # Regex for a resolvable hostname
  304. @@ -98,10 +99,15 @@ def virtual_machines(request, cluster_slug):
  305. vms = cluster.virtual_machines.all()
  306. else:
  307. vms = user.filter_on_perms(['admin'], VirtualMachine, cluster=cluster)
  308. -
  309. - return render_to_response("virtual_machine/table.html", \
  310. - {'cluster': cluster, 'vms':vms}, \
  311. - context_instance=RequestContext(request))
  312. +
  313. + vms = virtual_machine.render_vms(request, vms)
  314. +
  315. + return render_to_response("virtual_machine/table.html", {
  316. + 'cluster': cluster,
  317. + 'vms': vms
  318. + },
  319. + context_instance=RequestContext(request)
  320. + )
  321.  
  322.  
  323. @login_required
  324. diff --git a/ganeti/views/virtual_machine.py b/ganeti/views/virtual_machine.py
  325. index fe16b74..42f97f6 100644
  326. --- a/ganeti/views/virtual_machine.py
  327. +++ b/ganeti/views/virtual_machine.py
  328. @@ -28,6 +28,7 @@ from django.conf import settings
  329. from django.contrib.auth.decorators import login_required
  330. from django.contrib.auth.models import Group
  331. from django.core.urlresolvers import reverse
  332. +from django.core.paginator import Paginator, InvalidPage, EmptyPage
  333. from django.http import HttpResponse, HttpResponseRedirect, \
  334. HttpResponseNotAllowed, HttpResponseForbidden
  335. from django.shortcuts import get_object_or_404, render_to_response
  336. @@ -206,19 +207,51 @@ def list_(request):
  337. user = request.user
  338. if user.is_superuser:
  339. vms = VirtualMachine.objects.all()
  340. - can_create = True
  341. else:
  342. vms = user.get_objects_any_perms(VirtualMachine, ['admin', 'power','remove'])
  343. - can_create = user.has_any_perms(Cluster, ['create_vm'])
  344. -
  345. +
  346. + vms = render_vms(request, vms)
  347. +
  348. return render_to_response('virtual_machine/list.html', {
  349. - 'vms':vms,
  350. - 'can_create':can_create,
  351. + 'vms': vms,
  352. },
  353. - context_instance=RequestContext(request),
  354. + context_instance=RequestContext(request)
  355. + )
  356. +
  357. +
  358. +@login_required
  359. +def vm_table(request):
  360. + user = request.user
  361. + if user.is_superuser:
  362. + vms = VirtualMachine.objects.all()
  363. + else:
  364. + vms = user.get_objects_any_perms(VirtualMachine, ['admin', 'power','remove'])
  365. +
  366. + vms = render_vms(request, vms)
  367. +
  368. + return render_to_response('virtual_machine/inner_table.html', {
  369. + 'vms': vms
  370. + },
  371. + context_instance=RequestContext(request)
  372. )
  373.  
  374.  
  375. +def render_vms(request, query):
  376. + paginator = Paginator(query, 10)
  377. + page = 1
  378. + if request.is_ajax:
  379. + query = request.GET.get('page')
  380. + if query is not None:
  381. + page = query
  382. +
  383. + try:
  384. + vms = paginator.page(page)
  385. + except (EmptyPage, InvalidPage):
  386. + vms = paginator.page(paginator.num_pages)
  387. +
  388. + return vms
  389. +
  390. +
  391. @login_required
  392. def detail(request, cluster_slug, instance):
  393. cluster = get_object_or_404(Cluster, slug=cluster_slug)
  394. @@ -549,7 +582,6 @@ def cluster_defaults(request):
  395. content = json.dumps(cluster_default_info(cluster))
  396. return HttpResponse(content, mimetype='application/json')
  397.  
  398. -
  399. def cluster_default_info(cluster):
  400. """
  401. Returns a dictionary containing the following
  402. --
  403. 1.7.3.4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement