def process_bind_param(self, value, engine): try: return inet.pton(value); except: l = len (value) if l == 4: return inet.IPV4PREFIX + value if l == 16: return value raise Exception ('Invalid IP:(' + value +')')
def __before__(self, action, **params): # This is done as a closure so it can just be called from the footer # in the template, putting off the final time() as late as possible start_time = time() def time_elapsed(): return time() - start_time c.time_elapsed = time_elapsed c.query_log = QueryLog() c.config = Config().readdb(model.Config) c.empty_form = FormGenerator() c.error_msgs = [] c.route = request_config().mapper_dict c.javascripts = ['jquery-1.2.6.pack', 'common'] c.site_name = config.get('site_name', 'Ferrox') if 'user_id' in session: try: user_id = session['user_id'] c.auth_user = model.Session.query(model.User) \ .options(eagerload('active_bans')) \ .get(user_id) except InvalidRequestError: # User may have been deleted in the interim del session['user_id'] session.save() if c.auth_user: ip = request.environ['REMOTE_ADDR'] ip = inet.pton(ip) if c.auth_user.can('admin.auth'): session['admin_ip'] = ip cip = inet.ntop(ip) # Log IPs ip_log_q = model.Session.query(model.IPLogEntry) last_ip_record = ip_log_q.filter_by(user_id = user_id) \ .order_by(model.IPLogEntry.end_time.desc()).first() if last_ip_record and last_ip_record.ip == cip: last_ip_record.end_time = datetime.now() else: model.Session.add(model.IPLogEntry(user_id, ip)) # Check to see if there are any active bans to expire if c.auth_user.active_bans: for ban in c.auth_user.active_bans: if ban.expires <= datetime.now(): ban.expired = True c.auth_user.role = ban.revert_to # Magical commit. model.Session.commit() else: c.auth_user = model.GuestUser()
def process_bind_param(self, value, engine): try: return inet.pton(value) except: l = len(value) if l == 4: return inet.IPV4PREFIX + value if l == 16: return value raise Exception("Invalid IP:(" + value + ")")