···11-from typing import TYPE_CHECKING, Optional
22-33-from sqlalchemy import Column, String
44-from sqlmodel import Field, Relationship, SQLModel
55-66-if TYPE_CHECKING:
77- from kefi.models.kudos.models import Action
88- from kefi.models.plazas.models import Attendance
99-1010-1111-class User(SQLModel, table=True):
1212- """A slack user."""
1313-1414- id: int | None = Field(default=None, primary_key=True)
1515- first_name: str | None = ""
1616- last_name: str | None = ""
1717- slack_user_id: str = Field(sa_column=Column(String(100), unique=True, index=True))
1818- slack_username: str | None = ""
1919- transactions: list["Transaction"] = Relationship(
2020- back_populates="user",
2121- sa_relationship_kwargs=dict(foreign_keys="[Transaction.user_id]"),
2222- )
2323- transactions_sent: list["Transaction"] = Relationship(
2424- back_populates="sender",
2525- sa_relationship_kwargs=dict(foreign_keys="[Transaction.sender_id]"),
2626- )
2727- transactions_received: list["Transaction"] = Relationship(
2828- back_populates="receiver",
2929- sa_relationship_kwargs=dict(foreign_keys="[Transaction.receiver_id]"),
3030- )
3131- is_admin: bool = False
3232- attendances: list["Attendance"] = Relationship(back_populates="user")
3333-3434- def get_short_name(self):
3535- """Gets the short name of the user."""
3636- return self.first_name or self.slack_username
3737-3838-3939-class Transaction(SQLModel, table=True):
4040- """A transaction is a kefi movement, form the system to a user or from a user to
4141- another.
4242- """
4343-4444- id: int | None = Field(default=None, primary_key=True)
4545- amount: int
4646- user_id: int = Field(foreign_key="user.id")
4747- user: User = Relationship(
4848- back_populates="transactions",
4949- sa_relationship_kwargs=dict(foreign_keys="[Transaction.user_id]"),
5050- )
5151- message: str | None = Field(default=None)
5252-5353- # Action, the reference of the action used to send the transaction
5454- action_id: int | None = Field(default=None, foreign_key="action.id")
5555- action: Optional["Action"] = Relationship(back_populates="transactions")
5656-5757- # Attendance, the reference of the attendance used to send the transaction
5858- attendance_id: int | None = Field(default=None, foreign_key="attendance.id")
5959- attendance: Optional["Attendance"] = Relationship(back_populates="transactions")
6060-6161- # Sender user, if not defined, is a system transaction
6262- sender_id: int | None = Field(default=None, foreign_key="user.id")
6363- sender: User | None = Relationship(
6464- back_populates="transactions_sent",
6565- sa_relationship_kwargs=dict(foreign_keys="[Transaction.sender_id]"),
6666- )
6767-6868- # Receiver user
6969- receiver_id: int | None = Field(default=None, foreign_key="user.id")
7070- receiver: User | None = Relationship(
7171- back_populates="transactions_received",
7272- sa_relationship_kwargs=dict(foreign_keys="[Transaction.receiver_id]"),
7373- )
+121-1
kefi/models/database.py
···11-from sqlmodel import create_engine
11+import datetime
22+from typing import Optional
33+44+from sqlalchemy import Column, String
55+from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint, create_engine
2637from kefi.config import settings
4859# Creates the engine from the database url, to allow to create a session with
610# the database
711engine = create_engine(settings.db_url())
1212+1313+1414+class User(SQLModel, table=True):
1515+ """A slack user."""
1616+1717+ id: int | None = Field(default=None, primary_key=True)
1818+ first_name: str | None = ""
1919+ last_name: str | None = ""
2020+ slack_user_id: str = Field(sa_column=Column(String(100), unique=True, index=True))
2121+ slack_username: str | None = ""
2222+ transactions: list["Transaction"] = Relationship(
2323+ back_populates="user",
2424+ sa_relationship_kwargs=dict(foreign_keys="[Transaction.user_id]"),
2525+ )
2626+ transactions_sent: list["Transaction"] = Relationship(
2727+ back_populates="sender",
2828+ sa_relationship_kwargs=dict(foreign_keys="[Transaction.sender_id]"),
2929+ )
3030+ transactions_received: list["Transaction"] = Relationship(
3131+ back_populates="receiver",
3232+ sa_relationship_kwargs=dict(foreign_keys="[Transaction.receiver_id]"),
3333+ )
3434+ is_admin: bool = False
3535+ attendances: list["Attendance"] = Relationship(back_populates="user")
3636+3737+ def get_short_name(self):
3838+ """Gets the short name of the user."""
3939+ return self.first_name or self.slack_username
4040+4141+4242+class Transaction(SQLModel, table=True):
4343+ """A transaction is a kefi movement, form the system to a user or from a user to
4444+ another.
4545+ """
4646+4747+ id: int | None = Field(default=None, primary_key=True)
4848+ amount: int
4949+ user_id: int = Field(foreign_key="user.id")
5050+ user: User = Relationship(
5151+ back_populates="transactions",
5252+ sa_relationship_kwargs=dict(foreign_keys="[Transaction.user_id]"),
5353+ )
5454+ message: str | None = Field(default=None)
5555+5656+ # Action, the reference of the action used to send the transaction
5757+ action_id: int | None = Field(default=None, foreign_key="action.id")
5858+ action: Optional["Action"] = Relationship(back_populates="transactions")
5959+6060+ # Attendance, the reference of the attendance used to send the transaction
6161+ attendance_id: int | None = Field(default=None, foreign_key="attendance.id")
6262+ attendance: Optional["Attendance"] = Relationship(back_populates="transactions")
6363+6464+ # Sender user, if not defined, is a system transaction
6565+ sender_id: int | None = Field(default=None, foreign_key="user.id")
6666+ sender: User | None = Relationship(
6767+ back_populates="transactions_sent",
6868+ sa_relationship_kwargs=dict(foreign_keys="[Transaction.sender_id]"),
6969+ )
7070+7171+ # Receiver user
7272+ receiver_id: int | None = Field(default=None, foreign_key="user.id")
7373+ receiver: User | None = Relationship(
7474+ back_populates="transactions_received",
7575+ sa_relationship_kwargs=dict(foreign_keys="[Transaction.receiver_id]"),
7676+ )
7777+7878+7979+class Action(SQLModel, table=True):
8080+ """Each action a user can perform to give kefis to another user."""
8181+8282+ id: int | None = Field(default=None, primary_key=True)
8383+ keyword: str = Field(sa_column=Column(String(100), unique=True, index=True))
8484+ amount: int
8585+ transactions: list["Transaction"] = Relationship(back_populates="action")
8686+ # Responses data
8787+ header_template: str = ""
8888+ message_template: str = ""
8989+ context_template: str = ""
9090+ image: str | None
9191+ text: str | None
9292+9393+9494+class Plaza(SQLModel, table=True):
9595+ """A plaza is a meeting session that represents a moment scheduled to create a
9696+ group call with the different groups of attendees.
9797+ """
9898+9999+ id: int | None = Field(default=None, primary_key=True)
100100+ date: datetime.datetime = Field(
101101+ unique=True, index=True
102102+ ) # When the meetings are going to be created
103103+ attendances: list["Attendance"] = Relationship(back_populates="plaza")
104104+105105+106106+class Attendance(SQLModel, table=True):
107107+ """An attendance is a user who has spent kefis to be part of a meetings session in
108108+ the plaza.
109109+ """
110110+111111+ __table_args__ = (
112112+ UniqueConstraint("plaza_id", "user_id", name="plaza_id_user_id_unique"),
113113+ )
114114+ id: int | None = Field(default=None, primary_key=True)
115115+ # Meeting session that the attendance makes reference
116116+ plaza_id: int = Field(foreign_key="plaza.id")
117117+ plaza: Plaza = Relationship(
118118+ back_populates="attendances",
119119+ sa_relationship_kwargs=dict(foreign_keys="[Attendance.plaza_id]"),
120120+ )
121121+ # User who is going to attend to the meeting session
122122+ user_id: int = Field(foreign_key="user.id")
123123+ user: User = Relationship(
124124+ back_populates="attendances",
125125+ sa_relationship_kwargs=dict(foreign_keys="[Attendance.user_id]"),
126126+ )
127127+ transactions: list["Transaction"] = Relationship(back_populates="attendance")
···11-from typing import TYPE_CHECKING
22-33-from sqlalchemy import Column, String
44-from sqlmodel import Field, Relationship, SQLModel
55-66-if TYPE_CHECKING:
77- from kefi.models.core.models import Transaction
88-99-1010-class Action(SQLModel, table=True):
1111- """Each action a user can perform to give kefis to another user."""
1212-1313- id: int | None = Field(default=None, primary_key=True)
1414- keyword: str = Field(sa_column=Column(String(100), unique=True, index=True))
1515- amount: int
1616- transactions: list["Transaction"] = Relationship(back_populates="action")
1717- # Responses data
1818- header_template: str = ""
1919- message_template: str = ""
2020- context_template: str = ""
2121- image: str | None
2222- text: str | None
+2-3
kefi/models/plazas/helpers.py
···77from kefi.config import settings
88from kefi.models.core.exceptions import NotEnoughKefi
99from kefi.models.core.helpers import available_balance
1010-from kefi.models.core.models import Transaction, User
1010+from kefi.models.database import Attendance, Plaza, Transaction, User
1111from kefi.models.plazas.exceptions import AlreadyAttending, NotAttending
1212-from kefi.models.plazas.models import Attendance, Plaza
131214131514def generate_random_meet() -> str:
···8382 attendance = attendance_results.first()
8483 if not attendance:
8584 raise NotAttending("The user is not in the plaza")
8686- session.delete(attendance)
8785 transaction_query = select(Transaction).filter(
8886 Transaction.attendance == attendance, Transaction.user == user
8987 )
9088 transaction_results = session.exec(transaction_query)
9189 transaction = transaction_results.first()
9290 session.delete(transaction)
9191+ session.delete(attendance)
-46
kefi/models/plazas/models.py
···11-import datetime
22-from typing import TYPE_CHECKING
33-44-from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint
55-66-from kefi.models.core.models import User
77-88-if TYPE_CHECKING:
99-1010- from kefi.models.core.models import Transaction
1111-1212-1313-class Plaza(SQLModel, table=True):
1414- """A plaza is a meeting session that represents a moment scheduled to create a
1515- group call with the different groups of attendees.
1616- """
1717-1818- id: int | None = Field(default=None, primary_key=True)
1919- date: datetime.datetime = Field(
2020- unique=True, index=True
2121- ) # When the meetings are going to be created
2222- attendances: list["Attendance"] = Relationship(back_populates="plaza")
2323-2424-2525-class Attendance(SQLModel, table=True):
2626- """An attendance is a user who has spent kefis to be part of a meetings session in
2727- the plaza.
2828- """
2929-3030- __table_args__ = (
3131- UniqueConstraint("plaza_id", "user_id", name="plaza_id_user_id_unique"),
3232- )
3333- id: int | None = Field(default=None, primary_key=True)
3434- # Meeting session that the attendance makes reference
3535- plaza_id: int = Field(foreign_key="plaza.id")
3636- plaza: Plaza = Relationship(
3737- back_populates="attendances",
3838- sa_relationship_kwargs=dict(foreign_keys="[Attendance.plaza_id]"),
3939- )
4040- # User who is going to attend to the meeting session
4141- user_id: int = Field(foreign_key="user.id")
4242- user: User = Relationship(
4343- back_populates="attendances",
4444- sa_relationship_kwargs=dict(foreign_keys="[Attendance.user_id]"),
4545- )
4646- transactions: list["Transaction"] = Relationship(back_populates="attendance")