/src/CassandraMonitor/coredjango.py

https://github.com/chrischeyne/Cassandra-Monitor · Python · 111 lines · 79 code · 5 blank · 27 comment · 0 complexity · 3db2b6be6281e55d05596eee83475b11 MD5 · raw file

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. __author__ = "Chris T. Cheyne"
  16. __copyright__ = "Copyright 2011, The Cassandra Manager Project"
  17. __credits__ = ["Chris Cheyne"]
  18. __license__ = "GPL"
  19. __version__ = "0.0.1"
  20. __maintainer__ = "Chris T. Cheyne"
  21. __email__ = "maintainer@cassandra-manager.org"
  22. __status__ = "Alpha"
  23. """
  24. boots the tornado application server and our django test application
  25. """
  26. # FIXME: reference by module above
  27. # django settings must be called before importing models
  28. from django.conf import settings
  29. settings.configure(DATABASE_ENGINE='sqlite3', DATABASE_NAME='mytornado/db/dev.db')
  30. from django import forms
  31. from django.db import models
  32. # FIXME: reference localized python source
  33. import tornado.httpserver
  34. import tornado.ioloop
  35. import tornado.options
  36. import tornado.web
  37. class Message(models.Model):
  38. """
  39. Message is the django model class in lorder to use it you will need to
  40. create the database manually.
  41. sqlite> CREATE TABLE message (id integer primary key, subject varchar(30), content varchar(250));
  42. sqlite> insert into message values(1, 'subject', 'cool stuff');
  43. sqlite> SELECT * from message;
  44. """
  45. subject = models.CharField(max_length=30)
  46. content = models.TextField(max_length=250)
  47. class Meta:
  48. app_label = 'dj'
  49. db_table = 'message'
  50. def __unicode__(self):
  51. return self.subject + "--" + self.content
  52. class DjForm(forms.Form):
  53. subject = forms.CharField(max_length=100, required=True)
  54. content = forms.CharField()
  55. class ListMessagesHandler(tornado.web.RequestHandler):
  56. def get(self):
  57. messages = Message.objects.all()
  58. self.render("templates/index.html", title="Cassandra Manager",
  59. messages=messages)
  60. class FormHandler(tornado.web.RequestHandler):
  61. def get(self):
  62. form = DjForm()
  63. self.render("templates/form.html", title="Cassandra Manager - Forms", form=form)
  64. def post(self):
  65. data = {
  66. 'subject':self.request.arguments['subject'][0],
  67. 'content':self.request.arguments['content'][0],
  68. }
  69. form = DjForm(data=data)
  70. if form.is_valid():
  71. message = Message(**form.cleaned_data)
  72. message.save()
  73. self.redirect('/')
  74. else:
  75. self.render("templates/form.html", title="Cassandra Manager - Forms", form=form)
  76. class mainhandler(tornado.web.RequestHandler):
  77. def get(self):
  78. self.write("cassandra manager UP")
  79. class CactiHandler(tornado.web.RequestHandler):
  80. """ handle the cacti pages """
  81. def get(self):
  82. self.write("cassandra cacti manager UP")
  83. class GangliaHandler(tornado.web.RequestHandler):
  84. """ handle the ganglia pages """
  85. def get(self):
  86. self.write("cassandra ganglia manager UP")
  87. class GraphHandler(tornado.web.RequestHandler):
  88. """ handle the cacti pages """
  89. def get(self):
  90. self.write("cassandra graph manager UP")