Revision aa4109d4

b/snf-astakos-app/astakos/im/management/commands/user_add.py
37 37
from random import choice
38 38
from string import digits, lowercase, uppercase
39 39
from uuid import uuid4
40
from datetime import datetime
40 41

  
41 42
from django.core.management.base import BaseCommand, CommandError
42 43
from django.core.validators import validate_email
43 44
from django.core.exceptions import ValidationError
44 45

  
45
from astakos.im.models import AstakosUser, AstakosGroup
46
from astakos.im.models import AstakosUser, AstakosGroup, Membership
46 47
from astakos.im.util import reserved_email
47 48

  
48 49
from ._common import add_user_permission
......
121 122
            if groupname is not None:
122 123
                try:
123 124
                    group = AstakosGroup.objects.get(name=groupname)
124
                    user.astakos_groups.add(group)
125
                    Membership(group=group, person=user, date_joined=datetime.now()).save()
125 126
                    self.stdout.write('Group: %s added successfully\n' % groupname)
126 127
                except AstakosGroup.DoesNotExist, e:
127 128
                    self.stdout.write('Group named %s does not exist\n' % groupname)
b/snf-astakos-app/astakos/im/models.py
233 233
        through='Membership')
234 234
    
235 235
    __has_signed_terms = False
236
    __groupnames = []
237 236
    
238 237
    owner = models.ManyToManyField(AstakosGroup, related_name='owner', null=True)
239 238
    
......
243 242
    def __init__(self, *args, **kwargs):
244 243
        super(AstakosUser, self).__init__(*args, **kwargs)
245 244
        self.__has_signed_terms = self.has_signed_terms
246
        if self.id:
247
            self.__groupnames = [g.name for g in self.astakos_groups.all()]
248
        else:
245
        if not self.id:
249 246
            self.is_active = False
250 247
    
251 248
    @property
......
308 305
            self.activation_sent = None
309 306
        
310 307
        super(AstakosUser, self).save(**kwargs)
311
        
312
        # set group if does not exist
313
        groupname = 'shibboleth' if self.provider == 'shibboleth' else 'default'
314
        if groupname not in self.__groupnames:
315
            try:
316
                group = AstakosGroup.objects.get(name = groupname)
317
                Membership(group=group, person=self, date_joined=datetime.now()).save()
318
            except AstakosGroup.DoesNotExist, e:
319
                logger.exception(e)
320 308
    
321 309
    def renew_token(self):
322 310
        md5 = hashlib.md5()
......
576 564

  
577 565
post_save.connect(superuser_post_save, sender=User)
578 566

  
567
def set_default_group(sender, instance, created, **kwargs):
568
    if not created:
569
        return
570
    try:
571
        default = AstakosGroup.objects.get(name = 'default')
572
        Membership(group=default, person=instance, date_joined=datetime.now()).save()
573
    except AstakosGroup.DoesNotExist, e:
574
        logger.exception(e)
575

  
576
post_save.connect(set_default_group, sender=AstakosUser)
577

  
579 578
def get_resources():
580 579
    # use cache
581 580
    return Resource.objects.select_related().all()

Also available in: Unified diff