def create(self): """ Create method Create a new resource using posted data """ # create form instance from the ResourceModel resource_model_form = model_form(ResourceModel) form = resource_model_form(self.request.POST) context = { 'action': 'New', 'form': form, 'submit_routename': 'resource.create' } # since this method is only called from a post, # we do not need to check for request.method == "POST" # if self.form.validate() returns true, then save # the data if form.validate(): logging.debug('Form Validated!') entity = ResourceModel() # push form values into model form.populate_obj(entity) # save to data store key = entity.put() # redirect to index and/or edit form with new id logging.debug('key={0}'.format(key)) # redirect to the edit page for the created id return webapp2.redirect_to('resource.edit', id=key.id()) # the form did not validate, redisplay with errors return self.render_response('resource.html', context)
def edit(self, id): """ Edit method Edit a specific resource by id """ entity_id = int(id) resource_model_form = model_form(ResourceModel) entity = ResourceModel.get_by_id(entity_id) form = resource_model_form(self.request.POST, obj=entity) context = { 'action': 'Edit', 'id': id, 'form': form, 'submit_routename': 'resource.update' } self.render_response('resource.html', context)
def post(self): data = Resources.parser.parse_args() try: s3 = boto3.resource( 's3', aws_access_key_id=current_app.config['ACCESS_KEY'], aws_secret_access_key=current_app.config['SECRET_ACCESS_KEY']) image = data['image'] program_id = data['program_id'] upload_file_bucket = "precisionteachingonline" resource = ResourceModel(None, image.filename, program_id) resource.save() upload_file_key = str(resource.id) + image.filename s3.Bucket(upload_file_bucket).put_object(Key=upload_file_key, Body=data['image']) return "OK" except Exception as err: print(str(err)) return str(err)