Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate users #6

Open
wants to merge 24 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2302384
migrate users
emilda Aug 13, 2013
2de66d8
test
emilda Aug 27, 2013
dba02be
Revert "test"
emilda Aug 27, 2013
d904540
Revert 2de66d8..dba02be
emilda Aug 27, 2013
d19781a
Created folder migration-script
emilda Aug 27, 2013
c1aab5b
natural key implementation for model Experiment
emilda Sep 9, 2013
957a764
Natural key implementation for model ExperimentACLManager
emilda Sep 9, 2013
3772fa3
Natural key implementation for model Author_Experiment
emilda Sep 9, 2013
12c13ff
Merge branch 'dbmigrate' of https://github.com/Softcodes/mytardis int…
emilda Sep 9, 2013
6586322
Natural key implementation for model Dataset
emilda Sep 9, 2013
ec69560
Natural key implementation for model Dataset_File
emilda Sep 9, 2013
dd5707e
Bug fix for the exception - object has no attribute 'natural_key',
emilda Sep 9, 2013
10be8f3
Natural key implementation for model, ExperimentParameterSet
emilda Sep 9, 2013
e898ef4
Natural key implementation for model, DatasetParameterSet
emilda Sep 9, 2013
a9fca46
Natural key implementation for model, DatafileParameterSet
emilda Sep 9, 2013
e8f954d
Bug fix for value generated by dumpdata for parameterset in the json
emilda Sep 10, 2013
907f951
Natural key implementation for model, ExperimentParameter
emilda Sep 10, 2013
cd3b420
Natural key implementation for model, DatasetParameter
emilda Sep 10, 2013
1bf9cb4
Natural key implementation for model, DatafileParameter
emilda Sep 10, 2013
c52e9df
Natural key implementation for model, UserProfile
emilda Sep 10, 2013
f8be4af
Natural key implementation for model, UserAuthentication
emilda Sep 10, 2013
9972d0a
Natural key implementation for model, GroupAdmin
emilda Sep 10, 2013
15c8e94
Natural key implementation for model, Token
emilda Sep 10, 2013
83a5085
commented out the natural_key.dependencies
emilda Sep 10, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions createuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python
# first run the command below in /opt/mytardis/current
# ln -s bin/django django_env.py
#
# Run the command below to create users.json.
# bin/django dumpdata --indent=4 auth.User > users.json
# Transfer users.json across to the mytardis system to import to and run this script
import os
import sys
import site
import django_env

if __name__ == '__main__':

# Setup environ
os.environ['DJANGO_SETTINGS_MODULE'] = "tardis.settings"
site.addsitedir('/opt/mytardis/current/eggs')
sys.path.append('/opt/mytardis/current')

from django.contrib.auth.models import User
from django.db import transaction
from tardis.tardis_portal.models import UserProfile, UserAuthentication
import json
'''
Created on Aug 13, 2013

@author: sindhue
'''

@transaction.commit_on_success
def create_user(username, email, password):
status = 'failure'
try:
user = User.objects.create_user(username, email, password)

userProfile = UserProfile(user=user, isDjangoAccount=True)
userProfile.save()

authentication = UserAuthentication(userProfile=userProfile,
username=username,
authenticationMethod=u'localdb')
authentication.save()
status = 'success'
except:
transaction.rollback()
print 'Could not create user %s ', username
return status

jsonfile = open('users.json', 'r')
users = json.loads(jsonfile.read())
jsonfile.close()

for user in users:
fields = user['fields']
username = fields['username']
email = fields['email']
password = fields['password']
status =create_user(username, email, password)
print 'create user, %s: %s' % (username, status)

60 changes: 60 additions & 0 deletions migration-scripts/createuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python
# first run the command below in /opt/mytardis/current
# ln -s bin/django django_env.py
#
# Run the command below to create users.json.
# bin/django dumpdata --indent=4 auth.User > users.json
# Transfer users.json across to the mytardis system to import to and run this script
import os
import sys
import site
import django_env

if __name__ == '__main__':

# Setup environ
os.environ['DJANGO_SETTINGS_MODULE'] = "tardis.settings"
site.addsitedir('/opt/mytardis/current/eggs')
sys.path.append('/opt/mytardis/current')

from django.contrib.auth.models import User
from django.db import transaction
from tardis.tardis_portal.models import UserProfile, UserAuthentication
import json
'''
Created on Aug 13, 2013

@author: sindhue
'''

@transaction.commit_on_success
def create_user(username, email, password):
status = 'failure'
try:
user = User.objects.create_user(username, email, password)

userProfile = UserProfile(user=user, isDjangoAccount=True)
userProfile.save()

authentication = UserAuthentication(userProfile=userProfile,
username=username,
authenticationMethod=u'localdb')
authentication.save()
status = 'success'
except:
transaction.rollback()
print 'Could not create user %s ', username
return status

