#! /usr/bin/env python # coding=utf-8 ############################################################################# # # # File: admin.py # # # # Copyright (C) 2008 Du XiaoGang # # # # Home: http://gappproxy.googlecode.com # # # # This file is part of GAppProxy. # # # # GAppProxy 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. # # # # GAppProxy 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. # # # # You should have received a copy of the GNU General Public License # # along with GAppProxy. If not, see . # # # ############################################################################# import wsgiref.handlers from google.appengine.ext import webapp from google.appengine.api import users import accesslog class MainHandler(webapp.RequestHandler): def listPopDesti(self, count): # format self.response.headers['Content-Type'] = 'text/html' self.response.out.write( \ ''' GAppProxy 热门站点统计 ''' % count) ds = accesslog.listPopDesti(count) for d in ds: self.response.out.write( \ ''' ''' % (str(d[0]), d[1])) self.response.out.write( \ '''
GAppProxy 热门站点统计(TOP %d)
站点访问量
%s%d
''') def listFreqFro(self, count): # format self.response.headers['Content-Type'] = 'text/html' self.response.out.write( \ ''' GAppProxy 用户使用统计 ''' % count) ds = accesslog.listFreqFro(count) for d in ds: self.response.out.write( \ ''' ''' % (str(d[0]), d[1])) self.response.out.write( \ '''
GAppProxy 用户使用统计(TOP %d)
用户 IP访问量
%s%d
''') def get(self): user = users.get_current_user() obj = self.request.get('obj') cmd = self.request.get('cmd') # check if user: if user.email() == 'ldmiao@gmail.com': # OK, dispatch if obj.lower() == 'accesslog': # for AccessLog if cmd.lower() == 'clear': # clear log accesslog.clearAll() self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Clear OK!') elif cmd.lower() == 'list_pop_desti': # list the most popular destinations self.listPopDesti(50) elif cmd.lower() == 'list_freq_fro': # list the most frequent user self.listFreqFro(50) else: self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Wrong cmd!') else: self.listPopDesti(200) #self.response.headers['Content-Type'] = 'text/plain' #self.response.out.write('Wrong obj!') else: # FAILED, send response self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Forbidden!') else: # 'clear accesslog' is an exception, for batch operation if obj.lower() == 'accesslog' and cmd.lower() == 'clear': # need magic number magic = self.request.get('magic') if False: #if magic == '': # clear log accesslog.clearAll() self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Clear OK!') else: self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Forbidden!') else: self.redirect(users.create_login_url(self.request.uri)) def main(): application = webapp.WSGIApplication([('/admin.py', MainHandler)], debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main()