def add_resource(): user_id = session["email"] json_data = request.get_json() new_caption = json_data["new_caption"] new_url = json_data["new_url"] exercise_id = json_data["exercise_id"] try: model.add_resource(new_caption, new_url, user_id, exercise_id) return "FINISHED" except Exception as e: abort(400)
def POST(self): if not check_priv_lvl(2): return web.notfound("Insufficient privileges") form = self.form() i = web.input(userfile={}) web.debug(i['userfile'].filename) web.debug(i['userfile']) #define user resource directory user_resource_dir = 'static/resources/' + str(session.cin) + '/' #check if user added a file if 'userfile' in i: #Ensure any windows separators are replaced to *nix style separators filepath = i.userfile.filename.replace('\\','/') #Get file name from path filename = filepath.split('/')[-1] logger.info('Open file to write') try: #Construct a path for the file to be saved in if not ospath.exists(user_resource_dir): os.mkdir(user_resource_dir) fout = open(user_resource_dir + filename,'wb') logger.info('Write upoloaded file to local file') logger.debug('Filename: %s', filename) #Write a copy of the submitted file to the server fout.write(i.userfile.file.read()) fout.close() logger.info('Get filetype') dotindex = filename.find('.') if dotindex < 0: ftype = 'doc' else: ftype = filename[dotindex:] #Add resource path to database logger.info('Add file to database') logger.debug('Cin: %d, Ftype: %s, Category: %s, \ filename: %s, user privilege: %d', session.cin, ftype, \ i.category, filename, session.priv) model.add_resource(session.cin, ftype, i.category, filename, 0) logger.info('Upload successful!') raise web.seeother('/upload/1') except IOError: logger.info('Upload Failed!') #Just pass and reload page with "Upload Failed" pass d_list = (list(set([i.resourcecat for i in model.get_resource_categories() if i.resourcecat!='Archive']))) return render.upload(form, d_list, "Upload failed!")