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 _
|
|
|
|
|
|
|
|
from common.mixins import NoDeleteModelMixin
|
|
|
|
|
|
|
|
__all__ = ['UserGroup']
|
|
|
|
|
|
|
|
|
2017-03-04 08:38:26 +00:00
|
|
|
class UserGroup(NoDeleteModelMixin):
|
2017-11-23 06:08:01 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-03-04 08:38:26 +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'))
|
2016-12-20 16:43:52 +00:00
|
|
|
created_by = models.CharField(max_length=100)
|
|
|
|
|
2017-12-04 12:15:47 +00:00
|
|
|
def __str__(self):
|
2016-12-20 16:43:52 +00:00
|
|
|
return self.name
|
|
|
|
|
2017-01-08 05:35:59 +00:00
|
|
|
def delete(self, using=None, keep_parents=False):
|
2016-12-20 16:43:52 +00:00
|
|
|
if self.name != 'Default':
|
|
|
|
self.users.clear()
|
|
|
|
return super(UserGroup, self).delete()
|
|
|
|
return True
|
|
|
|
|
|
|
|
class Meta:
|
2016-12-21 16:36:31 +00:00
|
|
|
ordering = ['name']
|
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
|