def setUp(self):
        self.temp_db = tempfile.NamedTemporaryFile(mode="w", delete=False)
        self.temp_db_name = self.temp_db.name

        self.test_contact = Contact("Test User", "*****@*****.**")

        json.dump([self.test_contact],
                  self.temp_db,
                  indent=2,
                  cls=ContactAwareJSONEncoder)
        self.temp_db.close()

        self.contact_service = ContactService(self.temp_db_name)
Beispiel #2
0
    def __init__(self):
        super().__init__()

        self.filename = "data/contacts.json"
        self.contacts_service = ContactService(self.filename)

        self.contacts_model = QStandardItemModel()
        self.contacts_model.setColumnCount(2)

        self.fill_list_view()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.contactsTableView.setModel(self.contacts_model)
        self.ui.nameLineEdit.textChanged.connect(
            self.on_contact_line_edit_change)
        self.ui.emailLineEdit.textChanged.connect(
            self.on_contact_line_edit_change)
        self.ui.deleteButton.clicked.connect(self.on_delete)

        self.current_row = None
        self.mode = EditMode.NONE
class TestContactService(TestCase):
    def setUp(self):
        self.temp_db = tempfile.NamedTemporaryFile(mode="w", delete=False)
        self.temp_db_name = self.temp_db.name

        self.test_contact = Contact("Test User", "*****@*****.**")

        json.dump([self.test_contact],
                  self.temp_db,
                  indent=2,
                  cls=ContactAwareJSONEncoder)
        self.temp_db.close()

        self.contact_service = ContactService(self.temp_db_name)

    def tearDown(self):
        os.unlink(self.temp_db_name)

    def test_get_count(self):
        self.assertTrue(self.contact_service.get_count(), 1)

    def test_get_contacts(self):
        contacts = self.contact_service.get_contacts()

        self.assertEqual(contacts[0].name, self.test_contact.name)

    def test_find(self):
        pass

    def test_create(self):
        pass

    def test_update(self):
        pass

    def test_remove(self):
        pass
Beispiel #4
0
    def __init__(self):
        self.status = None
        self.content = None
        self.__errors = []
        self.__contact_service = ContactService()
        self.__email_service = EmailService()
        self.__phone_service = PhoneService()
        self.__user_service = UserService()

        def save(self, user_id, data):
            __user = self.__user_service.get(user_id)
            self.__verify_user(__user)

            if not data['name']:
                self.__errors.append(USER['CONTACT']['NAME']['NOT_FOUND'])

            if len(self.__errors) > 0:
                self.content = {'errors': self.__errors}
                self.status = STATUS_CODE['NOT_FOUND']
            else:
                __contact = self.__contact_service.save(data)
                self.content = {'data': __contact.toDICT()}
                self.status = STATUS_CODE['CREATED']

        def add_phone(self, user_id, contact_id, data):
            self.__verify_phone(data['phone'])
            __contact = self.__contact_service.get(user_id, contact_id)
            self.__verify_contact(__contact)

            if len(self.__errors) > 0:
                self.content = {'errors': self.__errors}
            else:
                data['user_id'] = user_id
                self.__phone_service.save(data)
                self.__get_contatc(__contact)

        def add_email(self, user_id, contact_id):
            self.__verify_email(data['email'])
            __contact = self.__contact_service.get(user_id, contact_id)
            self.__verify_contact(__contact)

            if len(self.__errors) > 0:
                self.content = {'errors': self.__errors}
            else:
                self.__email_service.save(data)
                self.__get_contatc(__contact)

        def __get_contatc(self, contact):
            __contact_dict = contact.toDICT()
            __phones = self.__phone_service.get(contact.id)
            __emails = self.__email_service.get(contact.id)
            __contact_dict['phones'] = super().__toLIST(__phones)
            __contact_dict['emails'] = super().__toLIST(__emails)
            self.content = {'data': __contact_dict}
            self.status = STATUS_CODE['OK']

        def get(self, user_id, contact_id):
            __contact = self.__contact_service.get(user_id, contact_id)
            self.__verify_contact(__contact)

            if len(self.__errors) > 0:
                self.content = {'errors': self.__errors}
            else:
                self.__get_contatc(__contact)

        def update(self):
            pass

        def remove(self):
            pass

        def __verify_user(self, user):
            if not user:
                self.__errors.append(USER['NOT_FOUND'])
                self.status = STATUS_CODE['NOT_FOUND']

        def __verify_contact(self, contact):
            if not contact:
                self.__errors.append(USER['CONTACT']['NOT_FOUND'])
                self.status = STATUS_CODE['NOT_FOUND']

        def __verify_email(self, email):
            if not email:
                self.__errors.append(USER['CONTACT']['EMAIL']['NOT_FOUND'])
                self.status = STATUS_CODE['NOT_FOUND']

            elif not is_valid_email(email):
                self.__errors.append(USER['CONTACT']['EMAIL']['INVALID'])
                self.status = STATUS_CODE['BAD_REQUEST']

        def __verify_phone(self, phone):
            if not phone:
                self.__errors.append(USER['CONTACT']['PHONE']['NOT_FOUND'])
                self.status = STATUS_CODE['NOT_FOUND']
