def _sole_query(self, site_id):
     exclude_ids = self._load_exclude_users(site_id)
     datefilter = self._form.get('datefilter', '')
     user_id = self._form.get('user_id', '')
     group_id = self._form.get('group_id', '')
     qtext = 'select user_id,count(user_id),max(timestamp)  from logins_registry '
     qvars = {}
     
     if user_id:
         qtext += ' where user_id like :user_id '
         qvars['user_id'] = '%'+user_id+'%'
     
     if datefilter:
         if 'where' in qtext:
             qtext += ' and timestamp > :lodate and timestamp < :hidate '
         else:
             qtext += ' where timestamp > :lodate and timestamp < :hidate '
         
         qvars['lodate'] = self._start
         qvars['hidate'] = self._end
         
     if group_id:
         if 'where' in qtext:
             qtext += ' and group_id like :group_id '
         else:
             qtext += ' where group_id like :group_id '
         qvars['group_id'] = '%|'+group_id + '|%'
     
     qtext += ' group by user_id; '
     
     print qtext
     results = Session.execute(qtext,qvars)
           
     return self._get_results(results)
 def _load_exclude_users(self, site_id):
     """Load user ids from login in the range. Used for performing negative logic"""
     exclude = self._form.get('exclude', '')
     if exclude:
         results = Session.query(distinct(LoginRecord.user_id)).filter(
                         LoginRecord.plone_site_id==site_id,
                         LoginRecord.timestamp>=self._start,
                         LoginRecord.timestamp<=self._end).all()
         return [user[0] for user in results]
     return None