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
Beispiel #3
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 #4
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):