Beispiel #5
0
from wtforms.validators import DataRequired, Email

from app.services.contact_service import ContactService

from .auth import UserService

DATABASE_URL = os.environ.get("DATABASE_URL", "data/contacts.json")

app = Flask(__name__)
app.config[
    "SECRET_KEY"] = "204f26b2c90fd38adb73d2b76b8c2769d51c1c7cf0c684d8b57b5365d3e106d7"

login_manager = LoginManager(app)
login_manager.login_view = "login"

contact_service = ContactService(DATABASE_URL)
user_service = UserService()


class ContactForm(FlaskForm):
    name = StringField("Name", validators=[DataRequired()])
    email = EmailField("E-mail", validators=[DataRequired(), Email()])


class LoginForm(FlaskForm):
    username = StringField("Username", validators=[DataRequired()])
    password = PasswordField("Password", validators=[DataRequired()])


@login_manager.user_loader
def load_user(user_id):
Beispiel #6
0
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.filename = "data/contacts.json"
        self.contacts_service = ContactService(self.filename)

        self.contacts_model = QStandardItemModel()
        self.contacts_model.setColumnCount(2)

        self.fill_list_view()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.contactsTableView.setModel(self.contacts_model)
        self.ui.nameLineEdit.textChanged.connect(
            self.on_contact_line_edit_change)
        self.ui.emailLineEdit.textChanged.connect(
            self.on_contact_line_edit_change)
        self.ui.deleteButton.clicked.connect(self.on_delete)

        self.current_row = None
        self.mode = EditMode.NONE

    def fill_list_view(self):
        self.contacts_model.clear()
        self.contacts_model.setHorizontalHeaderLabels(["Name", "Email"])
        for contact in self.contacts_service.get_contacts():
            self.contacts_model.appendRow([
                QStandardItem(contact.name),
                QStandardItem(contact.email),
            ])

    def on_contact_line_edit_change(self, value):
        if self.mode == EditMode.EDIT or self.mode == EditMode.CREATE:
            return

        self.mode = EditMode.CREATE

        self.ui.saveButton.setEnabled(True)
        self.ui.cancelButton.setEnabled(True)

    def on_contactsTableView_clicked(self, index: QModelIndex):
        print(index.data())

        num_to_edit = index.row() + 1
        contact = self.contacts_service.get_contact(num_to_edit)

        self.current_row = num_to_edit
        self.mode = EditMode.EDIT

        self.ui.nameLineEdit.setText(contact.name)
        self.ui.emailLineEdit.setText(contact.email)

        self.ui.deleteButton.setEnabled(True)
        self.ui.cancelButton.setEnabled(True)
        self.ui.saveButton.setEnabled(True)

    def on_cancelButton_clicked(self):
        self.ui.nameLineEdit.clear()
        self.ui.emailLineEdit.clear()

        self.ui.deleteButton.setEnabled(False)
        self.ui.cancelButton.setEnabled(False)
        self.ui.saveButton.setEnabled(False)

        self.ui.contactsTableView.clearSelection()
        self.current_row = None
        self.mode = EditMode.NONE

    def on_saveButton_clicked(self):
        name = self.ui.nameLineEdit.text().strip()
        email = self.ui.emailLineEdit.text().strip()

        if not name:
            self.ui.nameLineEdit.setFocus()
            self.ui.nameLineEdit.setToolTip("Name can't be empty")
            return

        if not email:
            self.ui.emailLineEdit.setFocus()
            self.ui.emailLineEdit.setToolTip("Email can't be empty")
            return

        if self.mode == EditMode.EDIT:
            self.contacts_service.update(self.current_row, name, email)
            self.ui.statusbar.showMessage("Contact saved", 3000)
            QMessageBox.information(self, "Success",
                                    "Contact was successfully saved!")
        else:
            self.contacts_service.create(name, email)
            self.ui.statusbar.showMessage("Contact created", 3000)
            QMessageBox.information(self, "Success",
                                    "Contact was successfully created!")

        self.on_cancelButton_clicked()

        self.fill_list_view()

    def on_delete(self):
        response = QMessageBox.question(
            self, "Confirm delete",
            "Do you really want to delete this contact?")
        if response != QMessageBox.Yes:
            return

        self.contacts_service.remove(self.current_row)

        self.ui.statusbar.showMessage("Contact removed", 3000)
        QMessageBox.information(self, "Success",
                                "Contact was successfully deleted!")

        self.on_cancelButton_clicked()
        self.fill_list_view()