seats – Find for those who get a seat in a board

This plugin adds functionality for a voting and election system. Voting and elections are done inside a board that has been made for a group. Users can then vote for their friends that are in the group or become a delegate. The voting system is called “Continuous Cascaded Voting” where the voting power of a person is carried onto the person he voted for.

This page contains code snippets (lines starting with >>>), which are being tested during our development workflow. The following snippet initializes the demo project used throughout this page.

>>> from lino_seggi.projects.seggi1.startup import *

Database models

The Seats plugin defines the following database models: Board, Vote, Seat (and in the future probably Tenure).

Board (BabelDesignated, Modified)

  • Is there to define an entity where within that entity the members of it vote to determine the leaders of that board. For example Parliment, apartment association.

  • It is also linked to the group model in Groups plugin which is used to link up users into being members of different groups so that then each group can have it’s own board that they’d vote for.

Vote (UserAuthored, Modified)

  • A model for the votes people make. It’s there to link the voter and the vote receiver(delegate) and then for the system to count them up and check for bubbles and so on.

DEPRICATED: Seat (UserAuthored, Modified)

  • A model to store the members of a board who have gained enough votes to get a seat of the board.

  • It just expresses the mathematical right of getting a seat in this board. Doesn’t mean that the user actually takes the seat (which is expressed by a tenure).

Tenure (DateRange)

  • The fact that a given user takes a seat in a board.

  • It has a start date and end end date.

  • Lino should not allow to create more tenures than available (num_seats)

  • When there are less tenures than seats, Lino should send a notification to those who are next. Also a table PendingTenures for the site admin where they can see when a board needs a tenure to be taken.


The plugin also defines two classes Chain and Election. These are volatile objects (not stored in the database). An Election is instantiated from a Board instance.

Chain:

  • The chain is basically a linked list, where the head is the user who’s receiving the votes and the tail of the chain is another chain. That connects the heads voters to him.

  • The function inside the class as_lines(…) is a recursive function that is used to print out the chain of chains as text, so we can visualize easily the state of the current votes. This is good for testing.

  • How it works in short: The chain objects head is put into the printed set (to remember if he’s in the chain already). If he is already in the chain the function exits so we don’t get an infinite looping of the function. Then the chain object is checked for how many voters it has. for every voter it calls the function again with an indent, so we can see how it’s all nested, when it’s printed out.

  • The printing out is done by the __str__ function that joins up all the lines with linebreaks added for each chain.

Election:

  • The way I’d describe the Election class is that it’s like a system hub, very much resembling the definition of the word, it’s a process.

  • In short, Election class has 4 methods inside it that are used to make sure all the voting chains are connected together in a way that doesn’t create duplicates, making a directed graph. These chains are then used to detect bubbles or count up the votes for the users to see who ends up sitting on the seat.

  • Here are these four methods:

    • get_chain(self, uid):

      • Is there to get the chain objects and if one doesn’t exist in case of a new voter then it makes one and returns it.

    • collect_vote(self, vote):

      • Firstly it gets the chain object from get_chain() then it checks if the vote would create a bubble by seeing if the this voters delegate is already in the chain of who voted for him.

      • Then it merges the delegates chain of votes with the current users.

      • Then it loops over each chain part and adds them to the delegates voter_ids set. Which is used to measure with it’s length the amount of votes the delegate has gotten.

    • load(self):

      • Loops over all the votes, sends them through the collect_vote and then sorts them. (They’re sorted by their weight, that was defined in the Chain class.)

    • save(self):

      • It checks if the new votes were loaded, then it checks the vote weights again, if it calculates a new result it is saved.


Votes by board

This table shows all the votes for a given board in chronological order.

>>> board = seats.Board.objects.get(pk=1)
>>> board
<Board: Riigikogu>
>>> ar = rt.login('robin')
>>> ar.show(seats.VotesByBoard, board)
==== ======================================== ======================================== =========
 ID   Voter                                    Delegate                                 Weight
