Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter D. Vargas <pynash@gmail.com>2011-05-24 08:14:23 (GMT)
committer Walter D. Vargas <pynash@gmail.com>2011-05-24 08:14:23 (GMT)
commitfc41ca13d0fa42e681d0063b235755c87cf4667c (patch)
treedbeb64a87c95891b27c73d17ae8712eb92f8400c
agregando archivos
-rw-r--r--Contactos.py474
-rwxr-xr-xsql_class.py146
2 files changed, 620 insertions, 0 deletions
diff --git a/Contactos.py b/Contactos.py
new file mode 100644
index 0000000..c8528fa
--- /dev/null
+++ b/Contactos.py
@@ -0,0 +1,474 @@
+#! /usr/bin/env python
+
+# Copyright (c) 2011, Walter Vargas <pynash@gmail.com>
+# Copyright (c) 2011, Joel Davila <joe74@fedoraproject.org>
+
+#This program is free software: you can redistribute it and/or modify
+#it under the terms of the GNU General Public License as published by
+#the Free Software Foundation, either version 3 of the License, or
+#(at your option) any later version.
+#
+#This program is distributed in the hope that it will be useful,
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+#GNU General Public License for more details.
+#
+#You should have received a copy of the GNU General Public License
+#along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from gettext import gettext as _
+import gtk
+import pygtk
+import os.path
+import sqlite3
+
+import sql_class as conn_db
+
+class Contactos:
+ '''Clase Principal'''
+
+ def __init__(self):
+ '''Ventana Principal'''
+
+ ################### Ventana e Interface ####################
+
+ self.window = gtk.Window()
+ #self.window.connect("delete-event", self._delete_event)
+ self.window.set_title(_('CONTACTOS'))
+ self.window.set_size_request(800, 600)
+ ################### Caja de Texto ####################
+
+ self.display_now = []
+ self.pulse = 0
+ self.fname = ""
+ self.lname = ""
+ self.id = ""
+ self.lista_name = []
+
+ ################## Ventana con scroll ##################
+
+ self.scrolled_window = gtk.ScrolledWindow()
+
+ self.scrolled_window.set_border_width(10)
+ self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
+
+ ################## Contenedor Principal ##################
+
+ self.container = gtk.HBox()
+
+ self.h2 = gtk.HBox()
+
+ ###### Vertical 1 ######
+
+ self.vb1 = gtk.VBox()
+
+ ###### Aca van la raiz y los datos formales.
+ ###### creamos el treestore con la columna tipo string como modelo
+
+ self.treestore = gtk.TreeStore(str)
+
+ db = conn_db.Conexion()
+
+ self.lista = []
+
+ self.categorias = ['familiares', 'amigos', 'companeros', 'otros']
+
+ for cat in self.categorias:
+ citer = self.treestore.append(None, ['%s' % cat])
+
+ lista = db._display_on_tree(cat)
+
+ for i in lista:
+ self.lista.append(i)
+ for i in range(len(lista)):
+ self.treestore.append(citer, ['%s' % lista[i][0] + " " + lista[i][1]])
+
+ #for i in lista:
+ # self.lista.append(i)
+
+ #for i in range(len(lista)):
+ # self.treestore.append(citer, ['%s' % lista[i]])
+
+ ###### Creamos el treeview, contenedor de treeviewcolumn y cellrender
+ ###### encargado de mostrar la vista del treestore
+
+ self.treeview = gtk.TreeView(self.treestore)
+
+ self.treeview.connect("row-activated", self._clicar)
+
+ ###### Creamos el treeviewcolumn que mostrara el dato
+
+ self.tv_column = gtk.TreeViewColumn('Categoria')
+
+ ###### Agregamos al treeview la columna
+
+ self.treeview.append_column(self.tv_column)
+
+ ###### Vamos a crear un objeto para renderizar los datos.
+
+ self.cell = gtk.CellRendererText()
+
+ ###### Add the cell al tv_column
+
+ self.tv_column.pack_start(self.cell, True)
+
+ self.tv_column.add_attribute(self.cell, 'text', 0)
+
+ self.treeview.set_search_column(0)
+
+ self.tv_column.set_sort_column_id(0)
+ self.treeview.set_reorderable(True)
+
+ self.scrolled_window.add_with_viewport(self.treeview)
+
+ self.vb1.pack_start(self.scrolled_window, True, True, 0)
+ self.h2.pack_start(self.vb1, True, True, 0)
+ self.scrolled_window.show()
+ self.treeview.show()
+
+ self.container.pack_start(self.h2, True, True, 0)
+
+ #--------------CENTER OPTIONS CONTAINER-------------#
+ self.opcionbox = gtk.VBox()
+
+ self.opcionlabel = gtk.Label(" Opciones ")
+
+ self.add = gtk.Button("Nuevo Contacto")
+ self.add.connect("clicked", self._add_user)
+
+ self.update = gtk.Button("Editar Contacto")
+ self.update.connect("clicked", self._update_user)
+
+ self.delete = gtk.Button("Eliminar Contacto")
+ self.delete.connect("clicked", self._del_user)
+
+ self.opcionbox.pack_start(self.opcionlabel, False, False, 20)
+ self.opcionbox.pack_start(self.add, False, False, 40)
+ self.opcionbox.pack_start(self.update, False, False, 40)
+ self.opcionbox.pack_start(self.delete, False, False, 40)
+
+ self.container.pack_start(self.opcionbox, False, False, 0)
+
+ #---------------RIGHT CONTAINER-------------#
+ self.c_right = gtk.VBox()
+
+
+ self.contacto = gtk.Label(" CONTACTO ")
+ self.c_right.pack_start(self.contacto, False, False, 20)
+
+
+ self.hr1 = gtk.HBox()
+
+ self.vr_2 = gtk.VBox()
+ self.containerl1 = gtk.HBox()
+ self.lf_name = gtk.Label("Nombres")
+ self.containerl1.pack_start(self.lf_name, False, False, 10)
+
+ self.ef_name = gtk.Entry()
+ self.ef_name.set_size_request(300, 30)
+ self.containerl1.pack_start(self.ef_name, False, False, 10)
+
+ self.vr_2.pack_start(self.containerl1, False, False, 30)
+
+ self.containerl2 = gtk.HBox()
+
+ self.ll_name = gtk.Label("Apellidos ")
+ self.containerl2.pack_start(self.ll_name, False, False, 10)
+
+ self.el_name = gtk.Entry()
+ self.el_name.set_size_request(300, 30)
+ self.containerl2.pack_start(self.el_name, False, False, 10)
+
+ self.vr_2.pack_start(self.containerl2, False, False, 30)
+
+ self.hr1.pack_start(self.vr_2, False, False, 20)
+
+ self.pic = gtk.Image()
+
+ #self.pic.set_from_file("./scrimmage1.jpg")
+
+ self.hr1.pack_start(self.pic, False, False, 0)
+
+ self.c_right.pack_start(self.hr1, False, False, 10)
+
+
+ self.hr2 = gtk.HBox()
+ self.l_address = gtk.Label(" Direccion")
+ self.e_address = gtk.Entry()
+ self.e_address.set_size_request(400, 30)
+ self.hr2.pack_start(self.l_address, False, False, 20)
+ self.hr2.pack_start(self.e_address, False, False, 30)
+ self.c_right.pack_start(self.hr2, False, False, 30)
+
+ self.hr3 = gtk.HBox()
+ self.l_phone = gtk.Label(" Telefono ")
+ self.e_phone = gtk.Entry()
+ self.e_phone.set_size_request(80, 30)
+ self.hr3.pack_start(self.l_phone, False, False, 20)
+ self.hr3.pack_start(self.e_phone, False, False, 20)
+ self.c_right.pack_start(self.hr3, False, False, 30)
+
+
+ self.hr2 = gtk.HBox()
+ self.l_ocupation = gtk.Label(" Ocupacion")
+ self.e_ocupation = gtk.Entry()
+ self.e_ocupation.set_size_request(200, 30)
+ self.hr2.pack_start(self.l_ocupation, False, False, 20)
+ self.hr2.pack_start(self.e_ocupation, False, False, 20)
+ self.c_right.pack_start(self.hr2, False, False, 30)
+
+ self.hr2 = gtk.HBox()
+ self.l_email = gtk.Label("Email ")
+ self.e_email = gtk.Entry()
+ self.e_email.set_size_request(200, 30)
+ self.hr2.pack_start(self.l_email, False, False, 25)
+ self.hr2.pack_start(self.e_email, False, False, 25)
+ self.l_email = gtk.Label("por Ej: midireccion@gmail.com")
+ self.hr2.pack_start(self.l_email, False, False, 20)
+
+
+ self.c_right.pack_start(self.hr2, False, False, 20)
+
+ self.hr2 = gtk.HBox()
+ self.l_category = gtk.Label("Categoria ")
+ self.e_category = gtk.Entry()
+ self.e_category.set_size_request(200, 30)
+ self.hr2.pack_start(self.l_category, False, False, 20)
+ self.hr2.pack_start(self.e_category, False, False, 20)
+ self.c_right.pack_start(self.hr2, False, False, 20)
+
+ ##-----------confirmation--------------------#
+
+ self.confirmation = gtk.HBox()
+
+ self.label_dialog = gtk.Label()
+ #self.label_dialog.set_text('HLA')
+
+ self.aceptar = gtk.Button("Guardar Cambios")
+ self.aceptar.connect("clicked", self._aceptar)
+ self.aceptar.set_sensitive(False)
+ self.aceptar.hide()
+
+ self.cancelar = gtk.Button("Cancelar")
+ self.cancelar.connect("clicked", self._cancelar)
+ self.cancelar.set_sensitive(False)
+ self.cancelar.hide()
+
+ self.confirmation.pack_start(self.label_dialog, False, False, 0)
+ self.confirmation.pack_start(self.aceptar, False, False, 0)
+ self.confirmation.pack_start(self.cancelar, False, False, 0)
+ self.c_right.pack_start(self.confirmation, False, False, 0)
+
+
+ self.container.pack_start(self.c_right, False, False, 0)
+
+ #-----cargar contacto default-----------#
+
+ self._load_contact(1)
+
+ #----------------------- displaying all--------------------------------#
+
+ self.window.add(self.container)
+ self.window.show_all()
+
+ #--------------finaliza __INIT__----------------------------------------#
+
+
+ #---- metodo que presentara el elemento clickeado en la lista de arbol ----#
+ def _clicar(self, tvcolumn, path, iter):
+ opcion = self.treestore[path][0]
+ if opcion in self.lista:
+ # No tendria porque ir self.lista_name = opcion.split(" ")
+ print opcion[0]
+ self._load_contact(metodo=0)
+ #pass
+
+
+ def _load_contact(self, metodo=1):
+ """Conect to sql bd file and return a lista"""
+ lista=[]
+ if metodo:
+ db = conn_db.Conexion(1)
+ lista = db._show_default(1)
+ else:
+ #pass
+ db = conn_db.Conexion(self.lista_name[0], self.lista_name[1])
+ lista = db._show_default(0)
+ self.display_now = lista
+ self._load_on_entry(self.display_now)
+
+ def _load_on_entry(self, lista):
+ """load on entry values to display"""
+ self._editable_entry(False)
+ self.ef_name.set_text(lista[0])
+ self.el_name.set_text(lista[1])
+ self.e_address.set_text(lista[2])
+ self.e_phone.set_text(lista[3])
+ self.e_ocupation.set_text(lista[4])
+ self.e_email.set_text(lista[5])
+ self.e_category.set_text(lista[6])
+ self.pic.set_from_file('./images/show.jpg')
+
+ def _sensitive_options(self, i):
+ self.add.set_sensitive(i)
+ self.update.set_sensitive(i)
+ self.delete.set_sensitive(i)
+ self.opcionlabel.set_sensitive(i)
+ self.scrolled_window.set_sensitive(i)
+
+ def _sensitive_confirmation(self, i):
+ self.aceptar.set_sensitive(i)
+ self.cancelar.set_sensitive(i)
+
+ def _read_datas(self):
+ f_name = self.ef_name.get_text().lower()
+ l_name = self.el_name.get_text().lower()
+ e_address = self.e_address.get_text().lower()
+ e_phone = self.e_phone.get_text().lower()
+ e_ocupation = self.e_ocupation.get_text().lower()
+ e_email = self.e_email.get_text().lower()
+ e_category = self.e_category.get_text().lower()
+ if self.pulse==1:
+ db = conn_db.Conexion(f_name=f_name, l_name=l_name, address=e_address, phone=e_phone, ocupation=e_ocupation, email=e_email, category=e_category)
+ db._add_user()
+ self._hiddeshow(0)
+ self._sensitive_options(1)
+ self._load_contact(1)
+ self._sensitive_confirmation(0)
+ self.label_dialog.set_text("register saved succefull")
+ self.pulse = 0
+ elif self.pulse == 2:
+ #pass
+ db = conn_db.Conexion(self.id, f_name, l_name, e_address, e_phone, e_ocupation, e_email, e_category)
+ db._update_user()
+ self._hiddeshow(0)
+ self._sensitive_options(1)
+ self._load_contact(1)
+ self._sensitive_confirmation(0)
+ self.label_dialog.set_text("register update succefull")
+ self.pulse = 0
+ else:
+ #pass
+ """aqui paso nombre y apellido para extraer el id"""
+ db = conn_db.Conexion(self.id)
+ db._del_user()
+ self._hiddeshow(0)
+ self._sensitive_options(1)
+ self._load_contact(1)
+ self._sensitive_confirmation(0)
+ self.label_dialog.set_text("delete succefull")
+ self.pulse = 0
+
+ def _aceptar(self, widget, data=None):
+ if self.ef_name.get_text()!= "" and self.el_name.get_text()!="" and self.e_category.get_text()!="":
+ f = self.ef_name.get_text().lower()
+ l = self.el_name.get_text().lower()
+
+ c = self.e_category.get_text().lower()
+ name = f + " " + l
+
+ db = conn_db.Conexion()
+ if self.pulse == 1:
+ if db._exists_user(name) or (c not in self.categorias):
+ self.label_dialog.set_text("dont save contact on self-name or distinct cat")
+ else:
+ self._read_datas()
+ elif self.pulse==2:
+ #pass
+ if (c not in self.categorias):
+ self.label_dialog.set_text("dont save contact on distinct category")
+ else:
+ self.id = db._return_id(self.fname, self.lname)
+ self._read_datas()
+ else:
+ #pass
+ self.id = db._return_id(self.fname, self.lname)
+ self._read_datas()
+
+ def _hiddeshow(self, i):
+ if i:
+ self.add.hide()
+ self.update.hide()
+ self.delete.hide()
+ self.aceptar.show()
+ self.cancelar.show()
+ else:
+ self.add.show()
+ self.update.show()
+ self.delete.show()
+ self.aceptar.hide()
+ self.cancelar.hide()
+
+ def _cancelar(self, widget, data=None):
+ self.pulse = 0
+ self._sensitive_options(1)
+ self._load_on_entry(self.display_now)
+ self._sensitive_confirmation(0)
+ self._hiddeshow(0)
+ self.label_dialog.set_text("")
+
+ def _add_user(self, widget, data=None):
+ self._clear_entry()
+ self.pic.set_from_file('./noavailable.png')
+ self.pulse = 1
+ self._sensitive_options(0)
+ self._sensitive_confirmation(1)
+ self._hiddeshow(1)
+ self._editable_entry(1)
+
+ def _update_user(self, widget, data=None):
+ self.pulse = 2
+ self.fname = self.ef_name.get_text().lower()
+ self.lname = self.el_name.get_text().lower()
+ self._editable_entry(True)
+ self._sensitive_options(0)
+ self._sensitive_confirmation(1)
+ self._hiddeshow(1)
+
+ def _del_user(self, widget, data=None):
+ #pass
+ self.pulse = 3
+ self.fname = self.ef_name.get_text().lower()
+ self.lname = self.el_name.get_text().lower()
+ self._sensitive_options(0)
+ self._sensitive_confirmation(1)
+ self._hiddeshow(1)
+
+ def _clear_entry(self):
+ self.ef_name.set_text("")
+ self.el_name.set_text("")
+ self.e_address.set_text("")
+ self.e_phone.set_text("")
+ self.e_ocupation.set_text("")
+ self.e_email.set_text("")
+ self.e_category.set_text("")
+
+ def _editable_entry(self, valor):
+ if valor:
+ self.ef_name.set_editable(True)
+ self.el_name.set_editable(True)
+ self.e_address.set_editable(True)
+ self.e_phone.set_editable(True)
+ self.e_ocupation.set_editable(True)
+ self.e_email.set_editable(True)
+ self.e_category.set_editable(True)
+ else:
+ self.ef_name.set_editable(False)
+ self.el_name.set_editable(False)
+ self.e_address.set_editable(False)
+ self.e_phone.set_editable(False)
+ self.e_ocupation.set_editable(False)
+ self.e_email.set_editable(False)
+ self.e_category.set_editable(False)
+
+ def _delete_event(self, widget, event, data=None):
+ gtk.main_quit()
+ return False
+
+ def main(self):
+ gtk.main()
+ return 0
+
+if __name__=="__main__":
+ c = Contactos()
+ c.main()
diff --git a/sql_class.py b/sql_class.py
new file mode 100755
index 0000000..d170367
--- /dev/null
+++ b/sql_class.py
@@ -0,0 +1,146 @@
+#! /usr/bin/env python
+
+# PROBLEMAS APARENTE CON EL LOAD_CONTACT
+
+###########################################################################
+# authors = {"pynash- Walter Danilo Vargas": "pynash AT gmail DOT com"} #
+# #
+# date = "January 30th, 2011" #
+# #
+# version = ":~$ revision 1.0$" #
+###########################################################################
+
+
+import os.path
+import sqlite3
+import os.path, subprocess as sp
+from random import randint
+
+
+class Conexion(object):
+ """Class bd's conection, basic operation"""
+
+ def __init__(self, id = "", f_name = " ", l_name = " ", address = " ", phone = " ", ocupation=" ", email=" ", category=" "):
+ """Initialiting var and conecting to bd"""
+ self.id = id
+ self.f_name = f_name
+ self.l_name = l_name
+ self.address = address
+ self.phone = phone
+ self.ocupation = ocupation
+ self.email = email
+ self.categoria = category
+ self.imagen = ""
+
+ if not (os.path.exists('sqlite3_localdb.db')):
+ self.conn = sqlite3.connect("sqlite3_localdb.db")
+ self.c = self.conn.cursor()
+ self.c.execute("""create table User (id INTEGER, \
+ f_name text,\
+ l_name text,\
+ address text,\
+ phone text,\
+ ocupation text,\
+ email text,\
+ categoria text,\
+ imagen BLOB)""")
+ #cargamos la imagen
+ imgdata = open('./varios/3.bmp', 'r').read()
+ self.imagen = sqlite3.Binary(imgdata)
+
+ simbol1 = (1, 'carlos', 'vargas', '', '12345', 'Estudiante', '', 'amigos', self.imagen, )
+ simbol2 = (2, 'walter danilo', 'vargas', 'Masaya', '12345', 'Estudiante', 'pynash@gmail.com', 'otros', self.imagen, )
+ self.c.execute("""insert into User values(?, ?, ?, ?, ?, ?, ?, ?, ?)""", simbol1)
+ self.c.execute("""insert into User values(?, ?, ?, ?, ?, ?, ?, ?, ?)""", simbol2)
+ self.conn.commit()
+ else:
+ self.conn = sqlite3.connect("sqlite3_localdb.db")
+ self.c = self.conn.cursor()
+
+ def _exists_user(self, name):
+ self.c.execute("select f_name, l_name from User ")
+ for i in self.c:
+ if (i[0].lower() + " " + i[1].lower()) == name:
+ return 1
+
+ def _operation_image(self, image):
+ if os.path.exists('./images/show.jpg'):
+ sp.call('rm ./images/show.jpg', shell=True)
+
+ with open('./images/show.jpg', 'w') as f:
+ f.write(image)
+
+ def _show_default(self, metodo=1):
+ self.conn.row_factory = sqlite3.Row
+ t = (self.f_name, self.l_name, )
+ if metodo:
+ self._randint()
+ self.c.execute('select * from User where id=?', (self.id, ))
+ else:
+ self.c.execute('select * from User where f_name=? and l_name=?', t)
+ r = self.c.fetchone()
+
+ return [r[1], r[2], r[3], r[4], r[5], r[6], r[7]]
+
+ def _add_user(self):
+ """register a new user to bd"""
+ self.id = self._id_calc()
+ imgdata = open('./noavailable.png', 'r').read()
+ self.imagen = sqlite3.Binary(imgdata)
+
+ simbol = (self.id, self.f_name, self.l_name, self.address, self.phone, self.ocupation, self.email, self.categoria, self.imagen, )
+ self.c.execute('insert into User \
+ values (?, ?, ?, ?, ?, ?, ?, ?, ?)', simbol)
+ self.conn.commit()
+
+ def _return_id(self, fname, lname):
+ t = (fname, lname, )
+ self.conn.row_factory = sqlite3.Row
+ self.c.execute('select id from User where f_name=? and l_name = ?', t)
+ id = self.c.fetchone()
+ return id[0]
+
+ def _update_user(self):
+ """udpate a user in the bd"""
+ t = (self.f_name, self.l_name, self.address, self.phone, self.ocupation, self.email, self.categoria, self.id)
+
+ self.c.execute('update User \
+ set f_name=?, l_name=?, address=?, phone=?, ocupation=?, email=?, categoria=?\
+ where id=?', t)
+ self.conn.commit()
+
+ def _del_user(self):
+ """delete a user"""
+ t = (self.id, )
+ self.c.execute('delete from User where id=?', t)
+ self.conn.commit()
+
+ def _id_calc(self):
+ self.conn.row_factory = sqlite3.Row
+ self.c.execute('select max(id) from user')
+ id = self.c.fetchone()
+ return id[0]+1
+
+ def _randint(self):
+ self.conn.row_factory = sqlite3.Row
+ self.c.execute('select id from User')
+ list1 = []
+ for i in self.c:
+ list1.append(i)
+ list2 = []
+ for i in list1:
+ list2.append(i[0])
+ self.id = list2[randint(1, len(list2)-1)]
+
+ def _display_on_tree(self, cat):
+ self.c.execute('select f_name, l_name from User where categoria=?', (cat, ))
+ lista = []
+ for i in self.c:
+ lista.append([i[0], i[1]])
+ return lista
+
+ #lista = []
+ #for i in self.c:
+ # lista.append(i[0] + " " + i[1])
+ #return lista
+