Esempio n. 1
0
 def document(self):
     """Render the error document"""
     icode = 404
     code= "404"
     status = "Not Found"
     resp = request.environ.get('pylons.original_response')
     if resp and resp.body:
         content = literal(resp.body)
     else:
         content = request.GET.get('message', '')
         if content:
             content = cgi.escape(content)
     if resp and resp.status_int: 
         icode = resp.status_int
         code = str(resp.status_int)
     elif request.GET.get('code', ''):
         code = request.GET.get('code')
         if code:
             code = cgi.escape(code)
         else:
             code = 404
     if resp and resp.status: 
         status = resp.status
     c.message = request.GET.get('message', '')
     if c.message:
         c.message = cgi.escape(c.message)
     else:
         c.message = content
     accept_list = None
     if 'HTTP_ACCEPT' in request.environ:
         try:
             accept_list = conneg_parse(request.environ['HTTP_ACCEPT'])
         except:
             accept_list= [MT("text", "plain")]
     if not accept_list:
         accept_list= [MT("text", "plain")]
     mimetype = accept_list.pop(0)
     while(mimetype):
         if str(mimetype).lower() in ["text/html", "text/xhtml"]:
             #page = error_document_template % \
             #dict(prefix=request.environ.get('SCRIPT_NAME', ''),
             #    code=code,
             #    message=content)
             #return page
             #c.status = status.replace(c.code, '').strip()
             return render('/error.html')
         elif str(mimetype).lower() in ["text/plain", "application/json"]:
             response.content_type = 'text/plain; charset="UTF-8"'
             response.status_int = icode
             response.status = status
             return content
         try:
             mimetype = accept_list.pop(0)
         except IndexError:
             mimetype = None
     #Whoops nothing satisfies - return text/plain
     response.content_type = 'text/plain; charset="UTF-8"'
     response.status_int = resp.status_int
     response.status = resp.status
     return content
 def managesource(self, source):
     c.silo_name = source
     c.ident = ''
     c.id =""
     c.q=""
     c.typ=""
     c.path =""
     c.user_logged_in_name=""
     return render('/manage_source.html')
Esempio n. 3
0
 def index(self):
     c.silo_name = ''
     c.ident = ''
     c.id =""
     c.path =""
     c.q=""
     c.typ=""
     c.user_logged_in_name=""
     return render('/cookies.html')
Esempio n. 4
0
    def login(self):
        #c.ident = None
        #c.ident = request.environ.get('repoze.who.identity')
        #script_name = request.environ.get('SCRIPT_NAME') or '/'
        #referer = request.environ.get('HTTP_REFERER', script_name)

        #if not c.ident:
        #    abort(401, "Not Authorised")
        c.login_counter = request.environ['repoze.who.logins']
        if c.login_counter > 0:
            session['login_flash'] = """Wrong credentials. Have you been registered?"""
            session.save()
        c.came_from = request.params.get('came_from') or "/"
        return render('/login.html')
