Skip to content

Commit

Permalink
Agenda Items persist logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrillkuettel committed Jun 25, 2024
1 parent 950e067 commit 621c617
Show file tree
Hide file tree
Showing 14 changed files with 436 additions and 79 deletions.
29 changes: 17 additions & 12 deletions src/privatim/cli/initialize_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,27 @@ def add_example_content(

if add_meeting:
attendees = [admin_user]
meeting = Meeting(
name='Cras Tristisque',
time=datetime.now(tz=DEFAULT_TIMEZONE),
attendees=attendees,
working_group=WorkingGroup(
name='1. Gremium', leader=admin_user, users=attendees
),
)
db.add(meeting)
db.flush()
db.refresh(meeting)
agenda_items = [
AgendaItem(
AgendaItem.create(
db,
title='Begrüssung',
description='Begrüssung der Anwesenden und Eröffnung der '
'Sitzung',
meeting=meeting
),
AgendaItem(
AgendaItem.create(
db,
title='Neque porro quisquam est qui dolorem',
description='Lorem ipsum dolor sit amet, consectetur '
'adipiscing elit. Nulla dui metus, viverra '
Expand All @@ -154,19 +168,10 @@ def add_example_content(
'convallis. Class aptent taciti sociosqu ad '
'litora torquent per conubia nostra, '
'per inceptos himenaeos. ',
meeting=meeting
),
]
meeting = Meeting(
name='Cras Tristisque',
time=datetime.now(tz=DEFAULT_TIMEZONE),
attendees=attendees,
working_group=WorkingGroup(
name='1. Gremium', leader=admin_user, users=attendees
),
agenda_items=agenda_items
)
db.add_all(agenda_items)
db.add(meeting)
db.flush()


Expand Down
3 changes: 0 additions & 3 deletions src/privatim/layouts/layout.pt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<!-- Sortable expects to be a ES6 module with "type=module" so we can't use fanstatic-->
<script type="module" src="${layout.static_url('privatim:static/js/custom/sortable.js')}"></script>
<title>Austauschplattform privatim</title>
</head>

Expand Down
4 changes: 2 additions & 2 deletions src/privatim/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pyramid.decorator import reify
from pyramid.renderers import get_renderer
from privatim.static import (bootstrap_css, bootstrap_js, tom_select_css,
comments_css, profile_css, custom_js)
comments_css, profile_css, sortable_custom)
from pytz import timezone
import re
from datetime import date, datetime
Expand Down Expand Up @@ -40,7 +40,7 @@ def __init__(self, context: Any, request: 'IRequest') -> None:
bootstrap_js.need()
tom_select_css.need()
comments_css.need()
custom_js.need()
sortable_custom.need()
profile_css.need()

def show_steps(self) -> bool:
Expand Down
68 changes: 61 additions & 7 deletions src/privatim/models/meeting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from uuid import uuid4

from sqlalchemy import Text
import uuid
from sqlalchemy import Text, Integer, select, func
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import Table, Column, ForeignKey
from sqlalchemy.orm import relationship
Expand All @@ -14,12 +13,16 @@


from typing import TYPE_CHECKING


if TYPE_CHECKING:
from privatim.models import User, WorkingGroup
from datetime import datetime
from privatim.types import ACL
from sqlalchemy.orm import Session


class AgendaItemCreationError(Exception):
"""Custom exception for errors in creating AgendaItem instances."""
pass


meetings_users_association = Table(
Expand All @@ -44,10 +47,56 @@ class AgendaItem(Base):

__tablename__ = 'agenda_items'

def __init__(
self,
title: str,
description: str,
meeting: 'Meeting',
position: int,
):
if position is None:
raise AgendaItemCreationError(
'AgendaItem objects must be created using the create class '
'method because the attribute `position` has to be set.'
)
self.id = str(uuid.uuid4())
self.title = title
self.description = description
self.meeting = meeting
self.position = position

@classmethod
def create(
cls,
session: 'Session',
title: str,
description: str,
meeting: 'Meeting'
) -> 'AgendaItem':

meeting_id = meeting.id
max_position = session.scalar(
select(func.max(AgendaItem.position)).where(
AgendaItem.meeting_id == meeting_id
)
)
new_position = 0 if max_position is None else max_position + 1
new_agenda_item = cls(
title=title,
description=description,
meeting=meeting,
position=new_position,
)
session.add(new_agenda_item)
return new_agenda_item

id: Mapped[UUIDStrPK]

title: Mapped[str] = mapped_column(Text, nullable=False)

# the custom order which may be changed by the user
position: Mapped[int] = mapped_column(Integer, nullable=False)

description: Mapped[str] = mapped_column(Text)

meeting_id: Mapped[UUIDStr] = mapped_column(
Expand All @@ -58,12 +107,15 @@ class AgendaItem(Base):
meeting: Mapped['Meeting'] = relationship(
'Meeting',
back_populates='agenda_items',
order_by='AgendaItem.position'
)

def __acl__(self) -> list['ACL']:
return [
(Allow, Authenticated, ['view']),
]
def __repr__(self) -> str:
return f'<AgendaItem {self.title} position {self.position}>'


class Meeting(Base, Commentable):
Expand All @@ -79,7 +131,7 @@ def __init__(
working_group: 'WorkingGroup',
agenda_items: list[AgendaItem] | None = None,
):
self.id = str(uuid4())
self.id = str(uuid.uuid4())
self.name = name
self.time = time
self.attendees = attendees
Expand All @@ -99,10 +151,12 @@ def __init__(
back_populates='meetings'
)

# Trantanden (=Themen)
# Traktanden (=Themen)
agenda_items: Mapped[list[AgendaItem]] = relationship(
AgendaItem,
back_populates='meeting',
order_by="AgendaItem.position",
cascade="all, delete-orphan"
)

# allfällige Beschlüsse
Expand Down
26 changes: 18 additions & 8 deletions src/privatim/static/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
from functools import lru_cache
from pathlib import Path

from fanstatic import Library
from fanstatic import Resource
from fanstatic.core import render_js as render_js_default


from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Callable
if TYPE_CHECKING:
from collections.abc import Iterable
from fanstatic.core import Dependable


js_library = Library('privatim:js', 'js')
css_library = Library('privatim:css', 'css')


def render_js_module(url: str) -> str:
return f'<script type="module" src="{url}"></script>'


def js(
relpath: str,
depends: 'Iterable[Dependable] | None' = None,
relpath: str,
depends: 'Iterable[Dependable] | None' = None,
supersedes: list[Resource] | None = None,
bottom: bool = False,
bottom: bool = False,
renderer: Callable[[str], str] = render_js_default # "text/javascript"
) -> Resource:

return Resource(
Expand All @@ -27,6 +32,7 @@ def js(
depends=depends,
supersedes=supersedes,
bottom=bottom,
renderer=renderer
)


Expand Down Expand Up @@ -62,9 +68,13 @@ def get_default_profile_pic_data() -> tuple[str, bytes]:

jquery = js('jquery.min.js')
bootstrap_core = js('bootstrap.bundle.min.js')
bootstrap_js = js('bootstrap_custom.js', depends=[jquery, bootstrap_core])
bootstrap_js = js(
'bootstrap_custom.js',
depends=[jquery, bootstrap_core]
)

custom_js = js('custom/custom.js')
sortable_custom = js('custom/sortable_custom.js', depends=[jquery],
renderer=render_js_module)


tom_select_css = css('tom-select.min.css')
Expand Down
15 changes: 0 additions & 15 deletions src/privatim/static/js/custom/sortable.js

This file was deleted.

Loading

0 comments on commit 621c617

Please sign in to comment.