Пример #1
0
def test_pageurl_webob():
    path = "/articles"
    application_url = "http://localhost:5000" + path
    params = MultiDict({"blah": "boo"})
    request = DummyRequest(application_url, path, params)
    purl = paginate.PageURL_WebOb(request)
    eq_(purl(2), "/articles?blah=boo&page=2")
    purl = paginate.PageURL_WebOb(request, qualified=True)
    eq_(purl(2), "http://localhost:5000/articles?blah=boo&page=2")
Пример #2
0
def student_assignment_list(request):
    conn = DBSession()
    page = int(request.params.get('page', 1))
    s_courseid = request.params.get('s_courseid')
    userid = request.user.id
    student = conn.query(Student).filter(Student.userid == userid).first()
    courses=conn.query(Course).filter(Course.id.in_(\
                            conn.query(Course_Class.courseid).filter(Course_Class.clazzid\
                            ==student.clazzid))).all()
    if s_courseid:
        items=conn.query(Assignment,Lesson).filter(Assignment.id.in_(conn.query(Lesson.assignmentid).filter(\
                Lesson.courseid==s_courseid,Lesson.courseid.in_(conn.query(Course_Class.courseid).\
                filter(Course_Class.clazzid==student.clazzid)))),Assignment.id==Lesson.assignmentid).all()
    else:
        items=conn.query(Assignment,Lesson).filter(Assignment.id.in_(conn.query(Lesson.assignmentid).filter(\
                Lesson.courseid.in_(conn.query(Course_Class.courseid).\
                filter(Course_Class.clazzid==student.clazzid)))),Assignment.id==Lesson.assignmentid).all()
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items, courses=courses)
Пример #3
0
def listsemester(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    semesters = conn.query(Semester).order_by(Semester.id)
    page_url = paginate.PageURL_WebOb(request)
    items = []
    class List_semester():
        def __init__(self):
            self.id = 0
            self.name = ""
            self.time = ""
            self.weeks = 0
    for semester in semesters:
        t = List_semester()
        t.id = semester.id
        t.time = date.fromtimestamp(semester.start)
        t.weeks = semester.weeks
        time = t.time
        name = str(time.year)
        mon = time.month
        if  mon >7 :
            name += u"年秋季"
        else :
            name += u"年春季"
        t.name = name 
        items.append(t)
    items = paginate.Page(
            items,
            page=int(page),
            items_per_page=10,
            url=page_url,
            )
    return dict(items=items) 
Пример #4
0
def user_list_view(request):
    """ Render the user list page.

    Return a paged user list from the database. The paged list can be
    ordered by username, first name or last name. Add an error flash message
    if the list is empty. Return also basic users statistics.

    :param request: a ``pyramid.request`` object
    """
    _ = request.translate
    stats = get_user_stats()
    sortable_columns = ['username', 'first_name', 'last_name']
    column = request.params.get('sort')
    search = request.params.get('search')
    # construct the query
    users = DBSession.query(AuthUser)
    if column and column in sortable_columns:
        users = users.order_by(column)
    else:
        users = users.order_by(AuthUser.username)
    if search:
        users = users.filter(AuthUser.username.like('%' + search + '%'))
    # add a flash message for empty results
    if users.count() == 0:
        request.session.flash(_(u"There is no results!"), 'error')
    # paginate results
    page_url = paginate.PageURL_WebOb(request)
    users = paginate.Page(users,
                          page=int(request.params.get("page", 1)),
                          items_per_page=20,
                          url=page_url)
    return dict(users=users, stats=stats)
Пример #5
0
def listcourse(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    if request.method == "POST":
        semesterid = request.params.get('semesterid')
        items = conn.query(Course).filter(
            Course.semesterid == semesterid).order_by(Course.id)
    else:
        items = conn.query(Course).order_by(Course.id)
    semesters = conn.query(Semester).order_by(Semester.id)
    lis = []
    for semester in semesters:
        t = List_semester()
        t.id = semester.id
        t.time = date.fromtimestamp(semester.start)
        t.weeks = semester.weeks
        time = t.time
        name = str(time.year)
        mon = time.month
        if mon > 7:
            name += u"年秋季"
        else:
            name += u"年春季"
        t.name = name
        lis.append(t)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items, lis=lis)
Пример #6
0
def mentor_lesson_listbycourse(request):
    page = int(request.params.get('page', 1))
    courseid = request.params.get('courseid')
    conn = DBSession()
    course=conn.query(Course).filter(Course.id==courseid,Course.mentorid.in_(\
            conn.query(Mentor.id).filter(Mentor.userid==request.user.id))).first()
    if course:
        course_classes = conn.query(Course_Class).filter(
            Course_Class.courseid == course.id).all()
        course.course_classes = course_classes
        items = conn.query(Lesson).filter(Lesson.courseid == course.id,
                                          Lesson.state != -1).all()
        for item in items:
            lesson_locations = conn.query(Lesson_Location).filter(
                Lesson_Location.lessonid == item.id).all()
            item.lesson_locations = lesson_locations

        page_url = paginate.PageURL_WebOb(request)
        items = paginate.Page(
            items,
            page=int(page),
            items_per_page=10,
            url=page_url,
        )
        return dict(items=items, course=course)
    return dict(code=0, error=u"课程不存在")
Пример #7
0
def mentor_courseware_course_add(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    userid = request.user.id
    courseid = request.params.get('courseid')
    course = None
    items = []
    if courseid:
        course = conn.query(Course).filter(Course.id == courseid).first()
        #items=conn.query(Courseware)
        wares = conn.query(
            Ware_Course.wareid).filter(Ware_Course.courseid == courseid)
        #if wares :
        items=conn.query(Courseware).filter(Courseware.mentorid.in_(conn.query(Mentor.id)\
                .filter(Mentor.userid==userid)),Courseware.id.notin_(wares))
        #else :
        #    items=conn.query(Courseware)
        #"""
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(code=0, course=course, items=items)
Пример #8
0
    def users(self, page=1, *args, **kw):
        """
        Users Managment.
        """
        users = DBSession.query(User)
        page_url = paginate.PageURL_WebOb(request)
        tmpl = ''
        dsc = True
        order = 'user_id'
        if kw:
            if 'desc' in kw and kw['desc'] != u'':
                if kw['desc'] != u'1':
                    dsc = False
            if 'order_by' in kw and kw['order_by'] != u'':
                order = kw['order_by']
        if dsc:
            users = users.order_by(desc(order).nullslast())
        else:
            users = users.order_by((order))

        currentPage = paginate.Page(users,
                                    page,
                                    url=page_url,
                                    items_per_page=30)
        return dict(page='index',
                    currentPage=currentPage,
                    tmpl=tmpl,
                    pname=None)
Пример #9
0
 def paginate_results(self, query):
     current_page = int(self.request.params.get('page', 1))
     page_url = paginate.PageURL_WebOb(self.request)
     records = paginate.Page(query,
                             current_page,
                             url=page_url,
                             items_per_page=self.page_size)
     return records
Пример #10
0
def filter_view(request):
    """ List of jobs """

    current_page = int(request.params.get('page', 0))
    q = (FactData.get_all(request).filter(
        UserFilter.get_by_pk(
            request, request.context.pk).build_query()).join(ScrapedData))
    page_url = paginate.PageURL_WebOb(request)
    records = paginate.Page(q, current_page, url=page_url)
    return dict(filter=request.context.name, page=records)
Пример #11
0
def listlocation(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    items = conn.query(Location).order_by(Location.id)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items)
Пример #12
0
def listuser(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    items = conn.query(User).order_by(User.regtime)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items)
Пример #13
0
 def _paginate(self, query, appstruct):
     """
         wraps the current SQLA query with pagination
     """
     # Url builder for page links
     page_url = paginate.PageURL_WebOb(self.request)
     current_page = appstruct['page']
     items_per_page = appstruct['items_per_page']
     return paginate.Page(query,
                          current_page,
                          url=page_url,
                          items_per_page=items_per_page)
Пример #14
0
def mentor_courseware_list(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    userid = request.user.id
    items=conn.query(Courseware).filter(Courseware.mentorid.in_(conn.query(Mentor.id)\
                .filter(Mentor.userid==userid))).all()
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items)
Пример #15
0
def mentor_assignment_markupload(request):
    conn = DBSession()
    page = int(request.params.get('page', 1))
    assignmentid = request.params.get('assignmentid')
    userid = request.user.id
    items = conn.query(AssignmentUpload).filter(
        AssignmentUpload.assignmentid == assignmentid).all()
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=20,
        url=page_url,
    )
    return dict(items=items)
Пример #16
0
def notice_lesson_list(request):
    conn = DBSession()
    page = int(request.params.get('page', 1))
    userid = request.user.id
    items = conn.query(LessonWorkItem).filter(
        LessonWorkItem.acceptuserid == userid).order_by(
            LessonWorkItem.viewstate, desc(LessonWorkItem.actiontime))
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=12,
        url=page_url,
    )
    return dict(items=items)