---- ---------------------------------------- ---------------------------------------- ---------
 1    Hans Altenberg (5)                       Annette Arens (4)                        1
 3    Andreas Arens (3)                        Charlotte Collard (8)                    1
 4    Annette Arens (4)                        Luc Faymonville (20)                     6
 5    Alfons Ausdemwald (6)                    Irene Ingels (25)                        2
 6    Laurent Bastiaensen (7)                  Jan Jousten (30)                         7
 7    Marc Chantraine (10)                     Marc Malmendier (36)                     19
 9    Ulrike Charlier (9)                      Alfons Ausdemwald (6)                    1
 10   Charlotte Collard (8)                    Laurent Bastiaensen (7)                  5
 11   Dorothée Demeulenaere (12)               Daniel Dericum (11)                      1
 12   Daniel Dericum (11)                      Gregory Groteclaes (22)                  2
 13   Dorothée Dobbelstein (14)                Guido Lambertz (32)                      2
 14   Dorothée Dobbelstein-Demeulenaere (13)   Marie-Louise Meier (39)                  2
 15   Edgar Engels (19)                        Marc Chantraine (10)                     18
 16   Berta Ernst (15)                         Dorothée Dobbelstein (14)                1
 18   Eberhart Evers (17)                      Bernd Evertz (16)                        1
 19   Bernd Evertz (16)                        Luc Faymonville (20)                     5
 20   Luc Faymonville (20)                     Irene Ingels (25)                        12
 22   Germaine Gernegroß (21)                  Michael Mießen (38)                      1
 23   Gregory Groteclaes (22)                  Charlotte Collard (8)                    3
 24   Henri Hilgers (24)                       Edgar Engels (19)                        17
 25   Hildegard Hilgers (23)                   Jacqueline Jacobs (27)                   21
 26   Irene Ingels (25)                        Karl Kaivers (31)                        15
 28   Jacqueline Jacobs (27)                   Melissa Meessen (37)                     22
 29   Jérémy Jansen (26)                       Rando Roosi (2)                          1
 30   Johann Johnen (28)                       Laurent Bastiaensen (7)                  1
 31   Jan Jousten (30)                         Daniel Dericum (11)
 33   Karl Kaivers (31)                        Henri Hilgers (24)                       16
 34   Guido Lambertz (32)                      Laura Laschet (33)                       3
 35   Laura Laschet (33)                       Annette Arens (4)                        4
 36   Line Lazarus (34)                        Dorothée Dobbelstein-Demeulenaere (13)   1
 37   Josefine Leffin (35)                     Bernd Evertz (16)                        3
 39   Marc Malmendier (36)                     Hildegard Hilgers (23)                   20
 40   Melissa Meessen (37)                     Josef Jonas (29)                         23
 41   Marie-Louise Meier (39)                  Line Lazarus (34)
 42   Michael Mießen (38)                      Josefine Leffin (35)                     2
 43   Daniel Emonts (18)
 44   Josef Jonas (29)
                                                                                        **239**
==== ======================================== ======================================== =========

Votes by user

User Hans Altenberg is member in both groups. That’s why he has two votes to fill.

>>> hans = users.User.objects.get(username='hans.altenberg')
>>> ar = rt.login(hans.username)
>>> ar.show(seats.MyVotes)
==================== ======================= ========
 Board                Delegate                Weight
-------------------- ----------------------- --------
 Riigikogu            Annette Arens (4)       1
 Juhatus              Alfons Ausdemwald (6)   1
 **Total (2 rows)**                           **2**
==================== ======================= ========
>>> rt.show(seats.VotesByUser, hans)
==== =========== ======================= ========
 ID   Board       Delegate                Weight
---- ----------- ----------------------- --------
 1    Riigikogu   Annette Arens (4)       1
 2    Juhatus     Alfons Ausdemwald (6)   1
                                          **2**
