2016-12-20 15:06:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-11-23 06:08:01 +00:00
|
|
|
import uuid
|
|
|
|
|
2016-12-20 16:43:52 +00:00
|
|
|
from django.db import models, IntegrityError
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2018-07-13 16:47:21 +00:00
|
|
|
from orgs.mixins import OrgModelMixin
|
|
|
|
|
2016-12-20 16:43:52 +00:00
|
|
|
__all__ = ['UserGroup']
|
|
|
|
|
|
|
|
|
2018-07-13 16:47:21 +00:00
|
|
|
class UserGroup(OrgModelMixin):
|
2017-11-23 06:08:01 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2018-07-16 06:47:06 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2016-12-20 16:43:52 +00:00
|
|
|
comment = models.TextField(blank=True, verbose_name=_('Comment'))
|
2017-04-07 08:59:09 +00:00
|
|
|
date_created = models.DateTimeField(auto_now_add=True, null=True,
|
|
|
|
verbose_name=_('Date created'))
|
2018-01-25 04:17:26 +00:00
|
|
|
created_by = models.CharField(max_length=100, null=True, blank=True)
|
2016-12-20 16:43:52 +00:00
|
|
|
|
2017-12-04 12:15:47 +00:00
|
|
|
def __str__(self):
|
2016-12-20 16:43:52 +00:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
2016-12-21 16:36:31 +00:00
|
|
|
ordering = ['name']
|
2018-08-16 08:32:49 +00:00
|
|
|
unique_together = [('org_id', 'name'),]
|
2018-03-05 04:59:14 +00:00
|
|
|
verbose_name = _("User group")
|
2016-12-20 16:43:52 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def initial(cls):
|
2017-03-04 08:38:26 +00:00
|
|
|
default_group = cls.objects.filter(name='Default')
|
|
|
|
if not default_group:
|
|
|
|
group = cls(name='Default', created_by='System', comment='Default user group')
|
|
|
|
group.save()
|
|
|
|
else:
|
|
|
|
group = default_group[0]
|
2016-12-20 16:43:52 +00:00
|
|
|
return group
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def generate_fake(cls, count=100):
|
|
|
|
from random import seed, choice
|
|
|
|
import forgery_py
|
|
|
|
from . import User
|
|
|
|
|
|
|
|
seed()
|
|
|
|
for i in range(count):
|
|
|
|
group = cls(name=forgery_py.name.full_name(),
|
|
|
|
comment=forgery_py.lorem_ipsum.sentence(),
|
|
|
|
created_by=choice(User.objects.all()).username)
|
|
|
|
try:
|
|
|
|
group.save()
|
|
|
|
except IntegrityError:
|
|
|
|
print('Error continue')
|
|
|
|
continue
|