django-orchestra/orchestra/apps/accounts/models.py

43 lines
1.6 KiB
Python
Raw Normal View History

2014-07-29 20:10:37 +00:00
from django.conf import settings as djsettings
2014-05-08 16:59:35 +00:00
from django.db import models
from django.utils.translation import ugettext_lazy as _
2014-07-10 10:03:22 +00:00
from orchestra.core import services
2014-09-04 15:55:43 +00:00
from orchestra.utils import send_email_template
2014-07-10 10:03:22 +00:00
2014-05-08 16:59:35 +00:00
from . import settings
class Account(models.Model):
2014-09-14 09:52:45 +00:00
# Users depends on Accounts (think about what should happen when you delete an account)
2014-07-29 20:10:37 +00:00
user = models.OneToOneField(djsettings.AUTH_USER_MODEL,
2014-09-10 16:53:09 +00:00
verbose_name=_("user"), related_name='accounts', null=True)
2014-07-29 20:10:37 +00:00
type = models.CharField(_("type"), choices=settings.ACCOUNTS_TYPES,
max_length=32, default=settings.ACCOUNTS_DEFAULT_TYPE)
2014-05-08 16:59:35 +00:00
language = models.CharField(_("language"), max_length=2,
choices=settings.ACCOUNTS_LANGUAGES,
default=settings.ACCOUNTS_DEFAULT_LANGUAGE)
2014-09-26 15:05:20 +00:00
registered_on = models.DateField(_("registered"), auto_now_add=True)
2014-05-08 16:59:35 +00:00
comments = models.TextField(_("comments"), max_length=256, blank=True)
is_active = models.BooleanField(default=True)
def __unicode__(self):
return self.name
2014-09-14 09:52:45 +00:00
@property
2014-05-08 16:59:35 +00:00
def name(self):
2014-09-14 09:52:45 +00:00
return self.user.username if self.user_id else str(self.pk)
2014-08-19 18:59:23 +00:00
@classmethod
def get_main(cls):
return cls.objects.get(pk=settings.ACCOUNTS_MAIN_PK)
2014-09-04 15:55:43 +00:00
def send_email(self, template, context, contacts=[], attachments=[], html=None):
contacts = self.contacts.filter(email_usages=contacts)
email_to = contacts.values_list('email', flat=True)
send_email_template(template, context, email_to, html=html,
attachments=attachments)
2014-07-10 10:03:22 +00:00
services.register(Account, menu=False)