Пример #1
0
def search_study_lines(request):
    """ Autocomplete search on lines in a study."""
    study_pk = request.GET.get('study', '')
    name_regex = re.escape(request.GET.get('term', ''))
    active_param = request.GET.get('active', None)
    active_value = 'true' == active_param if active_param in ('true', 'false') else None
    active = qfilter(value=active_value, fields=['active'])
    user = request.user

    if (not study_pk) or (not study_pk.isdigit()):
        raise ValidationError('study parameter is required and must be a valid integer')

    permission_check = Study.user_permission_q(user, StudyPermission.READ)
    # if the user's admin / staff role gives read access to all Studies, don't bother querying
    # the database for specific permissions defined on this study
    if Study.user_role_can_read(user):
        permission_check = Q()
    try:
        # Note: distinct() necessary in case the user has multiple permission paths to access
        # the study (e.g. individual and group permissions)
        study = Study.objects.filter(permission_check, pk=study_pk).distinct().get()
        query = study.line_set.filter(active)

    # if study doesn't exist or requesting user doesn't have read access, return an empty
    # set of lines
    except Study.DoesNotExist as e:
        query = Line.objects.none()

    query = query.filter(Q(name__iregex=name_regex) | Q(strains__name__iregex=name_regex))
    query = optional_sort(request, query)
    query = query.values('name', 'id')[:DEFAULT_RESULT_COUNT]
    return JsonResponse({
        'rows': list(query),
    })
Пример #2
0
 def _check_study_access(self, request, study_pk):
     access = Study.access_filter(request.user, StudyPermission.WRITE)
     writeable_studies = Study.objects.filter(access).distinct()
     try:
         self.study = writeable_studies.get(pk=study_pk)
     except Study.DoesNotExist:
         return self.send_error_response(
             _("Not Allowed"),
             _("You do not have permissions to modify this Study."),
             status=codes.forbidden,
         )
Пример #3
0
def search_study_lines(request):
    """ Autocomplete search on lines in a study."""
    study_pk = request.GET.get('study', '')
    name_regex = re.escape(request.GET.get('term', ''))
    user = request.user

    if (not study_pk) or (not study_pk.isdigit()):
        raise ValidationError(
            'study parameter is required and must be a valid integer')

    permission_check = Study.user_permission_q(user, StudyPermission.READ)
    # if the user's admin / staff role gives read access to all Studies, don't bother querying
    # the database for specific permissions defined on this study
    if Study.user_role_can_read(user):
        permission_check = Q()
    try:
        # Note: distinct() necessary in case the user has multiple permission paths to access
        # the study (e.g. individual and group permissions)
        study = Study.objects.filter(permission_check,
                                     pk=study_pk).distinct().get()
        query = study.line_set.all()

    # if study doesn't exist or requesting user doesn't have read acccess, return an empty
    # set of lines
    except Study.DoesNotExist as e:
        query = Line.objects.none()

    name_filters = [
        Q(name__iregex=name_regex),
        Q(strains__name__iregex=name_regex)
    ]
    query = query.filter(reduce(operator.or_, name_filters,
                                Q()))[:DEFAULT_RESULT_COUNT]
    return JsonResponse({
        'rows': [{
            'name': line.name,
            'id': line.id,
        } for line in query],
    })
Пример #4
0
 def study_columns(self):
     from main.models import Study
     return Study.export_columns(self.studies)