Esempio n. 5
0
    def registersource(self):
        c.silo_name = ''
        c.ident = ''
        c.id =""
        c.path =""
        c.user_logged_in_name=""
        c.q=""
        c.typ=""
        c.host=ag.host
        sourceinfo= SourceInfo()
        c.message=None
        #srcurl = ag.root +'/admin'
        sourceinfo.silo = request.params.get('silo', "")
        sourceinfo.title = request.params.get('title', "")
        sourceinfo.description = request.params.get('description', "")
        sourceinfo.notes = request.params.get('notes', "")
        sourceinfo.administrators = request.params.get('administrators', "")
        sourceinfo.managers = request.params.get('managers', "")
        sourceinfo.users = request.params.get('users', "")
        sourceinfo.disk_allocation = request.params.get('disk_allocation', "")
        
        try:
            s_q= meta.Session.query(SourceInfo).filter(SourceInfo.silo == sourceinfo.silo)
            s_q.one()
            c.message = "Source with the chosen name already exists. Choose another"
            c.kw = {'silo':sourceinfo.silo, 
                        'title':sourceinfo.title,                       
                        'description':sourceinfo.description,
                        'notes':sourceinfo.notes,
                        'users':sourceinfo.users,
                        'disk_allocation':sourceinfo.disk_allocation
                       }
            c.header="create"
            c.activate=None    
            return render("/create_new_source.html")    
        except orm.exc.NoResultFound:
            sourceinfo.activate =  False
            meta.Session.add(sourceinfo)
            meta.Session.commit()
            pass
        except IntegrityError:
            #log.error('Error adding user %s'%user_details['username'])
            #print traceback.format_exc()
            meta.Session.rollback()
            return False

        return redirect(url(controller='list_sources', action='index'))   
    def approve(self, source):
        c.silo_name = ""
        c.ident = ""
        c.id = ""
        c.path = ""
        c.user_logged_in_name = ""
        c.q = ""
        c.typ = ""
        c.src = ag.root
        c.host = ag.host
        c.silo = ""
        c.source = source
        c.message = None
        state_info = None
        print "source requested: "
        print c.source
        c.activate = ""
        c.header = "approve"
        c.kw = {}
        text = "Approval needed for the registered source: '" + source + "'"
        msg = MIMEText(text)
        msg["Subject"] = msg
        msg["From"] = "*****@*****.**"
        msg["To"] = "*****@*****.**"

        try:
            s_q = (
                meta.Session.query(SourceInfo).filter(SourceInfo.silo == c.source).filter(SourceInfo.activate == False)
            )
            for src in s_q:
                c.kw = {
                    "silo": src.silo,
                    "title": src.title,
                    "description": src.description,
                    "notes": src.notes,
                    "administrators": src.administrators,
                    "managers": src.managers,
                    "users": src.users,
                    "disk_allocation": src.disk_allocation,
                    "activate": src.activate,
                }
        except IntegrityError:
            meta.Session.rollback()
            return False
        return render("/create_new_source.html")
 def index(self):
     c.silo_name = ""
     c.ident = ""
     c.id = ""
     c.path = ""
     c.user_logged_in_name = ""
     c.q = ""
     c.typ = ""
     c.src = ag.root
     c.host = ag.host
     c.silo = ""
     c.source = ""
     c.kw = {}
     c.activate = None
     c.message = None
     c.header = "create"
     c.kw = {}
     return render("/create_new_source.html")
    def index(self):
        c.silo_name = ''
        c.ident = ''
        c.id =""
        c.q=""
        c.typ=""
        c.path =""
        c.user_logged_in_name=""
        c.src = ag.root
        c.host = ag.host
        c.message=""
        user_name = 'admin'
        password = '******'
        datastore = HTTPRequest(endpointhost=c.host)
        datastore.setRequestUserPass(endpointuser=user_name, endpointpass=password)

        (resp, respdata) = datastore.doHTTP_GET(resource="/silos", expect_type="application/JSON")
        c.sources =  json.loads(respdata)
        print c.sources
        
        c.source_infos = {}
        for source in c.sources:
            (resp, respdata) = datastore.doHTTP_GET(resource='/' + source + '/states', expect_type="application/JSON")
            state_info =  json.loads(respdata)
            c.source_infos[source] = [source, len(state_info['datasets'])]
        ##print "sourceinfos:"
        ##print c.source_infos
        
        c.unregistered_sources = []
        
        
        try:
            s_q= meta.Session.query(SourceInfo.silo).filter(SourceInfo.activate == False)
            for source in s_q:
                c.unregistered_sources.append(source.silo)
            print "Unregistered sources"
            print c.unregistered_sources
        except IntegrityError:
            meta.Session.rollback()
            return False
        return render('/list_of_sources.html')
Esempio n. 9
0
    def approve(self,source):
        c.silo_name = ''
        c.ident = ''
        c.id =""
        c.path =""
        c.user_logged_in_name=""
        c.q=""
        c.typ=""
        c.src = ag.root
        c.host = ag.host
        c.silo=""
        c.source = source
        c.message =None
        state_info = None       
        print "source requested: "
        print c.source 
        c.activate=""
        c.header = "approve"
        c.kw={}
        text = "Approval needed for the registered source: '" + source +"'"


        try:
            s_q= meta.Session.query(SourceInfo).filter(SourceInfo.silo == c.source).filter(SourceInfo.activate == False)  
            for src in s_q:
                c.kw = {'silo':src.silo, 
                        'title':src.title,                       
                        'description':src.description,
                        'notes':src.notes,
                        'administrators': src.administrators,
                        'managers':src.managers,
                        'users':src.users,
                        'disk_allocation':src.disk_allocation,
                        'activate':src.activate
                       }       
        except IntegrityError:
            meta.Session.rollback()
            return False     
        return render("/create_new_source.html")    
         
