PageRenderTime 2569ms CodeModel.GetById 41ms RepoModel.GetById 5ms app.codeStats 0ms

/sylva/engines/models.py

https://github.com/CulturePlex/Sylva
Python | 161 lines | 148 code | 12 blank | 1 comment | 5 complexity | 2fd6708bbbe03e868b50fcc790302d07 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import binascii
  3. from Crypto.Cipher import Blowfish
  4. from django.conf import settings
  5. from django.contrib.auth.models import User
  6. from django.utils.importlib import import_module
  7. from django.utils.translation import gettext as _
  8. from django.db import models
  9. from engines.gdb.utils import get_deploy_status
  10. default_engine = settings.GRAPHDATABASES["default"]["ENGINE"]
  11. def get_upload_to(instance, filename):
  12. return u"private/%s/%s" % (instance.owner.username, filename)
  13. class Instance(models.Model):
  14. name = models.CharField(_("name"), max_length=100)
  15. engine = models.CharField(_("engine"), max_length=50,
  16. default=default_engine)
  17. SCHEME_TYPES = (
  18. ("http", _("Hypertext Transfer Protocol (http)")),
  19. ("file", _("File URI scheme (file)")),
  20. ("https", _("Hypertext Transfer Protocol Secure (https)")),
  21. )
  22. scheme = models.CharField(_("scheme"), max_length=8, default="http",
  23. choices=SCHEME_TYPES)
  24. host = models.CharField(_("host"), max_length=250, default="localhost",
  25. null=True, blank=True,
  26. help_text=_("Host name or unit label"))
  27. port = models.PositiveIntegerField(_("port"), max_length=10,
  28. default="7474", null=True, blank=True)
  29. path = models.CharField(_("path"), max_length=250, default="db/data",
  30. help_text=_("URL or absolute file system path"))
  31. username = models.CharField(_("username"), max_length=250,
  32. null=True, blank=True)
  33. encrypted_password = models.CharField(_("encrypted password"),
  34. max_length=32,
  35. null=True, blank=True,
  36. help_text=_("Agregated field"))
  37. plain_password = models.CharField(_("password"),
  38. max_length=50,
  39. null=True, blank=True,
  40. help_text=_("Type again to change"))
  41. query = models.TextField(_("query"), null=True, blank=True)
  42. fragment = models.TextField(_("fragment"), null=True, blank=True)
  43. key_file = models.FileField(_("Key file"), upload_to=get_upload_to,
  44. null=True, blank=True,)
  45. cert_file = models.FileField(_("Cert file"), upload_to=get_upload_to,
  46. null=True, blank=True,)
  47. options = models.TextField(_("options"), null=True, blank=True)
  48. owner = models.ForeignKey(User, verbose_name=_('owner'),
  49. related_name="instances")
  50. TYPE_TYPES = (
  51. ("private", _("Private")),
  52. ("public", _("Public")),
  53. ("shared", _("Shared")),
  54. )
  55. type = models.CharField(_("type"), max_length=8, default="private",
  56. choices=TYPE_TYPES, null=True, blank=True)
  57. activation = models.CharField(_("activation code"), max_length=50,
  58. null=True, blank=True,
  59. help_text=_("Blank if already activated"))
  60. activated = models.NullBooleanField(_("activated"), default=True)
  61. def __init__(self, *args, **kwargs):
  62. super(Instance, self).__init__(*args, **kwargs)
  63. self._gdb = None
  64. def __unicode__(self):
  65. return u"%s ~ %s" % (self.name, self._get_connection_string())
  66. def save(self, *args, **kwargs):
  67. if self.plain_password:
  68. self._set_password(self.plain_password)
  69. self.plain_password = ""
  70. super(Instance, self).save(*args, **kwargs)
  71. @models.permalink
  72. def get_absolute_url(self):
  73. return ('engines.views.edit', [str(self.id)])
  74. def _get_connection_string(self):
  75. if self.scheme == u"file":
  76. if self.host:
  77. connection_string = "%s://%s/%s" % (self.scheme, self.host,
  78. self.path)
  79. else:
  80. connection_string = "%s://%s" % (self.scheme, self.path)
  81. else:
  82. if self.username and self.password and self.port:
  83. connection_string = "%s://%s:%s@%s:%s/%s/" % (self.scheme,
  84. self.username,
  85. self.password,
  86. self.host,
  87. self.port,
  88. self.path)
  89. elif self.username and self.port:
  90. connection_string = "%s://%s@%s:%s/%s/" % (self.scheme,
  91. self.username,
  92. self.host,
  93. self.port,
  94. self.path)
  95. elif self.port:
  96. connection_string = "%s://%s:%s/%s/" % (self.scheme, self.host,
  97. self.port, self.path)
  98. else:
  99. connection_string = "%s://%s/%s/" % (self.scheme, self.host,
  100. self.path)
  101. if self.query:
  102. connection_string = "%s?%s" % (connection_string, self.query)
  103. if self.fragment:
  104. connection_string = "%s#%s" % (connection_string,
  105. self.fragment)
  106. return connection_string
  107. def get_gdb(self, graph=None):
  108. if not self._gdb:
  109. connection_dict = {
  110. "name": self.name,
  111. "scheme": self.scheme,
  112. "host": self.host,
  113. "port": self.port,
  114. "path": self.path,
  115. "username": self.username,
  116. "password": self.password,
  117. "query": self.query,
  118. "fragment": self.fragment,
  119. "key_file": self.key_file and self.key_file.file,
  120. "cert_file": self.cert_file and self.cert_file.file,
  121. "options": self.options,
  122. }
  123. connection_string = self._get_connection_string()
  124. module = import_module(self.engine)
  125. self._gdb = module.GraphDatabase(connection_string,
  126. connection_dict, graph)
  127. return self._gdb
  128. def _get_password(self):
  129. if not self.encrypted_password:
  130. return u""
  131. enc_obj = Blowfish.new(settings.SECRET_KEY)
  132. hex_password = binascii.a2b_hex(self.encrypted_password)
  133. return u"%s" % enc_obj.decrypt(hex_password).rstrip()
  134. def _set_password(self, value):
  135. if not value:
  136. self.encrypted_password = u""
  137. else:
  138. enc_obj = Blowfish.new(settings.SECRET_KEY)
  139. repeat = 8 - (len(value) % 8)
  140. value = value + " " * repeat
  141. self.encrypted_password = binascii.b2a_hex(enc_obj.encrypt(value))
  142. password = property(_get_password, _set_password)
  143. def get_status(self):
  144. return get_deploy_status(self.engine, self.name)