corretor.py 2.24 KB
import pbclient
import os
import pyutil

class Corretor:
    
    def __init__(self, configuration, template_env):
       self.config = configuration
       self.env = template_env
       pbclient.set('endpoint', self.config['PYBOSSA_ENDPOINT'])
       pbclient.set('api_key', self.config['PYBOSSA_API_KEY'])
                  
    def __find_project(self, app_short_name):
        projects = pbclient.find_project(short_name=app_short_name)
        return projects[0] if len(projects) > 0 else None
    
    def __setup_project(self, project):
        self.__create_tasks(project)
        self.__update_project_info(project)
    
    def __create_tasks(self, project):
        test_signs = ["ENSINADO", "ENTANTO", "ENTENDIDO"]
        for sign in test_signs:
            task = dict(sign_name=sign)
            pbclient.create_task(project.id, task)
    
    def __update_project_info(self, project):    
        template = self.env.get_template('template.html')
        project.info['task_presenter'] = template.render(server=self.config['HOST_ENDPOINT'])
        project.info['thumbnail'] = self.config['HOST_ENDPOINT'] + "/img/thumbnail.png"
        pbclient.update_project(project)
        
    def create_project(self):
        app_short_name = self.config['PYBOSSA_APP_SHORT_NAME']
        project = self.__find_project(app_short_name)
        result_msg = ""
        if (project):
            result_msg = "The project " + app_short_name + " was already created."
        else:
            project = pbclient.create_project(self.config['PYBOSSA_APP_NAME'], app_short_name, self.config['PYBOSSA_APP_DESCRIPTION'])
            if (project):
                self.__setup_project(project)
                result_msg = "The project " + app_short_name + " was created."
            else: 
                result_msg = "The project " + app_short_name + " couldn't be created. Check the server log for details."
        pyutil.log(result_msg)
        return result_msg   

    def update_project(self): 
        app_short_name = self.config['PYBOSSA_APP_SHORT_NAME']
        project = self.__find_project(app_short_name)
        self.__update_project_info(project)
        result_msg = "The project " + app_short_name + " was updated."
        pyutil.log(result_msg)
        return result_msg