Пример #17
0
def listclazz(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    class infoClazz():
        def __init__(self):
            college = ""
            faculty = ""
            clazz = ""
            collegeNum = 0
            facultyNum = 0
            clazzNum = 0 
             
    infos = []
    info = infoClazz()
    info.college = ""
    info.faculty = ""
    info.clazz = ""
    info.collegeNum = 0
    info.facultyNum = 0
    info.clazzNum = 0
    colleges = conn.query(College).order_by(College.id)
    facultys = conn.query(Faculty).order_by(Faculty.id)
    clazzs = conn.query(Clazz).order_by(Clazz.id)
    if request.method == "POST":
        clazzid = request.params.get('clazzid')
        items = conn.query(Student).filter(Student.clazzid==clazzid)
        clazzs = conn.query(Clazz).filter(Clazz.id==clazzid)
        info.college = clazzs[0].faculty.college.name
        info.faculty = clazzs[0].faculty.name
        info.clazz = str(clazzs[0].year)+"级"+str(clazzs[0].num)+"班"
    else :
        items = []
        for college in colleges :
            if college.id > info.collegeNum :
                info.collegeNum = college.id
        for faculty in facultys :
            if faculty.id > info.facultyNum :
                info.facultyNum = faculty.id
    infos.append(info)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
            items,
            page=int(page),
            items_per_page=30,
            url=page_url,
    )
    return dict(items=items,colleges=colleges,facultys=facultys,clazzs=clazzs,infos=infos)
Пример #18
0
def listfaculty(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    if request.method == "POST":
        collegeid = request.params.get('collegeid')
        items = conn.query(Faculty).filter(Faculty.collegeid == collegeid)
    else:
        items = conn.query(Faculty).order_by(Faculty.id)
    lis = conn.query(College).order_by(College.id)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items, lis=lis)
Пример #19
0
def recent(request):
    """show most recent 20 pads and a few lines, and pagination"""
    try:
        current_page = int(request.params["page"])
    except KeyError:
        current_page = 1

    # get all pads
    pads = get_all_pads()

    # pad count
    pad_count = len(pads)

    # paginate pads
    page_url = paginate.PageURL_WebOb(request)
    pads = paginate.Page(pads, current_page, url=page_url, items_per_page=20)

    return {'pads': pads, 'pad_count': pad_count, 'current_page': current_page}
Пример #20
0
def admin_lesson_undolist(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    items = conn.query(Lesson).filter(Lesson.state == 0).all()
    items += conn.query(Lesson).filter(Lesson.state == -2).all()
    for item in items:
        lesson_locations = conn.query(Lesson_Location).filter(
            Lesson_Location.lessonid == item.id).all()
        item.lesson_locations = lesson_locations

    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items)
Пример #21
0
def listmentor(request):
    page = int(request.params.get('page', 1))
    collegeid = request.params.get('collegeid')
    conn = DBSession()
    if collegeid:
        items = conn.query(Mentor).filter(Mentor.collegeid == collegeid,
                                          Mentor.state != -1)
    else:
        items = conn.query(Mentor).filter(Mentor.state != -1)
    lis = conn.query(College).order_by(College.id)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items, lis=lis)
Пример #22
0
def courseware_course_list(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    userid = request.user.id
    courseid = request.params.get('courseid')
    course = None
    items = []
    if courseid:
        wares = conn.query(Ware_Course).filter(
            Ware_Course.courseid == courseid)
        course = conn.query(Course).filter(Course.id == courseid).first()
        for ware in wares:
            items.append(ware.courseware)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items, course=course)
Пример #23
0
def liststudent(request):
    page = int(request.params.get('page', 1))
    clazzid, identity = [
        request.params.get(x, None)
        for x in ['search_clazz', 'search_identity']
    ]
    items = DBSession().query(Student)
    if identity:
        items = items.filter(Student.identity == identity)
    if clazzid:
        items = items.filter(Student.clazzid == clazzid)

    items = items.order_by(Student.identity)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items)
