Esempio n. 1
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]
    setup_logging(config_uri)
    env = bootstrap('%s#dashboard'% config_uri)
    settings = env.get('registry').settings
    engine = engine_from_config(settings, 'sa.dashboard.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine

    google_opts = argv[2:]
    try:
        google_user = google_opts.pop(0)
    except IndexError:
        google_user = raw_input('Google Spreadsheet User: '******'Google Spreadsheet ID: ')
    google_password = getpass.getpass("Google Spreadsheet Password: ")

    gc = gspread.login(google_user, google_password)
    sht = gc.open_by_key(spreadsheet_key)
    worksheet = sht.get_worksheet(0)
    crs = worksheet.get_all_records()

    contracts = {}
    for row in crs:
        if not row['titolocommessa']:
            continue
        contract_uid = '%s_%s_%s' % (row['project_name'],
                                     row['titolocommessa'],
                                     row['customer_id'])
        contracts.setdefault(contract_uid, {'crs': []})
        contracts[contract_uid]['titolocommessa'] = row['titolocommessa']
        contracts[contract_uid]['nrcontratto'] = row['nrcontratto']
        contracts[contract_uid]['gg'] = row['gg'] or 0
        contracts[contract_uid]['amount'] = row['amount'] or 0
        contracts[contract_uid]['crs'].append(row['cr_id'])
        contracts[contract_uid]['stato'] = map_state(row['stato'])

    # now we have a structure:
    # contracts['ContractUID'] = {'crs': ['customer_request_id_1',
    #                                     'customer_request_id_2'],
    #                             'gg': '12'}

    with transaction.manager:
        session = DBSession()
        for contract_uid, opts in contracts.items():
            crs = [session.query(CustomerRequest).get(a) for a in opts['crs']]
            crs = [a for a in crs if a]
            if not crs:
                continue
            contract = crs[0].contract
            if not contract:
                contract = Contract(name=opts['titolocommessa'])
            contract.days = opts['gg']
            contract.ammount = opts['amount']
            contract.contract_number = opts['nrcontratto']
            contract.workflow_state = opts['stato']
            for cr in crs:
                if not cr:
                    continue
                cr.contract = contract
                contract.project_id = cr.project_id

        update_time_entries()
Esempio n. 2
0
    def handle_save(self, form, appstruct):
        """ The main handle method for the wizard. """
        customer = self.context.get_instance()

        # create new users
        recipients = []
        groups = {}
        mailer = get_mailer(self.request)
        for newuser in appstruct['new_users']:
            user = User(fullname=newuser['fullname'], email=newuser['email'])
            if not newuser['role'] in groups:
                groups[newuser['role']] = []
            groups[newuser['role']].append(user)
            if newuser['send_email_howto']:
                recipients.append(newuser['email'])
        for recipient in recipients:
            headers = {"header": u'Welcome to Penelope',
            "message": ('You were enabled as a user of Penelope, '
                        'our online projects and trouble ticket '
                        'management platform. Your username is <strong>%s</strong>. '
                        'With penelope you will be '
                        'able to open new tickets and follow the evolution '
                        'of the issues you opened. We recommend to double '
                        'check that the tickets you open have the '
                        '"Ticked opened by customer" field set at "SI" (Yes).' % recipient),
            "link": '%s/password_reset_form' % (self.request.application_url),
            "action": 'Activate your account NOW!'}
            message = Message(subject=WELCOME_SUBJECT,
                                recipients=[recipient],
                                body='Welcome to Penelope',
                                extra_headers={'X-MC-Template': 'general',
                                               'X-MC-MergeVars': json.dumps(headers)})
            mailer.send(message)

        #create project and set manager
        manager = self.request.authenticated_user
        project = Project(name=appstruct['project']['project_name'],
                          manager=manager)

        #set groups
        for g in appstruct['users']:
            if not g['role'] in groups:
                groups[g['role']] = []
            for u in g['usernames']:
                user = DBSession.query(User).get(u)
                groups[g['role']].append(user)

        for rolename, users in groups.items():
            role = DBSession.query(Role).filter(Role.name == rolename).one()
            group = Group(roles=[role, ], users=users)
            project.add_group(group)

        #create contract with cr
        crs = appstruct['contracts']['customer_requests']
        co =  appstruct['contracts']['contract']
        contract = Contract(**co)
        contract.project_id = project.id

        #create CR
        tickets = []
        for cr in crs:
            customer_request = CustomerRequest(name=cr['title'])
            person_types = {
                'junior': 'Junior',
                'senior': 'Senior',
                'graphic': 'Graphic',
                'pm': 'Project manager',
                'architect': 'Architect',
                'tester': 'Tester'
            }
            for key, value in person_types.items():
                if cr[key]:
                    Estimation(person_type=value,
                            days=cr[key],
                            customer_request=customer_request)
            project.add_customer_request(customer_request)
            customer_request.contract = contract
            if not cr['ticket']:
                continue
            tickets += [{'summary': cr['title'],
                        'customerrequest': customer_request,
                        'reporter': manager.email,
                        'type': 'task',
                        'priority': 'major',
                        'milestone': 'Backlog',
                        'owner': manager.email}]

        #create project management CR and tickets
        project_management_cr = CustomerRequest(name="Project management")
        project_management_cr.contract = contract
        project.add_customer_request(project_management_cr)
        project_management_tickets = PM_TICKETS
        for summary, description in project_management_tickets.items():
          tickets += [{ 'summary': summary,
                        'description': description,
                        'customerrequest': project_management_cr,
                        'reporter': manager.email,
                        'type': 'task',
                        'priority': 'major',
                        'sensitive': '1',
                        'milestone': 'Backlog',
                        'owner': manager.email}]

        #create google docs/folders
        for app_definition in appstruct['google_docs']:
            app = GoogleDoc(name=app_definition['name'],
                            api_uri=app_definition['uri'])
            if app_definition['share_with_customer']:
                acl = ApplicationACL(role_id='customer',
                                     permission_name='view')
                acl.project = app
            project.add_application(app)

        #create trac
        trac = Trac(name="Trac for %s" % appstruct['project']['project_name'])
        trac.settings = self.request.registry.settings # an ugly patch to pass 
                                                       # the missing settings 
        trac.milestones = appstruct['milestones']
        trac.tickets = tickets
        if appstruct['project']['trac_name']:
            trac.project_name = appstruct['project']['trac_name']
        else:
            trac.project_name = appstruct['project']['project_name']
        project.add_application(trac)

        customer.add_project(project)
        raise exc.HTTPFound(location=self.request.fa_url('Customer',
                                                         customer.id))