Esempio n. 10
0
    def index(self):
        c.silo_name = ''
        c.ident = ''
        c.id =""
        c.path =""
        c.user_logged_in_name=""
        c.q=""
        c.typ=""
        c.host=ag.host
        print "endpointhost"
        print c.host
        srcurl = ag.root +'/admin'
        silo = request.params.get('silo', "")
        title = request.params.get('title', "")
        description = request.params.get('description', "")
        notes = request.params.get('notes', "")
        administrators = request.params.get('administrators', "")
        managers = request.params.get('managers', "")
        users = request.params.get('users', "")
        disk_allocation = request.params.get('disk_allocation', "")

        user_name = 'admin'
        password = '******'
        datastore = HTTPRequest(endpointhost=c.host)
        
        datastore.setRequestUserPass(endpointuser=user_name, endpointpass=password)
        fields = \
            [ ("silo", silo),
              ("title", title),
              ("description", description),
              ("notes", notes),
              ("administrators", administrators),
              ("managers", managers),
              ("users", users),
              ("disk_allocation", disk_allocation)
            ]
        print fields
        files =[]
        (reqtype, reqdata) = SparqlQueryTestCase.encode_multipart_formdata(fields, files)
        
        (resp,respdata) = datastore.doHTTP_POST(reqdata, reqtype, resource="/admin", expect_type="application/JSON")
        print 'respdata', respdata
        print 'msg', resp.msg
        print 'reason', resp.reason
        print 'status',resp.status
        print resp.read()
##            print "response data for update metadata"
        if  resp.status== 204 or resp.status==201:
            c.message = "Source successfully created."
            
            #Modify the source entry in the sqllite database in the data finder. Change activate = True.
            try:
                s_q= meta.Session.query(SourceInfo).filter(SourceInfo.silo == silo)
                s_q.one()
                s_q.update({
                               'title':title,
                               'description':description,
                               'notes':notes,
                               'administrators':administrators,
                               'managers':managers,
                               'users':users,
                               'disk_allocation':disk_allocation,
                               'activate':True
                            })     
                meta.Session.commit()
            except orm.exc.NoResultFound:
                sourceinfo.activate =  True
                meta.Session.add(sourceinfo)
                meta.Session.commit()
                pass
            except IntegrityError:
                #log.error('Error adding user %s'%user_details['username'])
                #print traceback.format_exc()
                meta.Session.rollback()
                return False
            return redirect(url(controller='list_sources', action='index')) 
        else:
            c.message = "Source could not be successfully activated"
            c.message = c.message + " status: " + repr(resp.status) + " " + resp.reason
            c.kw = {    'silo':silo, 
                        'title':title,                       
                        'description':description,
                        'notes':notes,
                        'administrators': administrators,
                        'managers':managers,
                        'users':users,
                        'disk_allocation':disk_allocation
                       }
            c.header="approve"
            c.activate=None    
            return render("/create_new_source.html")    
Esempio n. 11
0
    def sourceinfo(self, source):
        c.silo_name = ''
        c.ident = ''
        c.id =""
        c.path =""
        c.user_logged_in_name=""
        c.q=""
        c.typ=""
        c.src = ag.root
