Exemplo n.º 1
0
Arquivo: ek.py Projeto: trmznt/rhombus
def lookup(request):
    """ return JSON for autocomplete """
    q = request.params.get('q')
    g = request.params.get('g','')
    dbh = get_dbhandler()
    g_key = dbh.get_ekey(g)

    if not g_key:
        return error_page(request, "Parent EK not found!")

    if not q:
        return error_page(request, "Please provide the query as q.")

    q = '%' + q.lower() + '%'

    ekeys = dbh.EK.query(dbh.session()).filter( dbh.EK.key.ilike(q),
            dbh.EK.member_of_id == g_key.id)

    # formating for select2 consumption

    result = [
        { 'id': k.id, 'text': '%s [ %s ]' % (k.key, k.desc) }
        for k in ekeys]

    return result
Exemplo n.º 2
0
def action(request):

    method = request.params.get('_method', None)

    if method == 'add-assay-files':

        if not request.POST:
            return error_page(request, 'Only accept POST request!')

        sample_id = request.POST.get('sample_id')
        sample = get_dbhandler().get_sample_by_id(sample_id)

        request.session.flash(
            ('success', 'Sample code [%s] has been added with %d assay files' %
             (sample.code, len(request.params.getall('genaf-assay_file')))))

        return HTTPFound(
            location=request.route_url('genaf.sample-view', id=sample.id))

    elif method == 'delete':

        dbh = get_dbhandler()
        sample_ids = request.POST.getall('sample-ids')
        samples = [dbh.get_sample_by_id(sample_id) for sample_id in sample_ids]

        return Response(modal_delete % ''.join('<li>%s | %s</li>' %
                                               (s.code, s.batch.code)
                                               for s in samples))

    elif method == 'delete/confirm':

        dbh = get_dbhandler()
        sample_ids = request.POST.getall('sample-ids')
        for sample_id in sample_ids:
            sample = dbh.get_sample_by_id(sample_id)
            if not sample:
                request.session.flash(
                    ('error',
                     'Sample with ID [%d] does not exists' % sample_id))
                continue
            if not sample.batch.is_manageable(request.user):
                request.session.flash(
                    ('error',
                     'You are not authorized to delete sample [%s | %s]' %
                     (sample.code, sample.batch.code)))
                continue

            sample_code = '%s | %s' % (sample.code, sample.batch.code)
            dbh.session().delete(sample)
            request.session.flash(
                ('success', 'Sample [%s] has been deleted.' % sample_code))

        return HTTPFound(
            location=request.referrer or request.route_url('genaf.batch'))

    else:
        return error_page(request, 'Unknown method!')
Exemplo n.º 3
0
def action(request):

    method = request.params.get('_method', None)

    if method == 'add-assay-files':

        if not request.POST:
            return error_page(request, 'Only accept POST request!')

        sample_id = request.POST.get('sample_id')
        sample = get_dbhandler().get_sample_by_id( sample_id )

        request.session.flash(
            (   'success',
                'Sample code [%s] has been added with %d assay files' % ( sample.code,
                            len(request.params.getall('genaf-assay_file')) )))

        return HTTPFound(location = request.route_url('genaf.sample-view', id=sample.id))

    elif method == 'delete':

        dbh = get_dbhandler()
        sample_ids = request.POST.getall('sample-ids')
        samples = [ dbh.get_sample_by_id(sample_id) for sample_id in sample_ids ]

        return Response(
            modal_delete % ''.join('<li>%s | %s</li>' % (s.code, s.batch.code) for s in samples )
        )

    elif method == 'delete/confirm':

        dbh = get_dbhandler()
        sample_ids = request.POST.getall('sample-ids')
        for sample_id in sample_ids:
            sample = dbh.get_sample_by_id(sample_id)
            if not sample:
                request.session.flash( ('error',
                    'Sample with ID [%d] does not exists' % sample_id) )
                continue
            if not sample.batch.is_manageable( request.user ):
                request.session.flash( ('error',
                    'You are not authorized to delete sample [%s | %s]'
                    % (sample.code, sample.batch.code) ))
                continue

            sample_code = '%s | %s' % (sample.code, sample.batch.code)
            dbh.session().delete( sample )
            request.session.flash(
                ('success', 'Sample [%s] has been deleted.' % sample_code ) )

        return HTTPFound( location = request.referrer or
            request.route_url( 'genaf.batch' ) )


    else:
        return error_page(request, 'Unknown method!')
