Example #1
0
def loadAttendanceFromDB(dbpath):
	ret = list()
	match = re.search('(\w+?)(\d+)\.db$', dbpath)
	if match is None:
		return ret
	series = match.group(1)
	year = int(match.group(2))

	session = Session()
	session.bind = create_engine('sqlite:///%s' % dbpath)
	settings = Settings()
	settings.load(session)

	for row in session.execute("select d.firstname, d.lastname, count(distinct r.eventid) from runs as r, cars as c, drivers as d where r.carid=c.id and c.driverid=d.id and r.eventid < 100 group by d.id"):
		count = int(row[2])
		ret.append((series.lower(), year, row[0].lower().strip(), row[1].lower().strip(), count, int(count > settings.useevents)))

	return ret
Example #2
0
	def __call__(self, environ, start_response):
		"""Invoke the Controller"""
		# WSGIController.__call__ dispatches to the Controller method
		# the request is routed to. This routing information is
		# available in environ['pylons.routes_dict']
		log.debug("start(%s)" % (environ['PATH_INFO']))

		self.srcip = request.environ.get("X_FORWARDED_FOR", request.environ["REMOTE_ADDR"]) 
		self.routingargs = environ['wsgiorg.routing_args'][1]
		self.database = self.routingargs.get('database', None)
		if os.path.exists(self.databasePath(self.database)):
			engine = create_engine('sqlite:///%s' % self.databasePath(self.database), poolclass=NullPool)
		else:
			engine = create_engine('sqlite:///:memory:', poolclass=NullPool)
			self.database = None

		self.session = Session()
		self.session.bind = engine
		metadata.bind = engine

		self.settings = Settings()
		if self.database is not None:
			self.settings.load(self.session)
			if self.settings.schema != '661':
				start_response('200 OK', [('content-type', 'text/html')], None)
				return "Software schema verison is 661 but series database is " + self.settings.schema +  \
					", database schema or software needs to be updated to match"

		try:
			try:
				return WSGIController.__call__(self, environ, start_response)
			except BeforePage, p:
				start_response('200 OK', [('content-type', 'text/html')], None)
				return p.data
		finally:
			Session.remove()
			log.debug("finish(%s): %f" % (environ['PATH_INFO'], time.time() - self.a))
Example #3
0
class BaseController(WSGIController):

	def __init__(self):
		self.a = time.time()

	def copyvalues(self, src, dst):
		for k, v in src.iteritems():
			if hasattr(dst, k):
				setattr(dst, k, v)

	def loadPythonFunc(self, func, text):
		# Create place to load stuff defined in loaded code, provide limited builtins
		loadenv = dict()
		sand = dict()
		for k in ['str', 'range']:
			sand[k] = __builtins__[k]
		loadenv['__builtins__'] = sand

		# Some flailing attempt at stopping bad behaviour
		if 'import' in text:
			raise Exception("python code to load contains import, not loading")
		
		text = str(text)
		text = text.replace('\r', '')
		exec text in loadenv
		return loadenv[func]
		
	def databasePath(self, database):
		return os.path.join(config['seriesdir'], '%s.db' % (database))

	def __call__(self, environ, start_response):
		"""Invoke the Controller"""
		# WSGIController.__call__ dispatches to the Controller method
		# the request is routed to. This routing information is
		# available in environ['pylons.routes_dict']
		log.debug("start(%s)" % (environ['PATH_INFO']))

		self.srcip = request.environ.get("X_FORWARDED_FOR", request.environ["REMOTE_ADDR"]) 
		self.routingargs = environ['wsgiorg.routing_args'][1]
		self.database = self.routingargs.get('database', None)
		if os.path.exists(self.databasePath(self.database)):
			engine = create_engine('sqlite:///%s' % self.databasePath(self.database), poolclass=NullPool)
		else:
			engine = create_engine('sqlite:///:memory:', poolclass=NullPool)
			self.database = None

		self.session = Session()
		self.session.bind = engine
		metadata.bind = engine

		self.settings = Settings()
		if self.database is not None:
			self.settings.load(self.session)
			if self.settings.schema != '661':
				start_response('200 OK', [('content-type', 'text/html')], None)
				return "Software schema verison is 661 but series database is " + self.settings.schema +  \
					", database schema or software needs to be updated to match"

		try:
			try:
				return WSGIController.__call__(self, environ, start_response)
			except BeforePage, p:
				start_response('200 OK', [('content-type', 'text/html')], None)
				return p.data
		finally:
			Session.remove()
			log.debug("finish(%s): %f" % (environ['PATH_INFO'], time.time() - self.a))