django-orchestra/orchestra/contrib/databases/models.py

83 lines
2.7 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
import hashlib
from django.db import models
from django.utils.translation import ugettext_lazy as _
from orchestra.core import validators, services
from . import settings
class Database(models.Model):
""" Represents a basic database for a web application """
MYSQL = 'mysql'
POSTGRESQL = 'postgresql'
2014-10-09 17:04:12 +00:00
name = models.CharField(_("name"), max_length=64, # MySQL limit
2014-05-08 16:59:35 +00:00
validators=[validators.validate_name])
2015-04-21 13:12:48 +00:00
users = models.ManyToManyField('databases.DatabaseUser', blank=True,
verbose_name=_("users"),related_name='databases')
2014-05-08 16:59:35 +00:00
type = models.CharField(_("type"), max_length=32,
choices=settings.DATABASES_TYPE_CHOICES,
default=settings.DATABASES_DEFAULT_TYPE)
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
related_name='databases')
class Meta:
unique_together = ('name', 'type')
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-05-08 16:59:35 +00:00
return "%s" % self.name
@property
def owner(self):
""" database owner is the first user related to it """
# Accessing intermediary model to get which is the first user
users = Database.users.through.objects.filter(database_id=self.id)
2015-04-21 13:12:48 +00:00
user = users.order_by('id').first()
if user is not None:
return user.databaseuser
return None
2014-05-08 16:59:35 +00:00
2015-03-04 21:06:16 +00:00
Database.users.through._meta.unique_together = (
('database', 'databaseuser'),
)
2014-05-08 16:59:35 +00:00
class DatabaseUser(models.Model):
2014-10-17 10:04:47 +00:00
MYSQL = Database.MYSQL
POSTGRESQL = Database.POSTGRESQL
2014-05-08 16:59:35 +00:00
2014-10-09 17:04:12 +00:00
username = models.CharField(_("username"), max_length=16, # MySQL usernames 16 char long
2014-05-08 16:59:35 +00:00
validators=[validators.validate_name])
2014-10-09 17:04:12 +00:00
password = models.CharField(_("password"), max_length=256)
2014-05-08 16:59:35 +00:00
type = models.CharField(_("type"), max_length=32,
choices=settings.DATABASES_TYPE_CHOICES,
default=settings.DATABASES_DEFAULT_TYPE)
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
related_name='databaseusers')
class Meta:
verbose_name_plural = _("DB users")
unique_together = ('username', 'type')
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-05-08 16:59:35 +00:00
return self.username
def get_username(self):
return self.username
def set_password(self, password):
if self.type == self.MYSQL:
# MySQL stores sha1(sha1(password).binary).hex
2015-04-02 16:14:55 +00:00
binary = hashlib.sha1(password.encode('utf-8')).digest()
2014-05-08 16:59:35 +00:00
hexdigest = hashlib.sha1(binary).hexdigest()
2014-10-09 17:04:12 +00:00
self.password = '*%s' % hexdigest.upper()
2014-05-08 16:59:35 +00:00
else:
raise TypeError("Database type '%s' not supported" % self.type)
services.register(Database)
services.register(DatabaseUser, verbose_name_plural=_("Database users"))