jsonfile = open('users.json', 'r')
users = json.loads(jsonfile.read())
jsonfile.close()

for user in users:
fields = user['fields']
username = fields['username']
email = fields['email']
password = fields['password']
status =create_user(username, email, password)
print 'create user, %s: %s' % (username, status)

9 changes: 8 additions & 1 deletion tardis/tardis_portal/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,14 @@ def external_users(self, request, experiment_id):
if group:
result += group
return result


def get_by_natural_key(self, title, username):
"""
Added by Sindhu Emilda for natural key support for model Experiment
"""
return self.get_query_set.get(title=title,
created_by=User.objects.get_by_natural_key(username)
)

class ParameterNameManager(models.Manager):
def get_by_natural_key(self, namespace, name):
Expand Down
50 changes: 50 additions & 0 deletions tardis/tardis_portal/models/access_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
from django.contrib.auth.models import User, Group
from django.db import models

class UserProfileManager():
"""
Added by Sindhu Emilda for natural key implementation.
The manager for the tardis_portal's UserProfile model.
"""
def get_by_natural_key(self, username):
return self.get(user=User.objects.get_by_natural_key(username),
)

class UserProfile(models.Model):
"""
UserProfile class is an extension to the Django standard user model.
Expand All @@ -20,6 +29,14 @@ class UserProfile(models.Model):
isDjangoAccount = models.BooleanField(
null=False, blank=False, default=True)

''' Added by Sindhu Emilda for natural key implementation '''
objects = UserProfileManager()

def natural_key(self):
return self.user.natural_key()

natural_key.dependencies = ['auth.User']

class Meta:
app_label = 'tardis_portal'

Expand All @@ -40,6 +57,15 @@ def isValidPublicContact(self):
def __unicode__(self):
return self.user.username

class GroupAdminManager():
"""
Added by Sindhu Emilda for natural key implementation.
The manager for the tardis_portal's GroupAdmin model.
"""
def get_by_natural_key(self, username, groupname):
return self.get(user=User.objects.get_by_natural_key(username),
group=Group.objects.get_by_natural_key(groupname),
)

class GroupAdmin(models.Model):
"""GroupAdmin links the Django User and Group tables for group
Expand All @@ -54,12 +80,28 @@ class GroupAdmin(models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(Group)

''' Added by Sindhu Emilda for natural key implementation '''
objects = GroupAdminManager()

def natural_key(self):
return (self.user.natural_key(),) + self.group.natural_key()

natural_key.dependencies = ['auth.User', 'auth.Group']

class Meta:
app_label = 'tardis_portal'

def __unicode__(self):
return '%s: %s' % (self.user.username, self.group.name)

class UserAuthenticationManager():
"""
Added by Sindhu Emilda for natural key implementation.
The manager for the tardis_portal's UserAuthentication model.
"""
def get_by_natural_key(self, username):
return self.get(userProfile=UserProfile.objects.get_by_natural_key(username),
)

# TODO: Generalise auth methods
class UserAuthentication(models.Model):
Expand All @@ -68,6 +110,14 @@ class UserAuthentication(models.Model):
username = models.CharField(max_length=50)
authenticationMethod = models.CharField(max_length=30, choices=CHOICES)

''' Added by Sindhu Emilda for natural key implementation '''
objects = UserAuthenticationManager()

def natural_key(self):
return self.userProfile.natural_key()

natural_key.dependencies = ['tardis_portal.UserProfile']

class Meta:
app_label = 'tardis_portal'

Expand Down
19 changes: 18 additions & 1 deletion tardis/tardis_portal/models/datafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
~Q(mimetype='image/x-icon')) | \
(Q(datafileparameterset__datafileparameter__name__units__startswith="image"))

class Dataset_FileManager(OracleSafeManager):
"""
Added by Sindhu Emilda for natural key implementation.
The manager for the tardis_portal's Dataset_File model.
"""
def get_by_natural_key(self, filename, description):
return self.get(filename=filename,
dataset=Dataset.objects.get_by_natural_key(description),
)

class Dataset_File(models.Model):
"""Class to store meta-data about a physical file

Expand Down Expand Up @@ -55,10 +65,17 @@ class Dataset_File(models.Model):
sha512sum = models.CharField(blank=True, max_length=128)
stay_remote = models.BooleanField(default=False)
verified = models.BooleanField(default=False)

objects = Dataset_FileManager() # Added by Sindhu Emilda

class Meta:
app_label = 'tardis_portal'

''' Added by Sindhu Emilda for natural key implementation '''
def natural_key(self):
return (self.filename,) + self.dataset.natural_key()

natural_key.dependencies = ['tardis_portal.Dataset']

@classmethod
def sum_sizes(cls, datafiles):
"""
Expand Down
32 changes: 31 additions & 1 deletion tardis/tardis_portal/models/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
import logging
logger = logging.getLogger(__name__)

