# Copyright 2009 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import logging import datetime import os from django.utils import simplejson from google.appengine.ext import webapp from google.appengine.ext.webapp import template from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext import db import settings import quartermile import models class WebHandler(webapp.RequestHandler): def render(self, template, data): self.response.headers.add_header('Content-Type', 'text/html;charset=UTF-8') path = os.path.join(os.path.dirname(__file__), "templates", "pages", template) self.response.out.write(webapp.template.render(path, data)) class AdminHandler(WebHandler): def get_user(self): user_id = self.request.get('user_id', '12345') container_id = self.request.get('container_id', 'testapi') return quartermile.user_get_or_insert(container_id, user_id) def post(self): action = self.request.get('action', 'none') user = self.get_user() message = 'Invalid operation' try: if action == 'delete_activity': activity_ids = map(int, self.request.get_all('activity_id')) activities = quartermile.model_get_by_id(models.Activity, activity_ids) quartermile.admin_delete_activities(activities) message = 'Deleted activities %s' % activity_ids elif action == 'create_activity': activity_text = self.request.get('activity_text', '') activity = quartermile.activity_create(user, activity_text) message = 'Created activity #%s' % activity.activity_id elif action == 'create_team': team_name = self.request.get('team_name', '') team = quartermile.team_create(user, team_name) message = 'Created team #%s' % team.team_id elif action == 'join_team': team_id = int(self.request.get('team_id', None)) team = quartermile.model_get_by_id(models.Team, team_id) if len(team) == 1: quartermile.team_join(user, team[0]) message = 'Joined team #%s' % team[0].team_id else: message = 'Invalid team specified' except quartermile.ConstraintError, ex: message = ex.message self.get(message) def get(self, message=''): user = self.get_user() data = { 'message' : message, 'user' : user, 'teams' : quartermile.admin_get_all_teams(), 'activities' : quartermile.admin_get_all_activities(user.account), } self.render('admin.html', data) if __name__ == '__main__' and settings.DEBUG == True: handlers = [ ('.*', AdminHandler), ] run_wsgi_app(webapp.WSGIApplication(handlers, debug=settings.DEBUG))