def crowd_new_study(request):

    if request.is_ajax() and UserType.objects.get(
            user_id=request.user.id).type == 'director':

        # shouldnt you add the condition 'and request.POST.get('btnType') == "create_study"' here?

        # get the attributes of the study (from the fields on the template)
        title = request.POST.get('study_title')
        print(request.POST.get('study_title'))
        labels = request.POST.getlist('labels[]')
        segment_duration = request.POST.get('segment_duration')
        step_size = request.POST.get('step_size')
        max_responses = request.POST.get('max_responses')
        max_segment_responses = request.POST.get('max_segment_responses')
        min_segment_responses = request.POST.get('min_segment_responses')
        threshold = request.POST.get('threshold')
        file_ids = request.POST.getlist('file_ids[]')

        # generate the random access code for workers to join the study
        code_length = 50
        code = ''.join(random.SystemRandom().choice(string.ascii_uppercase +
                                                    string.digits)
                       for _ in range(code_length))

        # make a new Study using the above (files and labels go into their own tables though) and the current user
        study = Study(title=title,
                      owner=request.user,
                      segment_duration=segment_duration,
                      step_size=step_size,
                      max_total_responses=max_responses,
                      max_responses_per_segment=max_segment_responses,
                      min_responses_per_segment=min_segment_responses,
                      threshold=threshold,
                      code=code)

        study.save()

        # label table
        for label in labels:
            new_label = Label(label_title=label, study=study)
            new_label.save()

        # files and segments
        for file_id in file_ids:
            file = File.objects.get(id=file_id)

            # link the study to each file
            new_study_file = StudyFile(study=study, file=file)
            new_study_file.save()

            # segments
            # get duration of file
            sound = AudioSegment.from_file(file.file.path, format="wav")

            duration_in_seconds = len(sound) // 1000
            # print(duration_in_seconds)
            start_times = [
                x * float(study.step_size)
                for x in range(0, duration_in_seconds)
            ]
            # print(start_times)
            for i in start_times:
                if (i + float(study.segment_duration)) < duration_in_seconds:
                    new_seg = Segment(start=i,
                                      stop=i + float(study.segment_duration),
                                      duration=study.segment_duration,
                                      study=study,
                                      file=file,
                                      status='low_priority')
                    new_seg.save()

        data = {'res': 'it worked'}

        # return if it was successful to the client
        return render_to_json_response(data)

    # ensure user is logged in and a director
    if (not request.user.is_authenticated()) or UserType.objects.get(
            user_id=request.user.id).type != 'director':
        return redirect('/')
    else:
        user_files = File.objects.filter(uploader=request.user)
        template = loader.get_template('main/create_study.html')
        fullname = request.user.first_name + ' ' + request.user.last_name  # get name of current user
        context = {'fullname': fullname, 'user_files': user_files}
        return HttpResponse(template.render(context, request))
    def handle(self, *args, **options):
        regions = [{
            'name': 'Красноярский край'
        }, {
            'name': 'Новосибирская область'
        }, {
            'name': 'Республика Хакассия'
        }, {
            'name': 'Москва'
        }]

        organizations = [{
            'name': 'Пионер - лагерь "Солнечный"',
            'region': 'Красноярский край',
            'site': '',
            'addres': 'с.Тесь',
            'phone': 'none'
        }, {
            'name': 'Мелкая конторка',
            'region': 'Красноярский край',
            'site': '',
            'addres': 'г.Минусинск',
            'phone': 'none'
        }, {
            'name': 'ЗАО "Мегафон"',
            'region': 'Новосибирская область',
            'site': 'megafonsib.ru',
            'addres': 'г.Новосибирск, Красный проспект, 86',
            'phone': '+7 (923) 250-82-81'
        }, {
            'name': 'ЗАО "ЦФТ"',
            'region': 'Новосибирская область',
            'site': 'cft.ru',
            'addres': 'г.Новосибирск, Мусы Джалиля, 11',
            'phone': '+7 (383) 336-49-49'
        }, {
            'name': 'ХТИ КГТУ',
            'region': 'Республика Хакассия',
            'site': 'khti.sfu-kras.ru',
            'addres': 'г.Абакан, Щетинкина, 27',
            'phone': '+7 (3902) 22‒53‒55'
        }, {
            'name': 'Сибинфорцентр',
            'region': 'Новосибирская область',
            'site': 'sibinfo.ru',
            'addres': 'г. Новосибирск, ул. Коммунистическая, 48а, офис 515',
            'phone': '+7 (383)36-200-36'
        }, {
            'name': 'Опыт',
            'region': 'Новосибирская область',
            'site': '',
            'addres': 'none',
            'phone': 'none'
        }, {
            'name': 'GeekBrains',
            'region': 'Москва',
            'site': 'geekbrains.ru',
            'addres': 'Москва, Ленинградский просп., 39, стр. 6',
            'phone': '8 (800) 505‑61-05'
        }]
        works = [{
            'organization': 'Пионер - лагерь "Солнечный"',
            'region': 'Красноярский край',
            'site': '',
            'position': 'Вожатый отряда',
            'duties': 'Обеспечение досуга и контроль детей',
            'period': '3'
        }, {
            'organization': 'Мелкая конторка',
            'region': 'Красноярский край',
            'site': '',
            'position': 'Мастер починки сотовых телефонов',
            'duties': 'Починка сотовых телефонов',
            'period': '5'
        }, {
            'organization': 'ЗАО "Мегафон"',
            'region': 'Новосибирская область',
            'site': 'megafonsib.ru',
            'position': 'Сервисный инженер',
            'duties': 'Поддержка пользователей',
            'period': '12'
        }, {
            'organization': 'ЗАО "Мегафон"',
            'region': 'Новосибирская область',
            'site': 'megafonsib.ru',
            'position': 'Системный администратор',
            'duties':
            'Обеспечение работоспособности промышленного Active Directory',
            'period': '12'
        }, {
            'organization': 'ЗАО "ЦФТ"',
            'region': 'Новосибирская область',
            'site': 'cft.ru',
            'position': 'Инженер сопровождения',
            'duties': 'Поддержка пользователей сервиса денежных переводов',
            'period': '24'
        }, {
            'organization': 'ЗАО "ЦФТ"',
            'region': 'Новосибирская область',
            'site': 'cft.ru',
            'position': 'Инженер прикладного сопровождения сервиса',
            'duties':
            'Анализ работы сервиса денежных переводов и постановка задач на доработку и исправление дефектов',
            'period': '24'
        }, {
            'organization': 'ЗАО "ЦФТ"',
            'region': 'Новосибирская область',
            'site': 'cft.ru',
            'position': 'Инженер прикладного администрирования',
            'duties': 'Инженер прикладного администрирования',
            'period': '24'
        }, {
            'organization': 'ЗАО "ЦФТ"',
            'region': 'Новосибирская область',
            'site': 'cft.ru',
            'position': 'Инженер прикладного сопровождения банковской системы',
            'duties': 'Поддержка пользователей банковской системы',
            'period': '30'
        }]

        hobbies = [{
            'hobbyname': 'Фехтование',
            'hobbylevel': '6',
            'period': '120'
        }, {
            'hobbyname': 'Фотография',
            'hobbylevel': '2',
            'period': '48'
        }, {
            'hobbyname': 'Автоспорт',
            'hobbylevel': '2',
            'period': '999'
        }, {
            'hobbyname': 'Автомобили',
            'hobbylevel': '4',
            'period': '999'
        }, {
            'hobbyname': 'Программирование',
            'hobbylevel': '2',
            'period': '6'
        }]

        studies = [{
            'organization': 'ХТИ КГТУ',
            'site': 'khti.sfu-kras.ru',
            'region': 'Республика Хакассия',
            'course':
            'Высшее техническое образование по автоматизации проектирования в машиностроении',
            'period': '60'
        }, {
            'organization': 'Сибинфорцентр',
            'site': 'sibinfo.ru',
            'region': 'Новосибирская область',
            'course': 'Oracle DBA level 1',
            'period': '1'
        }, {
            'organization': 'Сибинфорцентр',
            'site': 'sibinfo.ru',
            'region': 'Новосибирская область',
            'course': 'Oracle DBA level 2',
            'period': '1'
        }, {
            'organization': 'Сибинфорцентр',
            'site': 'sibinfo.ru',
            'region': 'Новосибирская область',
            'course': 'Oracle SQL Tuning',
            'period': '1'
        }, {
            'organization': 'Сибинфорцентр',
            'site': 'sibinfo.ru',
            'region': 'Новосибирская область',
            'course': 'Linux(Debian-family)',
            'period': '1'
        }, {
            'organization': 'Опыт',
            'site': '',
            'region': 'Новосибирская область',
            'course': 'Unix Solaris',
            'period': '48'
        }, {
            'organization': 'Сибинфорцентр',
            'site': 'sibinfo.ru',
            'region': 'Новосибирская область',
            'course': 'Shell(Bash)',
            'period': '1'
        }, {
            'organization': 'GeekBrains',
            'site': 'geekbrains.ru',
            'region': 'Москва',
            'course': 'HTML/CSS+Bootstrap',
            'period': '1'
        }, {
            'organization': 'GeekBrains',
            'site': 'geekbrains.ru',
            'region': 'Москва',
            'course': 'Javascript',
            'period': '1'
        }, {
            'organization': 'GeekBrains',
            'site': 'geekbrains.ru',
            'region': 'Москва',
            'course': 'Python3',
            'period': '1'
        }, {
            'organization': 'GeekBrains',
            'site': 'geekbrains.ru',
            'region': 'Москва',
            'course': 'Django',
            'period': '1'
        }]

        Region.objects.all().delete()
        for region in regions:
            region = Region(**region)
            region.save()

        Organization.objects.all().delete()
        for organization in organizations:
            reg_name = organization["region"]
            region = Region.objects.get(name=reg_name)
            organization['region'] = region
            organization = Organization(**organization)
            organization.save()

        Work.objects.all().delete()
        for work in works:
            org_name = work["organization"]
            reg_name = work["region"]
            site_name = work["site"]
            region = Region.objects.get(name=reg_name)
            organization = Organization.objects.get(name=org_name)
            organization = Organization.objects.get(site=site_name)
            work['organization'] = organization
            work['region'] = region
            work = Work(**work)
            work.save()

        Aboutme.objects.all().delete()
        for hobby in hobbies:
            hobby = Aboutme(**hobby)
            hobby.save()

        Study.objects.all().delete()
        for study in studies:
            org_name = study["organization"]
            reg_name = study["region"]
            region = Region.objects.get(name=reg_name)
            organization = Organization.objects.get(name=org_name)
            study['organization'] = organization
            study['region'] = region
            study = Study(**study)
            study.save()