Exemplo n.º 4
0
Arquivo: ek.py Projeto: trmznt/rhombus
def action(request):

    if not request.POST:
        return error_page()

    method = request.POST.get('_method')

    if method == 'delete':
        ids = request.POST.getall('ek-ids')
        eks = list( EK.query().filter( EK.id.in_( ids ) ) )

        if len(eks) == 0:
            return Response(modal_error)

        #return Response('Delete Enumerated Keys: ' + str(request.POST))
        return Response(modal_delete % ''.join( '<li>%s</li>' % x.key for x in eks ))

    elif method == 'delete/confirm':
        ids = request.POST.getall('ek-ids')

        count = 0
        for ek_id in ids:
            EK.delete( ek_id )
            count += 1

        request.session.flash( ('success', 'Successfully removed %d Enumerated Keys' % count) )

        return HTTPFound(location = request.referer)

    return Response(str(request.POST))
Exemplo n.º 5
0
def index(request):

    dbh = get_dbhandler()

    q = dbh.Sample.query(dbh.session()).join(dbh.Batch)

    batch_id = request.params.get('batch_id', False)
    location_id = request.params.get('location_id', False)

    if batch_id:
        batch = dbh.get_batch_by_id(batch_id)
        q = q.filter(dbh.Sample.batch_id == batch.id)
    else:
        batch = None

    if location_id:
        q = q.filter(dbh.Sample.location_id == location_id)

    q = q.order_by(dbh.Sample.code)

    samples = q.all()

    mode = request.params.get('mode', 'fsa')

    if mode == 'meta':
        html, jscode = format_sampleinfo(samples, request)
    elif mode == 'fsa':
        html, jscode = format_samplefsa(samples, request)
    else:
        return error_page(request, 'No suitable mode provided!')

    if not request.user.has_roles(GUEST):

        if batch:
            add_button = ('New sample',
                          request.route_url('genaf.sample-edit',
                                            id=0,
                                            _query={'batch_id': batch.id}))
            others = p()[
                'Batch code:',
                a(batch.code,
                  href=request.route_url('genaf.batch-view', id=batch.id))]
        else:
            add_button = others = None

        bar = selection_bar('sample-ids',
                            action=request.route_url('genaf.sample-action'),
                            add=add_button,
                            others=others)
        html, jscode = bar.render(html, jscode)

    return render_to_response("genaf:templates/sample/index.mako", {
        'samples': samples,
        'batch': batch,
        'html': str(html),
        'code': jscode,
    },
                              request=request)
Exemplo n.º 6
0
Arquivo: ek.py Projeto: trmznt/rhombus
def view(request):
    """ view a EnumKey along with its members """
    ek_id = int(request.matchdict.get('id', -1))
    dbh = get_dbhandler()
    ek = dbh.EK.get(ek_id, dbh.session())
    if not ek:
        return error_page(request)

    return render_to_response('rhombus:templates/ek/view.mako',
            { 'ek': ek }, request = request )
Exemplo n.º 7
0
def index(request):

    dbh = get_dbhandler()

    q = dbh.Sample.query(dbh.session()).join(dbh.Batch)

    batch_id = request.params.get('batch_id', False)
    location_id = request.params.get('location_id', False)

    if batch_id:
        batch = dbh.get_batch_by_id( batch_id )
        q = q.filter( dbh.Sample.batch_id == batch.id )
    else:
        batch = None

    if location_id:
        q = q.filter( dbh.Sample.location_id == location_id )

    q = q.order_by( dbh.Sample.code )

    samples = q.all()

    mode = request.params.get('mode','fsa')

    if mode == 'meta':
        html, jscode = format_sampleinfo(samples, request)
    elif mode == 'fsa':
        html, jscode = format_samplefsa(samples, request)
    else:
        return error_page(request, 'No suitable mode provided!')

    if not request.user.has_roles(GUEST):

        if batch:
            add_button = (  'New sample',
                        request.route_url('genaf.sample-edit', id=0, _query={ 'batch_id': batch.id })
            )
            others = p()[   'Batch code:',
                        a(batch.code, href=request.route_url('genaf.batch-view', id=batch.id))
            ]
        else:
            add_button = others = None

        bar = selection_bar('sample-ids', action=request.route_url('genaf.sample-action'),
                add=add_button, others=others)
        html, jscode = bar.render(html, jscode)

    return render_to_response("genaf:templates/sample/index.mako",
                    {   'samples': samples,
                        'batch': batch,
                        'html': str(html),
                        'code': jscode,
                    },
                    request = request)
