Пример #1
0
    def test_basic(self):
        """
        python -m unittest tests.models.heure.TestHeure.test_basic

        """
        # Add Fournisseur
        test_heure = Heure(self.d_index, self.d_data, path=self.path)
        test_heure.add()

        # Assert new Fournisseur is in the database
        df = test_heure.load_db(test_heure.path)

        for k, v in {'heure_id': self.heure_id + 1}.items():
            df = df.loc[df[k] == v]
        self.assertTrue(not df.empty)

        # Make sure same Fournisseur can't added twice
        try:
            test_heure = Heure({'heure_id': self.heure_id + 1},
                               self.d_data,
                               path=self.path)
            test_heure.add()
            self.assertTrue(False)
        except ValueError:
            self.assertTrue(True)

        # test alter Fournisseur
        test_heure.nb_heure_ch = 30
        test_heure.alter()

        # Assert record has bee changed in the database
        df = test_heure.load_db(test_heure.path)

        for k, v in {'heure_id': self.heure_id + 1}.items():
            df = df.loc[df[k] == v]
        self.assertEqual(df.iloc[0]['nb_heure_ch'], 30)

        # Assert deletion works
        test_heure.delete()
        df = test_heure.load_db(test_heure.path)
        for k, v in {'heure_id': self.heure_id + 1}.items():
            df = df.loc[df[k] == v]

        self.assertTrue(df.empty)
Пример #2
0
def process_form(table_key, d_data, action):

    # Code to download document at the completion of some form (Devis, Affaire, ...)
    script = u'$.ajax({method: "POST", url: "/url_success_form", data: {"table_key": %s, "index": %s}})' \
             u'.done(function (response, status, request) {alert(%s %s);});'

    if table_key in ['affaire', 'devis', 'facture', 'commande'
                     ] and 'Suprimer' not in action:
        script = u'$.ajax({method: "POST", url: "/url_success_form", data: {"table_key": %s, "index": %s}})' \
                 u'.done(function (response, status, request) { alert(%s %s);' \
                 u'var url = response["url"].concat(response["data"]);' \
                 u'window.location = url});'

    if table_key == 'employe':
        l_index, l_fields = Employe.l_index, Employe.l_fields()
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Employe,
                                      action,
                                      d_data=d_data,
                                      table_key=table_key)

    elif table_key == 'fournisseur':
        l_index, l_fields = Fournisseur.l_index, Fournisseur.l_fields()
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Fournisseur,
                                      action,
                                      d_data=d_data,
                                      table_key=table_key)

    elif table_key == 'client':
        l_index, l_fields = Client.l_index, Client.l_fields()
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Client,
                                      action,
                                      d_data=d_data,
                                      table_key=table_key)

    elif table_key == 'contact':
        l_index, l_fields = Contact.l_index, Contact.l_fields()
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Contact,
                                      action,
                                      d_data=d_data,
                                      table_key=table_key)

    elif table_key == 'chantier':
        l_index, l_fields = Chantier.l_index, Chantier.l_fields()
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Chantier,
                                      action,
                                      d_data=d_data,
                                      table_key=table_key)

    elif table_key == 'affaire':
        l_index, l_fields = Affaire.l_index, Affaire.l_fields()
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Affaire,
                                      action,
                                      d_data=d_data,
                                      table_key=table_key)

    elif table_key == 'devis':
        l_index, l_fields = Devis.l_index, [f for f in Devis.l_fields()]
        d_data['price'] = 0
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Devis,
                                      action,
                                      d_data,
                                      table_key=table_key)

    elif table_key == 'commande':

        # Create tmp dir
        driver = FileDriver('tmp_upload', '')
        tmpdir = driver.TempDir(create=True, prefix='tmp_upload_')

        # Create s3 ressources
        ressource = boto3.resource(
            's3',
            aws_access_key_id=d_sconfig['aws_access_key_id'],
            aws_secret_access_key=d_sconfig['aws_secret_access_key'])

        filename = ''.join(
            ['{}',
             mimetypes.guess_extension(d_data['details']['mimetype'])])
        d_files = {
            filename: {
                'f': d_data['details']['fp'],
                'bucket':
                ressource.Bucket(d_sconfig.get('bucket_name', 'lstcmd')),
                'tmpfile': FileDriver('', '').join(tmpdir.path, filename)
            }
        }

        l_index, l_fields = Commande.l_index, [f for f in Commande.l_fields()]
        d_data['montant_ttc'], d_data['montant_tva'] = 0, 0
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Commande,
                                      action,
                                      d_data,
                                      table_key=table_key,
                                      d_files=d_files)

    elif table_key == 'facture':
        l_index, l_fields = Facture.l_index, [f for f in Facture.l_fields()]
        d_data['montant_ttc'], d_data['montant_tva'] = 0, 0
        statue = generic_process_form(l_index,
                                      l_fields,
                                      Facture,
                                      action,
                                      d_data,
                                      table_key=table_key)

    elif table_key == 'heure':
        l_index, l_fields, statue = Heure.l_index, Heure.l_fields(), {
            'success': True
        }

        # Load current database of hours
        df, l_hid = Heure.load_db(), []
        df = df.loc[df['semaine'] == d_data['index']]

        # Alter or add hours that have been sent
        for d_heure in d_data['heure']:
            d_data_ = {
                k: v
                for k, v in d_heure.items() +
                [('semaine', d_data['index']), ('index', d_data['index'])]
            }

            if d_heure['heure_id'] == l_index[0].sn.missing:
                statue = generic_process_form(l_index,
                                              l_fields,
                                              Heure,
                                              'Ajouter',
                                              d_data_,
                                              table_key=table_key)
            else:
                l_hid += [d_data_[l_index[0].name]]
                statue = generic_process_form(l_index,
                                              l_fields,
                                              Heure,
                                              'Modifier',
                                              d_data_,
                                              table_key=table_key)

        # Remove hours that have been deleted if any
        for hid in set(df.heure_id.values).difference(l_hid):
            d_data_ = {'index': d_data['index'], l_index[0].name: hid}
            statue = generic_process_form(l_index,
                                          l_fields,
                                          Heure,
                                          'Suprimer',
                                          d_data_,
                                          table_key=table_key)

        if 'success' in statue:
            statue.update({
                'data':
                (u'"{}"'.format(table_key), u'"{}"'.format(d_data['index']),
                 u'"{}: {}'.format(table_key, d_data['index']),
                 u' {} avec succes"'.format("edite"))
            })
    else:
        statue = {'error': u'Unknown table'}

    if 'success' not in statue:
        if statue['error'] == 'index':
            script = \
                u'alert("Erreur: l\'element {} existe dans la base. Modifier l\'element existant ou changer la valeur' \
                u' du champ {}");'.format(statue['index'], statue['index_name'])

        else:
            raise ValueError('{}'.format(statue))

    else:
        script = script % statue['data']
    return script