Пример #24
0
def student_course_list(request):
    page = int(request.params.get('page', 1))
    conn = DBSession()
    semesterid = request.params.get('semesterid')
    userid = request.user.id
    student = conn.query(Student).filter(Student.userid == userid).first()
    if semesterid:
        items = conn.query(Course).filter(
            Course.semesterid == semesterid,
            Course.id.in_(
                conn.query(Course_Class.courseid).filter(
                    Course_Class.clazzid == student.clazzid))).order_by(
                        Course.id)
    else:
        items = conn.query(Course).filter(Course.id.in_(conn.query(Course_Class.courseid).filter(\
                    Course_Class.clazzid==student.clazzid))).order_by(Course.id)
    semesters = conn.query(Semester).order_by(Semester.id)
    lis = []
    for semester in semesters:
        t = List_semester()
        t.id = semester.id
        t.time = date.fromtimestamp(semester.start)
        t.weeks = semester.weeks
        time = t.time
        name = str(time.year)
        mon = time.month
        if mon > 7:
            name += u"年秋季"
        else:
            name += u"年春季"
        t.name = name
        lis.append(t)
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items, lis=lis)
Пример #25
0
def list(request):
    """customers list """
    search = request.params.get("search", "")

    sort = "company_name"
    if request.GET.get("sort") and request.GET.get("sort") in \
            ["company_name", "contact_first_name", "contact_last_name", "category"]:
        sort = request.GET.get("sort")
    if sort == "category":
        sort = "category.name"

    direction = "asc"
    if request.GET.get("direction") and request.GET.get("direction") in [
            "asc", "desc"
    ]:
        direction = request.GET.get("direction")

    # db query
    dbsession = DBSession()
    query = dbsession.query(Customer).join(Category).\
        filter(Customer.company_name.like(search + "%")).\
                   order_by(sort + " " + direction)

    # paginate
    page_url = paginate.PageURL_WebOb(request)
    customers = Page(query,
                     page=int(request.params.get("page", 1)),
                     items_per_page=10,
                     url=page_url)

    if "partial" in request.params:
        # Render the partial list page
        return render_to_response("customer/listPartial.html",
                                  {"customers": customers},
                                  request=request)
    else:
        # Render the full list page
        return render_to_response("customer/list.html",
                                  {"customers": customers},
                                  request=request)
Пример #26
0
def list(request):
    """countries list """
    search = request.params.get("search", "")

    sort = "code"
    if request.GET.get("sort") and request.GET.get("sort") in ["code", "name"]:
        sort = request.GET.get("sort")

    direction = "asc"
    if request.GET.get("direction") and request.GET.get("direction") in [
            "asc", "desc"
    ]:
        direction = request.GET.get("direction")

    # db query
    dbsession = DBSession()
    query = dbsession.query(Country).\
        filter(or_(Country.code.like(search + "%"),
                   Country.name.like(search + "%"))).\
                   order_by(sort + " " + direction)

    # paginate
    page_url = paginate.PageURL_WebOb(request)
    countries = Page(query,
                     page=int(request.params.get("page", 1)),
                     items_per_page=10,
                     url=page_url)

    if "partial" in request.params:
        # Render the partial list page
        return render_to_response("country/listPartial.html",
                                  {"countries": countries},
                                  request=request)
    else:
        # Render the full list page
        return render_to_response("country/list.html",
                                  {"countries": countries},
                                  request=request)
Пример #27
0
def mentor_assignment_list(request):
    conn = DBSession()
    page = int(request.params.get('page', 1))
    s_courseid = request.params.get('s_courseid')
    userid = request.user.id
    courses=conn.query(Course).filter(Course.mentorid.in_(\
                            conn.query(Mentor.id).filter(Mentor.userid==userid))).all()
    items=conn.query(Assignment,Lesson).filter(Assignment.id==Lesson.assignmentid,
                     Lesson.courseid.in_(conn.query(Course.id).filter(Course.mentorid.in_(\
                            conn.query(Mentor.id).filter(Mentor.userid==userid))))).all()
    if s_courseid:
        items=conn.query(Assignment,Lesson).filter(Assignment.id==Lesson.assignmentid,
                     Lesson.courseid.in_(conn.query(Course.id).filter(Course.mentorid.in_(\
                            conn.query(Mentor.id).filter(Mentor.userid==userid)),\
                            Course.id.in_(courses)))).all()
    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items, courses=courses)
Пример #28
0
def listlessonsbycourse(request):
    page = int(request.params.get('page', 1))
    courseid = request.params.get('courseid')
    conn = DBSession()
    course = conn.query(Course).filter(Course.id == courseid).first()
    course_classes = conn.query(Course_Class).filter(
        Course_Class.courseid == courseid).all()
    course.course_classes = course_classes
    items = conn.query(Lesson).filter(Lesson.courseid == courseid,
                                      Lesson.state != -1).all()
    for item in items:
        lesson_locations = conn.query(Lesson_Location).filter(
            Lesson_Location.lessonid == item.id).all()
        item.lesson_locations = lesson_locations

    page_url = paginate.PageURL_WebOb(request)
    items = paginate.Page(
        items,
        page=int(page),
        items_per_page=10,
        url=page_url,
    )
    return dict(items=items, course=course)
Пример #29
0
def blog_view(context, request, size=5):
    current_page = request.params.get('page', '0')
    # XXX hack, 很奇怪会附加一个/
    if current_page.endswith('/'):
        current_page = current_page[:-1]
    current_page = int(current_page)
    page_url = paginate.PageURL_WebOb(request)
    blog_subpaths = context.get_recent_file_subpaths()
    blog_page = paginate.Page(blog_subpaths,
                              current_page,
                              items_per_page=size,
                              url=page_url)

    posts = []
    for subpath in blog_page:
        obj = context.get_obj_by_subpath(subpath)
        if obj is not None:
            raw_html = obj.render_html(request)
            converted_html = raw_html.replace(
                'src="img/', 'src="%s/../img/' % obj.url(request))
            dc = obj.metadata
            created = dc.get('modified', dc.get('created', datetime.now()))
            posts.append({
                'title': obj.title,
                'description': dc.get('description', ''),
                'url': subpath,
                'created': getDisplayTime(created),
                'creator': dc.get('creator', ''),
                'body': converted_html,
            })

    batch = blog_page.pager()
    return render('templates/bloglist.pt', dict(
        result=posts,
        batch=batch,
    ))
