diff --git a/createuser.py b/createuser.py new file mode 100644 index 0000000000..18f411360b --- /dev/null +++ b/createuser.py @@ -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) + diff --git a/migration-scripts/createuser.py b/migration-scripts/createuser.py new file mode 100644 index 0000000000..18f411360b --- /dev/null +++ b/migration-scripts/createuser.py @@ -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) + diff --git a/tardis/tardis_portal/managers.py b/tardis/tardis_portal/managers.py index 05403c2f88..457e55b8bb 100644 --- a/tardis/tardis_portal/managers.py +++ b/tardis/tardis_portal/managers.py @@ -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): diff --git a/tardis/tardis_portal/models/access_control.py b/tardis/tardis_portal/models/access_control.py index e538ae68f3..5805c39668 100644 --- a/tardis/tardis_portal/models/access_control.py +++ b/tardis/tardis_portal/models/access_control.py @@ -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. @@ -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' @@ -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 @@ -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): @@ -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' diff --git a/tardis/tardis_portal/models/datafile.py b/tardis/tardis_portal/models/datafile.py index 707c0222ee..c36fb4a26a 100644 --- a/tardis/tardis_portal/models/datafile.py +++ b/tardis/tardis_portal/models/datafile.py @@ -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 @@ -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): """ diff --git a/tardis/tardis_portal/models/dataset.py b/tardis/tardis_portal/models/dataset.py index e5974fa6cb..7170ed9ddf 100644 --- a/tardis/tardis_portal/models/dataset.py +++ b/tardis/tardis_portal/models/dataset.py @@ -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 @@ -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. diff --git a/tardis/tardis_portal/models/experiment.py b/tardis/tardis_portal/models/experiment.py index 336611ed61..97b2e32e81 100644 --- a/tardis/tardis_portal/models/experiment.py +++ b/tardis/tardis_portal/models/experiment.py @@ -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) @@ -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 @@ -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): """ @@ -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): @@ -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: diff --git a/tardis/tardis_portal/models/parameters.py b/tardis/tardis_portal/models/parameters.py index 93f1dcbb72..c90a395337 100644 --- a/tardis/tardis_portal/models/parameters.py +++ b/tardis/tardis_portal/models/parameters.py @@ -274,11 +274,28 @@ def _getParameter(parameter): else: return None +class DatafileParameterSetManager(OracleSafeManager): + """ + Added by Sindhu Emilda for natural key implementation. + The manager for the tardis_portal's DatafileParameterSet model. + """ + def get_by_natural_key(self, namespace, filename, description): + return self.get(schema=Schema.objects.get_by_natural_key(namespace), + dataset_file=Dataset_File.objects.get_by_natural_key(filename, description), + ) class DatafileParameterSet(models.Model): schema = models.ForeignKey(Schema) dataset_file = models.ForeignKey(Dataset_File) + ''' Added by Sindhu Emilda for natural key implementation ''' + objects = DatafileParameterSetManager() + + def natural_key(self): + return self.schema.natural_key() + self.dataset_file.natural_key() + + natural_key.dependencies = ['tardis_portal.Schema', 'tardis_portal.Dataset_File'] + def __unicode__(self): return '%s / %s' % (self.schema.namespace, self.dataset_file.filename) @@ -286,11 +303,28 @@ class Meta: app_label = 'tardis_portal' ordering = ['id'] +class DatasetParameterSetManager(OracleSafeManager): + """ + Added by Sindhu Emilda for natural key implementation. + The manager for the tardis_portal's DatasetParameterSet model. + """ + def get_by_natural_key(self, namespace, description): + return self.get(schema=Schema.objects.get_by_natural_key(namespace), + dataset=Dataset.objects.get_by_natural_key(description), + ) class DatasetParameterSet(models.Model): schema = models.ForeignKey(Schema) dataset = models.ForeignKey(Dataset) + ''' Added by Sindhu Emilda for natural key implementation ''' + objects = DatasetParameterSetManager() + + def natural_key(self): + return self.schema.natural_key() + self.dataset.natural_key() + + natural_key.dependencies = ['tardis_portal.Schema', 'tardis_portal.Dataset'] + def __unicode__(self): return '%s / %s' % (self.schema.namespace, self.dataset.description) @@ -298,10 +332,27 @@ class Meta: app_label = 'tardis_portal' ordering = ['id'] +class ExperimentParameterSetManager(OracleSafeManager): + """ + Added by Sindhu Emilda for natural key implementation. + The manager for the tardis_portal's ExperimentParameterSet model. + """ + def get_by_natural_key(self, namespace, title, username): + return self.get(schema=Schema.objects.get_by_natural_key(namespace), + experiment=Experiment.objects.get_by_natural_key(title, username), + ) class ExperimentParameterSet(models.Model): schema = models.ForeignKey(Schema) experiment = models.ForeignKey(Experiment) + + ''' Added by Sindhu Emilda for natural key implementation ''' + objects = ExperimentParameterSetManager() + + def natural_key(self): + return self.schema.natural_key() + self.experiment.natural_key() + + natural_key.dependencies = ['tardis_portal.Schema', 'tardis_portal.Experiment'] def __unicode__(self): return '%s / %s' % (self.schema.namespace, self.experiment.title) @@ -310,6 +361,15 @@ class Meta: app_label = 'tardis_portal' ordering = ['id'] +class DatafileParameterManager(OracleSafeManager): + """ + Added by Sindhu Emilda for natural key implementation. + The manager for the tardis_portal's DatafileParameter model. + """ + def get_by_natural_key(self, nmspace, filename, description, namespace, name): + return self.get(parameterset=DatafileParameterSet.objects.get_by_natural_key(nmspace, filename, description), + name=ParameterName.objects.get_by_natural_key(namespace, name), + ) class DatafileParameter(models.Model): @@ -318,7 +378,15 @@ class DatafileParameter(models.Model): string_value = models.TextField(null=True, blank=True, db_index=True) numerical_value = models.FloatField(null=True, blank=True, db_index=True) datetime_value = models.DateTimeField(null=True, blank=True, db_index=True) - objects = OracleSafeManager() + #objects = OracleSafeManager() # Commented by Sindhu E for natural key support + + ''' Added by Sindhu Emilda for natural key implementation ''' + objects = DatafileParameterManager() + + def natural_key(self): + return (self.parameterset.natural_key(),) + self.name.natural_key() + + natural_key.dependencies = ['tardis_portal.DatafileParameterSet', 'tardis_portal.ParameterName'] def get(self): return _getParameter(self) @@ -330,6 +398,15 @@ class Meta: app_label = 'tardis_portal' ordering = ['name'] +class DatasetParameterManager(OracleSafeManager): + """ + Added by Sindhu Emilda for natural key implementation. + The manager for the tardis_portal's DatasetParameter model. + """ + def get_by_natural_key(self, namespace, name, description): + return self.get(name=ParameterName.objects.get_by_natural_key(namespace, name), + parameterset=DatasetParameterSet.objects.get_by_natural_key(description), + ) class DatasetParameter(models.Model): @@ -338,7 +415,15 @@ class DatasetParameter(models.Model): string_value = models.TextField(null=True, blank=True, db_index=True) numerical_value = models.FloatField(null=True, blank=True, db_index=True) datetime_value = models.DateTimeField(null=True, blank=True, db_index=True) - objects = OracleSafeManager() + #objects = OracleSafeManager() # Commented by Sindhu E for natural key support + + ''' Added by Sindhu Emilda for natural key implementation ''' + objects = DatasetParameterManager() + + def natural_key(self): + return (self.name.natural_key(),) + self.parameterset.natural_key() + + natural_key.dependencies = ['tardis_portal.ParameterName', 'tardis_portal.DatasetParameterSet'] def get(self): return _getParameter(self) @@ -350,13 +435,31 @@ class Meta: app_label = 'tardis_portal' ordering = ['name'] +class ExperimentParameterManager(OracleSafeManager): + """ + Added by Sindhu Emilda for natural key implementation. + The manager for the tardis_portal's ExperimentParameter model. + """ + def get_by_natural_key(self, namespace, name, nmspace, title, username): + return self.get(name=ParameterName.objects.get_by_natural_key(namespace, name), + parameterset=ExperimentParameterSet.objects.get_by_natural_key(nmspace, title, username), + ) + class ExperimentParameter(models.Model): parameterset = models.ForeignKey(ExperimentParameterSet) name = models.ForeignKey(ParameterName) string_value = models.TextField(null=True, blank=True, db_index=True) numerical_value = models.FloatField(null=True, blank=True, db_index=True) datetime_value = models.DateTimeField(null=True, blank=True, db_index=True) - objects = OracleSafeManager() + #objects = OracleSafeManager() # Commented by Sindhu E for natural key support + + ''' Added by Sindhu Emilda for natural key implementation ''' + objects = ExperimentParameterManager() + + def natural_key(self): + return (self.name.natural_key(),) + self.parameterset.natural_key() + + natural_key.dependencies = ['tardis_portal.ParameterName', 'tardis_portal.ExperimentParameterSet'] def save(self, *args, **kwargs): super(ExperimentParameter, self).save(*args, **kwargs) diff --git a/tardis/tardis_portal/models/token.py b/tardis/tardis_portal/models/token.py index 59ca91b296..130773d689 100644 --- a/tardis/tardis_portal/models/token.py +++ b/tardis/tardis_portal/models/token.py @@ -13,6 +13,17 @@ def _token_expiry(): import datetime as dt return dt.datetime.now().date() + dt.timedelta(settings.TOKEN_EXPIRY_DAYS) +class TokenManager(OracleSafeManager): + """ + Added by Sindhu Emilda for natural key implementation. + The manager for the tardis_portal's Token model. + """ + def get_by_natural_key(self, token, title, username): + return self.get(token=token, + experiment=Experiment.objects.get_by_natural_key(title, username), + user=User.objects.get_by_natural_key(username), + ) + class Token(models.Model): token = models.CharField(max_length=30, unique=True) @@ -24,7 +35,15 @@ class Token(models.Model): _TOKEN_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' - objects = OracleSafeManager() + #objects = OracleSafeManager() # Commented by Sindhu E for natural key support + + ''' Added by Sindhu Emilda for natural key implementation ''' + objects = TokenManager() + + def natural_key(self): + return (self.token,) + (self.experiment.natural_key(),) + self.user.natural_key() + + natural_key.dependencies = ['tardis_portal.Experiment', 'auth.User'] class Meta: app_label = 'tardis_portal'