#        if not request.environ.get('repoze.who.identity'):
#            abort(401, "Not Authorised")
#        if not ag.granary.issilo(silo):
#            abort(404)
#        ident = request.environ.get('repoze.who.identity')
#        c.ident = ident
#        c.silo = silo
#        silos = ag.authz(ident, permission=['administrator', 'manager'])
#        if not silo in silos:
#            abort(403, "Do not have administrator or manager credentials for silo %s"%silo)
#        user_groups = list_user_groups(ident['user'].user_name)
#        if ('*', 'administrator') in user_groups:
#            #User is super user
#            c.roles = ["admin", "manager", "user"]
#        elif (silo, 'administrator') in user_groups:
#            c.roles = ["admin", "manager", "user"]
#        elif (silo, 'manager') in user_groups:
#            c.roles = ["manager", "user"]
#        else:
#            abort(403, "Do not have administrator or manager credentials for silo %s"%silo)
        http_method = request.environ['REQUEST_METHOD']
        ## hardcoded for now
        c.roles = ["admin", "manager", "user"]
        ##c.kw = ag.granary.describe_silo(source)
        c.host = ag.host
        c.silo=""
        
        
        user_name = 'admin'
        password = '******'
        datastore = HTTPRequest(endpointhost=c.host)
        datastore.setRequestUserPass(endpointuser=user_name, endpointpass=password)
        c.source = source
        c.message =""
        state_info = None     
        print "source requested: "
        print c.source 
        c.kw={}
#        try:
#            c.kw = {    'silo':src.silo, 
#                        'title':src.title,                       
#                        'description':src.description,
#                        'notes':src.notes,
#                        'users':src.users,
#                        'disk_allocation':src.disk_allocation,
#                        'activate':src.activate
#                    }    
#            s_q= meta.Session.query(SourceInfo).filter(SourceInfo.silo == c.source).filter(SourceInfo.activate == False)  
#            s_q.one()
#            return render("/admin_sourceview.html") 
#        except orm.exc.NoResultFound:
#            sourceinfo.activate =  False
#            meta.Session.add(sourceinfo)
#            meta.Session.commit()
#            pass      
#        except IntegrityError:
#            meta.Session.rollback()
#            return False     
            
            ## If the source is not activated only then get the source information from the registered information area
        (resp, respdata) = datastore.doHTTP_GET(resource='/' + c.source + '/states', expect_type="application/JSON")
        state_info =  json.loads(respdata)       
        ##print json.loads(respdata)
        c.kw=state_info
        
        print "http_method = "
        print http_method
        if http_method == "GET":
            return render("/admin_sourceinfo.html")
        elif http_method == "POST":
            ##silo = request.params.get('silo', None)
            title = request.params.get('title', '')
            description = request.params.get('description', '')
            notes = request.params.get('notes', '')
            administrators = request.params.get('administrators', '')
            managers = request.params.get('managers', '')
            users = request.params.get('users', '')
            disk_allocation = request.params.get('disk_allocation', '')
            fields = \
                [ ("silo", source),
                  ("title", title),
                  ("description", description),
                  ("notes", notes),
                  ("administrators", administrators),
                  ("managers", managers),
                  ("users", users),
                  ("disk_allocation", disk_allocation)
                ]
            print fields
            files =[]
            (reqtype, reqdata) = SparqlQueryTestCase.encode_multipart_formdata(fields, files)
            (resp,respdata) = datastore.doHTTP_POST(reqdata, reqtype, resource='/' + source + "/admin", expect_type="application/JSON")
            resource ='/' + source + "/admin"
            print 'respdata', respdata
            print 'msg', resp.msg
            print 'reason', resp.reason
            print 'status',resp.status
            print resp.read()