Пример #30
0
    def index(self, page=1, *args, **kw):
        pname = request.environ['PATH_INFO'].split('/')[1]
        project = DBSession.query(Projects).filter_by(name=pname).first()
        page_url = paginate.PageURL_WebOb(request)
        import pickle
        try:
            cells = pickle.loads([test.cell_line for test in project.tests if test.name == 'CT'][0])
        except:
            cells = None
        lcompound = DBSession.query(LCompound).join(LCompound.mol).filter(Compound.project.any(Projects.name==pname)).filter(LCompound.showme==True)
        
        dsc = True
        order = LCompound.id
        tmpl = ''
        alltags =[tag for tag in DBSession.query(Tags).order_by('name').all() ]
        selection = None
        similarity = None
        userid = request.identity['repoze.who.userid']
        user = DBSession.query(User).filter_by(user_name=userid).first()
        ulist = None
        ulists = set([l for l in user.lists if l.table == 'Results'] + [l for l in user.tg_user_lists if l.table == 'Results'])
        items = user.items_per_page
        
        try:
            if kw['search'] != u'':
                search_clicked = kw['search']
            else:
                search_clicked = None
        except Exception:
            search_clicked = None
        if kw:
            if kw.has_key('mylist'):
                try:
                    ulist_id = int(kw['mylist'])
                    ulist = DBSession.query(UserLists).get(ulist_id)
                except Exception:
                    flash(l_(u'List error'), 'error')
                    redirect(request.headers['Referer'])
                if (ulist in user.lists) or (user in ulist.permitusers):
                    if ulist.elements:
                        import pickle
                        elements = [int(el) for el in pickle.loads(ulist.elements)]
                        if ulist.table == 'Results':
                            lcompound = DBSession.query(LCompound).join(LCompound.mol).filter(Compound.project.any(Projects.name==pname)).filter(LCompound.id.in_(elements))
                        else:
                            flash(l_(u'Table error'), 'error')
                            redirect(request.headers['Referer'])
                else:
                    flash(l_(u'Permission denied'), 'error')
                    redirect(request.headers['Referer'])
            for k, v in kw.iteritems():
                if str(k) == 'desc' and str(v) != '1':
                    dsc = None
                elif str(k) == 'order_by':
                    if v in ('gid', 'create_date', 'box', 'form', 'state', 'entry', 'source', 'MDM2', 'MDM4', 'lcode'):
                        if v=='lcode':
                            order = LCompound.lcode
                        else:
                            order = LCompound.__getattribute__(LCompound, v)
                    else:
                        if v=='last_point':
                            lcompound=lcompound.join(LCompound.solubility)
                            order = v
                        elif hasattr(LCompound, v):
                            order = LCompound.__getattribute__(LCompound, v)
                        elif 'CTOX_' in v:
                            v = v.replace('CTOX_', '')
                            all_lcompounds = DBSession.query(LCompound).join(LCompound.mol).filter(Compound.project.any(Projects.name==pname)).all()
                            for l in all_lcompounds:
                                l.avg_ct = v.replace('pp', '+')
                            order = '_avg_ct'
                        else:
                            order = v
                if str(k) != 'select' and str(k) != 'remove' and str(v) != u'':
                    tmpl += str(k) + '=' + str(v) + '&'
                elif str(k) == 'select':
                    try:
                        if isinstance(kw['select'], basestring):
                            selection = [kw['select']]
                        else:
                            selection = [id for id in kw['select']]
                    except Exception:
                        selection = None
                        
            if search_clicked:
                try:
                    smiles = str(kw['smiles'])
                    if 'pp' in smiles:
                        smiles = smiles.replace('pp', '+')
                    method = str(kw['method'])
                except Exception:
                    smiles = None
                    method = None
                if smiles:
                    if checksmi(smiles):
                        from razi.functions import functions
                        from razi.expression import TxtMoleculeElement
                        if method == 'similarity':
