def getDocURL(self): """@return: URL to reach this lead base directory""" (clientDir, leadDir, businessDir, inputDir, deliveryDir) = getLeadDirs(self) url = pydici.settings.DOCUMENT_PROJECT_URL_DIR + leadDir[ len(pydici.settings.DOCUMENT_PROJECT_PATH):] return url
def lead_documents(request, lead_id): """Gather documents relative to this lead as a fragment page for an ajax call""" lead = Lead.objects.get(id=lead_id) documents = [] # List of name/url docs grouped by type clientDir, leadDir, businessDir, inputDir, deliveryDir = getLeadDirs(lead) lead_url_dir = settings.DOCUMENT_PROJECT_URL_DIR + leadDir[len(settings.DOCUMENT_PROJECT_PATH):] lead_url_file = settings.DOCUMENT_PROJECT_URL_FILE + leadDir[len(settings.DOCUMENT_PROJECT_PATH):] for directory in (businessDir, inputDir, deliveryDir): # Create project tree if at least one directory is missing if not os.path.exists(directory): createProjectTree(lead) break for directory in (businessDir, inputDir, deliveryDir): directoryName = directory.split("/")[-1] dirs = [] files = [] for fileName in os.listdir(directory): filePath = os.path.join(directory, fileName) fileName = fileName.encode('utf-8', 'surrogateescape').decode('utf-8', 'replace') # fs encoding mixup if os.path.isdir(filePath): dirs.append((fileName + "/", lead_url_dir + "/" + directoryName + "/" + fileName + "/")) else: files.append((fileName, lead_url_file + "/" + directoryName + "/" + fileName)) dirs.sort(key=lambda x: x[0]) files.sort(key=lambda x: x[0]) documents.append([directoryName, dirs + files]) return render(request, "leads/lead_documents.html", {"documents": documents, "lead_doc_url": lead_url_dir, "user": request.user})
def lead_documents(request, lead_id): """Gather documents relative to this lead as a fragment page for an ajax call""" lead = Lead.objects.get(id=lead_id) documents = [] # List of name/url docs grouped by type clientDir, leadDir, businessDir, inputDir, deliveryDir = getLeadDirs(lead) lead_url_dir = pydici.settings.DOCUMENT_PROJECT_URL_DIR + leadDir[len(pydici.settings.DOCUMENT_PROJECT_PATH):] lead_url_file = pydici.settings.DOCUMENT_PROJECT_URL_FILE + leadDir[len(pydici.settings.DOCUMENT_PROJECT_PATH):] for directory in (businessDir, inputDir, deliveryDir): # Create project tree if at least one directory is missing if not os.path.exists(directory): createProjectTree(lead) break for directory in (businessDir, inputDir, deliveryDir): directoryName = directory.split(u"/")[-1] dirs = [] files = [] for fileName in os.listdir(directory): filePath = os.path.join(directory.encode(sys.getfilesystemencoding()), fileName) if isinstance(fileName, str): # Corner case, files are not encoded with filesystem encoding but another... fileName = fileName.decode("utf8", "ignore") if os.path.isdir(filePath): dirs.append((fileName + u"/", lead_url_dir + u"/" + directoryName + u"/" + fileName + u"/")) else: files.append((fileName, lead_url_file + u"/" + directoryName + u"/" + fileName)) dirs.sort(key=lambda x: x[0]) files.sort(key=lambda x: x[0]) documents.append([directoryName, dirs + files]) return render(request, "leads/lead_documents.html", {"documents": documents, "lead_doc_url": lead_url_dir, "user": request.user})
def lead_documents(request, lead_id): """Gather documents relative to this lead as a fragment page for an ajax call""" lead = Lead.objects.get(id=lead_id) documents = [] # List of name/url docs grouped by type clientDir, leadDir, businessDir, inputDir, deliveryDir = getLeadDirs(lead) leadDocURL = lead.getDocURL() for directory in (businessDir, inputDir, deliveryDir): # Create project tree if at least one directory is missing if not os.path.exists(directory): createProjectTree(lead) break for directory in (businessDir, inputDir, deliveryDir): directoryName = directory.split(u"/")[-1] dirs = [] files = [] for fileName in os.listdir(directory): filePath = os.path.join(directory.encode(sys.getfilesystemencoding()), fileName) if isinstance(fileName, str): # Corner case, files are not encoded with filesystem encoding but another... fileName = fileName.decode("utf8", "ignore") if os.path.isdir(filePath): dirs.append((fileName + u"/", leadDocURL + directoryName + u"/" + fileName + u"/")) else: files.append((fileName, leadDocURL + directoryName + u"/" + fileName)) dirs.sort(key=lambda x: x[0]) files.sort(key=lambda x: x[0]) documents.append([directoryName, dirs + files]) return render(request, "leads/lead_documents.html", {"documents": documents, "lead_doc_url": leadDocURL, "user": request.user})
def checkBusinessDoc(self): """Ensure business doc are put on file server if business propoal has been sent @return: True is doc is ok, else False""" if self.state in ("WON", "OFFER_SENT", "NEGOTIATION"): clientDir, leadDir, businessDir, inputDir, deliveryDir = getLeadDirs(self) try: if len(os.listdir(businessDir)) == 0: return False except OSError: # Document directory may not exist or may not be accessible return False return True
def checkDeliveryDoc(self): """Ensure delivery doc are put on file server if lead is won and archived @return: True is doc is ok, else False""" if self.state == "WON" and self.mission_set.filter(active=True).count() == 0: clientDir, leadDir, businessDir, inputDir, deliveryDir = getLeadDirs(self) try: if len(os.listdir(deliveryDir)) == 0: return False except OSError: # Document directory may not exist or may not be accessible return False return True
def remove_lead_tag(lead_id, tag_id): """ Remove tag on given lead""" connection = None try: lead = Lead.objects.get(id=lead_id) tag_name = Tag.objects.get(id=tag_id).name connection = connect_to_nextcloud_db() cursor = connection.cursor() cursor.execute(GET_TAG_ID, (tag_name, )) rows = cursor.fetchall() if len(rows) == 0: # Tag doesn't exist, hence we don't do anything return else: # Tag exists, fetch the first result nextcloud_tag_id = rows[0][0] # Get document directories (client_dir, lead_dir, business_dir, input_dir, delivery_dir) = getLeadDirs(lead, with_prefix=False) # Find all files of the lead, except input cursor.execute( GET_FILES_ID_BY_DIR, (business_dir + '%', ",".join(settings.NEXTCLOUD_DB_EXCLUDE_TYPES), settings.NEXTCLOUD_DB_FILE_STORAGE)) lead_files = cursor.fetchall() cursor.execute( GET_FILES_ID_BY_DIR, (delivery_dir + '%', ",".join(settings.NEXTCLOUD_DB_EXCLUDE_TYPES), settings.NEXTCLOUD_DB_FILE_STORAGE)) lead_files.extend(cursor.fetchall()) data_file_mapping = [] for lead_file in lead_files: data_file_mapping.append({ 'file_id': lead_file[0], 'object_type': 'files', 'tag_id': nextcloud_tag_id }) cursor.executemany(UNTAG_FILE, data_file_mapping) # Commit the changes to the database connection.commit() except Exception as e: raise e finally: if connection: connection.close()
def remove_lead_tag(lead_id, tag_id): """ Remove tag on given lead""" connection = None try: lead = Lead.objects.get(id=lead_id) tag_name = Tag.objects.get(id=tag_id).name connection = connect_to_nextcloud_db() cursor = connection.cursor() cursor.execute(GET_TAG_ID, (tag_name, )) rows = cursor.fetchall() if len(rows) == 0: # Tag doesn't exist, hence we don't do anything return else: # Tag exists, fetch the first result nextcloud_tag_id = rows[0][0] # Get document directories (client_dir, lead_dir, business_dir, input_dir, delivery_dir) = getLeadDirs(lead, with_prefix=False) # Find all files of the lead, except input cursor.execute(GET_FILES_ID_BY_DIR, (business_dir+'%', ",".join(settings.NEXTCLOUD_DB_EXCLUDE_TYPES), settings.NEXTCLOUD_DB_FILE_STORAGE)) lead_files = cursor.fetchall() cursor.execute(GET_FILES_ID_BY_DIR, (delivery_dir+'%', ",".join(settings.NEXTCLOUD_DB_EXCLUDE_TYPES), settings.NEXTCLOUD_DB_FILE_STORAGE)) lead_files.extend(cursor.fetchall()) data_file_mapping = [] for lead_file in lead_files: data_file_mapping.append({ 'file_id': lead_file[0], 'object_type': 'files', 'tag_id': nextcloud_tag_id }) cursor.executemany(UNTAG_FILE, data_file_mapping) # Commit the changes to the database connection.commit() except Exception as e: raise e finally: if connection: connection.close()
def tag_leads_files(leads_id): """Tag all files of given leads. Can be called from tag views (when adding tags) or tag batch (for new files or initial sync)""" connection = None try: connection = connect_to_nextcloud_db() cursor = connection.cursor() for lead_id in leads_id: lead = Lead.objects.get(id=lead_id) # Get all the lead tags tags = lead.tags.all().values_list('name', flat=True) # Get document directories (client_dir, lead_dir, business_dir, input_dir, delivery_dir) = getLeadDirs(lead, with_prefix=False) tag_id_list = [] for tag in tags: # Get the tag id in nextcloud database cursor.execute(GET_TAG_ID, (tag, )) rows = cursor.fetchall() if len(rows) == 0: # Tag doesn't exist, we create it cursor.execute(CREATE_TAG, (tag, "1", "1")) tag_id = cursor.lastrowid else: # Tag exists, fetch the first result tag_id = rows[0][0] tag_id_list.append(tag_id) data_file_mapping = [] for (directory_tag_name, directory) in ((settings.DOCUMENT_PROJECT_BUSINESS_DIR, business_dir), (settings.DOCUMENT_PROJECT_DELIVERY_DIR, delivery_dir)): cursor.execute(GET_FILES_ID_BY_DIR, (directory+'%', ",".join(settings.NEXTCLOUD_DB_EXCLUDE_TYPES), settings.NEXTCLOUD_DB_FILE_STORAGE)) files = cursor.fetchall() cursor.execute(GET_TAG_ID, (directory_tag_name, )) rows = cursor.fetchall() if len(rows) == 0: # Tag doesn't exist, we create it cursor.execute(CREATE_TAG, (directory_tag_name, "1", "1")) directory_tag_id = cursor.lastrowid else: # Tag exists, fetch the first result directory_tag_id = rows[0][0] for file_id in files: data_file_mapping.append({ 'file_id': file_id[0], 'object_type': 'files', 'tag_id': directory_tag_id }) for tag_id in tag_id_list: data_file_mapping.append({ 'file_id': file_id[0], 'object_type': 'files', 'tag_id': tag_id }) cursor.executemany(TAG_FILE, data_file_mapping) # Commit the changes to the database for each lead connection.commit() except Exception as e: raise e finally: if connection: connection.close()
def getDocURL(self): """@return: URL to reach this lead base directory""" (clientDir, leadDir, businessDir, inputDir, deliveryDir) = getLeadDirs(self) url = settings.DOCUMENT_PROJECT_URL_DIR + leadDir[len(settings.DOCUMENT_PROJECT_PATH):] return url
def tag_leads_files(leads_id): """Tag all files of given leads. Can be called from tag views (when adding tags) or tag batch (for new files or initial sync)""" connection = None try: connection = connect_to_nextcloud_db() cursor = connection.cursor() for lead_id in leads_id: lead = Lead.objects.get(id=lead_id) # Get all the lead tags tags = lead.tags.all().values_list('name', flat=True) # Get document directories (client_dir, lead_dir, business_dir, input_dir, delivery_dir) = getLeadDirs(lead, with_prefix=False) tag_id_list = [] for tag in tags: # Get the tag id in nextcloud database cursor.execute(GET_TAG_ID, (tag, )) rows = cursor.fetchall() if len(rows) == 0: # Tag doesn't exist, we create it cursor.execute(CREATE_TAG, (tag, "1", "1")) tag_id = cursor.lastrowid else: # Tag exists, fetch the first result tag_id = rows[0][0] tag_id_list.append(tag_id) data_file_mapping = [] for (directory_tag_name, directory) in ((settings.DOCUMENT_PROJECT_BUSINESS_DIR, business_dir), (settings.DOCUMENT_PROJECT_DELIVERY_DIR, delivery_dir)): cursor.execute(GET_FILES_ID_BY_DIR, (directory + '%', ",".join( settings.NEXTCLOUD_DB_EXCLUDE_TYPES), settings.NEXTCLOUD_DB_FILE_STORAGE)) files = cursor.fetchall() cursor.execute(GET_TAG_ID, (directory_tag_name, )) rows = cursor.fetchall() if len(rows) == 0: # Tag doesn't exist, we create it cursor.execute(CREATE_TAG, (directory_tag_name, "1", "1")) directory_tag_id = cursor.lastrowid else: # Tag exists, fetch the first result directory_tag_id = rows[0][0] for file_id in files: data_file_mapping.append({ 'file_id': file_id[0], 'object_type': 'files', 'tag_id': directory_tag_id }) for tag_id in tag_id_list: data_file_mapping.append({ 'file_id': file_id[0], 'object_type': 'files', 'tag_id': tag_id }) cursor.executemany(TAG_FILE, data_file_mapping) # Commit the changes to the database for each lead connection.commit() except Exception as e: raise e finally: if connection: connection.close()