def createFolder(): # Chargement de la BD db = InviteUtils.loadJson() # Authentification userId = getUserId(db, request) if not userId: return Error("Utilisateur n'est pas connecté") # Obtention des infos folders = db["folder"] body = request.json folderName = body["name"] if not folderName: return Error("Le nom du dossier est requis") # Creation du dossier folderId = uuid.uuid4().hex newfolder = { "id": folderId, "name": folderName, "files": [], "invitedClients": [], "clients": [userId], "administrator": userId } folders.append(newfolder) # Sauvegarde de la BD InviteUtils.unloadJson(db) # Reponse a l'utilisateur return json.dumps(newfolder, indent=4)
def sendInvite(folderId): # 1 - OUVRIR LA BASE DE DONNEE jsondata = InviteUtils.loadJson() clientId = request.json["clientId"] folderId = folderId clientId = clientId if jsondata == None: return Error("Erreur lors de l'ouverture de la BD.") folders = jsondata["folder"] # 2 - VERIFIER SI LE DOSSIER EXISTE found, findex = InviteUtils.isExists(jsondata, folderId, "FOLDER") if not found: return Error("Erreur lors de la recherche du repertoire.") # 3 - VERIFIER SI LE CLIENT EXISTE found, cindex = InviteUtils.isExists(jsondata, clientId, "USER") if not found: return Error("Erreur lors de la recherche du client.") if not request.headers["Authorization"] in folders[findex]["administrator"]: return Error("Erreur le client n'a pas les droits.") #4 - ECRIRE DANS "INVITEDCLIENTS" SI PAS DEJA PRESENT if not clientId in folders[findex]["invitedClients"]: folders[findex]["invitedClients"].append(clientId) InviteUtils.unloadJson(jsondata) return Success("Invitation envoyée")
def changeAdmin(folderId): # Chargement de la BD db = InviteUtils.loadJson() # Authentification userId = getUserId(db, request) if not userId: return Error("Utilisateur n'est pas connecté") # Obtention des infos folders = db["folder"] folder = next(filter(lambda f: f["id"] == folderId, folders), None) body = request.json newAdminUserId = body["userId"] if folder["administrator"] is not userId: return Error("Seul l'administrateur peut modifier l'administrateur") if not newAdminUserId: return Error("L'id du nouvel admin est requis") users = db["user"] user = next(filter(lambda u: u["id"] == newAdminUserId, users), None) if not user: return Error("L'id du nouvel admin n'existe pas") # Remplacement de l'admin folder["administrator"] = newAdminUserId # Sauvegarde de la BD InviteUtils.unloadJson(db) # Reponse a l'utilisateur return Success("L'administrateur a été modifié")
def replaceFile(folderId, fileId): # Validations if 'file' not in request.files: return Error("Aucun fichier sélectionné") file = request.files['file'] if file.filename == '': return Error("Aucun fichier sélectionné") # Chargement de la BD db = InviteUtils.loadJson() # Authentification userId = getUserId(db, request) if not userId: return Error("Utilisateur n'est pas connecté") # Obtention des infos folders = db['folder'] folder = next(filter(lambda f: f["id"] == folderId, folders), None) fileEntry = next(filter(lambda f: f["id"] == fileId, folder["files"]), None) if not fileEntry: return Error("Aucun fichier n'a cet id") filename = secure_filename(file.filename) # Sauvegarde du fichier sur le disque file.save(os.path.join(app.config['UPLOAD_FOLDER'], fileId)) # Modification de l'entree du fichier fileEntry["name"] = filename # Sauvegarde de la BD InviteUtils.unloadJson(db) # Reponse a l'utilisateur return Success("Le fichier a été téléversé")
def replyInvite(userId, folderId): # 1 - OUVRIR LA BASE DE DONNEE jsondata = InviteUtils.loadJson() answer = int(flask.request.json["answer"]) # 0 : FALSE # TRUE clientId = int(userId) folderId = int(folderId) # 2 - VERIFIER SI LE CLIENT EXISTE found, cindex = InviteUtils.isExists(jsondata, clientId, "USER") if not found: return Error("Erreur lors de la recherche du client.") # 3 - VERIFIER SI LE DOSSIER EXISTE found, findex = InviteUtils.isExists(jsondata, folderId, "FOLDER") if not found: return Error("Erreur lors de la recherche du repertoire.") folder = jsondata["folder"][findex] isclient = clientId in folder["clients"] isinvite = clientId in folder["invitedClients"] # 4 - VERIFIER LES DEMANDES DACCESS if not isinvite and not isclient: return Error("Le client n'a pas eu l'access au dossier.") # 5 - AJOUTER L'ACCES needwrite = False if isinvite: folder["invitedClients"].remove(clientId) needwrite = True if answer != 0 and not isclient: folder["clients"].append(clientId) needwrite = True if needwrite: InviteUtils.unloadJson(jsondata) if answer == 0: return Success("Acces enleve.") return Success("Acces ajoute.")
def getFile(folderId, fileId): # Chargement de la BD db = InviteUtils.loadJson() # Authentification userId = getUserId(db, request) if not userId: return Error("Utilisateur n'est pas connecté") # Obtention des infos folders = db['folder'] folder = next(filter(lambda f: f["id"] == folderId, folders), None) fileEntry = next(filter(lambda f: f["id"] == fileId, folder["files"]), None) if not fileEntry: return Error("Aucun fichier n'a cet id") file = open(os.path.join(app.config['UPLOAD_FOLDER'], fileId), "rb") # Reponse a l'utilisateur return file.read()
def parse_args(self, args=None, values=None, verbose=True): """ This method overrides the base class parse_args by performing an initial iteration through the input arguments (defaults to sys.argv[1:] in the same fashion as in the parent class), and storing them in an ordered list. This gets returned the the caller as the third element of a tuple, the other two being the usual opts and args returned by the parent class parse_args() method. This method also "pre-scans" the options list for a "--ini" flag, and parses the '*.ini' file for a set of command-line arguments to insert into sys.argv. This allows command-line options to be stored in a .ini file. Assuming a vaid section is located in a *.ini file, the extra arguments which will be prepended to the existing list of command line args, is also returned, as the fourth and final item in the returned tuple. """ ordered_args = [] sectionargs = [] sections = [] if args is None: args = sys.argv[1:] # Pre-scan argument list for an --ini argument for arg_as_input in args: if arg_as_input.startswith('--ini'): i_opt, i_arg = BasicOptionParser.parse_args( self, [ arg_as_input, ], values) sections.extend(i_opt.ini) if len(sections) > 0: sectionargs = self.__parse_ini_files__(sections) # Now re-scan arguments, including any additional arguments which # were inserted from a valid section in the .ini file args += sectionargs for arg_as_input in args: if not arg_as_input.startswith('--'): continue arg_as_input = arg_as_input.replace('--', '') ordered_args.append(re.sub('=.*$', '', arg_as_input)) p_opts, p_args = BasicOptionParser.parse_args(self, args, values) # Verify a stream was defined if not hasattr(p_opts, 'orderedStreams'): Error('You must define at least 1 filter stream (e.g. --st1=...)') # Display summary output if verbose: print print "Requested and implied arguments:" for arg in sys.argv[1:] + sectionargs: print ' %s' % arg print return p_opts, p_args, ordered_args, sectionargs, self.iniFiles
def getFolder(folderId): # Chargement de la BD db = InviteUtils.loadJson() # Authentification userId = getUserId(db, request) if not userId: return Error("Utilisateur n'est pas connecté") # Obtention des infos folders = db["folder"] folder = next(filter(lambda f: f["id"] == folderId, folders), None) return json.dumps(folder, indent=4)
def getInvites(userId): # 1 - OUVRIR LA BASE DE DONNEE jsondata = InviteUtils.loadJson() clientId = int(userId) if jsondata == None: return Error("Erreur lors de l'ouverture de la BD.") folders = jsondata["folder"] # 2 - VERIFIER SI LE CLIENT EXISTE found, cindex = InviteUtils.isExists(jsondata, clientId, "USER") if not found: return Error("Erreur lors de la recherche du client.") invitedFolders = [] for folder in folders: if clientId in folder["invitedClients"]: invitedFolders.append(folder) return Success(json.dumps(invitedFolders, indent = 4))
def getFolders(): # Chargement de la BD db = InviteUtils.loadJson() # Authentification userId = getUserId(db, request) if not userId: return Error("Utilisateur n'est pas connecté") # Obtention des infos folders = db["folder"] userFolders = list(filter(lambda f: userId in f["clients"], folders)) for folder in userFolders: folder.pop("files", None) return json.dumps(userFolders, indent=4)
def deleteFile(folderId, fileId): # Chargement de la BD db = InviteUtils.loadJson() # Authentification userId = getUserId(db, request) if not userId: return Error("Utilisateur n'est pas connecté") # Obtention des infos folders = db['folder'] folder = next(filter(lambda f: f["id"] == folderId, folders), None) fileEntry = next(filter(lambda f: f["id"] == fileId, folder["files"]), None) if not fileEntry: return Error("Aucun fichier n'a cet id") # Suppression de l'entree du fichier dans la BD newFilesArray = list(filter(lambda f: f["id"] != fileId, folder["files"])) folder["files"] = newFilesArray # Suppression du fichier sur le disque os.remove(os.path.join(app.config['UPLOAD_FOLDER'], fileId)) # Sauvegarde de la BD InviteUtils.unloadJson(db) # Reponse a l'utilisateur return Success("Le fichier a été supprimé")
def __parse_ini_files__(self, sections): new_args = [] defaultSection = 'section01' for item in sections: itemelements = item.split(',') if len(itemelements) == 1: filename, section = itemelements[0], defaultSection elif len(itemelements) == 2: filename, section = itemelements[0], itemelements[1].lower() else: Error("--ini=%s is an invalid option! %s" % item) try: config = INIParser(filename) config.readfp() self.iniFiles.append(filename) except IOError: Error("--ini=%s requested but no %s file found" % (item, filename)) if section not in config.sections(): Error("No such section '%s' found in %s" % (section, filename)) for option in config.items_as_arguments(section): new_args.append(option) return new_args