Organizing the project views

52 बार देखा गया
नहीं पढ़े गए पहले मैसेज पर जाएं

Pablo Gonçalves

नहीं पढ़ी गई,
8 जून 2019, 1:42:43 pm8/6/19
ईमेल पाने वाला bottlepy
Hello guys, I'm developing my project using bottle.py almost a year ago and I felt it was very difficult to organize the project views. In my project struture I using in the root path python  files as Controllers, one file database.py for the Models and in the views directory I put all the views files, being that I use three view files for each method of the controllers, one for the content and the anothers two for script and style.

Today I have more of seventy views files, in this way it is increasingly difficult to work with the project due to the organization, I seach in the Google and in the foruns solutions for this same problem and I found many ways for solve it, but no standardized solution.

I see a lot of people speaking in your tutorials that Bottle it's for small projects, but the development process is so natural and easy that I can not see the Bottle only for small projects.

I ended up solving my problem by creating a function to include the subdirectories in the views directory of automatically, being that as a rule no file can have the same name, even if it is not in the same directory.

import bottle
from os import listdir
from os.path import dirname, realpath, join, isdir

def config_views_directory(all_subdirs=False):
    """ configura o local das views """
    abs_app_dir_path = dirname(realpath(__file__))
    abs_views_path = join(abs_app_dir_path, 'views')
    index = 0

    # insere o caminho das views
    bottle.TEMPLATE_PATH.insert(index, abs_views_path)

    # procedimento para verificar o diretório
    def process_directory(index, path):
        """ processa o diretório """
        for arquivo in listdir(path):
            next_path = join(path, arquivo)

            # verifica se é um diretório
            if isdir(next_path):
                index += 1

                # insere o caminho das views
                bottle.TEMPLATE_PATH.insert(index, next_path)

                # verifica se é para processar todos os subdiretórios
                if all_subdirs:
                    index = process_directory(index, next_path)
        
        return index

    process_directory(index, abs_views_path)




सभी प्रषकों को उत्तर दें
लेखक को उत्तर दें
आगे भेजें
0 नया मैसेज