Search code examples
pythontrac

Trac AttributeError: 'str' object has no attribute 'db_query' error when trying to get Components available in the system


I'm trying get all components in the current environment, but a reason I can't understand I get the following error:

AttributeError: 'str' object has no attribute 'db_query'

Here is the code (some parts are skipped):

  from trac.core import *
  from trac.ticket import model

  class TracIteraDirectory(Component):

  implements(INavigationContributor, ITemplateProvider, IRequestHandler)

  def process_request(self, req):
       myenv = '/home/konart/backup/Trac/TracDB/Planing'
       self.db = self.env.get_db_cnx()

       components = []
       test = model.Component(myenv)
       test1 = test.select(myenv)

       for each in test1:
           components.append((each.name, each.owner))

       #for component in model.Component(myenv).select(myenv):
           #components.append((component.name, component.owner))

I've checked out some plugin exanples on trac-hacks.org and core files, but just can't understand what I'm doing wrong here (or not doing at all)


Solution

  • I guess I overworked a bit. The answer was really simple - I was passing a simple string with a path to my env, instead of passing an actual Environment object:

    def process_request(self, req):
    
        if req.path_info.startswith('/traciteradirectory'):
    
            components = []
            test = model.Component(self.env)
            test1 = test.select(self.env)