==== =========== ======================= ========
>>> user = users.User.objects.get(username='robin')
>>> rt.show(seats.VotesByUser, user)
No data to display

Parliament elections

>>> obj = seats.Board.objects.get(id=1)
>>> obj
<Board: Riigikogu>
>>> rt.show(seats.VotesByBoard, obj)
==== ======================================== ======================================== =========
 ID   Voter                                    Delegate                                 Weight
---- ---------------------------------------- ---------------------------------------- ---------
 1    Hans Altenberg (5)                       Annette Arens (4)                        1
 3    Andreas Arens (3)                        Charlotte Collard (8)                    1
 4    Annette Arens (4)                        Luc Faymonville (20)                     6
 5    Alfons Ausdemwald (6)                    Irene Ingels (25)                        2
 6    Laurent Bastiaensen (7)                  Jan Jousten (30)                         7
 7    Marc Chantraine (10)                     Marc Malmendier (36)                     19
 9    Ulrike Charlier (9)                      Alfons Ausdemwald (6)                    1
 10   Charlotte Collard (8)                    Laurent Bastiaensen (7)                  5
 11   Dorothée Demeulenaere (12)               Daniel Dericum (11)                      1
 12   Daniel Dericum (11)                      Gregory Groteclaes (22)                  2
 13   Dorothée Dobbelstein (14)                Guido Lambertz (32)                      2
 14   Dorothée Dobbelstein-Demeulenaere (13)   Marie-Louise Meier (39)                  2
 15   Edgar Engels (19)                        Marc Chantraine (10)                     18
 16   Berta Ernst (15)                         Dorothée Dobbelstein (14)                1
 18   Eberhart Evers (17)                      Bernd Evertz (16)                        1
 19   Bernd Evertz (16)                        Luc Faymonville (20)                     5
 20   Luc Faymonville (20)                     Irene Ingels (25)                        12
 22   Germaine Gernegroß (21)                  Michael Mießen (38)                      1
 23   Gregory Groteclaes (22)                  Charlotte Collard (8)                    3
 24   Henri Hilgers (24)                       Edgar Engels (19)                        17
 25   Hildegard Hilgers (23)                   Jacqueline Jacobs (27)                   21
 26   Irene Ingels (25)                        Karl Kaivers (31)                        15
 28   Jacqueline Jacobs (27)                   Melissa Meessen (37)                     22
 29   Jérémy Jansen (26)                       Rando Roosi (2)                          1
 30   Johann Johnen (28)                       Laurent Bastiaensen (7)                  1
 31   Jan Jousten (30)                         Daniel Dericum (11)
 33   Karl Kaivers (31)                        Henri Hilgers (24)                       16
 34   Guido Lambertz (32)                      Laura Laschet (33)                       3
 35   Laura Laschet (33)                       Annette Arens (4)                        4
 36   Line Lazarus (34)                        Dorothée Dobbelstein-Demeulenaere (13)   1
 37   Josefine Leffin (35)                     Bernd Evertz (16)                        3
 39   Marc Malmendier (36)                     Hildegard Hilgers (23)                   20
 40   Melissa Meessen (37)                     Josef Jonas (29)                         23
 41   Marie-Louise Meier (39)                  Line Lazarus (34)
 42   Michael Mießen (38)                      Josefine Leffin (35)                     2
 43   Daniel Emonts (18)
 44   Josef Jonas (29)
                                                                                        **239**
==== ======================================== ======================================== =========
../_images/1.png

Every vote has weight of at least one voice, except vote #32 (29 votes for 8), which is invalid because it would cause a circular reference (“vote bubble”): when 29 tries to vote for 8, 8 votes already for 7 who votes for 29.

>>> rt.show(seats.TenuresByBoard, obj)
========================= ========= ============ ========== ================== ================
 Delegate                  weight    Start date   End date   Deprecated since   Resigned since