##            print "response data for update metadata"
            if  resp.status== 204:
                c.message = "Metadata updated"
                return render("/admin_sourceinfo.html")
            else:
                abort(resp.status, respdata )
                
        elif http_method == "DELETE":
            ##fields = [("silo", source)]
            ##print fields
            ##files =[]
            (reqtype, reqdata) = SparqlQueryTestCase.encode_multipart_formdata(fields, files)
            ##(resp,respdata) = datastore.doHTTP_DELETE(reqdata, reqtype, resource='/' + source + "/admin", expect_type="application/JSON")
          
            (resp,respdata) = datastore.doHTTP_DELETE(resource='/' + source + "/admin")
            resource ='/' + source + "/admin"
            print resp.read()
 
            print "Response Status = "
            print resp.status 

            if  resp.status== 200:
                #Modify the source entry in the sqllite database in the data finder. Change activate = False.
                try:
                    s_q= meta.Session.query(SourceInfo).filter(SourceInfo.silo == source)
                    s_q.one()
                    s_q.update({
                                   'title':title,
                                   'description':description,
                                   'notes':notes,
                                   'administrators':administrators,
                                   'managers':managers,
                                   'users':users,
                                   'disk_allocation':disk_allocation,
                                   'activate':False
                                })     
                    meta.Session.commit()
                except orm.exc.NoResultFound:
                    sourceinfo.activate =  False
                    meta.Session.add(sourceinfo)
                    meta.Session.commit()
                    pass
                except IntegrityError:
                    #log.error('Error adding user %s'%user_details['username'])
                    #print traceback.format_exc()
                    meta.Session.rollback()
                    return False
                c.message = "Metadata deleted"
                return True 
            else:
                print "Failure"
                abort(resp.status, respdata )
Esempio n. 12
0
    def raw(self):
        ident = request.environ.get('repoze.who.identity')  
        c.ident = ident

        silos = None
        if ag.metadata_embargoed:
            if not ident:
                abort(401, "Not Authorised")
            silos = ag.authz(ident)

        if silos and not isinstance(silos, basestring) and type(silos).__name__ == 'list':
            silos = ' '.join(silos)
               
        http_method = request.environ['REQUEST_METHOD']        
        if http_method == "GET":
            params = request.GET
        elif http_method == "POST":
            params = request.POST

        if not "q" in params:
            abort(400, "Parameter 'q' is not available")

        #If ag.metadata_embargoed, search only within your silos
        if params['q'] == '*':
            if silos:
                params['q'] = """silo:(%s)"""%silos
            else:
                params['q'] = "*:*"
        elif silos and not 'silo:' in params['q']:
            params['q'] = """%s AND silo:(%s)"""%(params['q'], silos)        

        accept_list = None
        if 'wt' in params and params['wt'] == "json":
            accept_list = [MT("application", "json")]
        elif 'wt' in params and params['wt'] == "xml":
            accept_list = [MT("text", "xml")]
        else:                       
            if 'HTTP_ACCEPT' in request.environ:
                try:
                    accept_list = conneg_parse(request.environ['HTTP_ACCEPT'])
                except:
                    accept_list= [MT("text", "html")]
            if not accept_list:
                accept_list= [MT("text", "html")]

            mimetype = accept_list.pop(0)
            while(mimetype):
                if str(mimetype).lower() in ["text/html", "text/xhtml"]:
                    params['wt'] = 'json'
                    accept_list= [MT("text", "html")]
                    break                 
                elif str(mimetype).lower() in ["text/plain", "application/json"]:
                    params['wt'] = 'json'
                    accept_list= [MT("application", "json")]
                    break
                elif str(mimetype).lower() in ["application/rdf+xml", "text/xml"]:
                    params['wt'] = 'xml'
                    accept_list = [MT("text", "xml")]
                    break
                # Whoops - nothing satisfies
                try:
                    mimetype = accept_list.pop(0)
                except IndexError:
                    mimetype = None

        if not 'wt' in params or not params['wt'] in ['json', 'xml']:
            params['wt'] = 'json'
            accept_list= [MT("text", "html")]        
        if not 'fl' in params or not params['fl']:
            #Also include the following fields - date modified, publication year / publication date, embargo status, embargo date, version
            params['fl'] = "id,silo,mediator,creator,title,score"
        if not 'start' in params or not params['start']:
            params['start'] = '0'
        if not 'rows' in params or not params['rows']:
            params['rows'] = '100'
        try:            
            result = ag.solr.raw_query(**params)
        except:
            result = {}
        
        mimetype = accept_list.pop(0)
        while(mimetype):
            if str(mimetype).lower() in ["text/html", "text/xhtml"]:
                c.result = result                    
                return render('/raw_search.html')        
            elif str(mimetype).lower() in ["text/plain", "application/json"]:
                response.content_type = 'application/json; charset="UTF-8"'
                response.status_int = 200
                response.status = "200 OK"
                return result
            elif str(mimetype).lower() in ["application/rdf+xml", "text/xml"]:
                response.content_type = 'text/xml; charset="UTF-8"'
                response.status_int = 200
                response.status = "200 OK"
                return result
            # Whoops - nothing satisfies
            try:
                mimetype = accept_list.pop(0)
            except IndexError:
                mimetype = None
        #Whoops - nothing staisfies - default to text/html
        c.result = result         
        return render('/raw_search.html')
