Commit 9c635ffb authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

Progress VI

parent 36cccd57
[
{
"model": "auth.group",
"pk": 1,
"fields": {
"name": "default"
}
},
{
"model": "auth.group",
"pk": 2,
"fields": {
"name": "academic"
}
},
{
"model": "auth.group",
"pk": 3,
"fields": {
"name": "shibboleth"
}
},
{
"model": "auth.group",
"pk": 4,
"fields": {
"name": "helpdesk"
}
},
{
"model": "auth.group",
"pk": 4,
"fields": {
"name": "faculty"
},
"permissions": ""
},
{
"model": "auth.group",
"pk": 4,
"fields": {
"name": "ugrad"
}
},
{
"model": "auth.group",
"pk": 4,
"fields": {
"name": "grad"
}
},
{
"model": "auth.group",
"pk": 4,
"fields": {
"name": "researcher"
}
},
{
"model": "auth.group",
"pk": 4,
"fields": {
"name": "associate"
}
},
{
"model": "im.GroupKind",
"pk": 1,
"fields": {
"name": "course"
}
},
{
"model": "im.GroupKind",
"pk": 2,
"fields": {
"name": "project"
}
},
{
"model": "im.GroupKind",
"pk": 3,
"fields": {
"name": "laboratory"
}
},
{
"model": "im.GroupKind",
"pk": 4,
"fields": {
"name": "organization"
}
}
]
......@@ -41,9 +41,8 @@ from uuid import uuid4
from django.core.management.base import BaseCommand, CommandError
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from django.contrib.auth.models import Group
from astakos.im.models import AstakosUser
from astakos.im.models import AstakosUser, AstakosGroup
from astakos.im.util import reserved_email
from ._common import add_user_permission
......@@ -121,10 +120,10 @@ class Command(BaseCommand):
groupname = options.get('add-group')
if groupname is not None:
try:
group = Group.objects.get(name=groupname)
user.groups.add(group)
group = AstakosGroup.objects.get(name=groupname)
user.astakos_groups.add(group)
self.stdout.write('Group: %s added successfully\n' % groupname)
except Group.DoesNotExist, e:
except AstakosGroup.DoesNotExist, e:
self.stdout.write('Group named %s does not exist\n' % groupname)
pname = options.get('add-permission')
......
......@@ -85,7 +85,7 @@ class Command(BaseCommand):
active = format_bool(user.is_active)
admin = format_bool(user.is_superuser)
fields = (id, user.email, user.realname, active, admin, user.provider,
','.join([g.name for g in user.groups.all()]))
','.join([g.name for g in user.astakos_groups.all()]))
if options['csv']:
line = '|'.join(fields)
......
......@@ -32,13 +32,15 @@
# or implied, of GRNET S.A.
from optparse import make_option
from datetime import datetime
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import Group, Permission
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from astakos.im.models import AstakosUser
from astakos.im.models import AstakosUser, AstakosGroup, Membership
from ._common import remove_user_permission, add_user_permission
class Command(BaseCommand):
......@@ -135,18 +137,24 @@ class Command(BaseCommand):
groupname = options.get('add-group')
if groupname is not None:
try:
group = Group.objects.get(name=groupname)
user.groups.add(group)
except Group.DoesNotExist, e:
group = AstakosGroup.objects.get(name=groupname)
m = Membership(person=user, group=group, date_joined=datetime.now())
m.save()
except AstakosGroup.DoesNotExist, e:
self.stdout.write("Group named %s does not exist\n" % groupname)
except IntegrityError, e:
self.stdout.write("User is already member of %s\n" % groupname)
groupname = options.get('delete-group')
if groupname is not None:
try:
group = Group.objects.get(name=groupname)
user.groups.remove(group)
except Group.DoesNotExist, e:
group = AstakosGroup.objects.get(name=groupname)
m = Membership.objects.get(person=user, group=group)
m.delete()
except AstakosGroup.DoesNotExist, e:
self.stdout.write("Group named %s does not exist\n" % groupname)
except Membership.DoesNotExist, e:
self.stdout.write("User is not a member of %s\n" % groupname)
pname = options.get('add-permission')
if pname is not None:
......
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(DataMigration):
def forwards(self, orm):
def _create_groupkind(name):
try:
orm.GroupKind(name=name).save()
except:
pass
t = ('default', 'course', 'project', 'laboratory', 'organization')
map(_create_groupkind, t)
default = orm.GroupKind.objects.get(name='default')
def _create_astakogroup(name):
try:
orm.AstakosGroup.objects.get(name=name)
except orm.AstakosGroup.DoesNotExist:
try:
g = orm['auth.Group'].objects.get(name=name)
extended = orm.AstakosGroup(group_ptr_id=g.pk)
extended.__dict__.update(g.__dict__)
extended.kind = default
extended.approval_date = datetime.datetime.now()
extended.issue_date = datetime.datetime.now()
extended.moderation_enabled = False
extended.save()
map(lambda u:orm.Membership( group=extended,
person=orm.AstakosUser.objects.get(id=u.id),
date_joined=datetime.datetime.now()).save(),
g.user_set.all())
except orm['auth.Group'].DoesNotExist:
orm.AstakosGroup( name=name,
kind=default,
approval_date=datetime.datetime.now(),
issue_date=datetime.datetime.now(),
moderation_enabled=False).save()
# catch integrate
t = ('default', 'shibboleth', 'helpdesk', 'faculty', 'ugrad', 'grad', 'researcher', 'associate')
map(_create_astakogroup, t)
def backwards(self, orm):
def _delete_groupkind(name):
try:
orm.GroupKind.objects.get(name=name).delete()
except orm.GroupKind.DoesNotExist:
pass
def _delete_astakosgroup(name):
try:
orm.AstakosGroup.objects.get(name=name).delete()
except orm.AstakosGroup.DoesNotExist:
pass
t = ('default', 'shibboleth', 'helpdesk', 'faculty', 'ugrad', 'grad', 'researcher', 'associate')
map(_delete_astakosgroup, t)
t = ('default', 'course', 'project', 'laboratory', 'organization')
map(_delete_groupkind, t)
models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'im.additionalmail': {
'Meta': {'object_name': 'AdditionalMail'},
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
},
'im.approvalterms': {
'Meta': {'object_name': 'ApprovalTerms'},
'date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 8, 12, 40, 8, 181485)', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'location': ('django.db.models.fields.CharField', [], {'max_length': '255'})
},
'im.astakosgroup': {
'Meta': {'object_name': 'AstakosGroup', '_ormbases': ['auth.Group']},
'approval_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 8, 12, 40, 8, 175548)'}),
'desc': ('django.db.models.fields.TextField', [], {'null': 'True'}),
'estimated_participants': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
'expiration_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
'group_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.Group']", 'unique': 'True', 'primary_key': 'True'}),
'issue_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
'kind': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.GroupKind']"}),
'moderation_enabled': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'policy': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['im.Resource']", 'null': 'True', 'through': "orm['im.AstakosGroupQuota']", 'blank': 'True'})
},
'im.astakosgroupquota': {
'Meta': {'unique_together': "(('resource', 'group'),)", 'object_name': 'AstakosGroupQuota'},
'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosGroup']", 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'limit': ('django.db.models.fields.PositiveIntegerField', [], {}),
'resource': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Resource']"})
},
'im.astakosuser': {
'Meta': {'unique_together': "(('provider', 'third_party_identifier'),)", 'object_name': 'AstakosUser', '_ormbases': ['auth.User']},
'activation_sent': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'affiliation': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'astakos_groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.AstakosGroup']", 'symmetrical': 'False', 'through': "orm['im.Membership']", 'blank': 'True'}),
'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}),
'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
'date_signed_terms': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'email_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'has_credits': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'has_signed_terms': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'invitations': ('django.db.models.fields.IntegerField', [], {'default': '100'}),
'is_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'level': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'owner': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'owner'", 'null': 'True', 'to': "orm['im.AstakosGroup']"}),
'policy': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.Resource']", 'null': 'True', 'through': "orm['im.AstakosUserQuota']", 'symmetrical': 'False'}),
'provider': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'third_party_identifier': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
'updated': ('django.db.models.fields.DateTimeField', [], {}),
'user_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True', 'primary_key': 'True'})
},
'im.astakosuserquota': {
'Meta': {'unique_together': "(('resource', 'user'),)", 'object_name': 'AstakosUserQuota'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'limit': ('django.db.models.fields.PositiveIntegerField', [], {}),
'resource': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Resource']"}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
},
'im.emailchange': {
'Meta': {'object_name': 'EmailChange'},
'activation_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '40', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'new_email_address': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
'requested_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 8, 8, 12, 40, 8, 183025)'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'emailchange_user'", 'unique': 'True', 'to': "orm['im.AstakosUser']"})
},
'im.groupkind': {
'Meta': {'object_name': 'GroupKind'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'})
},
'im.invitation': {
'Meta': {'object_name': 'Invitation'},
'code': ('django.db.models.fields.BigIntegerField', [], {'db_index': 'True'}),
'consumed': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'inviter': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'invitations_sent'", 'null': 'True', 'to': "orm['im.AstakosUser']"}),
'is_consumed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'realname': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
},
'im.membership': {
'Meta': {'unique_together': "(('person', 'group'),)", 'object_name': 'Membership'},
'date_joined': ('django.db.models.fields.DateField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
'date_requested': ('django.db.models.fields.DateField', [], {'default': 'datetime.datetime(2012, 8, 8, 12, 40, 8, 179349)', 'blank': 'True'}),
'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosGroup']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'person': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"})
},
'im.resource': {
'Meta': {'object_name': 'Resource'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'meta': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.ResourceMetadata']", 'symmetrical': 'False'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
'service': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Service']"})
},
'im.resourcemetadata': {
'Meta': {'object_name': 'ResourceMetadata'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
'value': ('django.db.models.fields.CharField', [], {'max_length': '255'})
},
'im.service': {
'Meta': {'object_name': 'Service'},
'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}),
'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
'icon': ('django.db.models.fields.FilePathField', [], {'max_length': '100', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
'url': ('django.db.models.fields.FilePathField', [], {'max_length': '100'})
}
}
complete_apps = ['im']
......@@ -42,6 +42,7 @@ from base64 import b64encode
from urlparse import urlparse, urlunparse
from random import randint
from collections import defaultdict
from south.signals import post_migrate
from django.db import models, IntegrityError
from django.contrib.auth.models import User, UserManager, Group
......@@ -51,7 +52,7 @@ from django.template.loader import render_to_string
from django.core.mail import send_mail
from django.db import transaction
from django.db.models.signals import post_save, post_syncdb
from django.db.models import Q
from django.db.models import Q, Count
from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, \
AUTH_TOKEN_DURATION, BILLING_FIELDS, QUEUE_CONNECTION, SITENAME, \
......@@ -236,7 +237,7 @@ class AstakosUser(User):
super(AstakosUser, self).__init__(*args, **kwargs)
self.__has_signed_terms = self.has_signed_terms
if self.id:
self.__groupnames = [g.name for g in self.groups.all()]
self.__groupnames = [g.name for g in self.astakos_groups.all()]
else:
self.is_active = False
......@@ -305,9 +306,9 @@ class AstakosUser(User):
groupname = 'shibboleth' if self.provider == 'shibboleth' else 'default'
if groupname not in self.__groupnames:
try:
group = Group.objects.get(name = groupname)
self.groups.add(group)
except Group.DoesNotExist, e:
group = AstakosGroup.objects.get(name = groupname)
Membership(group=group, person=self, date_joined=datetime.now()).save()
except AstakosGroup.DoesNotExist, e:
logger.exception(e)
def renew_token(self):
......@@ -562,6 +563,16 @@ def superuser_post_syncdb(sender, **kwargs):
post_syncdb.connect(superuser_post_syncdb)
def set_default_group(sender, **kwargs):
try:
default = AstakosGroup.objects.get(name='default')
orphans = AstakosUser.objects.annotate(num_groups=Count('astakos_groups')).filter(num_groups = 0)
map ( lambda u: Membership(group=default, person=u).save(), orphans )
except AstakosGroup.DoesNotExist:
pass
post_migrate.connect(set_default_group)
def superuser_post_save(sender, instance, **kwargs):
if instance.is_superuser:
create_astakos_user(instance)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment