admin.py
149 lines
| 5.3 KiB
| text/x-python
|
PythonLexer
r760 | # -*- coding: utf-8 -*- | |||
""" | ||||
r853 | rhodecode.controllers.admin.admin | |||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
r760 | ||||
r825 | Controller for Admin panel of Rhodecode | |||
r1203 | ||||
r760 | :created_on: Apr 7, 2010 | |||
:author: marcink | ||||
r1824 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> | |||
r760 | :license: GPLv3, see COPYING for more details. | |||
""" | ||||
r1206 | # This program is free software: you can redistribute it and/or modify | |||
# it under the terms of the GNU General Public License as published by | ||||
# the Free Software Foundation, either version 3 of the License, or | ||||
# (at your option) any later version. | ||||
r1203 | # | |||
r547 | # This program is distributed in the hope that it will be useful, | |||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
r1203 | # | |||
r547 | # You should have received a copy of the GNU General Public License | |||
r1206 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
r760 | ||||
r547 | import logging | |||
r1040 | ||||
r3062 | from pylons import request, tmpl_context as c, url | |||
r1040 | from sqlalchemy.orm import joinedload | |||
from webhelpers.paginate import Page | ||||
r3062 | from whoosh.qparser.default import QueryParser | |||
from whoosh import query | ||||
r3072 | from sqlalchemy.sql.expression import or_, and_, func | |||
r1040 | ||||
from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator | ||||
r547 | from rhodecode.lib.base import BaseController, render | |||
r3062 | from rhodecode.model.db import UserLog, User | |||
r3063 | from rhodecode.lib.utils2 import safe_int, remove_prefix, remove_suffix | |||
r3062 | from rhodecode.lib.indexers import JOURNAL_SCHEMA | |||
r3070 | from whoosh.qparser.dateparse import DateParserPlugin | |||
r3062 | ||||
r547 | ||||
log = logging.getLogger(__name__) | ||||
r1245 | ||||
r3070 | def _journal_filter(user_log, search_term): | |||
r3062 | """ | |||
Filters sqlalchemy user_log based on search_term with whoosh Query language | ||||
http://packages.python.org/Whoosh/querylang.html | ||||
:param user_log: | ||||
:param search_term: | ||||
""" | ||||
r3063 | log.debug('Initial search term: %r' % search_term) | |||
r3062 | qry = None | |||
if search_term: | ||||
qp = QueryParser('repository', schema=JOURNAL_SCHEMA) | ||||
r3070 | qp.add_plugin(DateParserPlugin()) | |||
r3062 | qry = qp.parse(unicode(search_term)) | |||
r3063 | log.debug('Filtering using parsed query %r' % qry) | |||
def wildcard_handler(col, wc_term): | ||||
if wc_term.startswith('*') and not wc_term.endswith('*'): | ||||
#postfix == endswith | ||||
wc_term = remove_prefix(wc_term, prefix='*') | ||||
r3072 | return func.lower(col).endswith(wc_term) | |||
r3063 | elif wc_term.startswith('*') and wc_term.endswith('*'): | |||
#wildcard == ilike | ||||
wc_term = remove_prefix(wc_term, prefix='*') | ||||
wc_term = remove_suffix(wc_term, suffix='*') | ||||
r3072 | return func.lower(col).contains(wc_term) | |||
r3062 | ||||
def get_filterion(field, val, term): | ||||
r3063 | ||||
r3062 | if field == 'repository': | |||
field = getattr(UserLog, 'repository_name') | ||||
elif field == 'ip': | ||||
field = getattr(UserLog, 'user_ip') | ||||
elif field == 'date': | ||||
field = getattr(UserLog, 'action_date') | ||||
elif field == 'username': | ||||
r3063 | field = getattr(UserLog, 'username') | |||
r3062 | else: | |||
field = getattr(UserLog, field) | ||||
r3063 | log.debug('filter field: %s val=>%s' % (field, val)) | |||
r3062 | ||||
#sql filtering | ||||
if isinstance(term, query.Wildcard): | ||||
r3063 | return wildcard_handler(field, val) | |||
r3062 | elif isinstance(term, query.Prefix): | |||
r3072 | return func.lower(field).startswith(func.lower(val)) | |||
r3070 | elif isinstance(term, query.DateRange): | |||
return and_(field >= val[0], field <= val[1]) | ||||
r3072 | return func.lower(field) == func.lower(val) | |||
r3062 | ||||
r3070 | if isinstance(qry, (query.And, query.Term, query.Prefix, query.Wildcard, | |||
query.DateRange)): | ||||
r3062 | if not isinstance(qry, query.And): | |||
qry = [qry] | ||||
for term in qry: | ||||
field = term.fieldname | ||||
r3070 | val = (term.text if not isinstance(term, query.DateRange) | |||
else [term.startdate, term.enddate]) | ||||
r3062 | user_log = user_log.filter(get_filterion(field, val, term)) | |||
elif isinstance(qry, query.Or): | ||||
filters = [] | ||||
for term in qry: | ||||
field = term.fieldname | ||||
r3070 | val = (term.text if not isinstance(term, query.DateRange) | |||
else [term.startdate, term.enddate]) | ||||
r3063 | filters.append(get_filterion(field, val, term)) | |||
r3062 | user_log = user_log.filter(or_(*filters)) | |||
return user_log | ||||
r547 | class AdminController(BaseController): | |||
r760 | ||||
r547 | @LoginRequired() | |||
def __before__(self): | ||||
super(AdminController, self).__before__() | ||||
r760 | ||||
@HasPermissionAllDecorator('hg.admin') | ||||
r547 | def index(self): | |||
r2662 | users_log = UserLog.query()\ | |||
r1040 | .options(joinedload(UserLog.user))\ | |||
r3062 | .options(joinedload(UserLog.repository)) | |||
#FILTERING | ||||
c.search_term = request.GET.get('filter') | ||||
try: | ||||
r3070 | users_log = _journal_filter(users_log, c.search_term) | |||
r3062 | except: | |||
# we want this to crash for now | ||||
raise | ||||
users_log = users_log.order_by(UserLog.action_date.desc()) | ||||
r1040 | ||||
r2845 | p = safe_int(request.params.get('page', 1), 1) | |||
r3062 | ||||
def url_generator(**kw): | ||||
return url.current(filter=c.search_term, **kw) | ||||
c.users_log = Page(users_log, page=p, items_per_page=10, url=url_generator) | ||||
r547 | c.log_data = render('admin/admin_log.html') | |||
r1421 | ||||
if request.environ.get('HTTP_X_PARTIAL_XHR'): | ||||
r547 | return c.log_data | |||
r760 | return render('admin/admin.html') | |||