Skip to content

Commit

Permalink
feat: Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Feb 19, 2024
1 parent e220801 commit 45fa040
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 21 deletions.
20 changes: 19 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions app/blueprints/profile/repositories.py
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 8 additions & 20 deletions app/blueprints/profile/routes.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions app/blueprints/profile/services.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions app/repositories/BaseRepository.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions app/services/BaseService.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 45fa040

Please sign in to comment.