def post(self): user = users.get_current_user() file = self.request.POST['file'] # no need to retrieve the Account entity, just link it by userId which is the key entity = DatastoreFile(data=file.value, mimetype=file.type, userId=user.user_id()) entity.put() file_url = "http://%s/file/download/%d" % (self.request.host, entity.key.id()) # or you can link this entity to other entity (e.g. link to an album) self.response.out.write("Your uploaded file is now available at <a href='%s'>%s</a>" % (file_url,file_url))
def post(self, slug): project = Project.all().filter('slug =', slug).fetch(1)[0] try: file = self.request.POST['file'] f = DatastoreFile(data=file.value, mimetype=file.type, project=project, name=file.filename) f.put() url = "http://%s/file/%s/%d/%s" % (self.request.host, slug, f.key().id(), f.name) except Exception, e: logging.error("error uploading file: %s" % e)
def get(self, slug): # we want canonocal urls so redirect to add a trailing slash if needed if self.request.path[-1] != "/": self.redirect("%s/" % self.request.path, True) return user = users.get_current_user() output = None # if not logged in then use a cached version if not user: output = get_cache("project_%s" % slug) # if we don't have a cached version or are logged in if output is None: try: project = Project.all().filter('slug =', slug).fetch(1)[0] issues = Issue.all().filter('project =', project) files = DatastoreFile.all().filter('project =', project) except IndexError: self.render_404() return logging.info("Files in this project: %d" % files.count()) # check to see if we have admin rights over this project if project.user == user or users.is_current_user_admin(): owner = True else: owner = False context = { 'project': project, 'issues': issues, 'owner': owner, 'files': files, } output = self.render("project.html", context) if not user: # only save a cached version if we're not logged in # so as to avoid revelaving user details memcache.add("project_%s" % slug, output, 3600) self.response.out.write(output)
def get(self, id): # can use get_by_id if you know the number id of the entity entity = DatastoreFile.get_by_id(int(id)) self.response.headers['Content-Type'] = str(entity.mimetype) self.response.out.write(entity.data)
def get(self, slug, id, filename): entity = DatastoreFile.get_by_id(int(id)) self.response.headers['Content-Type'] = entity.mimetype self.response.out.write(entity.data)