Пример #3
0
    def load_view(return_cols=True):

        # Load affaire db
        df = Affaire.load_db()
        l_models_cols = []

        # Join devis information
        df_devis = Devis.load_db()
        df_devis = df_devis[[
            c for c in df_devis.columns
            if c not in ['creation_date', 'maj_date']
        ]]
        df_devis = df_devis.rename(columns={
            c: '{}_devis'.format(c)
            for c in df_devis.columns if c != 'devis_id'
        })
        df = df.merge(df_devis, on='devis_id', how='left')

        l_models_cols += [c for c in df_devis.columns if c != 'devis_id']

        # Load billing and Build affaire index
        df_facture = Facture.load_db()
        df_facture = FeuilleTravaux.split_main_indices(df_facture)

        # For each situation join amount factured
        for i in range(1, 13):
            df_facture_ = df_facture.loc[df_facture['situation'] == i]\
                .rename(columns={'montant_ht': 'montant_situation_{}'.format(i)})
            df = FeuilleTravaux.merge_to_main(
                df, df_facture_, ['montant_situation_{}'.format(i)])
            l_models_cols += ['montant_situation_{}'.format(i)]

        # Join command information
        df_commande = Commande.load_db()
        df_commande = FeuilleTravaux.split_main_indices(df_commande)

        df = FeuilleTravaux\
            .merge_to_main(
                df, df_commande.rename(columns={'montant_ht': 'montant_total_commande'}), ['montant_total_commande']
            )

        l_models_cols += ['montant_total_commande']

        # Load hours information
        df_heure = Heure.load_db()
        df_heure = FeuilleTravaux.split_main_indices(df_heure)

        # Heures non interimaires
        df_heure_ = df_heure.loc[df_heure['name'] != 'interim']\
            .rename(columns={'heure_prod': 'heure_prod_saisie', 'heure_autre': 'heure_autre_saisie'})
        df = FeuilleTravaux.merge_to_main(
            df, df_heure_, ['heure_prod_saisie', 'heure_autre_saisie'])

        # Heures interimaires
        df_heure_ = df_heure.loc[df_heure['name'] == 'interim']\
            .rename(columns={'heure_prod': 'heure_prod_saisie_interim', 'heure_autre': 'heure_autre_saisie_interim'})
        df = FeuilleTravaux.merge_to_main(
            df, df_heure_,
            ['heure_prod_saisie_interim', 'heure_autre_saisie_interim'])

        l_models_cols += [
            'heure_prod_saisie', 'heure_autre_saisie',
            'heure_prod_saisie_interim', 'heure_autre_saisie_interim'
        ]

        if return_cols:
            return df, l_models_cols

        return df