|
| 1 | +import pymysql.cursors |
| 2 | +from model.group import Group |
| 3 | +from model.add_new import AddNew |
| 4 | +import re |
| 5 | + |
| 6 | + |
| 7 | +class DbFixture: |
| 8 | + |
| 9 | + def __init__(self, host, name, user, password): |
| 10 | + self.host = host |
| 11 | + self.name = name |
| 12 | + self.user = user |
| 13 | + self.password = password |
| 14 | + self.connection = pymysql.connect(host=host, database=name, user=user, password=password, autocommit=True) |
| 15 | + |
| 16 | + def get_group_list(self): |
| 17 | + list = [] |
| 18 | + cursor = self.connection.cursor() |
| 19 | + try: |
| 20 | + cursor.execute("select group_id, group_name, group_header, group_footer from group_list") |
| 21 | + for row in cursor: |
| 22 | + (id, name, header, footer) = row |
| 23 | + list.append(Group(id=str(id), name=name, header=header, footer=footer)) |
| 24 | + finally: |
| 25 | + cursor.close() |
| 26 | + return list |
| 27 | + |
| 28 | + def get_contact_list(self): |
| 29 | + list = [] |
| 30 | + cursor = self.connection.cursor() |
| 31 | + try: |
| 32 | + cursor.execute( |
| 33 | + "select id, firstname, lastname, address, home, mobile, work, email, email2, email3, phone2 from addressbook where deprecated='0000-00-00 00:00:00'") |
| 34 | + for row in cursor: |
| 35 | + (id, firstname, lastname, address, home, mobile, work, email, email2, email3, phone2) = row |
| 36 | + current_contact = AddNew(my_id=str(id), my_f_name=firstname, my_l_name=lastname, |
| 37 | + my_home_address=address, |
| 38 | + my_h_telefon=home, my_mobile=mobile, my_work_telefon=work, |
| 39 | + my_secondary_phone=phone2, |
| 40 | + my_company_mail=email, my_second_mail=email2, my_third_mail=email3 |
| 41 | + ) |
| 42 | + final_contact = AddNew(my_id=str(id), my_f_name=self.removing_spaces(firstname), |
| 43 | + my_l_name=self.removing_spaces(lastname), |
| 44 | + my_home_address=self.removing_spaces(address) |
| 45 | + ) |
| 46 | + final_contact.all_phones_from_home_page = self.merge_phones_like_on_home_page(current_contact) |
| 47 | + final_contact.all_emails_from_home_page = self.merge_emails_like_on_home_page(current_contact) |
| 48 | + list.append(final_contact) |
| 49 | + |
| 50 | + finally: |
| 51 | + cursor.close() |
| 52 | + return list |
| 53 | + |
| 54 | + def destroy(self): |
| 55 | + self.connection.close() |
| 56 | + |
| 57 | + def clear(self, s): |
| 58 | + return re.sub("[() -]", "", s) |
| 59 | + |
| 60 | + def merge_phones_like_on_home_page(self, contacts): |
| 61 | + return "\n".join(filter(lambda x: x != "", |
| 62 | + map(lambda x: self.clear(x), |
| 63 | + filter(lambda x: x is not None, |
| 64 | + [contacts.my_h_telefon, contacts.my_mobile, contacts.my_work_telefon, |
| 65 | + contacts.my_secondary_phone])))) |
| 66 | + |
| 67 | + def merge_emails_like_on_home_page(self, contacts): |
| 68 | + |
| 69 | + return "\n".join(filter(lambda x: x != "", |
| 70 | + map(lambda x: self.clear(x), |
| 71 | + filter(lambda x: x is not None, |
| 72 | + [contacts.my_company_mail, contacts.my_second_mail, |
| 73 | + contacts.my_third_mail])))) |
| 74 | + |
| 75 | + def removing_spaces(self, s): |
| 76 | + return re.sub(" ", " ", s.strip()) |
0 commit comments