Esempio n. 13
0
    def advanced(self):

        c.q = "*:*"
        c.typ = 'all'

        # Search controls
        format = 'html'
        c.sort = 'score desc'
        c.sort_text = c.sort_options[c.sort]
        
        c.chosen_fields = []
        c.chosen_fields.extend(c.search_fields)

        c.fields_to_facet = []
        c.fields_to_facet.extend(c.facetable_fields)

        c.facet_limit = 10

        c.chosen_facets = {}
        
        query_filter = ""
        
        #Setup to capture all the url parameters needed to regenerate this search
        c.search = {}
        filter_url = ""
        
        c.truncate = 450
        c.start = 0
        c.rows = 25
        c.search['truncate'] = c.truncate
        c.search['type'] = c.typ

        solr_params = {}
        
        if c.q:
            solr_params['q'] = c.q.encode('utf-8')+query_filter  
            solr_params['wt'] = 'json'
            solr_params['fl'] = ','.join(c.chosen_fields)
            solr_params['rows'] = c.rows
            solr_params['start'] = c.start
            if c.sort:
                solr_params['sort'] = c.sort                
            if c.fields_to_facet:
                solr_params['facet'] = 'true'
                solr_params['facet.limit'] = c.facet_limit
                solr_params['facet.mincount'] = 1
                solr_params['facet.field'] = []
                for facet in c.fields_to_facet:
                    solr_params['facet.field'].append(facet)
            try:
                solr_response = ag.solr.raw_query(**solr_params)
            except:
                solr_response = None
        
      
            c.add_facet =  u"%ssearch/detailed?q=%s&" % (ag.root, c.q.encode('utf-8'))
            c.add_facet = c.add_facet + urlencode(c.search) + filter_url
 
            if not solr_response:
                # FAIL - do something here:
                c.message = 'Sorry, either that search "%s" resulted in no matches, or the search service is not functional.' % c.q
                h.redirect_to(controller='/search', action='index')
        
            search = json.loads(solr_response)
                
            numFound = search['response'].get('numFound',None)
            try:
                c.numFound = int(numFound)
            except:
                c.numFound = 0
            c.docs = search['response'].get('docs',None)
        
            if c.fields_to_facet:
                c.returned_facets = {}
                for facet in search['facet_counts']['facet_fields']:       
                    facet_list = search['facet_counts']['facet_fields'][facet]
                    keys = facet_list[::2]
                    values = facet_list[1::2]
                    c.returned_facets[facet] = []
                    for index in range(len(keys)):
                        c.returned_facets[facet].append((keys[index],values[index]))

        return render('/search_advanced.html')
