def get_login_authorization(request): logger.debug("get_login_authorization") calipso_experiment_services = CalipsoExperimentsServices() json_settings_data = calipso_experiment_services.get_external_is_authorized( request.user.username) return JsonResponse(json_settings_data)
class Command(BaseCommand): help = 'Insert new user' experiments_services = CalipsoExperimentsServices() def add_arguments(self, parser): parser.add_argument('--userlogin', dest='userlogin', default='', help='The username', type=str) def handle(self, *args, **options): username = options['userlogin'] if not username: raise CommandError( 'python manage.py insert_user --userlogin username') try: User.objects.get(username=username) self.stdout.write( self.style.ERROR('User %s already exists' % username)) except User.DoesNotExist as udne: new_user = User.objects.create_user(username, '') new_user.save() self.stdout.write( self.style.SUCCESS('Successfully inserted user %s' % username))
class Command(BaseCommand): help = 'Remove experiment' experiments_services = CalipsoExperimentsServices() def add_arguments(self, parser): parser.add_argument('--public_number', dest='public_number', default='', help='The public number of the experiment', type=str) def handle(self, *args, **options): public_number = options['public_number'] if not public_number: raise CommandError( 'python manage.py remove_experiment --public_number code') try: self.experiments_services.remove_experiment(public_number) self.stdout.write( self.style.SUCCESS('Successfully removed experiment "%s"' % public_number)) except Exception as e: raise CommandError( 'Can not be able to remove experiment with this public number: %s, error:%s' % (public_number, e))
class Command(BaseCommand): help = 'Add experiment' experiments_services = CalipsoExperimentsServices() def add_arguments(self, parser): parser.add_argument('--public_number', dest='public_number', default='', help='The public number of the experiment', type=str) parser.add_argument('--title', dest='title', default='', help='The title of the experiment', type=str) parser.add_argument('--description', dest='description', default='', help='The description of the experiment', type=str) parser.add_argument('--beamline_code', dest='beamline_code', default='', help='The beam line code of experiment', type=str) def handle(self, *args, **options): public_number = options['public_number'] title = options['title'] description = options['description'] beamline_code = options['beamline_code'] if not public_number or not title or not description or not beamline_code: raise CommandError( 'python manage.py add_experiment --public_number public_number' ' --title title --description description --beamline_code beam line coe' ) try: self.experiments_services.add_experiment(public_number, title, description, beamline_code) self.stdout.write( self.style.SUCCESS('Successfully added experiment "%s"' % public_number)) except Exception as e: raise CommandError( 'Can not be able to add experiment with this public number: %s, error:%s' % (public_number, e))
class Command(BaseCommand): help = 'Add experiment from csv file' experiments_services = CalipsoExperimentsServices() def add_arguments(self, parser): parser.add_argument('--path', dest='path', default='', help='absolute csv filename path', type=str) def handle(self, *args, **options): done = 0 path = options['path'] if not path: raise CommandError( 'python manage.py add_experiment_csv --path absolute-file-path' ) try: with open(path) as csv_file: reader = csv.DictReader(csv_file) for line, row in enumerate(reader, start=2): public_number = row['public_number'] title = row['title'] description = row['description'] beamline_code = row['beamline_code'] try: self.experiments_services.add_experiment( public_number, title, description, beamline_code) self.stdout.write( self.style.SUCCESS('line %d ..%s Ok!' % (line, public_number))) done += 1 except Exception as e: self.stdout.write( self.style.ERROR('line %d error:%s' % (line, e))) self.stdout.write( self.style.SUCCESS('File processed. %d/%d done.!' % (done, line - 1))) except Exception as e: raise CommandError('Problems, %s' % e)
class Command(BaseCommand): help = 'Remove user from experiment' experiments_services = CalipsoExperimentsServices() def add_arguments(self, parser): parser.add_argument('--userlogin', dest='userlogin', default='', help='The title of the experiment', type=str) parser.add_argument('--public_number', dest='public_number', default='', help='The public number of the experiment', type=str) def handle(self, *args, **options): username = options['userlogin'] public_number = options['public_number'] if not public_number or not username: raise CommandError( 'python manage.py remove_user --userlogin username --public_number public_number' ) try: self.experiments_services.remove_user_from_experiment( username, public_number) self.stdout.write( self.style.SUCCESS( 'Successfully removed user %s from experiment:%s' % (username, public_number))) except Exception as e: raise CommandError( 'Can not be able to remove user:%s from experiment: %s, error:%s' % (username, public_number, e))
class Command(BaseCommand): help = 'Inserts users from csv file' experiments_services = CalipsoExperimentsServices() def add_arguments(self, parser): parser.add_argument('--path', dest='path', default='', help='Absolute csv filename path', type=str) def handle(self, *args, **options): done = 0 path = options['path'] if not path: raise CommandError( 'python manage.py insert_users_csv --path=absolute-file-path') try: with open(path) as csv_file: reader = csv.DictReader(csv_file) for line, row in enumerate(reader, start=2): username = row['userlogin'] try: User.objects.get(username=username) self.stdout.write(self.style.ERROR('line %d, User %s already exists' % (line,username))) except User.DoesNotExist as udne: new_user = User.objects.create_user(username, '') new_user.save() self.stdout.write( self.style.SUCCESS('line %d Successfully inserted user %s' % (line, username))) done += 1 self.stdout.write(self.style.SUCCESS('File processed. %d/%d done.!' % (done, line-1))) except Exception as e: raise CommandError('Problems, %s' % e)
def setUp(self): self.logger.debug('#### setUp ExperimentServiceTestCase START ####') self.service = CalipsoExperimentsServices() self.logger.debug('#### setUp ExperimentServiceTestCase END ####')
class ExperimentServiceTestCase(APITestCase): logger = logging.getLogger(__name__) fixtures = ['users.json', 'experiments.json'] def setUp(self): self.logger.debug('#### setUp ExperimentServiceTestCase START ####') self.service = CalipsoExperimentsServices() self.logger.debug('#### setUp ExperimentServiceTestCase END ####') def test_add_two_experiments_to_user(self): self.logger.debug( '#### TEST test_add_two_experiments_to_user START ####') calipso_user = CalipsoUser.objects.get(pk=1) experiment_1 = CalipsoExperiment.objects.get(pk=1) experiment_2 = CalipsoExperiment.objects.get(pk=2) self.service.add_user_to_experiment( username=calipso_user.user.username, public_number=experiment_1.serial_number) self.service.add_user_to_experiment( username=calipso_user.user.username, public_number=experiment_2.serial_number) experiments = self.service.get_user_experiments( calipso_user.user.username) self.assertEqual(len(experiments), 2) self.logger.debug( '#### TEST test_add_two_experiments_to_user END ####') def test_remove_user_from_experiment(self): self.logger.debug( '#### TEST test_remove_user_from_experiment START ####') calipso_user = CalipsoUser.objects.get(pk=1) experiment_1 = CalipsoExperiment.objects.get(pk=1) experiment_2 = CalipsoExperiment.objects.get(pk=2) self.service.add_user_to_experiment( username=calipso_user.user.username, public_number=experiment_1.serial_number) self.service.add_user_to_experiment( username=calipso_user.user.username, public_number=experiment_2.serial_number) self.service.remove_user_from_experiment( username=calipso_user.user.username, public_number=experiment_1.serial_number) self.service.remove_user_from_experiment( username=calipso_user.user.username, public_number=experiment_2.serial_number) experiments = self.service.get_user_experiments( calipso_user.user.username) self.assertEqual(len(experiments), 0) self.logger.debug( '#### TEST test_remove_user_from_experiment END ####') def test_update_title_experiment(self): self.logger.debug('#### TEST test_update_title_experiment START ####') experiment_1 = CalipsoExperiment.objects.get(pk=1) self.assertEqual(experiment_1.subject, "Experiment 1") self.service.update_experiment( beamline_code=experiment_1.beam_line, description=experiment_1.body, public_number=experiment_1.serial_number, title="Experiment A") experiment = CalipsoExperiment.objects.get(pk=1) self.assertEqual(experiment.subject, "Experiment A") self.logger.debug('#### TEST test_update_title_experiment END ####') def test_update_description_experiment(self): self.logger.debug( '#### TEST test_update_description_experiment START ####') experiment_1 = CalipsoExperiment.objects.get(pk=1) self.assertEqual(experiment_1.body, "Description of experiment 1") self.service.update_experiment( beamline_code=experiment_1.beam_line, description="New description", public_number=experiment_1.serial_number, title=experiment_1.subject) experiment = CalipsoExperiment.objects.get(pk=1) self.assertEqual(experiment.body, "New description") self.logger.debug( '#### TEST test_update_description_experiment END ####')
from rest_framework import pagination, filters from rest_framework.exceptions import PermissionDenied from rest_framework.generics import ListAPIView from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from apprest.serializers.experiment import CalipsoExperimentSerializer from apprest.services.experiment import CalipsoExperimentsServices from calipsoplus.settings_calipso import PAGE_SIZE_EXPERIMENTS from django_filters.rest_framework import DjangoFilterBackend service = CalipsoExperimentsServices() class ExperimentsPagination(pagination.PageNumberPagination): page_size = PAGE_SIZE_EXPERIMENTS max_page_size = 20 def get_paginated_response(self, data): return Response({ 'next': self.get_next_link(), 'previous': self.get_previous_link(), 'count': self.page.paginator.count, 'results': data, 'page_size': self.page_size, })
def get_login_type(request): calipso_experiment_services = CalipsoExperimentsServices() json_settings_data = calipso_experiment_services.get_external_is_staff( request.user.username) return JsonResponse(json_settings_data)