From 45fa040cf6d3f853d6518d677c614f5f9b8241fe Mon Sep 17 00:00:00 2001 From: David Romero Date: Mon, 19 Feb 2024 15:30:08 +0100 Subject: [PATCH] feat: Improve code --- app/__init__.py | 20 ++++++++++++++- app/blueprints/profile/repositories.py | 7 ++++++ app/blueprints/profile/routes.py | 28 ++++++--------------- app/blueprints/profile/services.py | 14 +++++++++++ app/repositories/BaseRepository.py | 34 ++++++++++++++++++++++++++ app/services/BaseService.py | 15 ++++++++++++ 6 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 app/blueprints/profile/repositories.py create mode 100644 app/blueprints/profile/services.py create mode 100644 app/repositories/BaseRepository.py create mode 100644 app/services/BaseService.py diff --git a/app/__init__.py b/app/__init__.py index 365ef17a..c47a2940 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,7 +3,7 @@ import logging import importlib.util -from flask import Flask, render_template, Blueprint +from flask import Flask, render_template, Blueprint, flash, redirect, url_for from flask_login import current_user from flask_sqlalchemy import SQLAlchemy from dotenv import load_dotenv @@ -66,6 +66,18 @@ def load_user(user_id): def inject_flask_app_name(): return dict(FLASK_APP_NAME=os.getenv('FLASK_APP_NAME')) + # Injecting methods + @app.context_processor + def handle_service_response(result, errors, success_url, success_msg, error_template, form): + if result: + flash(success_msg, 'success') + return redirect(url_for(success_url)) + else: + for error_field, error_messages in errors.items(): + for error_message in error_messages: + flash(f'{error_field}: {error_message}', 'error') + return render_template(error_template, form=form) + return app @@ -115,6 +127,12 @@ def get_authenticated_user_profile(): return None +def get_authenticated_user(): + if current_user.is_authenticated: + return current_user + return None + + def datasets_counter() -> int: from app.blueprints.dataset.models import DataSet count = DataSet.query.count() diff --git a/app/blueprints/profile/repositories.py b/app/blueprints/profile/repositories.py new file mode 100644 index 00000000..9b671773 --- /dev/null +++ b/app/blueprints/profile/repositories.py @@ -0,0 +1,7 @@ +from app.blueprints.profile.models import UserProfile +from app.repositories.BaseRepository import BaseRepository + + +class UserProfileRepository(BaseRepository): + def __init__(self): + super().__init__(UserProfile) diff --git a/app/blueprints/profile/routes.py b/app/blueprints/profile/routes.py index fb293f47..fab9880f 100644 --- a/app/blueprints/profile/routes.py +++ b/app/blueprints/profile/routes.py @@ -1,33 +1,21 @@ -from flask import request, render_template, flash +from flask import request, render_template, flash, redirect, url_for from flask_login import login_required from app.blueprints.profile import profile_bp from app.blueprints.profile.forms import UserProfileForm from app import get_authenticated_user_profile +from app.blueprints.profile.services import UserProfileService + @profile_bp.route('/profile/edit', methods=['GET', 'POST']) @login_required def edit_profile(): form = UserProfileForm() + service = UserProfileService() + if request.method == 'POST': + result, errors = service.update_profile(get_authenticated_user_profile().id, form) + return handle_service_response(result, errors, 'profile.view_profile', 'Profile updated successfully', 'profile/edit.html', form) - if request.method == 'POST' and form.validate_on_submit(): - orcid = form.orcid.data.strip() - name = form.name.data.strip() - surname = form.surname.data.strip() - affiliation = form.affiliation.data.strip() - - profile = get_authenticated_user_profile() - profile.orcid = orcid - profile.name = name - profile.surname = surname - profile.affiliation = affiliation - profile.save() - - flash('Saved profile', 'success') - - return render_template('profile/edit.html', form=form) - - else: - return render_template('profile/edit.html', form=form) + return render_template('profile/edit.html', form=form) diff --git a/app/blueprints/profile/services.py b/app/blueprints/profile/services.py new file mode 100644 index 00000000..dd49b2ac --- /dev/null +++ b/app/blueprints/profile/services.py @@ -0,0 +1,14 @@ +from app.blueprints.profile.repositories import UserProfileRepository +from app.services.BaseService import BaseService + + +class UserProfileService(BaseService): + def __init__(self): + super().__init__(UserProfileRepository()) + + def update_profile(self, user_profile_id, form): + if form.validate(): + updated_instance = self.update(user_profile_id, **form.data) + return updated_instance, None + else: + return None, form.errors \ No newline at end of file diff --git a/app/repositories/BaseRepository.py b/app/repositories/BaseRepository.py new file mode 100644 index 00000000..db4b1b11 --- /dev/null +++ b/app/repositories/BaseRepository.py @@ -0,0 +1,34 @@ +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + + +class BaseRepository: + def __init__(self, model): + self.model = model + + def create(self, **kwargs): + instance = self.model(**kwargs) + db.session.add(instance) + db.session.commit() + return instance + + def get_by_id(self, id): + return self.model.query.get(id) + + def update(self, id, **kwargs): + instance = self.get_by_id(id) + if instance: + for key, value in kwargs.items(): + setattr(instance, key, value) + db.session.commit() + return instance + return None + + def delete(self, id): + instance = self.get_by_id(id) + if instance: + db.session.delete(instance) + db.session.commit() + return True + return False diff --git a/app/services/BaseService.py b/app/services/BaseService.py new file mode 100644 index 00000000..b409abdb --- /dev/null +++ b/app/services/BaseService.py @@ -0,0 +1,15 @@ +class BaseService: + def __init__(self, repository): + self.repository = repository + + def create(self, **kwargs): + return self.repository.create(**kwargs) + + def get_by_id(self, id): + return self.repository.get_by_id(id) + + def update(self, id, **kwargs): + return self.repository.update(id, **kwargs) + + def delete(self, id): + return self.repository.delete(id)