-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e220801
commit 45fa040
Showing
6 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |