def create(self): """ create data by rest method """ if self.options is None: pass elif self.check_args(self.options.prompt): pass else: print "Erreur de saisie pour l'ajout" sys.exit(1) self.rqst = RequestServer.post_method(self.rest_method, self.attribut, )
def set_tag(self, ecriture_id, create=False): """ Insert a tag if attribute exist and tag name exists """ if self.tag: if not self.tag_id: tags = Tag() tags.attribut = {} tags.filter = {'nom': self.tag} tags.get() if tags.rqst != False: self.tag_id = tags.rqst[0]['id'] elif create: tags.attribut['nom'] = self.tag tags.attribut['valeur'] = "" tags.create() self.tag_id = re.match(r"^\/tag\/(?P<tag_id>.*)\/$", tags.rqst.headers['Location'] ).group('tag_id') self.rqst = RequestServer.post_method("tag_ecriture", {'ecriture_id': ecriture_id, 'tag_id': self.tag_id }, )
def create(self): """ Redefine create to add an ecriture """ if not self.check_args(self.options.prompt): print "Erreur de saisie pour l'ajout, champ manquant ou utiliser prompt option" sys.exit(1) #Add information for table montant if self.options.prompt: self.prompt_categorie() while True: data = unicode(raw_input("Montant : ")) if re.match(r"^[-+]?\d+[,\.]?\d*$", data): self.attribut['montant'] = data break self.rqst = RequestServer.post_method('ecriture', self.attribut, ) ecriture_id = re.match(r"^\/ecriture\/(?P<ecriture_id>.*)\/$", self.rqst.headers['Location'] ).group('ecriture_id') self.set_tag(ecriture_id, False)
def import_data(self, extension='csv'): """Import ofx file to database, by account Import data from a file For csv file, there must hae a header like it : date;nom;montant In attribute, we muste define these parameter : compte_id By default, all import has default categorie (5) A Tag is created with default date for the import """ if not self.options.importfile: sys.exit(1) if extension == 'ofx': ofx_file = open(self.options.importfile) ofx_buf = ofx_file.read() parser = LinkParser() parser.feed(ofx_buf) parser.close() ofx_file.close() elif extension == 'csv': with open(self.options.importfile, 'rb') as csvfile: csvreader = csv.DictReader(csvfile, delimiter=';', quoting=csv.QUOTE_NONE) for row in csvreader: print "import %s - %s - %s" % (row['date'], row['montant'], row['nom']) self.attribut['date'] = datetime.strptime(row['date'], "%d/%m/%y").strftime("%Y/%m/%d") self.attribut['nom'] = row['nom'] self.attribut['montant'] = str(row['montant']) if self.options.prompt: self.prompt_categorie() else: self.attribut['categorie_id'] = '5' self.rqst = RequestServer.post_method(self.rest_method, self.attribut, ) ecriture_id = re.match(r"^\/ecriture\/(?P<ecriture_id>.*)\/$", self.rqst.headers['Location'] ).group('ecriture_id') self.set_tag(ecriture_id, True)
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