import datetime import logging import webapp2 from google.appengine.api import mail from google.appengine.api import users from google.appengine.ext import ndb ############################################################################### html_header = ''' Request a Prank

Request a Prank

Are you planning a prank on a teammate? You can use this site to request a prank consultation.

''' html_footer = '
' mappings = list() ############################################################################### def clean(value): return value.replace('"', '"').replace('<', '<').replace('\n', '
') ############################################################################### class PrankRequest(ndb.Model): requestor = ndb.StringProperty() time_requested = ndb.DateTimeProperty() description = ndb.TextProperty() timeline = ndb.StringProperty() hidden = ndb.BooleanProperty(default=False) ############################################################################### class MainPageHandler(webapp2.RequestHandler): def get(self): user = users.get_current_user() self.response.out.write(html_header) if user: if users.is_current_user_admin(): self.response.out.write('') else: self.response.out.write('') else: url = users.create_login_url('/') login_msg = 'Click here to login with Google and get started.' % url self.response.out.write(login_msg) self.response.out.write(html_footer) mappings.append(('/', MainPageHandler)) ############################################################################### class SaveRequestHandler(webapp2.RequestHandler): def post(self): user = users.get_current_user() if user: request_detail = clean(self.request.get('description')) timeline = clean(self.request.get('timeline')) if len(timeline) > 500: self.response.out.write('The timeline contains too many characters.') elif len(user.email) > 500: self.response.out.write('Your email address contains too many characters.') else: try: prank_request = PrankRequest() prank_request.description = request_detail prank_request.requestor = user.email() prank_request.timeline = timeline prank_request.time_requested = datetime.datetime.now() prank_request.put() self.response.out.write('OK') except ndb.Error as e: self.response.out.write('There was an issue saving the data.') logging.error('Caught exception: %s' % e.strerror) else: self.response.out.write('Please login.') mappings.append(('/save', SaveRequestHandler)) ############################################################################### class SendMessageHandler(webapp2.RequestHandler): def post(self): user = users.get_current_user() if user and users.is_current_user_admin(): key = self.request.get('id') message = self.request.get('message') key = ndb.Key(urlsafe=key) prank_request = key.get() mailto = prank_request.requestor mailfrom = user.email() mailmessage = message + '\n\n' + prank_request.description mailmessage = mailmessage.replace('
', '\n') mail.send_mail(mailfrom, mailto, 'Prank Consultation', mailmessage) self.response.out.write('OK') mappings.append(('/send', SendMessageHandler)) ############################################################################### class HideRequestHandler(webapp2.RequestHandler): def post(self): user = users.get_current_user() if user and users.is_current_user_admin(): request_key = self.request.get('id') try: key = ndb.Key(urlsafe=request_key) prank_request = key.get() prank_request.hidden = True prank_request.put() self.response.out.write('OK') except ndb.Error as e: self.response.out.write('There was an issue updating the request.') logging.error('Caught exception: %s' % e.strerror) else: response.out.write('Please login as an admin.') mappings.append(('/hide', HideRequestHandler)) ############################################################################### class ListRequestHandler(webapp2.RequestHandler): def post(self): user = users.get_current_user() if user and users.is_current_user_admin(): self.response.out.write('[') query = PrankRequest.query().filter(PrankRequest.hidden == False) first = True for prank_request in query.fetch(): if first: first = False else: self.response.out.write(',') self.response.out.write('{') self.response.out.write('"id":"%s",' % prank_request.key.urlsafe()) self.response.out.write('"requestor":"%s",' % prank_request.requestor) self.response.out.write('"time_requested":"%s",' % str(prank_request.time_requested)) self.response.out.write('"description":"%s",' % prank_request.description) self.response.out.write('"timeline":"%s"' % prank_request.timeline) self.response.out.write('}') self.response.out.write(']') else: response.out.write('Please login as an admin.') mappings.append(('/list', ListRequestHandler)) ############################################################################### app = webapp2.WSGIApplication(mappings, debug=True)