Esempio n. 1
0
    def split_ecriture(self):
        """ Update an ecriture"""

        data = {}
        data['compte_id'] = self.options.compte
        data['id'] = self.options.id
        if not self.options.ec:
            response = RequestServer.get_method("ecriture",
                                                ecriture=data['id'],
                                                compte=data['compte_id'],
                                                filter={"sort": "ecriture_categorie",}
                                               )
            if response.status_code == 404:
                raise Exception("Impossible de trouver ecriture categorie")
            tmp_response = response.json()
            data['ecriture_categorie_id'] = tmp_response[0]["ecriture_categorie_id"]
        else:    
            data['ecriture_categorie_id'] = self.options.ec
        if not self.options.montant:
            raise Exception("Montant obligatoire")
        if not re.match(r"^\d+([\.,]\d{1,2})?$", self.options.montant):
            raise Exception("Erreur dans le montant")
        data['montant'] = self.options.montant
        if not self.options.categorie:
            raise Exception("Categorie obligatoire")
        if not re.match(r"^\d{1,2}$",self.options.categorie):
            raise Exception("Erreur de categorie")
        data['categorie'] = self.options.categorie

        print data
        response = RequestServer.put_method("split", dumps(data))
        print response
Esempio n. 2
0
 def get(self):
     """ get data by rest method and print it"""
     self.rqst = RequestServer.get_method(self.rest_method,
                                          self.filter,
                                          self.sort,
                                          self.attribut
                                         )
Esempio n. 3
0
    def list_ecriture(self):
        """ List ecriture """

        id = None
        compte = None
        filter = {}
        if self.options.id:
            id = self.options.id
        if self.options.compte:
            compte = self.options.compte
        if self.options.filter:
            filter["filter"] = self.options.filter
        if self.options.valid:
            filter["valide"] = "yes"
        if self.options.unvalid:
            filter["valide"] = "no"
        if self.options.sort:
            filter["sort"] = self.options.sort
        #rqst = RequestServer('localhost', '8080')
        #print rqst.get('ecriture')
        if not filter:
            filter = None
        response = RequestServer.get_method("ecriture",
                                            ecriture=id,
                                            compte=compte,
                                            filter=filter,
                                           )
        if response.status_code == 404:
            return 1
        tmp_response = response.json()
        if isinstance(tmp_response, list):
            for response in tmp_response:
                print "%s - %40s | %8s | %2s | %15s / (%s/%s)" % (response["date"],
                                                   response["nom"],
                                                   response["montant"],
                                                   response["type"],
                                                   response["categorie"],
                                                   response["id"],
                                                   response["ecriture_categorie_id"],
                                                  )
        elif isinstance(tmp_response, dict):
            for k, v in tmp_response.iteritems():
                print "%s -> %s" % (k, v)
        else:
            print tmp_response
Esempio n. 4
0
    def prompt_categorie(self):
        """ print prompt to choose your categorie """
        if self.cache_categorie is None:
            self.cache_categorie = RequestServer.get_method("categorie",
                                                            {'odata': 'count lt 20'},
                                                            ['nom',],
                                                            []
                                                           )

        for i in range(1, 10):
            tmp_str = ""
            for categorie in self.cache_categorie[(i-1)*5:i*5]:
                tmp_str += "%2d - %10s | " % (categorie['id'], categorie['nom'].strip()[0:10])
            print tmp_str[:-3]
        while True:
            data = unicode(raw_input("categorie (defaut: 5): "))
            if data == "":
                self.attribut['categorie_id'] = 5
                break
            if re.match(r"^\d{1,2}$", data):
                self.attribut['categorie_id'] = data
                break
