Skip to content

Commit

Permalink
update: Modernize eprihlaska for 2024
Browse files Browse the repository at this point in the history
* Track dependencies with Poetry

* Bump the dependencies to 2024

* Get rid of Facebook OAuth option

Signed-off-by: mr.Shu <[email protected]>
  • Loading branch information
mrshu committed Feb 12, 2024
1 parent 09ce1f1 commit 29e8e76
Show file tree
Hide file tree
Showing 4 changed files with 1,213 additions and 41 deletions.
29 changes: 17 additions & 12 deletions eprihlaska/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from flask_mail import Mail
from flask_session import Session

from authlib.flask.client import OAuth
from authlib.client.apps import google, facebook
from authlib.integrations.flask_client import OAuth

from flask_uploads import configure_uploads
from .consts import MENU, receipts, uploaded_files
Expand All @@ -23,6 +22,7 @@
from logging import Formatter



nav = Nav()
items = [View(*x) for x in MENU]
right_items = [UserGreeting(), LogInLogOut()]
Expand All @@ -38,7 +38,11 @@
register_renderer(app, 'eprihlaska_nav_renderer', ePrihlaskaNavRenderer)

csrf.init_app(app)
babel = Babel(app)

def get_locale():
return request.accept_languages.best_match(['sk_SK', 'en'])

babel = Babel(app, locale_selector=get_locale)
app.config['BABEL_DEFAULT_LOCALE'] = 'sk_SK'
locale.setlocale(locale.LC_ALL, 'sk_SK.utf8')

Expand Down Expand Up @@ -68,10 +72,6 @@
app.logger.addHandler(mail_handler)


@babel.localeselector
def get_locale():
return request.accept_languages.best_match(['sk_SK', 'en'])


db = SQLAlchemy(app)

Expand All @@ -86,11 +86,16 @@ def get_locale():
def loader(user_id):
return User.query.get(int(user_id))


oauth = OAuth(fetch_token=fetch_token)
oauth.init_app(app)
google.register_to(oauth)
facebook.register_to(oauth)
CONF_URL = 'https://accounts.google.com/.well-known/openid-configuration'
oauth = OAuth(app)
oauth.register(
name='google',
server_metadata_url=CONF_URL,
client_kwargs={
'scope': 'openid email profile'
},
fetch_token=fetch_token
)

mail = Mail(app)
sess = Session(app)
Expand Down
33 changes: 4 additions & 29 deletions eprihlaska/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import flask.json
from flask_mail import Message
from flask_uploads import UploadNotAllowed
from eprihlaska import app, db, mail
from eprihlaska import app, db, mail, oauth
from eprihlaska.forms import (StudyProgrammeForm, PersonalDataForm,
AddressForm, PreviousStudiesForm,
AdmissionWaiversForm, FinalForm,
Expand All @@ -12,7 +12,6 @@
AIS2CookieForm, AIS2SubmitForm)
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import login_user, login_required, logout_user, current_user
from authlib.client.apps import google, facebook
import datetime
import uuid
import sys
Expand Down Expand Up @@ -802,13 +801,13 @@ def create_or_get_user_and_login(site, token, name, surname, email):
@app.route('/google/login', methods=['GET'])
def google_login():
callback_uri = url_for('google_authorize', _external=True)
return google.authorize_redirect(callback_uri)
return oauth.google.authorize_redirect(callback_uri)


@app.route('/google/auth', methods=['GET'])
def google_authorize():
token = google.authorize_access_token()
profile = google.parse_openid(token)
token = oauth.google.authorize_access_token()
profile = oauth.google.userinfo(token=token)

create_or_get_user_and_login('google', token,
profile.data.get('given_name', ''),
Expand All @@ -818,30 +817,6 @@ def google_authorize():
return redirect(url_for('study_programme'))


@app.route('/facebook/login', methods=['GET'])
def facebook_login():
callback_uri = url_for('facebook_authorize', _external=True)
return facebook.authorize_redirect(callback_uri)


@app.route('/facebook/auth', methods=['GET'])
def facebook_authorize():
token = facebook.authorize_access_token()
profile = facebook.fetch_user()

data = profile.data.get('name', '').split(' ')
name = '' if not len(data) else data[0]
surname = '' if len(data) <= 1 else data[-1]

create_or_get_user_and_login('facebook',
token,
name,
surname,
profile.email)

return redirect(url_for('study_programme'))


def process_apps(apps):
for application in apps:
out_app = {}
Expand Down
Loading

0 comments on commit 29e8e76

Please sign in to comment.