Exemplo n.º 8
0
Arquivo: ek.py Projeto: trmznt/rhombus
def edit(request):
    """ edit a EnumKey """

    dbh = get_dbhandler()
    ek_id = int(request.matchdict.get('id', -1))
    if ek_id < 0:
        return error_page(request)
    if ek_id == 0:
        ek = dbh.EK()
        ek.id = 0
        ek.member_of_id = int(request.params.get('member_of_id', 0))
    else:
        ek = dbh.EK.get(ek_id)

    eform = edit_form(ek, dbh, request)

    return render_to_response('rhombus:templates/ek/edit.mako',
            { 'ek': ek, 'form': eform }, request = request )
Exemplo n.º 9
0
def lookup(request):
    """ return JSON for autocomplete """
    q = request.params.get('q')

    if not q:
        return error_page(request)

    q = '%' + q.lower() + '%'

    dbh = get_dbhandler()
    users = dbh.User.query(dbh.session()).filter( or_( dbh.User.login.ilike(q),
            dbh.User.lastname.ilike(q), dbh.User.firstname.ilike(q)) )

    # formating for select2 consumption

    result = [
        { 'id': u.id, 'text': u.render()}
        for u in users]

    return result
Exemplo n.º 10
0
Arquivo: ek.py Projeto: trmznt/rhombus
def save(request):
    """ save a EnumKey """
    ek_id = int(request.matchdict.get('id', -1))
    dbh = get_dbhandler()
    ek = parse_form(request.POST, dbh)
    if ek_id == 0:
        session = dbh.session()
        session.add( ek )
        session.flush()
        db_ek = ek
    else:
        db_ek = dbh.EK.get(ek_id)
        if not db_ek:
            return error_page(request)
        db_ek.update( ek )

    if ek_id != 0:
        location = request.route_url('rhombus.ek-view', id=ek_id)
    elif ek.member_of_id:
        location = request.route_url('rhombus.ek-view', id = ek.member_of_id)
    else:
        location = request.route_url('rhombus.ek-view', id = ek.id)

    return HTTPFound( location = location )
Exemplo n.º 11
0
def edit(request):
    """ editing sample metadata """

    sample_id = int(request.matchdict.get('id'))
    if sample_id < 0:
        return error_page(request, 'Please provide valid sample ID')

    dbh = get_dbhandler()

    if request.method == 'GET':
        sample = dbh.get_sample_by_id(sample_id)

        batch = sample.batch

        # XXX: check authorization here: whether current user belongs to batch owner group
        if not request.user.in_group(batch.group):
            return error_page(request,
                              'User is not a member of batch: %s' % batch.code)

        # prepare form

        eform = edit_form(sample, dbh, request)

        return render_to_response("genaf:templates/sample/edit.mako", {
            'sample': sample,
            'eform': eform,
        },
                                  request=request)

    elif request.method == 'POST':

        sample_d = parse_form(request.POST)
        if sample_d['id'] != sample_id:
            return error_page(request, 'Inconsistent sample ID!')

        try:
            if sample_id == 0:

                pass

            else:

                sample = dbh.get_sample_by_id(sample_id)
                batch = sample.batch

                # authorisation: current user must belongs to batch owner group
                if not request.user.in_group(batch.group):
                    return error_page(
                        request,
                        'User is not a member of batch: %s' % batch.code)

                sample.update(sample_d)
                dbh.session().flush()
                request.session.flash(
                    ('success', 'Sample [%s] has been updated.' % sample.code))

        except RuntimeError as err:
            return error_Page(request, str(err))

        except sqlalchemy.exc.IntegrityError as err:
            dbh.session().rollback()
            detail = err.args[0]
            if not sample.id: sample.id = sample_id
            editform = edit_form(sample, dbh, request)
            # use constraint name to detect error type
            if 'uq_samples_code_batch_id' in detail:
                editform.get('genaf-sample_code').add_error(
                    'The sample code: %s is '
                    'already being used. Please use other sample code!' %
                    sample_d['code'])
            r = render_to_response("genaf:templates/sample/edit.mako", {
                'sample': None,
                'eform': editform,
            },
                                   request=request)
            transaction.abort()
            return r

        return HTTPFound(
            location=request.route_url('genaf.sample-view', id=sample.id))

    return error_page(request, 'Invalid method!')