Esempio n. 5
0
    def insert_ecriture(self):
        """ Insert an ecriture """

        data = {}
        data['compte_id'] = self.options.compte
        if not self.options.type in ["Pr", "Vr", "Prs"]:
            data['nom'] = raw_input("nom :")

        data['montant'] = unicode(raw_input("montant :"))
        if not re.match(r"^\d+([\.,]\d{1,2})?$", data['montant']):
            raise Exception("Erreur dans le montant")

        if not self.options.dc:
            data['dc'] = unicode(raw_input("Débit d / Crédit c : "))
            if data['dc'] == "d":
                data['dc'] = -1
            elif data['dc'] == "c":
                data['dc'] = 1
            else:
                raise Exception("Erreur dans Débit/Crédit")
        else:
            if self.options.dc == "d":
                data['dc'] = -1
            elif self.options.dc == "c":
                data['dc'] = 1

        if not self.options.type:
            data['type'] = unicode(raw_input("Typr [Pr, Vr, Cb, Re, Ch, Li]: "))
            if not re.match(r"^(Pr|Vr|Cb|Re|Ch|Li)$", data['type']):
                raise Exception("Erreur dans le type")
        elif self.options.type in ["Pr", "Vr", "Prs"]:
            if self.options.type == 'Pr':
                data['type'] = 'Pr'
                return_compte = RequestServer.get_method("compte",
                                                         filter="prv"
                                                        )
            elif self.options.type == 'Vr':
                data['type'] = 'Vr'
                return_compte = RequestServer.get_method("compte",
                                                         filter="vir"
                                                        )
            elif self.options.type == 'Prs':
                return_compte = RequestServer.get_method("compte",
                                                         filter="prs"
                                                        )
                data['type'] = 'Vr'
            i = 1
            list_compte = []
            for prs in return_compte.json():
                print "%d - %s" % (i, prs['nom'])
                list_compte.append((prs['nom'], prs['id']))
                i += 1
            input_type = raw_input("Virement :")
            data['nom'] = list_compte[int(input_type)-1][0]
            data['nom_id'] = list_compte[int(input_type)-1][1]
        else:
            data['type'] = self.options.type
        if self.options.description:
            data['description'] = self.options.description
        data['date'] = unicode(raw_input("date [YYYY/]DD/MM] :"))
        if not re.match(r"^(201[0-9]\/)?\d{1,2}\/\d{1,2}$", data['date']):
            raise Exception("Erreur dans la date")
        if re.match(r"^\d{2}\/\d{2}$", data['date']):
            data['date'] = str(date.today().year) + "/" + data['date']
        date_now = datetime.now()
        if date_now < datetime.strptime(data["date"], "%Y/%m/%d"):
            raise Exception("La date ne doit pas être dans le future")
        response = RequestServer.get_method("categorie", filter={"sort": "count"})
        list_categorie = response.json()
        for i in range(1, 5):
            tmp_str = ""
            for categorie in list_categorie[(i-1)*5:i*5]:
                tmp_str += "%2d - %16s | " % (categorie['id'], categorie['nom'].strip())
            print tmp_str[:-3]

        data['categorie'] = unicode(raw_input("categorie [consommation : 5] :"))
        if not data['categorie'].isnumeric():
            raise Exception("Erreur de categorie")

        print data
        response = RequestServer.post_method("ecriture", dumps(data))
        print response
Esempio n. 6
0
    def check_args(self, prompt=False):
        """ Method to check wich argument is mandatory

            Do an introspection in database
            Check for witch argument give by parse_arg if arugment is missing

            If prompt is True, display en prompt to give argument, test it and save it
            return True if OK
            else False
        """
        #attrs = dict((key,value) for key,value in self.attribut.items())
        attrs = copy.deepcopy(self.attribut)
        for database in self.database:
            mapper = inspect(database)
            for column in mapper.attrs:
                if isinstance(column, ColumnProperty) and \
                   not column.columns[0].primary_key:
                    if not attrs.has_key(column.key) and \
                       not column.columns[0].foreign_keys and \
                       not column.columns[0].nullable:
                        if prompt:
                            # Particularity of type (Pr,Vr) and Name : must return compte_id
                            if mapper.tables[0].name == 'ecriture' and \
                               self.attribut.has_key('type') and \
                               self.attribut['type'] in ('Pr', 'Vr') and \
                               column.key == 'nom':
                                if self.attribut['type'] == 'Pr':
                                    compte_type = {'type': 'prv',
                                                   'archive': 'false'
                                                  }
                                else:
                                    compte_type = {'type': 'prs/vir',
                                                   'archive': 'false'
                                                  }
                                list_compte = RequestServer.get_method("compte",
                                                                       compte_type,
                                                                       ['nom',],
                                                                       []
                                                                      )
                                dict_compte = {}
                                for compte in list_compte:
                                    print "%d - %s" % (compte['id'], compte['nom'])
                                    dict_compte[compte['id']] = compte['nom']
                                while True:
                                    data = unicode(raw_input("compte : "))
                                    if re.match(r"^\d{1,2}$", data):
                                        self.attribut['nom_id'] = data
                                        self.attribut['nom'] = dict_compte[int(data)]
                                        break
                            else:
                                while True:
                                    data = unicode(raw_input("%s [%s]: " % (column.key,
                                                                            column.columns[0].key
                                                                           )
                                                            )
                                                  )
                                    data = self.check_type_data(column.columns[0], data)
                                    if data is None:
                                        print "Type non reconnue pour %s (%s)" % (column.key, column.columns[0].type)
                                    else:
                                        self.attribut[column.key] = data
                                        break
                        else:
                            return False
                    elif attrs.has_key(column.key):
                        del(attrs[column.key])
        if attrs:
            print "champs non reconnu %s" % attrs
            return False
        return True