Пример #7
0
def _process_read(read_label, raw_datablocks):
    """ Categorize each type of datablock """
    logging.debug("_process_read()")
    datablock = None
    metadata_entries = {}
    metadatablocks = []
    logging.debug("categorizing datablocks")
    for block in raw_datablocks:
        label = block[1]
        data = block[2]
        is_control = False
        if label == 'Data':
            # Read in a 96 well grid of some measurements
            datablock = block
        elif label == 'ControlData':
            # TODO: Add in example
            datablock = block
            is_control = True
        elif label == 'Metadata':
            # Read in a dictionary of metadata items
            metadata_entries.update(data)
        else:
            # Read in a 96 well grid of some metadata item
            metadatablocks.append(block)

    # Get the user identity
    logging.debug('getting user')
    if not metadata_entries['experimenter_ID']:
        raise Exception("An experimenter_ID that matches your EDD username is required to submit "
                        "a sheet.")
    User = get_user_model()
    experimenter = User.objects.get(username=metadata_entries['experimenter_ID'])
    if not isinstance(experimenter, User):
        raise Exception("User id \"%s\" was not found, or did not return unique results." %
                        metadata_entries['experimenter_ID'])
    # NOTE: removed modification of User object (email); do not modify auth values from import code
    if (metadata_entries['experimenter_email'] and
            experimenter.email and
            experimenter.email.upper() != metadata_entries['experimenter_email'].upper()):
        raise Exception("User email \"%s\" did not match the email in the database." %
                        metadata_entries['experimenter_email'])

    # Generate or collect Study
    if 'Study_name' not in metadata_entries:
        raise Exception("Study_name must be given!")
    study_name = metadata_entries['Study_name']
    study = None
    try:
        study = Study.objects.get(name=study_name)
        if study.contact_id != experimenter.id:
            raise Exception("The existing study \"%s\" is not associated with the "
                            " experimenter \"%s\"" % (study_name, experimenter.username))
    except Study.DoesNotExist:
        study = Study(name=study_name, contact=experimenter)
    if metadata_entries.get('extra_contact_information', None):
        study.contact_extra = metadata_entries.get('extra_contact_information', None)
    if 'Study_description' in metadata_entries:
        study.study_description = metadata_entries['Study_description']
    # TODO: Expand exception text support better feedback to handle multiple reads (read_label)
    study.save()

    # Add metadata to study hstore
    # TODO: move some of these to Line after disambiguation
    # NOTE: all of these are line metadata, not study metadata
    logger.debug('study hstore population')
    if 'machine_internal_temperature_unit' in metadata_entries:
        try:
            temp = MetadataType.objects.get(type_name='Machine internal temperature')
            if temp.postfix != metadata_entries['machine_internal_temperature_unit']:
                raise Exception("Machine internal temperature given in unknown unit \"%s\", "
                                "expected unit \"%s\"" % (
                                    metadata_entries['machine_internal_temperature_unit'],
                                    temp.postfix))
        except MetadataType.DoesNotExist:
            raise Exception("ERROR: 'Machine internal temperature' MetadataType not found "
                            "in database.")
    if metadata_entries['machine_internal_temperature']:
        pass

    # TODO: CONSIDER: Migrate to Line
    if 'device_name' not in metadata_entries:
        raise Exception("Device name not given!")

    try:
        reaction_temp_type = MetadataType.objects.get(type_name='Well reaction temperature')
        if 'well_reaction_temperature_unit' in metadata_entries:
            if reaction_temp_type.postfix != metadata_entries['well_reaction_temperature_unit']:
                raise Exception("Well reaction temperature given in unknown unit \"%s\", "
                                "expected unit \"%s\"" % (
                                    metadata_entries['well_reaction_temperature_unit'],
                                    reaction_temp_type.postfix))
    except MetadataType.DoesNotExist:
        raise Exception("ERROR: MetadataType 'Well reaction temperature' not found in database")

    try:
        shaking_speed = MetadataType.objects.get(type_name='Shaking speed')
        if 'shaking_speed_unit' in metadata_entries:
            if not metadata_entries['shaking_speed_unit'] == shaking_speed.postfix:
                raise Exception("Shaking speed given in unknown unit \"%s\"" %
                                metadata_entries['shaking_speed_unit'])
        if 'shaking_speed' in metadata_entries:
            pass
    except MetadataType.DoesNotExist:
        raise Exception("ERROR: MetadataType 'Shaking speed' not found in database")

    protocol = _collect_metadata_object_for_key(
        read_label, metadata_entries, 'protocol_name', Protocol)
    measurement_type = _collect_metadata_object_for_key(
        read_label, metadata_entries, 'measurement_type', MeasurementType)
    measurement_unit = _collect_metadata_object_for_key(
        read_label, metadata_entries, 'measurement_unit', MeasurementUnit)
    if not measurement_unit:
        raise Exception("measurement_unit not specified in read \"%s\"" % (read_label))

    # TODO: ALL ENZYES
    known_enzymes = {}
    for key in metadata_entries.keys():
        if key.startswith('Enzyme_'):
            logger.debug('Adding \"%s\" to known_enzymes' % metadata_entries[key])
            strain = Strain.objects.get(registry_id=metadata_entries[key])
            if not strain:
                raise Exception("The PartID for Enzyme \"%s\" was not recognized in read \"%s\"" %
                                (key, read_label))
            # TODO: deal with multi case
            known_enzymes[key] = strain

    # Handle measurements and well specific metadata
    logger.debug('Handle measurements and well specific metadata')

    # TODO: add merging for existing lines
    x = 0
    while x < 96:
        column = str((x % 12) + 1)
        row = chr(int(x / 12) + ord('A'))

        # TODO: Assay and Line metastore
        line = study.line_set.create(
            name='line %s%s from read %s' % (row, column, read_label), control=is_control,
            experimenter=experimenter, contact=experimenter)
        logger.debug(line.name)
        _update_line_metastore(read_label, x, row, column, line, metadatablocks, known_enzymes)
        line.save()
        assay = line.assay_set.create(
            name='assay %s%s from read %s' % (row, column, read_label), experimenter=experimenter,
            protocol=protocol)
        logger.debug(assay.name)
        _update_assay_metastore(read_label, x, row, column, assay, metadatablocks)
        assay.save()
        measurement = assay.measurement_set.create(
            experimenter=experimenter, measurement_type=measurement_type, x_units_id=1,
            y_units=measurement_unit)
        measurement.measurementvalue_set.create(
            x=[0, ], y=[datablock[2][x], ])
        x += 1
Пример #8
0
 def has_permission(self, request, view):
     result = super(StudyResourcePermissions,
                    self).has_permission(request, view)
     if request.method == 'POST':
         return result and Study.user_can_create(request.user)
     return result