def add(self, account_main: AccountMain): sql = """ INSERT INTO account_main(email, name, hash_password, is_confirmed) VALUES (%s, %s, %s, %s) RETURNING id, created_at, edited_at;""" with self.pool.getconn() as conn: with conn.cursor() as cur: try: cur.execute(sql, (account_main.email, account_main.name, account_main.hash_password, account_main.is_confirmed)) row = cur.fetchone() cur.close() conn.commit() except psycopg2.InternalError as err: if err.pgcode == errorcodes.IN_FAILED_SQL_TRANSACTION: conn.rollback() return None, "RETURN" else: raise TypeError except psycopg2.IntegrityError as err: if err.pgcode == errorcodes.UNIQUE_VIOLATION: return None, "Not unique email" else: raise TypeError self.pool.putconn(conn) account_main.id = row[0] account_main.created_at = row[1] account_main.edited_at = row[2] return account_main, None
def delete_teacher(auth_account_main_id: int, organisation_id: int): teacher = Teacher(organisation=Organisation(id=organisation_id, account_main=AccountMain(id=auth_account_main_id))) teacher, err = TeacherService.delete_teacher(teacher) if err: return json.dumps(err) return json.dumps(get_response_delete_teacher(teacher))
def send_new_auth_code(auth_account_main_id: int): main_code, err = AuthCodeDao().get_by_account_main_id( auth_account_main_id) if err: return None, err if main_code and get_passed_time( main_code.edited_at) < SEND_CODE_INTERVAL: return None, "Сообщения можно отправлять не чаще, чем раз в 60 секунд" auth_code = AuthCode(account_main=AccountMain(id=auth_account_main_id)) auth_code.create_random_code() if main_code: _, err = AuthCodeDao().update(main_code.id, auth_code) else: _, err = AuthCodeDao().add(auth_code) account_main, err = AccountMainDao().get_email_by_id( auth_account_main_id) if err: return None, err _ = MailServer.send_email(EMAIL_CODE_TYPE, account_main.email, auth_code.code) return None, None
def del_parents(auth_account_main_id: int): if request.method == 'DELETE': parent = Parents(account_main=AccountMain(id=auth_account_main_id)) parent, err = ParentsService.delete_parent(parent) if err: return json.dumps(err) return json.dumps(get_response_delete_parent(parent))
def delete_organisation(auth_account_main_id: int): if request.method == 'POST': organisations = Organisation(account_main=AccountMain(id=auth_account_main_id)) organisations, err = OrganisationService.delete_organisation(organisations) if err: return json.dumps(err) return json.dumps(get_response_delete_organisation(organisations))
def _create_session(account_main: AccountMain): account_session = AccountSession(account_main=account_main) account_session, err = AccountSessionDao().add(account_session) if err: return None, err account_main.auth_token = account_session.create_token() return account_main, None
def main_page(auth_account_main_id: int): if request.method == 'GET': organisation = Organisation(account_main=AccountMain(id=auth_account_main_id)) list_teacher_organisation, err = OrganisationService.get_all_by_account_id(organisation.account_main.id) if err: return json.dumps(err) return json.dumps(get_response_detail_organisation(list_teacher_organisation))
def auth_login(account_main: AccountMain): account_main.create_hash_password() account_main, err = AccountMainDao().get_by_email_and_hash_password( account_main) if err: return None, err if not account_main: return None, "Неверный email" account_session = AccountSession(account_main=account_main) account_session, err = AccountSessionDao().add(account_session) if err: return None, err account_main.auth_token = account_session.create_token() return account_main, None
def _des_from_db_info_child_with_parents(row_dict) -> Children: return Children(id=row_dict['children_id'], name=row_dict['children_name'], surname=row_dict['children_surname'], date_born=row_dict['children_date_born'], parents=Parents( id=row_dict['children_parents_id'], account_main=AccountMain( id=row_dict['parents_account_main_id'])))
def register(account_main: AccountMain): account_main.create_hash_password() account_main.is_confirmed = False account_main, errors = AuthService._add_account_main_and_create_session( account_main) if errors: return None, errors auth_code = AuthCode(account_main=account_main) auth_code.create_random_code() _, errors = AuthCodeDao().add(auth_code) if errors: return None, errors account_main.is_email_sent = MailServer.send_email( EMAIL_CODE_TYPE, account_main.email, auth_code.code) return account_main, None
def _deserializer_from_db_full(account_main_record): return AccountMain( id=account_main_record.get('account_main_id'), created_at=account_main_record.get('account_main_created_at'), edited_at=account_main_record.get('account_main_edited_at'), email=account_main_record.get('account_main_email'), name=account_main_record.get('account_main_name'), hash_password=account_main_record.get( 'account_main_hash_password'), is_confirmed=account_main_record.get('account_main_is_confirmed'))
def confirm_code(auth_account_main_id: int): validate_errors = ConfirmCodeSchema().validate(dict(code=request.form.get('code'))) if validate_errors: return json.dumps(validate_errors) auth_code = AuthCode(account_main=AccountMain(id=auth_account_main_id), code=request.form.get('code')) _, err = AuthService.confirm_code(auth_code) if err: return json.dumps(err) return json.dumps(True)
def wrapper(*args, auth_account_main_id: int, **kwargs): account_main = AccountMain(id=auth_account_main_id) parent, err = ParentsService.get_by_account_id(account_main.id) if err: return json.dumps(err) response = func(*args, auth_account_main_id=account_main.id, parent_id=parent.id, **kwargs) return response
def edit_parents(auth_account_main_id: int): if request.method == 'POST': errors = EditParentsSchema().validate(dict(name=request.form['name'], surname=request.form['surname'])) if errors: return json.dumps(errors) parent = ParentsDeserializer.deserialize(request.form, DES_FROM_EDIT_PARENTS) parent.account_main = AccountMain(id=auth_account_main_id) parent, err = ParentsService.update_parent(parent) if err: return json.dumps(err) return json.dumps(get_response_edit_parent(parent))
def wrapper(*args, auth_account_main_id: int, **kwargs): account_main = AccountMain(id=auth_account_main_id) organisation, err = OrganisationService.get_by_account_id( account_main.id) if err: return json.dumps(err) response = func(*args, auth_account_main_id=account_main.id, organisation_id=organisation.id, **kwargs) return response
def wrapper(*args, auth_account_main_id: int, parent_id: int, **kwargs): children = Children(parents=Parents( id=parent_id, account_main=AccountMain(id=auth_account_main_id))) list_children, err = ChildrenService.get_children_by_parents_id( children) if err: return json.dumps(err) response = func(*args, list_children=list_children, parent_id=list_children[0].parents.id, **kwargs) return response
def edit_teacher(auth_account_main_id: int, organisation_id: int): errors = EditTeacherSchema().validate(dict(name=request.form['name'], surname=request.form['surname'], specialty=request.form['specialty'])) if errors: return json.dumps(errors) teacher = TeacherDeserializer.deserialize(request.form, DES_FOR_EDIT_TEACHER) teacher.organisation = Organisation(id=organisation_id, account_main=AccountMain(id=auth_account_main_id)) teacher, err = TeacherService.edit_teacher(teacher) if err: return json.dumps(err) return json.dumps(get_response_edit_teacher(teacher))
def get_requests(auth_account_main_id: int, organisation_id: int): if request.method == 'GET': request_to_organisation = RequestToOrganisation( events=Events(organisation=Organisation( id=organisation_id, account_main=AccountMain(id=auth_account_main_id) )) ) list_request_to_organisation, err = RequestToOrganisationService.get_all_requests_by_org_id(request_to_organisation) if err: return json.dumps(err) return json.dumps(get_response_with_get_all_requests(list_request_to_organisation))
def recovery_password(account_main: AccountMain): account_main, err = AccountMainDao().get_by_email_and_name( account_main) if err: return None, err if not account_main: return None, "Неверный email" if not account_main.is_confirmed: return None, "email не подтвержден" account_main.password = get_temp_password() account_main.create_hash_password() account_main, err = AccountMainDao().set_temp_psw(account_main) if err: return None, err account_main.is_email_sent = MailServer.send_email( EMAIL_RECOVERY_PASSWORD_TYPE, account_main.email, account_main.password) return account_main, None
def get_detail_request(auth_account_main_id: int, organisation_id: int, request_id: int): if request.method == 'POST': request_to_organisation = RequestToOrganisation( id=request_id, events=Events(organisation=Organisation( id=organisation_id, account_main=AccountMain(id=auth_account_main_id) )) ) # TODO статус = тру, достаем id ребенка для events_child, request_to_organisation, err = RequestToOrganisationService.accept_request(request_to_organisation) if err: return json.dumps(err) return json.dumps(get_response_accept_request(request_to_organisation))
def edit_organisation(auth_account_main_id: int): if request.method == 'POST': errors = EditOrganisationSchema().validate(dict(name=request.form['name'], login=request.form['login'], photo_link=request.form['photo_link'], description=request.form['description'])) if errors: return json.dumps(errors) organisations = OrganisationDeserializer.deserialize(request.form, DES_FOR_EDIT_ORGANISATION) organisations.account_main = AccountMain(id=auth_account_main_id) organisations, err = OrganisationService.edit_organisation(organisations) if err: return json.dumps(err) return json.dumps(get_response_edit_organisation(organisations))
def index(parent_id: int, auth_account_main_id: int): if request.method == 'POST': errors = AddChildSchema().validate( dict(name=request.form['name'], surname=request.form['surname'], date_born=request.form['date_born'])) if errors: return json.dumps(errors) children = ChildrenDeserialize.deserialize(request.form, DES_FOR_ADD_CHILD) children.parents = Parents( id=parent_id, account_main=AccountMain(id=auth_account_main_id)) children, err = ChildrenService.add_child(children) if err: return json.dumps(err) return json.dumps(get_response_add_children(children)) elif request.method == 'GET': children = Children(parents=Parents( id=parent_id, account_main=AccountMain(id=auth_account_main_id))) list_children, err = ChildrenService.get_children_by_parents_id( children) if err: return json.dumps(err) return json.dumps(get_response_get_list_child(list_children))
def get_email_by_id_into_transaction(self, auth_account_main_id: int): sql = """SELECT email AS account_main_email FROM account_main WHERE account_main.id = %s""" cur = self.conn.cursor(cursor_factory=extras.RealDictCursor) cur.execute(sql, (auth_account_main_id,)) row = cur.fetchone() cur.close() account_email = row['account_main_email'] if not account_email: return None, "Интересно..." return AccountMain(email=account_email), None
def request_add_child(parent_id: int, auth_account_main_id: int, children_id: int, events_id: int): if request.method == 'POST': if not events_id: return json.dumps('Выберите событие') request_to_organisation = RequestToOrganisation( children=Children(id=children_id), parents=Parents(id=parent_id, account_main=AccountMain(id=auth_account_main_id)), events=Events(id=events_id)) request_to_organisation, err = RequestToOrganisationService.make_request( request_to_organisation) if err: return json.dumps(err) return json.dumps(get_response_make_request(request_to_organisation))
def add_event(auth_account_main_id: int, organisation_id: int): if request.method == 'POST': errors = AddEventSchema().validate(dict(type=request.form['type'], name=request.form['name'], date_event=request.form['date_event'], event_hours=request.form['hours'], skill=request.form['skill'])) if errors: return json.dumps(errors) event: Events = EventDeserializer.deserialize(request.form, DES_FROM_ADD_EVENT) event.organisation = Organisation(id=organisation_id, account_main=AccountMain(id=auth_account_main_id)) event, err = EventsService.add_event(event) if err: return None, err return json.dumps(get_response_add_event(event))
def get_email_by_id(self, auth_account_main_id: int): sql = """SELECT email AS account_main_email FROM account_main WHERE account_main.id = %s""" with self.pool.getconn() as conn: with conn.cursor(cursor_factory=extras.RealDictCursor) as cur: cur.execute(sql, (auth_account_main_id,)) row = cur.fetchone() cur.close() account_email = row['account_main'] if not account_email: return None, "Интересно..." return AccountMain(email=account_email), None
def wrapper(*args, auth_account_main_id: int, organisation_id: int, **kwargs): if not request.args.get('teacher'): return json.dumps("Прикрепите учителя") teacher = Teacher( name=request.args.get('teacher').split(' ')[0], surname=request.args.get('teacher').split(' ')[1], organisation=Organisation( id=organisation_id, account_main=AccountMain(id=auth_account_main_id))) teacher, err = TeacherService.get_by_organisation_id(teacher) if err: return json.dumps(err) response = func(*args, organisation_id=teacher.organisation.id, teacher_id=teacher.id, **kwargs) return response
def get_by_account_id(self, account_main_id: int): sql = """ SELECT id AS parent_id, name AS parent_name, surname AS parent_surname, account_main_id AS account_main_id FROM parents WHERE account_main_id = %s""" with self.pool.getconn() as conn: with conn.cursor(cursor_factory=extras.RealDictCursor) as cur: cur.execute(sql, (account_main_id, )) row = cur.fetchone() cur.close() self.pool.putconn(conn) if not row: return None, "Сначала зарегистрируйте себя как родителя" parents = Parents( id=row['parent_id'], name=row['parent_name'], surname=row['parent_surname'], account_main=AccountMain(id=row['account_main_id'])) return parents, None
def get_by_id(self, account_main_id: int): sql = """ SELECT id AS organisation_id, name AS organisation_name, login AS organisation_login, account_main_id AS account_main_id FROM organisation WHERE account_main_id = %s""" with self.pool.getconn() as conn: with conn.cursor(cursor_factory=extras.RealDictCursor) as cur: cur.execute(sql, (account_main_id, )) row = cur.fetchone() cur.close() self.pool.putconn(conn) if not row: return None, "Сначала зарегистрируйте себя как организацию" organisation = Organisation( id=row['organisation_id'], name=row['organisation_name'], login=row['organisation_login'], account_main=AccountMain(id=row['account_main_id'])) return organisation, None
def _deserializer_from_recovery_password(account_main_dict): return AccountMain(name=account_main_dict.get('name'), email=account_main_dict.get('email'))