class DatasetManager(OracleSafeManager):
"""
Added by Sindhu Emilda for natural key implementation.
The manager for the tardis_portal's Dataset model.
"""
# Uncomment the following commented lines before loading Datasets.
#def get_by_natural_key(self, description, title, username):
def get_by_natural_key(self, description):
""" Ideally the natural key for Dataset should be a combination of description
and Experiment. But the ManyToManyField relationship manager 'ManyRelatedManager'
throws the exception - object has no attribute 'natural_key'. So Experiment needs
to be commented out for loading models other than Dataset.
"""
return self.get(description=description,
#experiments=Experiment.objects.get_by_natural_key(title, username),
)

class Dataset(models.Model):
"""Class to link datasets to experiments

Expand All @@ -21,11 +38,24 @@ class Dataset(models.Model):
experiments = models.ManyToManyField(Experiment, related_name='datasets')
description = models.TextField(blank=True)
immutable = models.BooleanField(default=False)
objects = OracleSafeManager()
#objects = OracleSafeManager() # Commented by Sindhu E
objects = DatasetManager() # For natural key support added by Sindhu E

class Meta:
app_label = 'tardis_portal'

''' Added by Sindhu Emilda for natural key implementation '''
def natural_key(self):
""" Ideally the natural key for Dataset should be a combination of description
and Experiment. But the ManyToManyField relationship manager 'ManyRelatedManager'
throws the exception - object has no attribute 'natural_key'. So Experiment needs
to be commented out for loading models other than Dataset.
"""
#return (self.description,) + self.experiments.natural_key()
return (self.description,)

#natural_key.dependencies = ['tardis_portal.Experiment']

def getParameterSets(self, schemaType=None):
"""Return the dataset parametersets associated with this
experiment.
Expand Down
48 changes: 45 additions & 3 deletions tardis/tardis_portal/models/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ class Experiment(models.Model):
default=PUBLIC_ACCESS_NONE)
license = models.ForeignKey(License, #@ReservedAssignment
blank=True, null=True)
objects = OracleSafeManager()
safe = ExperimentManager() # The acl-aware specific manager.


''' Added by Sindhu Emilda for natural key implementation '''
#objects = OracleSafeManager() # Commented by Sindhu E
objects = ExperimentManager() # For natural key support added by Sindhu E
safe = ExperimentManager() # The acl-aware specific manager.

def natural_key(self):
return (self.title,) + self.created_by.natural_key()

class Meta:
app_label = 'tardis_portal'

natural_key.dependencies = ['auth.User']

def save(self, *args, **kwargs):
super(Experiment, self).save(*args, **kwargs)
Expand Down Expand Up @@ -179,6 +187,15 @@ def get_owners(self):
isOwner=True)
return [acl.get_related_object() for acl in acls]

class ExperimentACLManager(models.Manager):
"""
Added by Sindhu Emilda for natural key implementation.
The manager for the tardis_portal's ExperimentACL model.
"""
def get_by_natural_key(self, entityId, title, username):
return self.get(entityId=entityId,
experiment=Experiment.objects.get_by_natural_key(title, username),
)

class ExperimentACL(models.Model):
"""The ExperimentACL table is the core of the `Tardis
Expand Down Expand Up @@ -222,6 +239,14 @@ class ExperimentACL(models.Model):
expiryDate = models.DateField(null=True, blank=True)
aclOwnershipType = models.IntegerField(
choices=__COMPARISON_CHOICES, default=OWNER_OWNED)

''' Added by Sindhu Emilda for natural key implementation '''
objects = ExperimentACLManager()

def natural_key(self):
return (self.entityId,) + self.experiment.natural_key()

natural_key.dependencies = ['tardis_portal.Experiment']

def get_related_object(self):
"""
Expand Down Expand Up @@ -250,6 +275,15 @@ class Meta:
app_label = 'tardis_portal'
ordering = ['experiment__id']

class Author_ExperimentManager(models.Manager):
"""
Added by Sindhu Emilda for natural key implementation.
The manager for the tardis_portal's Author_Experiment model.
"""
def get_by_natural_key(self, author, title, username):
return self.get(author=author,
experiment=Experiment.objects.get_by_natural_key(title, username),
)

class Author_Experiment(models.Model):

Expand All @@ -261,6 +295,14 @@ class Author_Experiment(models.Model):
blank=True,
help_text="URL identifier for the author")

''' Added by Sindhu Emilda for natural key implementation '''
objects = Author_ExperimentManager()

def natural_key(self):
return (self.author,) + self.experiment.natural_key()

natural_key.dependencies = ['tardis_portal.Experiment']

def save(self, *args, **kwargs):
super(Author_Experiment, self).save(*args, **kwargs)
try:
Expand Down
Loading