Skip to content
6 changes: 6 additions & 0 deletions src/onegov/agency/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
from onegov.org.app import get_redactor_asset as redactor_assets


from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable
from webob import Response


class AgencyApp(OrgApp, ApiApp):

request_class = AgencyRequest
Expand Down
15 changes: 15 additions & 0 deletions src/onegov/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
if TYPE_CHECKING:
from _typeshed import SupportsRichComparison
from collections.abc import Callable, Collection, Iterator
from datetime import datetime
from fs.base import FS, SubFS
from re import Match
from sqlalchemy import Column
Expand Down Expand Up @@ -1181,3 +1182,17 @@ def batched(
return

yield batch


# copied from election_day/common.py
def add_last_modified_header(
response: 'Response',
last_modified: 'datetime | None'
) -> None:
""" Adds the give date to the response as Last-Modified header. """

if last_modified:
response.headers.add(
'Last-Modified',
last_modified.strftime("%a, %d %b %Y %H:%M:%S GMT")
)
1 change: 1 addition & 0 deletions src/onegov/election_day/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __setitem__(self, key: _KT, value: _VT) -> None:
super().move_to_end(key)


# TODO: move to general utils
def add_last_modified_header(
response: 'Response',
last_modified: 'datetime | None'
Expand Down
18 changes: 17 additions & 1 deletion src/onegov/file/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from morepath import App
from onegov.core.custom import json
from onegov.core.security import Private, Public
from onegov.core.utils import is_valid_yubikey, yubikey_public_id
from onegov.core.utils import is_valid_yubikey, yubikey_public_id, \
add_last_modified_header
from onegov.file.collection import FileCollection
from onegov.file.errors import AlreadySignedError
from onegov.file.errors import InvalidTokenError
Expand Down Expand Up @@ -429,6 +430,7 @@ def configure_depot_tween_factory(
app: DepotApp,
handler: 'Callable[[CoreRequest], Response]'
) -> 'Callable[[CoreRequest], Response]':
print('*** tschupre configure depot tween')

assert app.has_database_connection, "This module requires a db backed app."

Expand All @@ -443,6 +445,7 @@ def render_depot_file(
file: 'StoredFile',
request: 'CoreRequest'
) -> 'Response':
print('*** tschupre render depot file')
return request.get_response(
FileServeApp(file, cache_max_age=3600 * 24 * 7))

Expand Down Expand Up @@ -479,11 +482,13 @@ def include_x_robots_tag_header(response: 'Response') -> None:

@DepotApp.path(model=File, path='/storage/{id}')
def get_file(app: DepotApp, id: str) -> File | None:
print('*** tschupre get file by id')
return FileCollection(app.session()).by_id(id)


@DepotApp.view(model=File, render=render_depot_file, permission=Public)
def view_file(self: File, request: 'CoreRequest') -> 'StoredFile':
print('*** tschupre view_file') # only on browser 'force refresh'
respond_with_alt_text(self, request)
respond_with_caching_header(self, request)
respond_with_x_robots_tag_header(self, request)
Expand All @@ -500,6 +505,7 @@ def view_thumbnail(
self: File,
request: 'CoreRequest'
) -> 'StoredFile | Response':
print('*** tschupre view thumbnail')
if request.view_name in ('small', 'medium'):
size = request.view_name
else:
Expand All @@ -520,11 +526,17 @@ def view_thumbnail(
@DepotApp.view(model=File, render=render_depot_file, permission=Public,
request_method='HEAD')
def view_file_head(self: File, request: 'CoreRequest') -> 'StoredFile':
print('*** tschupre view file head')

@request.after
def set_cache(response: 'Response') -> None:
response.cache_control.max_age = 60

@request.after
def add_headers(response: 'Response') -> None:
print('*** tschupre add last modified header')
add_last_modified_header(response, self.last_change)

return view_file(self, request)


Expand All @@ -534,6 +546,7 @@ def view_thumbnail_head(
self: File,
request: 'CoreRequest'
) -> 'StoredFile | Response':
print('*** tschupre view thumbnail head')

@request.after
def set_cache(response: 'Response') -> None:
Expand All @@ -545,6 +558,7 @@ def set_cache(response: 'Response') -> None:
@DepotApp.view(model=File, name='note', request_method='POST',
permission=Private)
def handle_note_update(self: File, request: 'CoreRequest') -> None:
print('*** tschupre handle note update')
request.assert_valid_csrf_token()
note = request.POST.get('note')

Expand All @@ -562,6 +576,7 @@ def handle_note_update(self: File, request: 'CoreRequest') -> None:
@DepotApp.view(model=File, name='rename', request_method='POST',
permission=Private)
def handle_rename(self: File, request: 'CoreRequest') -> None:
print('*** tschupre handle rename')
request.assert_valid_csrf_token()
name = request.POST.get('name')

Expand Down Expand Up @@ -596,6 +611,7 @@ def delete_file(self: File, request: 'CoreRequest') -> None:
New tokens can be acquired through ``request.new_csrf_token``.

"""
print('*** tschupre delete file')
request.assert_valid_csrf_token()

if self.signed:
Expand Down
12 changes: 4 additions & 8 deletions src/onegov/people/models/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,10 @@ def organigram_file(self, value):
normalize_for_url(self.title),
extension_for_content_type(content_type_from_fileobj(value))
)
if self.organigram:
self.organigram.reference = as_fileintent(value, filename)
self.organigram.name = filename
else:
organigram = AgencyOrganigram(id=random_token())
organigram.reference = as_fileintent(value, filename)
organigram.name = filename
self.organigram = organigram
organigram = AgencyOrganigram(id=random_token())
organigram.reference = as_fileintent(value, filename)
organigram.name = filename
self.organigram = organigram

def add_person(self, person_id, title, **kwargs):
""" Appends a person to the agency with the given title. """
Expand Down