Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify static files and remove login manager #370

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sipa/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
disjoint from any blueprint.
"""
from flask import request, session
from flask_login import AnonymousUserMixin
from flask_login import AnonymousUserMixin, LoginManager
from werkzeug.routing import IntegerConverter as BaseIntegerConverter

from sipa.login_manager import SipaLoginManager
from sipa.backends import backends

login_manager = SipaLoginManager()
login_manager = LoginManager()
login_manager.login_view = "generic.login"
login_manager.login_message = "Bitte melde Dich an, um die Seite zu sehen."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not run the code, but self.localize_callback = gettext seems to miss compared to SipaLoginManager?



class IntegerConverter(BaseIntegerConverter):
Expand Down
65 changes: 26 additions & 39 deletions sipa/blueprints/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,37 @@
import os

from flask import Blueprint, send_from_directory, current_app
from flask_login import current_user
from flask.views import View
from flask.blueprints import BlueprintSetupState
from flask_login import login_required

from sipa.base import login_manager

class StaticFilesBlueprint(Blueprint):
def add_static_files(self, rule, endpoint, directory, decorators=()):
def add(state: BlueprintSetupState):
app = state.app
nonlocal directory
if not os.path.isabs(directory):
directory = os.path.join(app.root_path, directory)

bp_documents = Blueprint('documents', __name__)
def send_static_file(filename):
cache_timeout = app.get_send_file_max_age(filename)
return send_from_directory(directory, filename,
cache_timeout=cache_timeout)

for decorator in decorators:
send_static_file = decorator(send_static_file)

class StaticFiles(View):
def __init__(self, directory, login_required=False, member_required=False):
self.directory = directory
self.login_required = login_required
self.member_required = member_required
app.add_url_rule(rule, endpoint, send_static_file)

def dispatch_request(self, filename):
if self.login_required and not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
self.record(add)

if self.member_required and not current_user.is_member:
return current_app.login_manager.unauthorized()

if os.path.isabs(self.directory):
directory = self.directory
else:
directory = os.path.join(current_app.root_path, self.directory)
cache_timeout = current_app.get_send_file_max_age(filename)
return send_from_directory(directory, filename,
cache_timeout=cache_timeout)


bp_documents.add_url_rule('/images/<path:filename>',
view_func=StaticFiles.as_view('show_image',
'../content/images'))
login_manager.ignore_endpoint('documents.show_image')

bp_documents.add_url_rule('/documents/<path:filename>',
view_func=StaticFiles.as_view('show_document',
'../content/documents'))
login_manager.ignore_endpoint('documents.show_document')


bp_documents.add_url_rule('/documents_restricted/<path:filename>',
view_func=StaticFiles.as_view('show_document_restricted',
'../content/documents_restricted',
login_required=True,
member_required=True))
bp_documents = StaticFilesBlueprint('documents', __name__)
bp_documents.add_static_files('/images/<path:filename>', 'show_image',
'../content/images')
bp_documents.add_static_files('/documents/<path:filename>', 'show_document',
'../content/documents')
bp_documents.add_static_files('/documents_restricted/<path:filename>',
'show_document_restricted',
'../content/documents_restricted',
[login_required])
54 changes: 0 additions & 54 deletions sipa/login_manager.py

This file was deleted.

62 changes: 1 addition & 61 deletions tests/test_login_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from flask import Flask, Blueprint, url_for, session, request
from flask import Flask, url_for
from flask_login import current_user, login_user
from flask_testing import TestCase

Expand Down Expand Up @@ -51,63 +51,3 @@ def test_authentication_works(self):
self.login()
response = self.client.get(url_for('restricted'))
self.assertEqual(response.data.decode('utf-8'), "test_user")

def test_decorator_called_without_parameter(self):
with self.assertRaises(TypeError):
@self.app.route('/view')
@self.mgr.disable_user_loading # note that `()` is missing
def view():
return False


class AppLevelUserLoadingDisabledTest(SipaLoginManagerTest):
def create_app(self):
app = super().create_app()

@app.route('/images')
@self.mgr.disable_user_loading()
def show_images():
# We don't take kindly to your types around here!
self.assertFalse(current_user.is_authenticated)
return "Images :-)"

return app

def test_login_manager(self):
self.login()
response = self.client.get(url_for('show_images'))
self.assertEqual(response.data.decode('utf-8'), "Images :-)")
self.assertIn('show_images', self.mgr.ignored_endpoints)


class BlueprintLevelUserLoadingDisabledTest(SipaLoginManagerTest):
def create_app(self):
app = super().create_app()
bp = Blueprint(name='documents', import_name='documents')

@bp.route('/documents')
@self.mgr.disable_user_loading(bp)
def show_documents():
self.assertFalse(current_user.is_authenticated)
return "Documents :-)"

@bp.route('/images')
def show_images_as_well():
self.assertFalse(current_user.is_authenticated)
return "Images :-)"
self.mgr.ignore_endpoint('documents.show_images_as_well')

app.register_blueprint(bp)
return app

def test_documents_no_user(self):
self.login()
response = self.client.get(url_for('documents.show_documents'))
self.assertEqual(response.data.decode('utf-8'), "Documents :-)")
self.assertIn('documents.show_documents', self.mgr.ignored_endpoints)

def test_images_no_user(self):
self.login()
response = self.client.get(url_for('documents.show_images_as_well'))
self.assertEqual(response.data.decode('utf-8'), "Images :-)")
self.assertIn('documents.show_images_as_well', self.mgr.ignored_endpoints)