|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import webapp2 |
| 16 | +from webapp2_extras import jinja2 |
| 17 | +from google.appengine.api import app_identity |
| 18 | +from google.appengine.ext import blobstore, ndb |
| 19 | +from google.appengine.ext.webapp import blobstore_handlers |
| 20 | + |
| 21 | +BUCKET = app_identity.get_default_gcs_bucket_name() |
| 22 | + |
| 23 | + |
| 24 | +class BaseHandler(webapp2.RequestHandler): |
| 25 | + 'Derived request handler mixing-in Jinja2 support' |
| 26 | + @webapp2.cached_property |
| 27 | + def jinja2(self): |
| 28 | + return jinja2.get_jinja2(app=self.app) |
| 29 | + |
| 30 | + def render_response(self, _template, **context): |
| 31 | + self.response.write(self.jinja2.render_template(_template, **context)) |
| 32 | + |
| 33 | + |
| 34 | +class Visit(ndb.Model): |
| 35 | + 'Visit entity registers visitor IP address & timestamp' |
| 36 | + visitor = ndb.StringProperty() |
| 37 | + timestamp = ndb.DateTimeProperty(auto_now_add=True) |
| 38 | + file_blob = ndb.BlobKeyProperty() |
| 39 | + |
| 40 | + |
| 41 | +def store_visit(remote_addr, user_agent, upload_key): |
| 42 | + 'create new Visit entity in Datastore' |
| 43 | + Visit(visitor='{}: {}'.format(remote_addr, user_agent), |
| 44 | + file_blob=upload_key).put() |
| 45 | + |
| 46 | + |
| 47 | +def fetch_visits(limit): |
| 48 | + 'get most recent visits' |
| 49 | + return Visit.query().order(-Visit.timestamp).fetch(limit) |
| 50 | + |
| 51 | + |
| 52 | +class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): |
| 53 | + 'Upload blob (POST) handler' |
| 54 | + def post(self): |
| 55 | + uploads = self.get_uploads() |
| 56 | + blob_id = uploads[0].key() if uploads else None |
| 57 | + store_visit(self.request.remote_addr, self.request.user_agent, blob_id) |
| 58 | + self.redirect('/', code=307) |
| 59 | + |
| 60 | + |
| 61 | +class ViewBlobHandler(blobstore_handlers.BlobstoreDownloadHandler): |
| 62 | + 'view uploaded blob (GET) handler' |
| 63 | + def get(self, blob_key): |
| 64 | + self.send_blob(blob_key) if blobstore.get(blob_key) else self.error(404) |
| 65 | + |
| 66 | + |
| 67 | +class MainHandler(BaseHandler): |
| 68 | + 'main application (GET/POST) handler' |
| 69 | + def get(self): |
| 70 | + self.render_response('index.html', |
| 71 | + upload_url=blobstore.create_upload_url('/upload', |
| 72 | + gs_bucket_name=BUCKET)) |
| 73 | + |
| 74 | + def post(self): |
| 75 | + visits = fetch_visits(10) |
| 76 | + self.render_response('index.html', visits=visits) |
| 77 | + |
| 78 | + |
| 79 | +app = webapp2.WSGIApplication([ |
| 80 | + ('/', MainHandler), |
| 81 | + ('/upload', UploadHandler), |
| 82 | + ('/view/([^/]+)?', ViewBlobHandler), |
| 83 | +], debug=True) |
0 commit comments