Esempio n. 14
0
    def detailed(self, query=None, additional_fields=[]):

        c.silo_name = ''
        c.ident = ''
        c.id =""
        c.path =""
        c.user_logged_in_name=""
        c.q=""
        c.typ=""
        c.docs=""
        c.returned_facets=""

        if query:
            c.q = query
        else:        
            c.q = request.params.get('q', None)
        try:
            c.q = unquote(c.q)
        except:
            pass

        c.typ = 'all'
        if request.params.get("type", None):
            c.typ = request.params.get("type")

        if not c.q or c.q == '*' or c.q == "":
            c.q = "*:*"
 
        # Search controls
        truncate = request.params.get('truncate', None)
        start = request.params.get('start', None)
        rows = request.params.get('rows', None)
        sort = request.params.get('sort', None)
        fields = request.params.get('fl', None)
        res_format = request.params.get('format', None)
        if not res_format:
            accept_list = None
            if 'HTTP_ACCEPT' in request.environ:
                try:
                    accept_list = conneg_parse(request.environ['HTTP_ACCEPT'])
                except:
                    accept_list= [MT("text", "html")]
            if not accept_list:
                accept_list= [MT("text", "html")]
            mimetype = accept_list.pop(0)
            while(mimetype):
                if str(mimetype).lower() in ["text/html", "text/xhtml"]:
                    res_format = 'html'
                    break
                elif str(mimetype).lower() in ["text/plain", "application/json"]:
                    res_format = 'json'
                    break
                elif str(mimetype).lower() in ["text/xml"]:
                    res_format = 'xml'
                    break
                elif str(mimetype).lower() in ["text/csv"]:
                    res_format = 'csv'
                    break
                try:
                    mimetype = accept_list.pop(0)
                except IndexError:
                    mimetype = None
            # Whoops - nothing satisfies - return text/plain
            if not res_format:
                res_format = 'html'

        c.sort = 'score desc'
        # Lock down the sort parameter.
        if sort and sort in c.sort_options:
            c.sort = sort
        c.sort_text = c.sort_options[c.sort]
        
        c.chosen_fields = []
        c.chosen_fields.extend(c.search_fields)

        if fields:
            fields = fields.split(',')
        if fields and type(fields).__name__ == 'list':
            fields = [x.strip() for x in fields]
            for fld in fields:
                if fld in c.all_fields and not fld in c.chosen_fields:
                    c.chosen_fields.append(fld)

        for fld in additional_fields:
            if not fld in c.chosen_fields:
                c.chosen_fields.append(fld)

        c.fields_to_facet = []
        c.fields_to_facet.extend(c.facetable_fields)

        c.facet_limit = 10

        c.chosen_facets = {}
        
        query_filter = ""
        
        #Setup to capture all the url parameters needed to regenerate this search
        c.search = {}
        filter_url = ""
        
        for field in c.all_fields:
            if request.params.get("filter"+field, None):
                multi = request.params.getall("filter"+field)
                c.chosen_facets[field] = []
                #c.search["filter"+field] = ""
                for m in multi:
                    try:
                        m = unquote(m)
                    except:
                        pass
                    m = m.strip()
                    m = m.strip('"')
                    c.chosen_facets[field].append(m)
                    query_filter += ' AND %s:"%s"'%(field, m)
                    try:
                        filter_url += '&filter%s=%s'%(field, quote('"%s"'%m))
                    except:
                        filter_url += '&filter%s=%s'%(field, '"%s"'%m)
                #if field in c.fields_to_facet:
                #    del c.fields_to_facet[field]
        
        for field in c.chosen_facets:
            if field not in c.chosen_fields:
                c.chosen_fields.append(field)

        c.truncate = 450
        c.start = 0
        c.rows = 25
        
        # Parse/Validate search controls
        if truncate:
            try:
                c.truncate = int(truncate)
            except ValueError:
                pass
            if c.truncate < 10:
                c.truncate = 10
            if c.truncate > 1000:
                c.truncate = 1000
             
        if start:
            try:
                c.start = int(start)
            except ValueError:
                pass
            if c.start < 0:
                c.start = 0

        if rows:
            try:
                c.rows = int(rows)
            except ValueError:
                pass
        if c.rows < 5:
            c.rows = 5
        elif c.rows > 5000:
            c.rows=5000
            
        #c.search['rows'] = c.rows
        c.search['truncate'] = c.truncate
        c.search['type'] = c.typ
        #c.search['start'] = c.start
        #c.search['sort'] = c.sort
        #if c.q:
        #    c.search['q'] = c.q.encode('utf-8')
        solr_params = {}
        
        if c.q:
            if c.typ and 'silo' in c.typ:
                solr_params['q'] = c.q.encode('utf-8')+query_filter+" AND type:silo"
            elif c.typ and 'dataset' in c.typ:
                solr_params['q'] = c.q.encode('utf-8')+query_filter+" AND type:dataset"
            elif c.typ and 'item' in c.typ and c.q != "*:*":
                #solr_params['q'] = """aggregatedResource:"%s" %s"""%(c.q.encode('utf-8'),query_filter)
                solr_params['q'] = """filename:"%s" %s"""%(c.q.encode('utf-8'),query_filter)
            else:
                solr_params['q'] = c.q.encode('utf-8')+query_filter  

            if res_format in ['json', 'xml', 'python', 'php']:
                solr_params['wt'] = res_format
            else:
                solr_params['wt'] = 'json'

            solr_params['fl'] = ','.join(c.chosen_fields)
            solr_params['rows'] = c.rows
            solr_params['start'] = c.start

            if c.sort:
                solr_params['sort'] = c.sort
                 
            if c.fields_to_facet:
                solr_params['facet'] = 'true'
                solr_params['facet.limit'] = c.facet_limit
                solr_params['facet.mincount'] = 1
                solr_params['facet.field'] = []
                for facet in c.fields_to_facet:
                    solr_params['facet.field'].append(facet)

            solr_response = None 
            try:
                solr_response = ag.solr.raw_query(**solr_params)
                print solr_response
            except:
                pass
            c.add_finder_facet =  u"%ssearch/detailed?q=%s&" % (ag.root, c.q.encode('utf-8'))

            c.add_finder_facet = c.add_finder_facet + urlencode(c.search) + filter_url
            
            c.add_facet = u"%ssearch/detailed?q=%s&" % ('/', c.q.encode('utf-8'))
            c.add_facet = c.add_facet + urlencode(c.search) + filter_url
            c.src = ag.root
            if not solr_response:
                # conneg return
                response.status_int = 200
                response.status = "200 OK"
                if res_format == "html":
                    c.numFound = 0
                    c.message = 'Sorry, either that search "%s" resulted in no matches, or the search service is not functional.' % c.q
                    return render('/search.html')
                elif res_format == 'xml':
                    response.headers['Content-Type'] = 'application/xml'
                    response.charset = 'utf8'
                    c.atom = {}
                    return render('/atom_results.html')
                elif res_format == 'json':
                    response.headers['Content-Type'] = 'application/json'
                    response.charset = 'utf8'
                    return {}
                else:
                    response.headers['Content-Type'] = 'application/text'
                    response.charset = 'utf8'
                    return solr_response
        
            response.status_int = 200
            response.status = "200 OK"

            if res_format == 'xml':
                response.headers['Content-Type'] = 'application/xml'
                response.charset = 'utf8'
                c.atom = solr_response
                return render('/atom_results.html')
            elif res_format == 'json':
                response.headers['Content-Type'] = 'application/json'
                response.charset = 'utf8'
                return solr_response
            elif res_format in ['csv', 'python', 'php']:
                response.headers['Content-Type'] = 'application/text'
                response.charset = 'utf8'
                return solr_response
                
            search = json.loads(solr_response)
                
            numFound = search['response'].get('numFound',None)
            
            c.numFound = 0
            c.permissible_offsets = []
            
            c.pages_to_show = 5
            
            try:
                c.numFound = int(numFound)
                remainder = c.numFound % c.rows
                if remainder > 0:
                    c.lastPage = c.numFound - remainder
                else:
                    c.lastPage = c.numFound - c.rows
                
                if c.numFound > c.rows:
                    offset_start = c.start - ( (c.pages_to_show/2) * c.rows )
                    if offset_start < 0:
                        offset_start = 0
                    
                    offset_end = offset_start + (c.pages_to_show * c.rows)
                    if offset_end > c.numFound:
                        offset_end = c.numFound
                        if remainder > 0:
                            offset_start = c.lastPage - (c.pages_to_show * c.rows)
                        else:
                            offset_start = c.lastPage - ((c.pages_to_show-1) * c.rows)
                        
                        if offset_start < 0:
                            offset_start = 0
                    
                    c.permissible_offsets = list( xrange( offset_start, offset_end, c.rows) )
            except ValueError:
                pass

            c.docs = search['response'].get('docs',None)
        
            if c.fields_to_facet:
                c.returned_facets = {}
                for facet in search['facet_counts']['facet_fields']:       
                    facet_list = search['facet_counts']['facet_fields'][facet]
                    keys = facet_list[::2]
                    values = facet_list[1::2]
                    c.returned_facets[facet] = []
                    for index in range(len(keys)):
                        c.returned_facets[facet].append((keys[index],values[index]))

        return render('/search.html')