Exemplo n.º 12
0
def edit(request):

    user_id = int(request.matchdict['id'])
    if user_id < 0:
        return error_page(request, 'Please provide userclass ID')

    dbh = get_dbhandler()

    if request.method == 'GET':

        if user_id == 0:
            user = dbh.User()
            user.id = 0

        else:
            user = dbh.get_user(user_id)

        editform = edit_form(user, dbh, request)

        return render_to_response( "rhombus:templates/generics/page.mako",
                {   'html': editform,
                }, request = request
        )

    elif request.POST:

        user_d = parse_form( request.POST )
        if user_d['id'] != user_id:
            return error_page(request, "Inconsistent data!")

        try:
            if user_id == 0:
                # create a new user

                userclass = dbh.get_userclass( user_d['userclass_id'])
                user = userclass.add_user(
                    login = user_d['login'],
                    lastname = user_d['lastname'],
                    firstname = user_d['firstname'],
                    email = user_d['email'],
                    primarygroup = user_d['primarygroup_id'],

                )
                user.institution = user_d['institution']
                dbh.session().flush()
                request.session.flash(
                    (   'success',
                        'User [%s] has been created.' % user.login )
                )

            else:
                user = dbh.get_user(user_id)
                user.update( user_d )
                dbh.session().flush()

        except RuntimeError as err:
            return error_page(request, str(err))

        except:
            raise

        return HTTPFound(location = request.route_url('rhombus.user-view', id=user.id))

    raise NotImplementedError()
Exemplo n.º 13
0
def edit(request):
    """ editing sample metadata """

    sample_id = int(request.matchdict.get('id'))
    if sample_id < 0:
        return error_page(request, 'Please provide valid sample ID')

    dbh = get_dbhandler()

    if request.method == 'GET':
        sample = dbh.get_sample_by_id( sample_id )

        batch = sample.batch

        # XXX: check authorization here: whether current user belongs to batch owner group
        if not request.user.in_group( batch.group ):
            return error_page(request, 'User is not a member of batch: %s' % batch.code)


        # prepare form

        eform = edit_form(sample, dbh, request)

        return render_to_response("genaf:templates/sample/edit.mako",
                    {   'sample': sample,
                        'eform': eform,
                    },
                    request = request)

    elif request.method == 'POST':

        sample_d = parse_form( request.POST )
        if sample_d['id'] != sample_id:
            return error_page(request, 'Inconsistent sample ID!')

        try:
            if sample_id == 0:

                pass

            else:

                sample = dbh.get_sample_by_id( sample_id )
                batch = sample.batch

                # authorisation: current user must belongs to batch owner group
                if not request.user.in_group( batch.group ):
                    return error_page(request,
                        'User is not a member of batch: %s' % batch.code)

                sample.update(sample_d)
                dbh.session().flush()
                request.session.flash(
                    (   'success',
                        'Sample [%s] has been updated.' % sample.code )
                )


        except RuntimeError as err:
            return error_Page(request, str(err))

        except sqlalchemy.exc.IntegrityError as err:
            dbh.session().rollback()
            detail = err.args[0]
            if not sample.id: sample.id = sample_id
            editform = edit_form(sample, dbh, request)
            # use constraint name to detect error type
            if 'uq_samples_code_batch_id' in detail:
                editform.get('genaf-sample_code').add_error('The sample code: %s is '
                        'already being used. Please use other sample code!'
                        % sample_d['code'])
            r = render_to_response( "genaf:templates/sample/edit.mako",
                    {   'sample': None,
                        'eform': editform,
                    },
                    request = request )
            transaction.abort()
            return r

        return HTTPFound(location = request.route_url('genaf.sample-view', id = sample.id))


    return error_page(request, 'Invalid method!')