from onegov.core.elements import Link
from onegov.core.security import Private
from onegov.pas import _
from onegov.pas import PasApp
from onegov.pas.collections import AttendenceCollection
from onegov.pas.collections import CommissionCollection
from onegov.pas.forms import AttendenceAddCommissionForm
from onegov.pas.forms import CommissionMembershipAddForm
from onegov.pas.forms import CommissionForm
from onegov.pas.layouts import CommissionCollectionLayout
from onegov.pas.layouts import CommissionLayout
from onegov.pas.models import Change
from onegov.pas.models import Commission
from onegov.pas.models import CommissionMembership
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from onegov.core.types import RenderData
from onegov.town6.request import TownRequest
from webob import Response
@PasApp.html(
model=CommissionCollection,
template='commissions.pt',
permission=Private
)
[docs]
def view_commissions(
self: CommissionCollection,
request: 'TownRequest'
) -> 'RenderData':
layout = CommissionCollectionLayout(self, request)
filters = {}
filters['active'] = [
Link(
text=request.translate(title),
active=self.active == value,
url=request.link(self.for_filter(active=value))
) for title, value in (
(_('Active'), True),
(_('Inactive'), False)
)
]
return {
'add_link': request.link(self, name='new'),
'filters': filters,
'layout': layout,
'commissions': self.query().all(),
'title': layout.title,
}
@PasApp.form(
model=CommissionCollection,
name='new',
template='form.pt',
permission=Private,
form=CommissionForm
)
[docs]
def add_commission(
self: CommissionCollection,
request: 'TownRequest',
form: CommissionForm
) -> 'RenderData | Response':
if form.submitted(request):
commission = self.add(**form.get_useful_data())
request.success(_('Added a new commission'))
return request.redirect(request.link(commission))
layout = CommissionCollectionLayout(self, request)
layout.breadcrumbs.append(Link(_('New'), '#'))
layout.include_editor()
return {
'layout': layout,
'title': _('New commission'),
'form': form,
'form_width': 'large'
}
@PasApp.html(
model=Commission,
template='commission.pt',
permission=Private
)
[docs]
def view_commission(
self: Commission,
request: 'TownRequest'
) -> 'RenderData':
layout = CommissionLayout(self, request)
return {
'layout': layout,
'commission': self,
'title': layout.title,
}
@PasApp.form(
model=Commission,
name='edit',
template='form.pt',
permission=Private,
form=CommissionForm
)
[docs]
def edit_commission(
self: Commission,
request: 'TownRequest',
form: CommissionForm
) -> 'RenderData | Response':
if form.submitted(request):
form.populate_obj(self)
request.success(_('Your changes were saved'))
return request.redirect(request.link(self))
form.process(obj=self)
layout = CommissionLayout(self, request)
layout.breadcrumbs.append(Link(_('Edit'), '#'))
layout.editbar_links = []
layout.include_editor()
return {
'layout': layout,
'title': layout.title,
'form': form,
'form_width': 'large'
}
@PasApp.view(
model=Commission,
request_method='DELETE',
permission=Private
)
[docs]
def delete_commission(
self: Commission,
request: 'TownRequest'
) -> None:
request.assert_valid_csrf_token()
collection = CommissionCollection(request.session)
collection.delete(self)
@PasApp.form(
model=Commission,
name='new-membership',
template='form.pt',
permission=Private,
form=CommissionMembershipAddForm
)
[docs]
def add_commission_membership(
self: Commission,
request: 'TownRequest',
form: CommissionMembershipAddForm
) -> 'RenderData | Response':
if form.submitted(request):
self.memberships.append(
CommissionMembership(**form.get_useful_data())
)
request.success(_('Added a new parliamentarian'))
return request.redirect(request.link(self))
layout = CommissionLayout(self, request)
layout.breadcrumbs.append(Link(_('New parliamentarian'), '#'))
layout.include_editor()
return {
'layout': layout,
'title': _('New parliamentarian'),
'form': form,
'form_width': 'large'
}
@PasApp.form(
model=Commission,
name='add-attendence',
template='form.pt',
permission=Private,
form=AttendenceAddCommissionForm
)
[docs]
def add_plenary_attendence(
self: Commission,
request: 'TownRequest',
form: AttendenceAddCommissionForm
) -> 'RenderData | Response':
if form.submitted(request):
data = form.get_useful_data()
parliamentarian_ids = data.pop('parliamentarian_id')
collection = AttendenceCollection(request.session)
for parliamentarian_id in parliamentarian_ids:
attendence = collection.add(
parliamentarian_id=parliamentarian_id,
**data
)
Change.add(request, 'add', attendence)
request.success(_('Added commission meeting'))
return request.redirect(request.link(self))
layout = CommissionLayout(self, request)
layout.breadcrumbs.append(Link(_('New commission meeting'), '#'))
return {
'layout': layout,
'title': _('New commission meeting'),
'form': form,
'form_width': 'large'
}