------------------------- --------- ------------ ---------- ------------------ ----------------
 Josef Jonas (29)          24
 Melissa Meessen (37)      23
 Jacqueline Jacobs (27)    22
 Hildegard Hilgers (23)    21
 Marc Malmendier (36)      20
 Marc Chantraine (10)      19        09/07/2025
 Edgar Engels (19)         18        15/02/2025
 Josefine Leffin (35)                19/09/2025              29/09/2025
 Jan Jousten (30)                    28/04/2025              29/09/2025
 Laurent Bastiaensen (7)             05/12/2024              29/09/2025
 Henri Hilgers (24)                  24/09/2024              29/09/2025
 Karl Kaivers (31)                   14/07/2024              29/09/2025
 **Total (12 rows)**       **147**
========================= ========= ============ ========== ================== ================

Elections for Juhatus

>>> obj = seats.Board.objects.get(id=2)
>>> obj
<Board: Juhatus>
../_images/2.png
>>> rt.show(seats.VotesByBoard, obj)
==== ====================== ======================================== ========
 ID   Voter                  Delegate                                 Weight
---- ---------------------- ---------------------------------------- --------
 2    Hans Altenberg (5)     Alfons Ausdemwald (6)                    1
 8    Marc Chantraine (10)   Marc Malmendier (36)                     1
 17   Berta Ernst (15)       Dorothée Dobbelstein-Demeulenaere (13)   1
 21   Luc Faymonville (20)   Irene Ingels (25)                        2
 27   Irene Ingels (25)      Guido Lambertz (32)                      3
 32   Jan Jousten (30)       Dorothée Dobbelstein (14)                1
 38   Josefine Leffin (35)   Luc Faymonville (20)                     1
                                                                      **10**
==== ====================== ======================================== ========
>>> rt.show(seats.TenuresByBoard, obj)
=========================== ======== ============ ========== ================== ================
 Delegate                    weight   Start date   End date   Deprecated since   Resigned since
--------------------------- -------- ------------ ---------- ------------------ ----------------
 Guido Lambertz (32)         4        11/12/2023
 Irene Ingels (25)           3
 Dorothée Dobbelstein (14)   2        21/02/2024
 Josef Jonas (29)                     03/05/2024              29/09/2025
 **Total (4 rows)**          **9**
=========================== ======== ============ ========== ================== ================

Don’t read this

20261112

The following snippets verify #7100 (Delegate chooser works from detail but not from grid view)

>>> from lino.core.layouthandle import get_layout_handle
>>> from lino.core.utils import get_choosers_dict
>>> from lino.core.utils import get_choosers_for
>>> a = seats.MyVotes
>>> get_choosers_dict(a)
{}
>>> get_choosers_dict(a.model)
{'delegate': Chooser(Vote.delegate, ('user',))}
>>> get_choosers_for(a)
{'delegate': Chooser(Vote.delegate, ('user',))}
>>> get_choosers_dict(a)
{}
>>> ah = a.get_handle()
>>> ah
<lino.ui.actor.ActorHandle object at ...>
>>> lh = ah.get_grid_layout()
>>> lh.layout
<lino.core.layouts.ColumnsLayout object at ...>
>>> lh.main
<GridElement main in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>
>>> pprint(lh.main.columns)
(<ForeignKeyElement board in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>,
 <ForeignKeyElement delegate in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>,
 <IntegerFieldElement weight in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>,
 <ForeignKeyElement user in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>)
>>> pprint(lh._names)
{'board': <ForeignKeyElement board in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>,
 'delegate': <ForeignKeyElement delegate in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>,
 'main': <GridElement main in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>,
 'user': <ForeignKeyElement user in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>,
 'weight': <IntegerFieldElement weight in lino.core.layouts.ColumnsLayout on lino_seggi.lib.seats.ui.MyVotes>}
>>> sorted(lh.hidden_elements)
['user']