#                            from razi.postgresql_rdkit import tanimoto_threshold
                            query_bfp = functions.morgan_b(TxtMoleculeElement(smiles), 2)
                            constraint = Compound.morgan.tanimoto_similar(query_bfp)
                            tanimoto_sml = Compound.morgan.tanimoto_similarity(query_bfp).label('tanimoto')
                            search = DBSession.query(LCompound, tanimoto_sml).join(LCompound.mol).join(LCompound.purity).filter(Compound.project.any(Projects.name==pname)).filter(constraint)
                            if order != LCompound.id:
                                if order == 'purity':
                                    order = LPurity.value
                                if dsc:
                                    search = search.order_by(desc(order).nullslast())
                                else:
                                    search = search.order_by(order)
                            else:
                                search = search.order_by(desc(tanimoto_sml)).all()
                            lcompound = ()
                            similarity = ()
                            for row in search:
                                lcompound += (row[0], )
                                similarity += (row[1], )
                            currentPage = paginate.Page(lcompound, page, url=page_url, items_per_page=items)
                            return dict(currentPage=currentPage,tmpl=tmpl, page='results', pname=pname, alltags=alltags, similarity=similarity,htmlRgb=htmlRgb, htmlRgb100=htmlRgb100, Num2Rgb=Num2Rgb, cells=cells, ulists=ulists, ulist=ulist)
    
                        elif method == 'substructure':
                            constraint = Compound.structure.contains(smiles)
                            lcompound = DBSession.query(LCompound).join(LCompound.mol).filter(Compound.project.any(Projects.name==pname)).filter(constraint)
                        elif method == 'identity':
                            lcompound = DBSession.query(LCompound).filter(Compound.project.any(Projects.name==pname)).join(LCompound.mol).filter(Compound.structure.equals(smiles))
                    else:
                        if method == 'smarts':
                            if dsc:
                                lcompound = lcompound.order_by(desc(order).nullslast())
                            else:
                                lcompound = lcompound.order_by(order)
                            search = lcompound.all()
                            sub_lcompounds = ()
                            patt = Chem.MolFromSmarts(smiles)
                            if not patt:
                                flash(l_(u'SMARTS error'), 'warning')
                                redirect(request.headers['Referer'])
                            for row in search:
                                m = Chem.MolFromSmiles(str(row.mol.structure))
                                mol = Chem.AddHs(m)
                                if mol.HasSubstructMatch(patt):
                                    sub_lcompounds += (row, )
                            currentPage = paginate.Page(sub_lcompounds, page, url=page_url, items_per_page=items)
                            return dict(currentPage=currentPage,tmpl=tmpl, page='results', pname=pname, alltags=alltags, similarity=similarity,htmlRgb=htmlRgb, htmlRgb100=htmlRgb100, Num2Rgb=Num2Rgb, cells=cells, ulists=ulists, ulist=ulist)
                        else:
                            flash(l_(u'SMILES error'), 'warning')
                            redirect(request.headers['Referer'])
                if kw.has_key('text_GID') and kw['text_GID'] !=u'':
                    try:
                        gid = int(kw['text_GID'])
                        lcompound = lcompound.filter(LCompound.gid == gid)
                    except Exception as msg:
                        flash(l_(u'GID should be a number: %s' % msg), 'error')
                        redirect(request.headers['Referer'])
                if kw.has_key('text_ID') and kw['text_ID'] !=u'':
                    try:
                        id = int(kw['text_ID'])
                        lcompound = lcompound.filter(LCompound.id == id)
                    except Exception as msg:
                        flash(l_(u'ID should be a number: %s' % msg), 'error')
                        redirect(request.headers['Referer'])
                if kw.has_key('text_name') and kw['text_name'] !=u'':
                    lcompound = lcompound.filter(Compound.names.any(Names.name.like(kw['text_name'].strip().replace('*', '%'))))
                if kw.has_key('text_notes') and kw['text_notes'] !=u'':
                    lcompound = lcompound.filter(LCompound.notes.like(kw['text_notes'].replace('*', '%')))
                if kw.has_key('text_lso') and kw['text_lso'] !=u'':
                    lcompound = lcompound.filter(LCompound.lso.like(kw['text_lso'].replace('*', '%')))
                if kw.has_key('text_entry') and kw['text_entry'] !=u'':
                    lcompound = lcompound.filter(LCompound.entry.like(kw['text_entry'].replace('*', '%')))
                if kw.has_key('text_box') and kw['text_box'] !=u'':
                    lcompound = lcompound.filter(LCompound.box.like(kw['text_box'].replace('*', '%')))
                if kw.has_key('date_from') and kw['date_from'] !=u'':
                    date_from = datetime.strptime(str(kw['date_from']), '%Y-%m-%d')
                    lcompound = lcompound.filter(LCompound.create_date > date_from)
                else:
                    date_from = None
                if kw.has_key('date_to') and kw['date_to'] !=u'':
                    date_to = datetime.strptime(str(kw['date_to']), '%Y-%m-%d')
                    if date_from:
                        if date_to>date_from:
                            lcompound = lcompound.filter(LCompound.create_date < date_to)
                        else:
                            flash(l_(u'The End date must be later than the initial'), 'error')
                            redirect(request.headers['Referer'])
                    else:
                        lcompound = lcompound.filter(LCompound.create_date < date_to)
                        
                if kw.has_key('text_mdm2_hill_from') and kw['text_mdm2_hill_from'] !=u'':
                    text_mdm2_hill_from = float(kw['text_mdm2_hill_from'])
                    lcompound = lcompound.filter(LCompound.avg_hillslope_mdm2 >= text_mdm2_hill_from)
                else:
                    text_mdm2_hill_from = None
                if kw.has_key('text_mdm2_hill_to') and kw['text_mdm2_hill_to'] !=u'':
                    text_mdm2_hill_to = float(kw['text_mdm2_hill_to'])
                    if text_mdm2_hill_from:
                        if text_mdm2_hill_to>=text_mdm2_hill_from:
                            lcompound = lcompound.filter(LCompound.avg_hillslope_mdm2 <= text_mdm2_hill_to)
                        else:
                            flash(l_(u'The final value must be greater than the initial'))
                            redirect(request.headers['Referer'])
                    else:
                        lcompound = lcompound.filter(LCompound.avg_hillslope_mdm2 <= text_mdm2_hill_to)
                if kw.has_key('text_mdm2_fluor_from') and kw['text_mdm2_fluor_from'] !=u'':
                    text_mdm2_fluor_from = float(kw['text_mdm2_fluor_from'])
                    lcompound = lcompound.filter(LCompound.avg_fluorescence_mdm2 >= text_mdm2_fluor_from)
                else:
                    text_mdm2_fluor_from = None
                if kw.has_key('text_mdm2_fluor_to') and kw['text_mdm2_fluor_to'] !=u'':
                    text_mdm2_fluor_to = float(kw['text_mdm2_fluor_to'])
                    if text_mdm2_fluor_from:
                        if text_mdm2_fluor_to>=text_mdm2_fluor_from:
                            lcompound = lcompound.filter(LCompound.avg_fluorescence_mdm2 <= text_mdm2_fluor_to)
                        else:
                            flash(l_(u'The final value must be greater than the initial'))
                            redirect(request.headers['Referer'])
                    else:
                        lcompound = lcompound.filter(LCompound.avg_fluorescence_mdm2 <= text_mdm2_fluor_to)
                if kw.has_key('text_mdm2_ki_from') and kw['text_mdm2_ki_from'] !=u'':
                    text_mdm2_ki_from = float(kw['text_mdm2_ki_from'])
                    lcompound = lcompound.filter(LCompound.avg_ki_mdm2 >= text_mdm2_ki_from)
                else:
                    text_mdm2_ki_from = None
                if kw.has_key('text_mdm2_ki_to') and kw['text_mdm2_ki_to'] !=u'':
                    text_mdm2_ki_to = float(kw['text_mdm2_ki_to'])
                    if text_mdm2_ki_from:
                        if text_mdm2_ki_to>=text_mdm2_ki_from:
                            lcompound = lcompound.filter(LCompound.avg_ki_mdm2 <= text_mdm2_ki_to)
                        else:
                            flash(l_(u'The final value must be greater than the initial'))
                            redirect(request.headers['Referer'])
                    else:
                        lcompound = lcompound.filter(LCompound.avg_ki_mdm2 <= text_mdm2_ki_to)

                if kw.has_key('text_mdm4_hill_from') and kw['text_mdm4_hill_from'] !=u'':
                    text_mdm4_hill_from = float(kw['text_mdm4_hill_from'])
                    lcompound = lcompound.filter(LCompound.avg_hillslope_mdm4 >= text_mdm4_hill_from)
                else:
                    text_mdm4_hill_from = None
                if kw.has_key('text_mdm4_hill_to') and kw['text_mdm4_hill_to'] !=u'':
                    text_mdm4_hill_to = float(kw['text_mdm4_hill_to'])
                    if text_mdm4_hill_from:
                        if text_mdm4_hill_to>=text_mdm4_hill_from:
                            lcompound = lcompound.filter(LCompound.avg_hillslope_mdm4 <= text_mdm4_hill_to)
                        else:
                            flash(l_(u'The final value must be greater than the initial'))
                            redirect(request.headers['Referer'])
                    else:
                        lcompound = lcompound.filter(LCompound.avg_hillslope_mdm4 <= text_mdm4_hill_to)
                        
                if kw.has_key('text_mdm4_fluor_from') and kw['text_mdm4_fluor_from'] !=u'':
                    text_mdm4_fluor_from = float(kw['text_mdm4_fluor_from'])
                    lcompound = lcompound.filter(LCompound.avg_fluorescence_mdm4 >= text_mdm4_fluor_from)
                else:
                    text_mdm4_fluor_from = None
                if kw.has_key('text_mdm4_fluor_to') and kw['text_mdm4_fluor_to'] !=u'':
                    text_mdm4_fluor_to = float(kw['text_mdm4_fluor_to'])
                    if text_mdm4_fluor_from:
                        if text_mdm4_fluor_to>=text_mdm4_fluor_from:
                            lcompound = lcompound.filter(LCompound.avg_fluorescence_mdm4 <= text_mdm4_fluor_to)
                        else:
                            flash(l_(u'The final value must be greater than the initial'))
                            redirect(request.headers['Referer'])
                    else:
                        lcompound = lcompound.filter(LCompound.avg_fluorescence_mdm4 <= text_mdm4_fluor_to)
                        
                if kw.has_key('text_mdm4_ki_from') and kw['text_mdm4_ki_from'] !=u'':
                    text_mdm4_ki_from = float(kw['text_mdm4_ki_from'])
                    lcompound = lcompound.filter(LCompound.avg_ki_mdm4 >= text_mdm4_ki_from)
                else:
                    text_mdm4_ki_from = None
                if kw.has_key('text_mdm4_ki_to') and kw['text_mdm4_ki_to'] !=u'':
                    text_mdm4_ki_to = float(kw['text_mdm4_ki_to'])
                    if text_mdm4_ki_from:
                        if text_mdm4_ki_to>=text_mdm4_ki_from:
                            lcompound = lcompound.filter(LCompound.avg_ki_mdm4 <= text_mdm4_ki_to)
                        else:
                            flash(l_(u'The final value must be greater than the initial'))
                            redirect(request.headers['Referer'])
                    else:
                        lcompound = lcompound.filter(LCompound.avg_ki_mdm4 <= text_mdm4_ki_to)

                try:
                        tags = kw['text_tags']
                except Exception:
                    tags = None
                    pass
                if tags:
                    if isinstance(tags, basestring):
                        tagi = eval(tags)
                        if type(tagi) != type([]):
                            tagi = [int(tags)]
                    else:
                        tagi = [int(tid) for tid in tags]
                    lcompound = lcompound.filter(Compound.tags.any(Tags.id.in_(tagi)))
                    
        if dsc:
            lcompound = lcompound.order_by(desc(order).nullslast())
        else:
            lcompound = lcompound.order_by(order)
            
        if search_clicked and kw['search'] == "Download":
            if kw['file_type'] and kw['file_type'] != u'' and kw['sell_type'] and kw['sell_type'] != u'':
                if kw['sell_type'] == u'all':
                    lcompounds = lcompound.all()
                elif kw['sell_type'] == u'selected':
                    if selection:
                        lcompounds = ()
                        for el in selection:
                            lcompounds += (DBSession.query(LCompound).get(el), )
                    else:
                        flash(l_(u'Lack of selected structures for download'), 'error')
                        redirect(request.headers['Referer'])
                elif kw['sell_type'] == u'range':
                    lcompounds = lcompound.all()
                    if kw.has_key('select_from') and kw['select_from'] != u'':
                        try:
                            select_from = int(kw['select_from']) -1 
                            if select_from<1 or select_from>len(lcompounds):
                                select_from = 0
                        except Exception:
                            select_from = 0
                    else:
                        select_from = 0
                    if kw.has_key('select_to') and kw['select_to'] != u'':
                        try:
                            select_to = int(kw['select_to'])
                            if select_to<2 or select_to>len(lcompounds):
                                select_to = len(lcompounds)
                        except Exception:
                            select_to = len(lcompounds)
                    else:
                        select_to = len(lcompounds)
                    lcompounds_new = ()
                    for el in range(select_from, select_to):
                        lcompounds_new += (lcompounds[el], )
                    lcompounds = lcompounds_new
                else:
                    flash(l_(u'Lack of items to download'), 'error')
                    redirect(request.headers['Referer'])
                try:
                    if isinstance(kw['options'], basestring):
                        options = [kw['options']]
                    else:
                        options = kw['options']
                except Exception:
                    flash(l_('Choose download options'), 'error')
                    redirect(request.headers['Referer'])
                if 'getsize' in kw:
                    size = int(kw['getsize']), int(kw['getsize'])
                else:
                    size = 100, 100
                if kw['file_type'] == 'pdf':
                    filename = userid + '_selected.pdf'
                    from xhtml2pdf.pisa import CreatePDF
                    from tg.render import render as render_template
                    import cStringIO
                    html = render_template({"length":len(lcompounds), "lcompound":lcompounds, "cells":cells, "options":options, "size":size}, "genshi", "molgears.templates.users.results.print2", doctype=None)
                    dest = './molgears/files/pdf/' + filename
                    result = file(dest, "wb")
                    CreatePDF(cStringIO.StringIO(html.encode("UTF-8")), result, encoding="utf-8")
                    result.close()
                    import paste.fileapp
                    f = paste.fileapp.FileApp('./molgears/files/pdf/'+ filename)
                    from tg import use_wsgi_app
                    return use_wsgi_app(f)
                elif kw['file_type'] == 'xls':
                    filename = userid + '_selected.xls'
                    filepath = os.path.join('./molgears/files/download/', filename)
                    from PIL import Image
                    import xlwt
                    wbk = xlwt.Workbook()
                    sheet = wbk.add_sheet('sheet1')
                    j=0
                    if 'nr' in options:
                        sheet.write(0,j,u'Nr.')
                        j+=1
                    if 'gid' in options:
                        sheet.write(0,j,u'GID')
                        j+=1
                    if 'id' in options:
                        sheet.write(0,j,u'ID')
                        j+=1
                    if 'name' in options:
                        sheet.write(0,j,u'Name')
                        j+=1
                    if 'names' in options:
                        sheet.write(0,j,u'Names')
                        j+=1
                    if 'image' in options:
                        sheet.write(0,j,u'Image')
                        j+=1
                    if 'smiles' in options:
                        sheet.write(0,j,u'SMILES')
                        j+=1
                    if 'inchi' in options:
                        sheet.write(0,j,u'InChi')
                        j+=1
                    if 'lso' in options:
                        sheet.write(0,j,u'LSO')
                        j+=1
                    if 'num_atoms' in options:
                        sheet.write(0,j,u'Atoms')
                        j+=1
                    if 'mw' in options:
                        sheet.write(0,j,u'MW')
                        j+=1
                    if 'hba' in options:
                        sheet.write(0,j,u'hba')
                        j+=1
                    if 'hbd' in options:
                        sheet.write(0,j,u'hbd')
                        j+=1
                    if 'tpsa' in options:
                        sheet.write(0,j,u'tpsa')
                        j+=1
                    if 'logp' in options:
                        sheet.write(0,j,u'logP')
                        j+=1
                    if 'purity' in options:
                        sheet.write(0,j, u'Purity')
                        j+=1
                    if 'create_date' in options:
                        sheet.write(0,j,u'Date')
                        j+=1
                    if 'box' in options:
                        sheet.write(0,j,u'Box')
                        j+=1
                    if 'entry' in options:
                        sheet.write(0,j,u'Entry')
                        j+=1
                    if 'source' in options:
                        sheet.write(0,j,u'Source')
                        j+=1
                    if 'content' in options:
                        sheet.write(0,j,u'Content')
                        j+=1
                    if 'tags' in options:
                        sheet.write(0,j,u'Tags')
                        j+=1
                    if 'notes' in options:
                        sheet.write(0,j,u'Notes')
                        j+=1
                    for cell_line in cells:
                        if '_CT_%s' % cell_line in options:
                            sheet.write(0,j,u'CT %s' % cell_line)
                            j+=1
                    i = 1
                    for row in lcompounds:
                        j=0
                        if 'nr' in options:
                            sheet.write(i,j, str(i))
                            j+=1
                        if 'gid' in options:
                            sheet.write(i,j, row.gid)
                            j+=1
                        if 'id' in options:
                            sheet.write(i,j, row.id)
                            j+=1
                        if 'name' in options:
                            sheet.write(i,j, row.mol.name)
                            j+=1
                        if 'names' in options:
                            names = u''
                            for n in row.mol.names:
                                names += n.name + u', '
                            sheet.write(i,j, names)
                            j+=1
                        if 'image' in options:
                            file_in = './molgears/public/img/%s.png' % row.gid
                            img = Image.open(file_in)
                            file_out = './molgears/public/img/bitmap/thumb%s.bmp' %row.gid
                            img.thumbnail(size, Image.ANTIALIAS)
                            img.save(file_out)
                            sheet.insert_bitmap(file_out , i,j, 5, 5)
                            j+=1
                        if 'smiles' in options:
                            sheet.write(i,j, str(row.mol.structure))
                            j+=1
                        if 'inchi' in options:
                            sheet.write(i,j, str(row.mol.inchi))
                            j+=1
                        if 'lso' in options:
                            sheet.write(i,j, row.lso)
                            j+=1
                        if 'num_atoms' in options:
                            sheet.write(i,j,str(row.mol.num_hvy_atoms)+'/'+str(row.mol.num_atoms))
                            j+=1
                        if 'mw' in options:
                            sheet.write(i,j, str(row.mol.mw))
                            j+=1
                        if 'hba' in options:
                            sheet.write(i,j, str(row.mol.hba))
                            j+=1
                        if 'hbd' in options:
                            sheet.write(i,j, str(row.mol.hbd))
                            j+=1
                        if 'tpsa' in options:
                            sheet.write(i,j, str(row.mol.tpsa))
                            j+=1
                        if 'logp' in options:
                            sheet.write(i,j, str(row.mol.logp))
                            j+=1
                        if 'state' in options:
                            sheet.write(i,j, str(row.state))
                            j+=1
                        if 'purity' in options:
                            pur = u''
                            for p in sorted(row.purity, key=lambda p: p.value, reverse=True):
                                pur += u'%s : %s\n' % (p.value, p.type)
                            sheet.write(i,j, pur)
                            j+=1
                        if 'create_date' in options:
                            sheet.write(i,j, str(row.create_date))
                            j+=1
                        if 'owner' in options:
                            sheet.write(i,j, row.owner)
                            j+=1
                        if 'box' in options:
                            sheet.write(i,j, row.box)
                            j+=1
                        if 'entry' in options:
                            sheet.write(i,j, row.entry)
                            j+=1
                        if 'source' in options:
                            sheet.write(i,j, row.source)
                            j+=1
                        if 'content' in options:
                            if row.content:
                                sheet.write(i,j, str(row.content.value))
                            else:
                                sheet.write(i,j, 'None')
                            j+=1
                        if 'tags' in options:
                            tagsy=u''
                            for tag in row.mol.tags:
                                tagsy += tag.name + u', '
                            sheet.write(i,j,tagsy)
                            j+=1
                        if 'notes' in options:
                            sheet.write(i,j, row.notes)
                            j+=1
                        for cell_line in cells:
                            if '_CT_%s' % cell_line in options:
                                res = []
                                if row.ctoxicity:
                                    for ct in sorted(row.ctoxicity, key=lambda ct: ct.id):
                                        if ct.cell_line==cell_line:
                                            res.append(ct.ic50)
                                if len(res)>0:
                                    sheet.write(i,j, str(round(sum(res)/len(res), 3)))
                                else:
                                    sheet.write(i,j, '')
                                j+=1
                        i += 1
                    wbk.save(filepath)
                    import paste.fileapp
                    f = paste.fileapp.FileApp(filepath)
                    from tg import use_wsgi_app
                    return use_wsgi_app(f)
                    
                elif kw['file_type'] == 'sdf':
                    filepath = './molgears/files/download/out.sdf'
                    ww = Chem.SDWriter(filepath)
                    from rdkit.Chem import AllChem
                    for row in lcompounds:
                        m2 = Chem.MolFromSmiles(str(row.mol.structure))
                        AllChem.Compute2DCoords(m2)
                        AllChem.EmbedMolecule(m2)
                        AllChem.UFFOptimizeMolecule(m2)
                        if 'smiles' in options:
                            m2.SetProp("smiles", str(row.mol.structure))
                        if 'name' in options:
                            m2.SetProp("_Name", str(row.mol.name.encode('ascii', 'ignore')))
                        if 'nr' in options:
                            m2.SetProp("Nr", str(lcompounds.index(row)+1))
                        if 'gid' in options:
                            m2.SetProp("GID", str(row.gid))
                        if 'names' in options:
                            names = u''
                            for n in row.mol.names:
                                names += n.name + ', '
                            m2.SetProp("names", str(names.encode('ascii', 'ignore')))
                        if 'inchi' in options:
                            m2.SetProp("InChi", str(row.mol.inchi))
                        if 'lso' in options:
                            m2.SetProp("LSO", str(row.lso))
                        if 'num_atoms' in options:
                           m2.SetProp("atoms", str(row.mol.num_hvy_atoms)+'/'+str(row.mol.num_atoms))
                        if 'mw' in options:
                            m2.SetProp("mw", str(row.mol.mw))
                        if 'hba' in options:
                            m2.SetProp("hba", str(row.mol.hba))
                        if 'hbd' in options:
                            m2.SetProp("hbd", str(row.mol.hbd))
                        if 'tpsa' in options:
                            m2.SetProp("TPSA", str(row.mol.tpsa))
                        if 'logp' in options:
                            m2.SetProp("logP", str(row.mol.tpsa))
                        if 'create_date' in options:
                            m2.SetProp("create_date", str(row.create_date))
                        if 'owner' in options:
                            m2.SetProp("owner", str(row.owner))
                        if 'tags' in options:
                            tagsy=u''
                            for tag in row.mol.tags:
                                tagsy += tag.name + u', '
                            m2.SetProp("tagi", str(tagsy.encode('ascii', 'ignore')))
                        if 'purity' in options:
                            pur = u''
                            for p in sorted(row.purity, key=lambda p: p.value, reverse=True):
                                pur += u'%s : %s \n' % (p.value, p.type)
                            m2.SetProp("purity", str(pur.encode('ascii', 'ignore')))
                        if 'content' in options:
                            if row.content:
                                m2.SetProp("content", str(row.content.value))
                            else:
                                m2.SetProp("content", "None")
                            j+=1
                        if 'box' in options:
                            m2.SetProp("box", str(row.box))
                        if 'entry' in options:
                            m2.SetProp("entry", str(row.entry))
                        if 'notes' in options:
                            if row.notes:
                                m2.SetProp("notes", str(row.notes.encode('ascii', 'ignore')))
                            else:
                                m2.SetProp("notes", " ")
                        for cell_line in cells:
                            if '_CT_%s' % cell_line in options:
                                res = []
                                if row.ctoxicity:
                                    for ct in sorted(row.ctoxicity, key=lambda ct: ct.id):
                                        if ct.cell_line==cell_line:
                                            res.append(ct.ic50)
                                if len(res)>0:
                                    m2.SetProp('CT_%s' % cell_line, str(round(sum(res)/len(res), 3)))
                                else:
                                    m2.SetProp('CT_%s' % cell_line, ' ')
                                
                        ww.write(m2)
                    ww.close()
                    import paste.fileapp
                    f = paste.fileapp.FileApp(filepath)
                    from tg import use_wsgi_app
                    return use_wsgi_app(f)
                    
                elif kw['file_type'] == 'csv' or 'txt':
                    filename = userid + '_selected.' + kw['file_type']
                    filepath = os.path.join('./molgears/files/download/', filename)
                    from molgears.widgets.unicodeCSV import UnicodeWriter
                    import csv
                    if kw['file_type'] == u'csv':
                        delimiter = ';'
                    else:
                        delimiter = ' '
                    with open(filepath, 'wb') as csvfile:
                        
                        spamwriter = UnicodeWriter(csvfile, delimiter=delimiter,
                                                quotechar='|', quoting=csv.QUOTE_MINIMAL)
                        for row in lcompounds:
                            line =[]
                            if 'smiles' in options:
                                line.append(str(row.mol.structure))
                            if 'name' in options:
                                line.append(row.mol.name)
                            if 'nr' in options:
                                line.append(unicode(lcompounds.index(row)+1))
                            if 'gid' in options:
                                line.append(unicode(row.gid))
                            if 'names' in options:
                                names = u''
                                for n in row.mol.names:
                                    names += n.name + u', '
                                line.append(names)
                            if 'inchi' in options:
                                line.append(row.mol.inchi)
                            if 'lso' in options:
                                line.append(row.lso)
                            if 'num_atoms' in options:
                               line.append(unicode(row.mol.num_hvy_atoms)+'/'+unicode(row.mol.num_atoms))
                            if 'mw' in options:
                                line.append(unicode(row.mol.mw))
                            if 'hba' in options:
                                line.append(unicode(row.mol.hba))
                            if 'hbd' in options:
                                line.append(unicode(row.mol.hbd))
                            if 'tpsa' in options:
                                line.append(unicode(row.mol.tpsa))
                            if 'logp' in options:
                                line.append(unicode(row.mol.logp))
                            if 'purity' in options:
                                pur = u''
                                for p in sorted(row.purity, key=lambda p: p.value, reverse=True):
                                    pur += u'%s : %s\n' % (p.value, p.type)
                                line.append(pur)
                            if 'create_date' in options:
                                line.append(unicode(row.create_date))
                            if 'owner' in options:
                                line.append(row.owner)
                            if 'box' in options:
                                line.append(row.box)
                            if 'entry' in options:
                                line.append(row.entry)
                            if 'source' in options:
                                line.append(row.source)
                            if 'content' in options:
                                if row.content:
                                    line.append(unicode(row.content.value))
                                else:
                                    line.append(u'None')
                            if 'tags' in options:
                                tagsy= ''
                                for tag in row.mol.tags:
                                    tagsy += tag.name + ', '
                                line.append(tagsy)
                            if 'notes' in options:
                                line.append(row.notes)
                            spamwriter.writerow(line)
                    import paste.fileapp
                    f = paste.fileapp.FileApp(filepath)
                    from tg import use_wsgi_app
                    return use_wsgi_app(f)

        if selection and not search_clicked:
            argv =''
            gids = ''
            for arg in selection:
                argv += '/' + arg
                tmp_result = DBSession.query(LCompound).get(arg)
                gids += '/' + str(tmp_result.gid)
            if kw['akcja'] == u'edit':
                redirect('/%s/molecules/multiedit/index%s' % (pname, gids))
            elif kw['akcja'] == u'results':
                if len(selection) == 1:
                    redirect('/%s/results/new_result%s' % (pname, argv))
                else:
                    redirect('/%s/results/multiresults/index%s' % (pname, argv))
            elif kw['akcja'] == u'htrf':
                if len(selection) == 1:
                    redirect('/%s/results/htrf/add_result2%s' % (pname, argv))
        currentPage = paginate.Page(lcompound, page, url=page_url, items_per_page=items)
        return dict(currentPage=currentPage,tmpl=tmpl, page='results', htmlRgb=htmlRgb, htmlRgb100=htmlRgb100, Num2Rgb=Num2Rgb, pname=pname, alltags=alltags, similarity=similarity, cells=cells, ulists=ulists, ulist=ulist)