Delete app
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- app/__init__.py +0 -0
- app/__pycache__/__init__.cpython-311.pyc +0 -0
- app/__pycache__/__init__.cpython-312.pyc +0 -0
- app/__pycache__/main.cpython-311.pyc +0 -0
- app/__pycache__/main.cpython-312.pyc +0 -0
- app/alembic.ini +0 -117
- app/alembic/README +0 -1
- app/alembic/__pycache__/env.cpython-312.pyc +0 -0
- app/alembic/env.py +0 -89
- app/alembic/script.py.mako +0 -27
- app/alembic/versions/2d970db44571_.py +0 -33
- app/alembic/versions/4a25da80a496_initial_migration.py +0 -70
- app/alembic/versions/4c9cd9554f1c_timezone_utc.py +0 -61
- app/alembic/versions/__pycache__/2d970db44571_.cpython-312.pyc +0 -0
- app/alembic/versions/__pycache__/4a25da80a496_initial_migration.cpython-312.pyc +0 -0
- app/alembic/versions/__pycache__/4c9cd9554f1c_timezone_utc.cpython-312.pyc +0 -0
- app/api/__init__.py +0 -0
- app/api/__pycache__/__init__.cpython-312.pyc +0 -0
- app/api/__pycache__/deps.cpython-312.pyc +0 -0
- app/api/__pycache__/main.cpython-312.pyc +0 -0
- app/api/__pycache__/poll.cpython-312.pyc +0 -0
- app/api/__pycache__/vote.cpython-312.pyc +0 -0
- app/api/deps.py +0 -60
- app/api/main.py +0 -7
- app/api/routes/__init__.py +0 -0
- app/api/routes/__pycache__/__init__.cpython-312.pyc +0 -0
- app/api/routes/__pycache__/auth.cpython-312.pyc +0 -0
- app/api/routes/__pycache__/poll.cpython-312.pyc +0 -0
- app/api/routes/__pycache__/user.cpython-312.pyc +0 -0
- app/api/routes/__pycache__/vote.cpython-312.pyc +0 -0
- app/api/routes/poll.py +0 -429
- app/api/routes/user.py +0 -11
- app/api/routes/vote.py +0 -56
- app/core/__init__.py +0 -0
- app/core/__pycache__/__init__.cpython-312.pyc +0 -0
- app/core/__pycache__/config.cpython-312.pyc +0 -0
- app/core/__pycache__/db.cpython-312.pyc +0 -0
- app/core/__pycache__/security.cpython-312.pyc +0 -0
- app/core/config.py +0 -46
- app/core/db.py +0 -14
- app/core/security.py +0 -8
- app/main.py +0 -22
- app/models/__init__.py +0 -16
- app/models/__pycache__/__init__.cpython-312.pyc +0 -0
- app/models/__pycache__/common.cpython-312.pyc +0 -0
- app/models/__pycache__/poll.cpython-312.pyc +0 -0
- app/models/__pycache__/vote.cpython-312.pyc +0 -0
- app/models/common.py +0 -12
- app/models/poll.py +0 -110
- app/models/vote.py +0 -23
app/__init__.py
DELETED
|
File without changes
|
app/__pycache__/__init__.cpython-311.pyc
DELETED
|
Binary file (172 Bytes)
|
|
|
app/__pycache__/__init__.cpython-312.pyc
DELETED
|
Binary file (160 Bytes)
|
|
|
app/__pycache__/main.cpython-311.pyc
DELETED
|
Binary file (530 Bytes)
|
|
|
app/__pycache__/main.cpython-312.pyc
DELETED
|
Binary file (1.03 kB)
|
|
|
app/alembic.ini
DELETED
|
@@ -1,117 +0,0 @@
|
|
| 1 |
-
# A generic, single database configuration.
|
| 2 |
-
|
| 3 |
-
[alembic]
|
| 4 |
-
# path to migration scripts
|
| 5 |
-
# Use forward slashes (/) also on windows to provide an os agnostic path
|
| 6 |
-
script_location = alembic
|
| 7 |
-
|
| 8 |
-
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
| 9 |
-
# Uncomment the line below if you want the files to be prepended with date and time
|
| 10 |
-
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
|
| 11 |
-
# for all available tokens
|
| 12 |
-
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
| 13 |
-
|
| 14 |
-
# sys.path path, will be prepended to sys.path if present.
|
| 15 |
-
# defaults to the current working directory.
|
| 16 |
-
prepend_sys_path = .
|
| 17 |
-
|
| 18 |
-
# timezone to use when rendering the date within the migration file
|
| 19 |
-
# as well as the filename.
|
| 20 |
-
# If specified, requires the python>=3.9 or backports.zoneinfo library.
|
| 21 |
-
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
|
| 22 |
-
# string value is passed to ZoneInfo()
|
| 23 |
-
# leave blank for localtime
|
| 24 |
-
# timezone =
|
| 25 |
-
|
| 26 |
-
# max length of characters to apply to the "slug" field
|
| 27 |
-
# truncate_slug_length = 40
|
| 28 |
-
|
| 29 |
-
# set to 'true' to run the environment during
|
| 30 |
-
# the 'revision' command, regardless of autogenerate
|
| 31 |
-
# revision_environment = false
|
| 32 |
-
|
| 33 |
-
# set to 'true' to allow .pyc and .pyo files without
|
| 34 |
-
# a source .py file to be detected as revisions in the
|
| 35 |
-
# versions/ directory
|
| 36 |
-
# sourceless = false
|
| 37 |
-
|
| 38 |
-
# version location specification; This defaults
|
| 39 |
-
# to alembic/versions. When using multiple version
|
| 40 |
-
# directories, initial revisions must be specified with --version-path.
|
| 41 |
-
# The path separator used here should be the separator specified by "version_path_separator" below.
|
| 42 |
-
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
|
| 43 |
-
|
| 44 |
-
# version path separator; As mentioned above, this is the character used to split
|
| 45 |
-
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
|
| 46 |
-
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
|
| 47 |
-
# Valid values for version_path_separator are:
|
| 48 |
-
#
|
| 49 |
-
# version_path_separator = :
|
| 50 |
-
# version_path_separator = ;
|
| 51 |
-
# version_path_separator = space
|
| 52 |
-
# version_path_separator = newline
|
| 53 |
-
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
|
| 54 |
-
|
| 55 |
-
# set to 'true' to search source files recursively
|
| 56 |
-
# in each "version_locations" directory
|
| 57 |
-
# new in Alembic version 1.10
|
| 58 |
-
# recursive_version_locations = false
|
| 59 |
-
|
| 60 |
-
# the output encoding used when revision files
|
| 61 |
-
# are written from script.py.mako
|
| 62 |
-
# output_encoding = utf-8
|
| 63 |
-
|
| 64 |
-
sqlalchemy.url = driver://user:pass@localhost/dbname
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
[post_write_hooks]
|
| 68 |
-
# post_write_hooks defines scripts or Python functions that are run
|
| 69 |
-
# on newly generated revision scripts. See the documentation for further
|
| 70 |
-
# detail and examples
|
| 71 |
-
|
| 72 |
-
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
| 73 |
-
# hooks = black
|
| 74 |
-
# black.type = console_scripts
|
| 75 |
-
# black.entrypoint = black
|
| 76 |
-
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
| 77 |
-
|
| 78 |
-
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
|
| 79 |
-
# hooks = ruff
|
| 80 |
-
# ruff.type = exec
|
| 81 |
-
# ruff.executable = %(here)s/.venv/bin/ruff
|
| 82 |
-
# ruff.options = --fix REVISION_SCRIPT_FILENAME
|
| 83 |
-
|
| 84 |
-
# Logging configuration
|
| 85 |
-
[loggers]
|
| 86 |
-
keys = root,sqlalchemy,alembic
|
| 87 |
-
|
| 88 |
-
[handlers]
|
| 89 |
-
keys = console
|
| 90 |
-
|
| 91 |
-
[formatters]
|
| 92 |
-
keys = generic
|
| 93 |
-
|
| 94 |
-
[logger_root]
|
| 95 |
-
level = WARN
|
| 96 |
-
handlers = console
|
| 97 |
-
qualname =
|
| 98 |
-
|
| 99 |
-
[logger_sqlalchemy]
|
| 100 |
-
level = WARN
|
| 101 |
-
handlers =
|
| 102 |
-
qualname = sqlalchemy.engine
|
| 103 |
-
|
| 104 |
-
[logger_alembic]
|
| 105 |
-
level = INFO
|
| 106 |
-
handlers =
|
| 107 |
-
qualname = alembic
|
| 108 |
-
|
| 109 |
-
[handler_console]
|
| 110 |
-
class = StreamHandler
|
| 111 |
-
args = (sys.stderr,)
|
| 112 |
-
level = NOTSET
|
| 113 |
-
formatter = generic
|
| 114 |
-
|
| 115 |
-
[formatter_generic]
|
| 116 |
-
format = %(levelname)-5.5s [%(name)s] %(message)s
|
| 117 |
-
datefmt = %H:%M:%S
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/alembic/README
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
Generic single-database configuration.
|
|
|
|
|
|
app/alembic/__pycache__/env.cpython-312.pyc
DELETED
|
Binary file (2.94 kB)
|
|
|
app/alembic/env.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from logging.config import fileConfig
|
| 3 |
-
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
from sqlalchemy import engine_from_config
|
| 7 |
-
from sqlalchemy import pool
|
| 8 |
-
from sqlmodel import SQLModel
|
| 9 |
-
|
| 10 |
-
from alembic import context
|
| 11 |
-
|
| 12 |
-
from models import Poll, PollOption, Vote, RollRange
|
| 13 |
-
|
| 14 |
-
load_dotenv()
|
| 15 |
-
# this is the Alembic Config object, which provides
|
| 16 |
-
# access to the values within the .ini file in use.
|
| 17 |
-
config = context.config
|
| 18 |
-
config.set_main_option(
|
| 19 |
-
"sqlalchemy.url",
|
| 20 |
-
f"{os.environ['DATABASE_URL']}",
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
# Interpret the config file for Python logging.
|
| 24 |
-
# This line sets up loggers basically.
|
| 25 |
-
if config.config_file_name is not None:
|
| 26 |
-
fileConfig(config.config_file_name)
|
| 27 |
-
|
| 28 |
-
# add your model's MetaData object here
|
| 29 |
-
# for 'autogenerate' support
|
| 30 |
-
# from myapp import mymodel
|
| 31 |
-
# target_metadata = mymodel.Base.metadata
|
| 32 |
-
target_metadata = SQLModel.metadata
|
| 33 |
-
|
| 34 |
-
# other values from the config, defined by the needs of env.py,
|
| 35 |
-
# can be acquired:
|
| 36 |
-
# my_important_option = config.get_main_option("my_important_option")
|
| 37 |
-
# ... etc.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def run_migrations_offline() -> None:
|
| 41 |
-
"""Run migrations in 'offline' mode.
|
| 42 |
-
|
| 43 |
-
This configures the context with just a URL
|
| 44 |
-
and not an Engine, though an Engine is acceptable
|
| 45 |
-
here as well. By skipping the Engine creation
|
| 46 |
-
we don't even need a DBAPI to be available.
|
| 47 |
-
|
| 48 |
-
Calls to context.execute() here emit the given string to the
|
| 49 |
-
script output.
|
| 50 |
-
|
| 51 |
-
"""
|
| 52 |
-
url = config.get_main_option("sqlalchemy.url")
|
| 53 |
-
context.configure(
|
| 54 |
-
url=url,
|
| 55 |
-
target_metadata=target_metadata,
|
| 56 |
-
literal_binds=True,
|
| 57 |
-
dialect_opts={"paramstyle": "named"},
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
with context.begin_transaction():
|
| 61 |
-
context.run_migrations()
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
def run_migrations_online() -> None:
|
| 65 |
-
"""Run migrations in 'online' mode.
|
| 66 |
-
|
| 67 |
-
In this scenario we need to create an Engine
|
| 68 |
-
and associate a connection with the context.
|
| 69 |
-
|
| 70 |
-
"""
|
| 71 |
-
connectable = engine_from_config(
|
| 72 |
-
config.get_section(config.config_ini_section, {}),
|
| 73 |
-
prefix="sqlalchemy.",
|
| 74 |
-
poolclass=pool.NullPool,
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
with connectable.connect() as connection:
|
| 78 |
-
context.configure(
|
| 79 |
-
connection=connection, target_metadata=target_metadata
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
with context.begin_transaction():
|
| 83 |
-
context.run_migrations()
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
if context.is_offline_mode():
|
| 87 |
-
run_migrations_offline()
|
| 88 |
-
else:
|
| 89 |
-
run_migrations_online()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/alembic/script.py.mako
DELETED
|
@@ -1,27 +0,0 @@
|
|
| 1 |
-
"""${message}
|
| 2 |
-
|
| 3 |
-
Revision ID: ${up_revision}
|
| 4 |
-
Revises: ${down_revision | comma,n}
|
| 5 |
-
Create Date: ${create_date}
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
from typing import Sequence, Union
|
| 9 |
-
|
| 10 |
-
from alembic import op
|
| 11 |
-
import sqlalchemy as sa
|
| 12 |
-
import sqlmodel
|
| 13 |
-
${imports if imports else ""}
|
| 14 |
-
|
| 15 |
-
# revision identifiers, used by Alembic.
|
| 16 |
-
revision: str = ${repr(up_revision)}
|
| 17 |
-
down_revision: Union[str, None] = ${repr(down_revision)}
|
| 18 |
-
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
| 19 |
-
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def upgrade() -> None:
|
| 23 |
-
${upgrades if upgrades else "pass"}
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def downgrade() -> None:
|
| 27 |
-
${downgrades if downgrades else "pass"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/alembic/versions/2d970db44571_.py
DELETED
|
@@ -1,33 +0,0 @@
|
|
| 1 |
-
"""empty message
|
| 2 |
-
|
| 3 |
-
Revision ID: 2d970db44571
|
| 4 |
-
Revises: 4a25da80a496
|
| 5 |
-
Create Date: 2024-10-11 22:59:38.796430
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
from typing import Sequence, Union
|
| 9 |
-
|
| 10 |
-
from alembic import op
|
| 11 |
-
import sqlalchemy as sa
|
| 12 |
-
import sqlmodel
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# revision identifiers, used by Alembic.
|
| 16 |
-
revision: str = '2d970db44571'
|
| 17 |
-
down_revision: Union[str, None] = '4a25da80a496'
|
| 18 |
-
branch_labels: Union[str, Sequence[str], None] = None
|
| 19 |
-
depends_on: Union[str, Sequence[str], None] = None
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def upgrade() -> None:
|
| 23 |
-
# ### commands auto generated by Alembic - please adjust! ###
|
| 24 |
-
op.drop_column('polloption', 'total_votes')
|
| 25 |
-
op.create_index(op.f('ix_vote_option_id'), 'vote', ['option_id'], unique=False)
|
| 26 |
-
# ### end Alembic commands ###
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def downgrade() -> None:
|
| 30 |
-
# ### commands auto generated by Alembic - please adjust! ###
|
| 31 |
-
op.drop_index(op.f('ix_vote_option_id'), table_name='vote')
|
| 32 |
-
op.add_column('polloption', sa.Column('total_votes', sa.INTEGER(), autoincrement=False, nullable=False))
|
| 33 |
-
# ### end Alembic commands ###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/alembic/versions/4a25da80a496_initial_migration.py
DELETED
|
@@ -1,70 +0,0 @@
|
|
| 1 |
-
"""initial migration
|
| 2 |
-
|
| 3 |
-
Revision ID: 4a25da80a496
|
| 4 |
-
Revises:
|
| 5 |
-
Create Date: 2024-10-11 16:31:22.565721
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
from typing import Sequence, Union
|
| 9 |
-
|
| 10 |
-
from alembic import op
|
| 11 |
-
import sqlalchemy as sa
|
| 12 |
-
import sqlmodel
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# revision identifiers, used by Alembic.
|
| 16 |
-
revision: str = '4a25da80a496'
|
| 17 |
-
down_revision: Union[str, None] = None
|
| 18 |
-
branch_labels: Union[str, Sequence[str], None] = None
|
| 19 |
-
depends_on: Union[str, Sequence[str], None] = None
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def upgrade() -> None:
|
| 23 |
-
# ### commands auto generated by Alembic - please adjust! ###
|
| 24 |
-
op.create_table('poll',
|
| 25 |
-
sa.Column('id', sa.Uuid(), nullable=False),
|
| 26 |
-
sa.Column('title', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
|
| 27 |
-
sa.Column('description', sqlmodel.sql.sqltypes.AutoString(length=1024), nullable=False),
|
| 28 |
-
sa.Column('is_private', sa.Boolean(), nullable=False),
|
| 29 |
-
sa.Column('created_at', sa.DateTime(), nullable=False),
|
| 30 |
-
sa.Column('creator_email', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
|
| 31 |
-
sa.Column('start_time', sa.DateTime(), nullable=False),
|
| 32 |
-
sa.Column('end_time', sa.DateTime(), nullable=False),
|
| 33 |
-
sa.PrimaryKeyConstraint('id')
|
| 34 |
-
)
|
| 35 |
-
op.create_table('polloption',
|
| 36 |
-
sa.Column('id', sa.Uuid(), nullable=False),
|
| 37 |
-
sa.Column('poll_id', sa.Uuid(), nullable=False),
|
| 38 |
-
sa.Column('option_text', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
|
| 39 |
-
sa.Column('total_votes', sa.Integer(), nullable=False),
|
| 40 |
-
sa.ForeignKeyConstraint(['poll_id'], ['poll.id'], ondelete='CASCADE'),
|
| 41 |
-
sa.PrimaryKeyConstraint('id')
|
| 42 |
-
)
|
| 43 |
-
op.create_table('rollrange',
|
| 44 |
-
sa.Column('id', sa.Uuid(), nullable=False),
|
| 45 |
-
sa.Column('start', sa.Integer(), nullable=False),
|
| 46 |
-
sa.Column('end', sa.Integer(), nullable=False),
|
| 47 |
-
sa.Column('poll_id', sa.Uuid(), nullable=False),
|
| 48 |
-
sa.ForeignKeyConstraint(['poll_id'], ['poll.id'], ondelete='CASCADE'),
|
| 49 |
-
sa.PrimaryKeyConstraint('id')
|
| 50 |
-
)
|
| 51 |
-
op.create_table('vote',
|
| 52 |
-
sa.Column('id', sa.Uuid(), nullable=False),
|
| 53 |
-
sa.Column('option_id', sa.Uuid(), nullable=False),
|
| 54 |
-
sa.Column('voter_email_hash', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
|
| 55 |
-
sa.Column('timestamp', sa.DateTime(), nullable=False),
|
| 56 |
-
sa.ForeignKeyConstraint(['option_id'], ['polloption.id'], ondelete='CASCADE'),
|
| 57 |
-
sa.PrimaryKeyConstraint('id')
|
| 58 |
-
)
|
| 59 |
-
op.create_index(op.f('ix_vote_id'), 'vote', ['id'], unique=False)
|
| 60 |
-
# ### end Alembic commands ###
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
def downgrade() -> None:
|
| 64 |
-
# ### commands auto generated by Alembic - please adjust! ###
|
| 65 |
-
op.drop_index(op.f('ix_vote_id'), table_name='vote')
|
| 66 |
-
op.drop_table('vote')
|
| 67 |
-
op.drop_table('rollrange')
|
| 68 |
-
op.drop_table('polloption')
|
| 69 |
-
op.drop_table('poll')
|
| 70 |
-
# ### end Alembic commands ###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/alembic/versions/4c9cd9554f1c_timezone_utc.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
"""timezone utc
|
| 2 |
-
|
| 3 |
-
Revision ID: 4c9cd9554f1c
|
| 4 |
-
Revises: 2d970db44571
|
| 5 |
-
Create Date: 2024-10-16 00:30:26.544361
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
from typing import Sequence, Union
|
| 9 |
-
|
| 10 |
-
from alembic import op
|
| 11 |
-
import sqlalchemy as sa
|
| 12 |
-
import sqlmodel
|
| 13 |
-
from sqlalchemy.dialects import postgresql
|
| 14 |
-
|
| 15 |
-
# revision identifiers, used by Alembic.
|
| 16 |
-
revision: str = '4c9cd9554f1c'
|
| 17 |
-
down_revision: Union[str, None] = '2d970db44571'
|
| 18 |
-
branch_labels: Union[str, Sequence[str], None] = None
|
| 19 |
-
depends_on: Union[str, Sequence[str], None] = None
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def upgrade() -> None:
|
| 23 |
-
# ### commands auto generated by Alembic - please adjust! ###
|
| 24 |
-
op.alter_column('poll', 'created_at',
|
| 25 |
-
existing_type=postgresql.TIMESTAMP(timezone=True),
|
| 26 |
-
type_=sa.DateTime(),
|
| 27 |
-
existing_nullable=False)
|
| 28 |
-
op.alter_column('poll', 'start_time',
|
| 29 |
-
existing_type=postgresql.TIMESTAMP(timezone=True),
|
| 30 |
-
type_=sa.DateTime(),
|
| 31 |
-
existing_nullable=False)
|
| 32 |
-
op.alter_column('poll', 'end_time',
|
| 33 |
-
existing_type=postgresql.TIMESTAMP(timezone=True),
|
| 34 |
-
type_=sa.DateTime(),
|
| 35 |
-
existing_nullable=False)
|
| 36 |
-
op.alter_column('vote', 'timestamp',
|
| 37 |
-
existing_type=postgresql.TIMESTAMP(timezone=True),
|
| 38 |
-
type_=sa.DateTime(),
|
| 39 |
-
existing_nullable=False)
|
| 40 |
-
# ### end Alembic commands ###
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def downgrade() -> None:
|
| 44 |
-
# ### commands auto generated by Alembic - please adjust! ###
|
| 45 |
-
op.alter_column('vote', 'timestamp',
|
| 46 |
-
existing_type=sa.DateTime(),
|
| 47 |
-
type_=postgresql.TIMESTAMP(timezone=True),
|
| 48 |
-
existing_nullable=False)
|
| 49 |
-
op.alter_column('poll', 'end_time',
|
| 50 |
-
existing_type=sa.DateTime(),
|
| 51 |
-
type_=postgresql.TIMESTAMP(timezone=True),
|
| 52 |
-
existing_nullable=False)
|
| 53 |
-
op.alter_column('poll', 'start_time',
|
| 54 |
-
existing_type=sa.DateTime(),
|
| 55 |
-
type_=postgresql.TIMESTAMP(timezone=True),
|
| 56 |
-
existing_nullable=False)
|
| 57 |
-
op.alter_column('poll', 'created_at',
|
| 58 |
-
existing_type=sa.DateTime(),
|
| 59 |
-
type_=postgresql.TIMESTAMP(timezone=True),
|
| 60 |
-
existing_nullable=False)
|
| 61 |
-
# ### end Alembic commands ###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/alembic/versions/__pycache__/2d970db44571_.cpython-312.pyc
DELETED
|
Binary file (1.64 kB)
|
|
|
app/alembic/versions/__pycache__/4a25da80a496_initial_migration.cpython-312.pyc
DELETED
|
Binary file (5.01 kB)
|
|
|
app/alembic/versions/__pycache__/4c9cd9554f1c_timezone_utc.cpython-312.pyc
DELETED
|
Binary file (2.64 kB)
|
|
|
app/api/__init__.py
DELETED
|
File without changes
|
app/api/__pycache__/__init__.cpython-312.pyc
DELETED
|
Binary file (164 Bytes)
|
|
|
app/api/__pycache__/deps.cpython-312.pyc
DELETED
|
Binary file (3.01 kB)
|
|
|
app/api/__pycache__/main.cpython-312.pyc
DELETED
|
Binary file (662 Bytes)
|
|
|
app/api/__pycache__/poll.cpython-312.pyc
DELETED
|
Binary file (13 kB)
|
|
|
app/api/__pycache__/vote.cpython-312.pyc
DELETED
|
Binary file (4.97 kB)
|
|
|
app/api/deps.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
from collections.abc import Generator
|
| 2 |
-
from typing import Annotated
|
| 3 |
-
|
| 4 |
-
import jwt
|
| 5 |
-
from fastapi import Depends, HTTPException, status
|
| 6 |
-
from fastapi.security import HTTPBearer
|
| 7 |
-
from sqlmodel import Session
|
| 8 |
-
|
| 9 |
-
from core.db import engine
|
| 10 |
-
from core.config import settings
|
| 11 |
-
from core import security
|
| 12 |
-
from models.common import AuthUser
|
| 13 |
-
from utils.email_validator import is_valid_cuet_email
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
httpBearer = HTTPBearer()
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def get_db() -> Generator[Session, None, None]:
|
| 20 |
-
with Session(engine) as session:
|
| 21 |
-
yield session
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
SessionDep = Annotated[Session, Depends(get_db)]
|
| 25 |
-
TokenDep = Annotated[str, Depends(httpBearer)]
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def get_current_user(session: SessionDep, token: TokenDep):
|
| 29 |
-
try:
|
| 30 |
-
payload = jwt.decode(
|
| 31 |
-
token.credentials,
|
| 32 |
-
settings.SECRET_KEY,
|
| 33 |
-
algorithms=[security.ALGORITHM],
|
| 34 |
-
audience=settings.AUDIENCE,
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
if not is_valid_cuet_email(payload.get("email")):
|
| 38 |
-
raise HTTPException(
|
| 39 |
-
status_code=status.HTTP_400_BAD_REQUEST, detail="Must be a CUET-student email")
|
| 40 |
-
|
| 41 |
-
return AuthUser(
|
| 42 |
-
email=payload.get("email"),
|
| 43 |
-
full_name=" ".join(payload.get(
|
| 44 |
-
"user_metadata").get("full_name").split()[:2]),
|
| 45 |
-
roll=payload.get("email")[1:8],
|
| 46 |
-
avatar_url=payload.get("user_metadata").get("avatar_url")
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
except jwt.ExpiredSignatureError:
|
| 50 |
-
raise HTTPException(
|
| 51 |
-
status_code=status.HTTP_401_UNAUTHORIZED, detail="Token expired")
|
| 52 |
-
except jwt.InvalidTokenError:
|
| 53 |
-
raise HTTPException(
|
| 54 |
-
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
| 55 |
-
except Exception as e:
|
| 56 |
-
raise HTTPException(
|
| 57 |
-
status_code=status.HTTP_401_UNAUTHORIZED, detail=f"Invalid authentication credentials. {e}")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
CurrentUser = Annotated[AuthUser, Depends(get_current_user)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/api/main.py
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
from fastapi import APIRouter
|
| 2 |
-
from api.routes import poll, vote, user
|
| 3 |
-
|
| 4 |
-
api_router = APIRouter()
|
| 5 |
-
api_router.include_router(poll.router, prefix="/polls", tags=["polls"])
|
| 6 |
-
api_router.include_router(vote.router, prefix="/votes", tags=["votes"])
|
| 7 |
-
api_router.include_router(user.router, prefix="/users", tags=["users"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/api/routes/__init__.py
DELETED
|
File without changes
|
app/api/routes/__pycache__/__init__.cpython-312.pyc
DELETED
|
Binary file (171 Bytes)
|
|
|
app/api/routes/__pycache__/auth.cpython-312.pyc
DELETED
|
Binary file (2.57 kB)
|
|
|
app/api/routes/__pycache__/poll.cpython-312.pyc
DELETED
|
Binary file (23.7 kB)
|
|
|
app/api/routes/__pycache__/user.cpython-312.pyc
DELETED
|
Binary file (611 Bytes)
|
|
|
app/api/routes/__pycache__/vote.cpython-312.pyc
DELETED
|
Binary file (3.47 kB)
|
|
|
app/api/routes/poll.py
DELETED
|
@@ -1,429 +0,0 @@
|
|
| 1 |
-
from datetime import datetime, timezone
|
| 2 |
-
from fastapi import APIRouter, HTTPException, status
|
| 3 |
-
from sqlmodel import select, func
|
| 4 |
-
from sqlalchemy.orm import subqueryload
|
| 5 |
-
from sqlalchemy.sql import or_, and_
|
| 6 |
-
from uuid import UUID
|
| 7 |
-
|
| 8 |
-
from api.deps import SessionDep, CurrentUser
|
| 9 |
-
from core.security import hash_email
|
| 10 |
-
from models.common import Message
|
| 11 |
-
from models.poll import (
|
| 12 |
-
Poll,
|
| 13 |
-
PollCreate,
|
| 14 |
-
PollCreateResponse,
|
| 15 |
-
PollOption,
|
| 16 |
-
PollOptions,
|
| 17 |
-
PollOptionsCreate,
|
| 18 |
-
PollResponse,
|
| 19 |
-
PollsResponse,
|
| 20 |
-
PollOptionResult,
|
| 21 |
-
PollResult,
|
| 22 |
-
RollRange,
|
| 23 |
-
RollRanges,
|
| 24 |
-
RollRangesCreate,
|
| 25 |
-
)
|
| 26 |
-
from models.vote import Vote
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
router = APIRouter()
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def _serialize_poll_public(poll, total_votes, selected_option) -> PollResponse:
|
| 33 |
-
return PollResponse(
|
| 34 |
-
id=poll.id,
|
| 35 |
-
title=poll.title,
|
| 36 |
-
description=poll.description,
|
| 37 |
-
is_private=poll.is_private,
|
| 38 |
-
creator_email=poll.creator_email,
|
| 39 |
-
created_at=poll.created_at,
|
| 40 |
-
start_time=poll.start_time,
|
| 41 |
-
end_time=poll.end_time,
|
| 42 |
-
options=poll.options,
|
| 43 |
-
selected_option=selected_option,
|
| 44 |
-
total_votes=total_votes,
|
| 45 |
-
roll_ranges=poll.roll_ranges
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def _get_polls(user, session, skip, limit, search, where_clause, order_by_clause=None) -> PollsResponse:
|
| 50 |
-
"""Get polls based on query parameters."""
|
| 51 |
-
vote_count_subquery = (
|
| 52 |
-
select(PollOption.poll_id, func.count(Vote.id).label('total_votes'))
|
| 53 |
-
.join(Vote, Vote.option_id == PollOption.id, isouter=True)
|
| 54 |
-
.group_by(PollOption.poll_id)
|
| 55 |
-
.subquery()
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
selected_option_subquery = (
|
| 59 |
-
select(Vote.option_id, PollOption.poll_id)
|
| 60 |
-
.join(PollOption, PollOption.id == Vote.option_id)
|
| 61 |
-
.where(Vote.voter_email_hash == hash_email(user.email) if user else None)
|
| 62 |
-
.subquery()
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
query = (
|
| 66 |
-
select(Poll, vote_count_subquery.c.total_votes, selected_option_subquery.c.option_id.label('selected_option'),
|
| 67 |
-
func.count().over().label('total_count'))
|
| 68 |
-
.join(vote_count_subquery, vote_count_subquery.c.poll_id == Poll.id, isouter=True)
|
| 69 |
-
.join(selected_option_subquery, selected_option_subquery.c.poll_id == Poll.id, isouter=True)
|
| 70 |
-
.where(where_clause)
|
| 71 |
-
.options(
|
| 72 |
-
subqueryload(Poll.options),
|
| 73 |
-
subqueryload(Poll.roll_ranges)
|
| 74 |
-
)
|
| 75 |
-
.order_by(func.coalesce(vote_count_subquery.c.total_votes, 0).desc() if order_by_clause is None else order_by_clause)
|
| 76 |
-
.offset(skip)
|
| 77 |
-
.limit(limit)
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
if search:
|
| 81 |
-
search = f"%{search}%"
|
| 82 |
-
query = query.where(
|
| 83 |
-
or_(Poll.title.ilike(search), Poll.description.ilike(search))
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
polls = session.exec(query).unique().all()
|
| 87 |
-
total_count = polls[0][3] if polls else 0
|
| 88 |
-
|
| 89 |
-
data = [
|
| 90 |
-
_serialize_poll_public(poll, total_votes or 0,
|
| 91 |
-
session.exec(
|
| 92 |
-
select(PollOption)
|
| 93 |
-
.where(PollOption.id == selected_option)).first()
|
| 94 |
-
)
|
| 95 |
-
for poll, total_votes, selected_option, _ in polls
|
| 96 |
-
]
|
| 97 |
-
|
| 98 |
-
return PollsResponse(data=data, count=total_count)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
@ router.get("/", response_model=PollsResponse)
|
| 102 |
-
def get_polls(user: CurrentUser, session: SessionDep, skip: int = 0, limit: int = 20, search: str = None):
|
| 103 |
-
"""Get all polls."""
|
| 104 |
-
polls = _get_polls(
|
| 105 |
-
user=user,
|
| 106 |
-
session=session,
|
| 107 |
-
skip=skip,
|
| 108 |
-
limit=limit,
|
| 109 |
-
search=search,
|
| 110 |
-
where_clause=or_(
|
| 111 |
-
Poll.is_private.is_(False),
|
| 112 |
-
Poll.creator_email == user.email,
|
| 113 |
-
Poll.roll_ranges.any(
|
| 114 |
-
and_(RollRange.start <= user.roll, RollRange.end >= user.roll)
|
| 115 |
-
),
|
| 116 |
-
),
|
| 117 |
-
order_by_clause=Poll.created_at.desc()
|
| 118 |
-
)
|
| 119 |
-
return polls
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
@ router.get("/public", response_model=PollsResponse)
|
| 123 |
-
def get_public_polls(session: SessionDep, skip: int = 0, limit: int = 20, search: str = None):
|
| 124 |
-
"""Get all public polls."""
|
| 125 |
-
polls = _get_polls(
|
| 126 |
-
user=None,
|
| 127 |
-
session=session,
|
| 128 |
-
skip=skip,
|
| 129 |
-
limit=limit,
|
| 130 |
-
search=search,
|
| 131 |
-
where_clause=Poll.is_private.is_(False),
|
| 132 |
-
order_by_clause=Poll.created_at.desc()
|
| 133 |
-
)
|
| 134 |
-
return polls
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
@ router.get("/my-polls", response_model=PollsResponse)
|
| 138 |
-
def get_my_polls(user: CurrentUser, session: SessionDep, skip: int = 0, limit: int = 20, search: str = None):
|
| 139 |
-
"""Get all polls created by the user."""
|
| 140 |
-
polls = _get_polls(
|
| 141 |
-
user=user,
|
| 142 |
-
session=session,
|
| 143 |
-
skip=skip,
|
| 144 |
-
limit=limit,
|
| 145 |
-
search=search,
|
| 146 |
-
where_clause=Poll.creator_email == user.email,
|
| 147 |
-
order_by_clause=Poll.created_at.desc()
|
| 148 |
-
)
|
| 149 |
-
return polls
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
@ router.get("/allowed-polls", response_model=PollsResponse)
|
| 153 |
-
def get_allowed_polls(user: CurrentUser, session: SessionDep, skip: int = 0, limit: int = 20, search: str = None):
|
| 154 |
-
"""Get all polls allowed for the user."""
|
| 155 |
-
polls = _get_polls(
|
| 156 |
-
user=user,
|
| 157 |
-
session=session,
|
| 158 |
-
skip=skip,
|
| 159 |
-
limit=limit,
|
| 160 |
-
search=search,
|
| 161 |
-
where_clause=or_(
|
| 162 |
-
Poll.is_private.is_(False),
|
| 163 |
-
Poll.creator_email == user.email,
|
| 164 |
-
Poll.roll_ranges.any(
|
| 165 |
-
and_(RollRange.start <= user.roll, RollRange.end >= user.roll)
|
| 166 |
-
)
|
| 167 |
-
),
|
| 168 |
-
order_by_clause=Poll.created_at.desc()
|
| 169 |
-
)
|
| 170 |
-
return polls
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
@ router.get("/popular-polls", response_model=PollsResponse)
|
| 174 |
-
def get_popular_polls(user: CurrentUser, session: SessionDep, skip: int = 0, limit: int = 20, search: str = None):
|
| 175 |
-
"""Get all popular polls."""
|
| 176 |
-
polls = _get_polls(
|
| 177 |
-
user=user,
|
| 178 |
-
session=session,
|
| 179 |
-
skip=skip,
|
| 180 |
-
limit=limit,
|
| 181 |
-
search=search,
|
| 182 |
-
where_clause=or_(
|
| 183 |
-
Poll.is_private.is_(False),
|
| 184 |
-
Poll.creator_email == user.email,
|
| 185 |
-
Poll.roll_ranges.any(
|
| 186 |
-
and_(RollRange.start <= user.roll, RollRange.end >= user.roll)
|
| 187 |
-
),
|
| 188 |
-
),
|
| 189 |
-
)
|
| 190 |
-
return polls
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
@ router.get("/upcoming-polls", response_model=PollsResponse)
|
| 194 |
-
def get_upcoming_polls(user: CurrentUser, session: SessionDep, skip: int = 0, limit: int = 20, search: str = None):
|
| 195 |
-
"""Get all upcoming polls."""
|
| 196 |
-
polls = _get_polls(
|
| 197 |
-
user=user,
|
| 198 |
-
session=session,
|
| 199 |
-
skip=skip,
|
| 200 |
-
limit=limit,
|
| 201 |
-
search=search,
|
| 202 |
-
where_clause=and_(
|
| 203 |
-
Poll.start_time > datetime.now(timezone.utc),
|
| 204 |
-
or_(
|
| 205 |
-
Poll.is_private.is_(False),
|
| 206 |
-
Poll.creator_email == user.email,
|
| 207 |
-
Poll.roll_ranges.any(
|
| 208 |
-
and_(RollRange.start <= user.roll, RollRange.end >= user.roll))
|
| 209 |
-
)
|
| 210 |
-
),
|
| 211 |
-
order_by_clause=Poll.start_time.asc()
|
| 212 |
-
)
|
| 213 |
-
return polls
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
@ router.get("/ended-polls/", response_model=PollsResponse)
|
| 217 |
-
def get_ended_polls(user: CurrentUser, session: SessionDep, skip: int = 0, limit: int = 20, search: str = None):
|
| 218 |
-
"""Get all ended polls."""
|
| 219 |
-
polls = _get_polls(
|
| 220 |
-
user=user,
|
| 221 |
-
session=session,
|
| 222 |
-
skip=skip,
|
| 223 |
-
limit=limit,
|
| 224 |
-
search=search,
|
| 225 |
-
where_clause=and_(
|
| 226 |
-
Poll.end_time < datetime.now(timezone.utc),
|
| 227 |
-
or_(
|
| 228 |
-
Poll.is_private.is_(False),
|
| 229 |
-
Poll.creator_email == user.email,
|
| 230 |
-
Poll.roll_ranges.any(
|
| 231 |
-
and_(RollRange.start <= user.roll, RollRange.end >= user.roll))
|
| 232 |
-
)
|
| 233 |
-
),
|
| 234 |
-
order_by_clause=Poll.end_time.desc()
|
| 235 |
-
)
|
| 236 |
-
return polls
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
@ router.post("/create/", response_model=PollCreateResponse)
|
| 240 |
-
def create_poll(request: PollCreate, user: CurrentUser, session: SessionDep):
|
| 241 |
-
"""Create a new poll."""
|
| 242 |
-
poll = Poll(
|
| 243 |
-
title=request.title,
|
| 244 |
-
description=request.description,
|
| 245 |
-
is_private=request.is_private,
|
| 246 |
-
creator_email=user.email,
|
| 247 |
-
start_time=request.start_time,
|
| 248 |
-
end_time=request.end_time,
|
| 249 |
-
)
|
| 250 |
-
|
| 251 |
-
session.add(poll)
|
| 252 |
-
session.commit()
|
| 253 |
-
session.refresh(poll)
|
| 254 |
-
return PollCreateResponse(poll_id=poll.id)
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
@ router.get("/{poll_id}", response_model=PollResponse)
|
| 258 |
-
def get_poll(poll_id: UUID, user: CurrentUser, session: SessionDep):
|
| 259 |
-
"""Get a poll by its ID."""
|
| 260 |
-
poll_with_votes = session.exec(
|
| 261 |
-
select(Poll, func.coalesce(func.count(Vote.id), 0).label('total_votes'))
|
| 262 |
-
.outerjoin(PollOption, PollOption.poll_id == Poll.id)
|
| 263 |
-
.outerjoin(Vote, Vote.option_id == PollOption.id)
|
| 264 |
-
.where(Poll.id == poll_id)
|
| 265 |
-
.group_by(Poll.id)
|
| 266 |
-
).first()
|
| 267 |
-
|
| 268 |
-
if not poll_with_votes:
|
| 269 |
-
raise HTTPException(
|
| 270 |
-
status_code=status.HTTP_404_NOT_FOUND, detail="Poll not found")
|
| 271 |
-
|
| 272 |
-
poll, total_votes = poll_with_votes
|
| 273 |
-
|
| 274 |
-
if poll.is_private and poll.creator_email != user.email and not any(roll_range.start <= user.roll <= roll_range.end for roll_range in poll.roll_ranges):
|
| 275 |
-
raise HTTPException(
|
| 276 |
-
status_code=status.HTTP_403_FORBIDDEN, detail="You are not authorized to view this poll")
|
| 277 |
-
|
| 278 |
-
selected_option = session.exec(
|
| 279 |
-
select(PollOption)
|
| 280 |
-
.join(Vote, Vote.option_id == PollOption.id)
|
| 281 |
-
.where(Vote.voter_email_hash == hash_email(user.email), PollOption.poll_id == poll_id)
|
| 282 |
-
).first()
|
| 283 |
-
|
| 284 |
-
return _serialize_poll_public(poll, total_votes, selected_option)
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
@ router.delete("/{poll_id}", response_model=Message)
|
| 288 |
-
def delete_poll(poll_id: UUID, user: CurrentUser, session: SessionDep):
|
| 289 |
-
"""Delete a poll by its ID."""
|
| 290 |
-
poll = session.get(Poll, poll_id)
|
| 291 |
-
if not poll:
|
| 292 |
-
raise HTTPException(
|
| 293 |
-
status_code=status.HTTP_404_NOT_FOUND, detail="Poll not found")
|
| 294 |
-
|
| 295 |
-
if poll.creator_email != user.email:
|
| 296 |
-
raise HTTPException(
|
| 297 |
-
status_code=403, detail="You are not authorized to delete this poll")
|
| 298 |
-
session.delete(poll)
|
| 299 |
-
session.commit()
|
| 300 |
-
return Message(message="Poll deleted successfully")
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
@ router.get("/{poll_id}/options", response_model=PollOptions)
|
| 304 |
-
def get_poll_options(poll_id: UUID, user: CurrentUser, session: SessionDep):
|
| 305 |
-
"""Get all options of a poll by its ID."""
|
| 306 |
-
poll = session.get(Poll, poll_id)
|
| 307 |
-
if not poll:
|
| 308 |
-
raise HTTPException(
|
| 309 |
-
status_code=status.HTTP_404_NOT_FOUND, detail="Poll not found")
|
| 310 |
-
|
| 311 |
-
if poll.is_private and poll.creator_email != user.email and not any(roll_range.start <= user.roll <= roll_range.end for roll_range in poll.roll_ranges):
|
| 312 |
-
raise HTTPException(
|
| 313 |
-
status_code=status.HTTP_403_FORBIDDEN, detail="You are not authorized to view this poll")
|
| 314 |
-
|
| 315 |
-
return PollOptions(data=poll.options, count=len(poll.options))
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
@ router.get("/{poll_id}/roll-ranges", response_model=RollRanges)
|
| 319 |
-
def get_roll_ranges(poll_id: UUID, user: CurrentUser, session: SessionDep):
|
| 320 |
-
"""Get all roll ranges of a poll by its ID."""
|
| 321 |
-
poll = session.get(Poll, poll_id)
|
| 322 |
-
if not poll:
|
| 323 |
-
raise HTTPException(
|
| 324 |
-
status_code=status.HTTP_404_NOT_FOUND, detail="Poll not found")
|
| 325 |
-
|
| 326 |
-
if poll.is_private and poll.creator_email != user.email and not any(roll_range.start <= user.roll <= roll_range.end for roll_range in poll.roll_ranges):
|
| 327 |
-
raise HTTPException(
|
| 328 |
-
status_code=status.HTTP_403_FORBIDDEN, detail="You are not authorized to view this poll")
|
| 329 |
-
|
| 330 |
-
return RollRanges(data=poll.roll_ranges, count=len(poll.roll_ranges))
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
@ router.post("/{poll_id}/options", response_model=Message)
|
| 334 |
-
def create_poll_option(poll_id: UUID, request: PollOptionsCreate, user: CurrentUser, session: SessionDep):
|
| 335 |
-
"""Create new poll options of a poll with poll_id."""
|
| 336 |
-
poll = session.get(Poll, poll_id)
|
| 337 |
-
|
| 338 |
-
if not poll:
|
| 339 |
-
raise HTTPException(
|
| 340 |
-
status_code=status.HTTP_404_NOT_FOUND, detail="Poll not found")
|
| 341 |
-
|
| 342 |
-
if poll.creator_email != user.email:
|
| 343 |
-
raise HTTPException(
|
| 344 |
-
status_code=status.HTTP_403_FORBIDDEN, detail="You are not authorized to add options to this poll")
|
| 345 |
-
|
| 346 |
-
option_texts = request.option_texts
|
| 347 |
-
if len(option_texts) < 2:
|
| 348 |
-
raise HTTPException(
|
| 349 |
-
status_code=400, detail="At least two options is required")
|
| 350 |
-
if len(option_texts) + len(poll.options) > 20:
|
| 351 |
-
raise HTTPException(
|
| 352 |
-
status_code=400, detail="A poll can have at most 20 options")
|
| 353 |
-
if len(option_texts) + len(poll.options) != len(set(option_texts + [option.option_text for option in poll.options])):
|
| 354 |
-
raise HTTPException(
|
| 355 |
-
status_code=400, detail="Options must be unique")
|
| 356 |
-
|
| 357 |
-
options = [PollOption(poll_id=poll_id, option_text=option_text)
|
| 358 |
-
for option_text in option_texts]
|
| 359 |
-
session.add_all(options)
|
| 360 |
-
session.commit()
|
| 361 |
-
for option in options:
|
| 362 |
-
session.refresh(option)
|
| 363 |
-
return Message(message="Options added successfully")
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
@ router.post("/{poll_id}/roll-ranges", response_model=Message)
|
| 367 |
-
def add_allowed_voters(poll_id: UUID, request: RollRangesCreate, user: CurrentUser, session: SessionDep):
|
| 368 |
-
poll = session.get(Poll, poll_id)
|
| 369 |
-
if not poll:
|
| 370 |
-
raise HTTPException(
|
| 371 |
-
status_code=status.HTTP_404_NOT_FOUND, detail="Poll not found")
|
| 372 |
-
if poll.creator_email != user.email:
|
| 373 |
-
raise HTTPException(
|
| 374 |
-
status_code=status.HTTP_403_FORBIDDEN, detail="You are not authorized to add roll ranges to this poll")
|
| 375 |
-
|
| 376 |
-
roll_ranges = request.roll_ranges
|
| 377 |
-
if len(roll_ranges) == 0:
|
| 378 |
-
raise HTTPException(
|
| 379 |
-
status_code=400, detail="At least one roll range is required")
|
| 380 |
-
if len(roll_ranges) + len(poll.roll_ranges) > 20:
|
| 381 |
-
raise HTTPException(
|
| 382 |
-
status_code=400, detail="A poll can have at most 20 roll ranges")
|
| 383 |
-
if len(roll_ranges) + len(poll.roll_ranges) != len(set(roll_ranges + [(roll_range.start, roll_range.end)
|
| 384 |
-
for roll_range in poll.roll_ranges])):
|
| 385 |
-
raise HTTPException(
|
| 386 |
-
status_code=400, detail="Roll ranges must be unique")
|
| 387 |
-
|
| 388 |
-
roll_ranges = [RollRange(poll_id=poll_id, start=start, end=end)
|
| 389 |
-
for start, end in roll_ranges]
|
| 390 |
-
session.add_all(roll_ranges)
|
| 391 |
-
session.commit()
|
| 392 |
-
for roll_range in roll_ranges:
|
| 393 |
-
session.refresh(roll_range)
|
| 394 |
-
|
| 395 |
-
return Message(message="Roll ranges added successfully")
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
@ router.get("/{poll_id}/result", response_model=PollResult)
|
| 399 |
-
def get_poll_result(poll_id: UUID, user: CurrentUser, session: SessionDep):
|
| 400 |
-
"""Get the result of a poll."""
|
| 401 |
-
poll = session.get(Poll, poll_id)
|
| 402 |
-
|
| 403 |
-
if not poll:
|
| 404 |
-
raise HTTPException(
|
| 405 |
-
status_code=status.HTTP_400_BAD_REQUEST, detail="Poll not found")
|
| 406 |
-
|
| 407 |
-
if poll.is_private and poll.creator_email != user.email and not any(roll_range.start <= user.roll <= roll_range.end for roll_range in poll.roll_ranges):
|
| 408 |
-
raise HTTPException(
|
| 409 |
-
status_code=status.HTTP_403_FORBIDDEN, detail="You are not authorized to view this poll")
|
| 410 |
-
|
| 411 |
-
if poll.end_time > datetime.now(timezone.utc):
|
| 412 |
-
raise HTTPException(
|
| 413 |
-
status_code=status.HTTP_400_BAD_REQUEST, detail="Poll has not ended yet")
|
| 414 |
-
|
| 415 |
-
results = session.exec(
|
| 416 |
-
select(PollOption.option_text, func.coalesce(
|
| 417 |
-
func.count(Vote.id), 0).label('votes'))
|
| 418 |
-
.outerjoin(Vote, Vote.option_id == PollOption.id)
|
| 419 |
-
.where(PollOption.poll_id == poll_id)
|
| 420 |
-
.group_by(PollOption.option_text)
|
| 421 |
-
).all()
|
| 422 |
-
|
| 423 |
-
total_votes = sum([result.votes for result in results])
|
| 424 |
-
data = [
|
| 425 |
-
PollOptionResult(option_text=result.option_text, votes=result.votes)
|
| 426 |
-
for result in results
|
| 427 |
-
]
|
| 428 |
-
|
| 429 |
-
return PollResult(data=data, total_votes=total_votes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/api/routes/user.py
DELETED
|
@@ -1,11 +0,0 @@
|
|
| 1 |
-
from fastapi import APIRouter
|
| 2 |
-
from api.deps import CurrentUser
|
| 3 |
-
from models.common import AuthUser
|
| 4 |
-
router = APIRouter()
|
| 5 |
-
|
| 6 |
-
@router.get("/current", response_model=AuthUser)
|
| 7 |
-
def get_current_user(current_user: CurrentUser):
|
| 8 |
-
"""
|
| 9 |
-
Get current user.
|
| 10 |
-
"""
|
| 11 |
-
return current_user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/api/routes/vote.py
DELETED
|
@@ -1,56 +0,0 @@
|
|
| 1 |
-
from datetime import datetime, timezone
|
| 2 |
-
from fastapi import APIRouter, HTTPException, status
|
| 3 |
-
from sqlmodel import select, and_
|
| 4 |
-
|
| 5 |
-
from core.security import hash_email
|
| 6 |
-
from api.deps import SessionDep, CurrentUser
|
| 7 |
-
from models.common import Message
|
| 8 |
-
from models.poll import Poll, PollOption
|
| 9 |
-
from models.vote import Vote, VoteCreateRequest
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
router = APIRouter()
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
@router.post("/vote", response_model=Message)
|
| 16 |
-
def vote(request: VoteCreateRequest, user: CurrentUser, session: SessionDep):
|
| 17 |
-
voter_email_hash = hash_email(user.email)
|
| 18 |
-
poll_option = session.get(PollOption, request.option_id)
|
| 19 |
-
if not poll_option:
|
| 20 |
-
raise HTTPException(
|
| 21 |
-
status_code=status.HTTP_404_NOT_FOUND, detail="PollOption not found")
|
| 22 |
-
|
| 23 |
-
poll = session.get(Poll, poll_option.poll_id)
|
| 24 |
-
if (poll.is_private and poll.creator_email != user.email
|
| 25 |
-
and not any(roll_range.start <= user.roll <= roll_range.end for roll_range in poll.roll_ranges)
|
| 26 |
-
):
|
| 27 |
-
raise HTTPException(
|
| 28 |
-
status_code=status.HTTP_403_FORBIDDEN, detail="You are not authorized to vote in this poll")
|
| 29 |
-
if poll.start_time > datetime.now(timezone.utc):
|
| 30 |
-
raise HTTPException(
|
| 31 |
-
status_code=status.HTTP_400_BAD_REQUEST, detail="Poll not started yet")
|
| 32 |
-
if poll.end_time < datetime.now(timezone.utc):
|
| 33 |
-
raise HTTPException(
|
| 34 |
-
status_code=status.HTTP_400_BAD_REQUEST, detail="Poll has ended")
|
| 35 |
-
|
| 36 |
-
if session.exec(
|
| 37 |
-
select(Vote)
|
| 38 |
-
.join(PollOption)
|
| 39 |
-
.where(
|
| 40 |
-
and_(
|
| 41 |
-
Vote.voter_email_hash == voter_email_hash,
|
| 42 |
-
PollOption.poll_id == poll.id
|
| 43 |
-
)
|
| 44 |
-
)
|
| 45 |
-
).first():
|
| 46 |
-
raise HTTPException(
|
| 47 |
-
status_code=status.HTTP_400_BAD_REQUEST, detail="You have already voted in this poll")
|
| 48 |
-
|
| 49 |
-
vote = Vote(
|
| 50 |
-
option_id=request.option_id,
|
| 51 |
-
voter_email_hash=voter_email_hash
|
| 52 |
-
)
|
| 53 |
-
session.add(vote)
|
| 54 |
-
session.commit()
|
| 55 |
-
session.refresh(vote)
|
| 56 |
-
return Message(message="Voted successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/core/__init__.py
DELETED
|
File without changes
|
app/core/__pycache__/__init__.cpython-312.pyc
DELETED
|
Binary file (165 Bytes)
|
|
|
app/core/__pycache__/config.cpython-312.pyc
DELETED
|
Binary file (2.22 kB)
|
|
|
app/core/__pycache__/db.cpython-312.pyc
DELETED
|
Binary file (900 Bytes)
|
|
|
app/core/__pycache__/security.cpython-312.pyc
DELETED
|
Binary file (550 Bytes)
|
|
|
app/core/config.py
DELETED
|
@@ -1,46 +0,0 @@
|
|
| 1 |
-
import secrets
|
| 2 |
-
from pydantic_settings import BaseSettings
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
from typing import Annotated, Any, Literal
|
| 5 |
-
from pydantic import (
|
| 6 |
-
AnyUrl,
|
| 7 |
-
BeforeValidator,
|
| 8 |
-
computed_field,
|
| 9 |
-
PostgresDsn
|
| 10 |
-
)
|
| 11 |
-
load_dotenv()
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def parse_cors(v: Any) -> list[str] | str:
|
| 15 |
-
if isinstance(v, str) and not v.startswith("["):
|
| 16 |
-
return [i.strip() for i in v.split(",")]
|
| 17 |
-
elif isinstance(v, list | str):
|
| 18 |
-
return v
|
| 19 |
-
raise ValueError(v)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
class Settings(BaseSettings):
|
| 23 |
-
API_V1_STR: str = "/api/v1"
|
| 24 |
-
DATABASE_URL: str
|
| 25 |
-
PROJECT_NAME: str
|
| 26 |
-
|
| 27 |
-
@computed_field # type: ignore[misc]
|
| 28 |
-
@property
|
| 29 |
-
def SQLALCHEMY_DATABASE_URL(self) -> PostgresDsn:
|
| 30 |
-
return self.DATABASE_URL
|
| 31 |
-
|
| 32 |
-
SECRET_KEY: str = secrets.token_urlsafe(32)
|
| 33 |
-
AUDIENCE: str = "authenticated"
|
| 34 |
-
# 60 minutes * 24 hours * 8 days = 8 days
|
| 35 |
-
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
|
| 36 |
-
|
| 37 |
-
DOMAIN: str = "localhost"
|
| 38 |
-
ENVIRONMENT: Literal["local", "development", "merge", "staging", "production"] = (
|
| 39 |
-
"local"
|
| 40 |
-
)
|
| 41 |
-
BACKEND_CORS_ORIGINS: Annotated[list[AnyUrl] | str, BeforeValidator(parse_cors)] = (
|
| 42 |
-
[]
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
settings = Settings()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/core/db.py
DELETED
|
@@ -1,14 +0,0 @@
|
|
| 1 |
-
from sqlmodel import SQLModel, create_engine, Session
|
| 2 |
-
from core.config import settings
|
| 3 |
-
|
| 4 |
-
# Create the engine for PostgreSQL
|
| 5 |
-
engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URL), echo=True)
|
| 6 |
-
|
| 7 |
-
# Create a session for the database connection
|
| 8 |
-
def get_session():
|
| 9 |
-
with Session(engine) as session:
|
| 10 |
-
yield session
|
| 11 |
-
|
| 12 |
-
# Function to initialize the database (to be used with Alembic for migrations)
|
| 13 |
-
def init_db():
|
| 14 |
-
SQLModel.metadata.create_all(engine)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/core/security.py
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
import hashlib
|
| 2 |
-
|
| 3 |
-
def hash_email(email: str) -> str:
|
| 4 |
-
"""Hash the email using SHA-256."""
|
| 5 |
-
return hashlib.sha256(email.encode()).hexdigest()
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
ALGORITHM = "HS256"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/main.py
DELETED
|
@@ -1,22 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from core.config import settings
|
| 3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from api.main import api_router
|
| 5 |
-
|
| 6 |
-
app = FastAPI(
|
| 7 |
-
title=settings.PROJECT_NAME,
|
| 8 |
-
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
if settings.BACKEND_CORS_ORIGINS:
|
| 12 |
-
app.add_middleware(
|
| 13 |
-
CORSMiddleware,
|
| 14 |
-
allow_origins=[
|
| 15 |
-
str(origin).strip("/") for origin in settings.BACKEND_CORS_ORIGINS
|
| 16 |
-
],
|
| 17 |
-
allow_credentials=True,
|
| 18 |
-
allow_methods=["*"],
|
| 19 |
-
allow_headers=["*"],
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/models/__init__.py
DELETED
|
@@ -1,16 +0,0 @@
|
|
| 1 |
-
from .vote import Vote
|
| 2 |
-
from .poll import(
|
| 3 |
-
Poll,
|
| 4 |
-
PollCreate,
|
| 5 |
-
PollCreateResponse,
|
| 6 |
-
PollOption,
|
| 7 |
-
PollOptions,
|
| 8 |
-
PollOptionsCreate,
|
| 9 |
-
PollOptionResult,
|
| 10 |
-
PollResponse,
|
| 11 |
-
PollsResponse,
|
| 12 |
-
RollRange,
|
| 13 |
-
RollRanges,
|
| 14 |
-
RollRangesCreate,
|
| 15 |
-
PollResult
|
| 16 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/models/__pycache__/__init__.cpython-312.pyc
DELETED
|
Binary file (538 Bytes)
|
|
|
app/models/__pycache__/common.cpython-312.pyc
DELETED
|
Binary file (735 Bytes)
|
|
|
app/models/__pycache__/poll.cpython-312.pyc
DELETED
|
Binary file (6.51 kB)
|
|
|
app/models/__pycache__/vote.cpython-312.pyc
DELETED
|
Binary file (1.6 kB)
|
|
|
app/models/common.py
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
from pydantic import EmailStr, BaseModel
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
class Message(BaseModel):
|
| 5 |
-
message: str
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
class AuthUser(BaseModel):
|
| 9 |
-
email: EmailStr
|
| 10 |
-
full_name: str
|
| 11 |
-
roll: int
|
| 12 |
-
avatar_url: str = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/models/poll.py
DELETED
|
@@ -1,110 +0,0 @@
|
|
| 1 |
-
import uuid
|
| 2 |
-
from uuid import UUID
|
| 3 |
-
from datetime import datetime, timezone, timedelta
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
-
from sqlmodel import SQLModel, Field, Relationship
|
| 6 |
-
from typing import TYPE_CHECKING, Optional
|
| 7 |
-
|
| 8 |
-
if TYPE_CHECKING:
|
| 9 |
-
from models.vote import Vote
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
class RollRange(SQLModel, table=True):
|
| 13 |
-
id: UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
| 14 |
-
start: int = Field(default=10 ** 6)
|
| 15 |
-
end: int = Field(default=10 ** 7)
|
| 16 |
-
poll_id: UUID = Field(foreign_key="poll.id", ondelete="CASCADE")
|
| 17 |
-
poll: "Poll" = Relationship(back_populates="roll_ranges")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
class RollRanges(BaseModel):
|
| 21 |
-
data: list[RollRange]
|
| 22 |
-
count: int
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
class RollRangesCreate(BaseModel):
|
| 26 |
-
roll_ranges: list[tuple[int, int]]
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
class Poll(SQLModel, table=True):
|
| 30 |
-
id: UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
| 31 |
-
title: str = Field(max_length=255)
|
| 32 |
-
description: str = Field(max_length=1024)
|
| 33 |
-
is_private: bool = Field(default=False)
|
| 34 |
-
created_at: datetime = Field(
|
| 35 |
-
default_factory=lambda: datetime.now(timezone.utc)
|
| 36 |
-
)
|
| 37 |
-
creator_email: str = Field(max_length=255)
|
| 38 |
-
start_time: datetime = Field(
|
| 39 |
-
default_factory=lambda: datetime.now(timezone.utc)
|
| 40 |
-
)
|
| 41 |
-
end_time: datetime = Field(
|
| 42 |
-
default_factory=lambda: datetime.now(timezone.utc) + timedelta(days=1)
|
| 43 |
-
)
|
| 44 |
-
options: list["PollOption"] = Relationship(
|
| 45 |
-
back_populates="poll", cascade_delete=True)
|
| 46 |
-
roll_ranges: list["RollRange"] = Relationship(
|
| 47 |
-
back_populates="poll", cascade_delete=True)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
class PollCreate(BaseModel):
|
| 51 |
-
title: str = Field(max_length=255)
|
| 52 |
-
description: str | None = Field(max_length=1024)
|
| 53 |
-
is_private: bool = Field(default=False)
|
| 54 |
-
start_time: datetime = Field(
|
| 55 |
-
default_factory=lambda: datetime.now(timezone.utc))
|
| 56 |
-
end_time: datetime = Field(
|
| 57 |
-
default_factory=lambda: datetime.now(timezone.utc) + timedelta(days=1)
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
class PollCreateResponse(BaseModel):
|
| 62 |
-
poll_id: UUID
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
class PollResponse(BaseModel):
|
| 66 |
-
id: UUID
|
| 67 |
-
title: str
|
| 68 |
-
description: str
|
| 69 |
-
is_private: bool
|
| 70 |
-
creator_email: str
|
| 71 |
-
created_at: datetime
|
| 72 |
-
start_time: datetime
|
| 73 |
-
end_time: datetime
|
| 74 |
-
roll_ranges: list[RollRange]
|
| 75 |
-
options: list["PollOption"]
|
| 76 |
-
selected_option: Optional["PollOption"]
|
| 77 |
-
total_votes: int
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
class PollsResponse(BaseModel):
|
| 81 |
-
data: list[PollResponse]
|
| 82 |
-
count: int
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
class PollOption(SQLModel, table=True):
|
| 86 |
-
id: UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
| 87 |
-
poll_id: UUID = Field(foreign_key="poll.id", ondelete="CASCADE")
|
| 88 |
-
option_text: str = Field(max_length=255)
|
| 89 |
-
poll: Poll = Relationship(back_populates="options")
|
| 90 |
-
votes: list["Vote"] = Relationship(
|
| 91 |
-
back_populates="poll_option", cascade_delete=True)
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
class PollOptions(BaseModel):
|
| 95 |
-
data: list[PollOption]
|
| 96 |
-
count: int
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
class PollOptionsCreate(BaseModel):
|
| 100 |
-
option_texts: list[str]
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
class PollOptionResult(BaseModel):
|
| 104 |
-
option_text: str
|
| 105 |
-
votes: int
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
class PollResult(BaseModel):
|
| 109 |
-
data: list[PollOptionResult]
|
| 110 |
-
total_votes: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/models/vote.py
DELETED
|
@@ -1,23 +0,0 @@
|
|
| 1 |
-
from sqlmodel import SQLModel, Field, Relationship
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from uuid import uuid4, UUID
|
| 4 |
-
from datetime import datetime, timezone
|
| 5 |
-
from typing import TYPE_CHECKING
|
| 6 |
-
|
| 7 |
-
if TYPE_CHECKING:
|
| 8 |
-
from models.poll import PollOption
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
class Vote(SQLModel, table=True):
|
| 12 |
-
id: UUID = Field(
|
| 13 |
-
default_factory=uuid4, primary_key=True, index=True)
|
| 14 |
-
option_id: UUID = Field(foreign_key="polloption.id",
|
| 15 |
-
ondelete="CASCADE", index=True)
|
| 16 |
-
voter_email_hash: str = Field(max_length=255)
|
| 17 |
-
timestamp: datetime = Field(
|
| 18 |
-
default_factory=lambda: datetime.now(timezone.utc))
|
| 19 |
-
poll_option: "PollOption" = Relationship(back_populates="votes")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
class VoteCreateRequest(BaseModel):
|
| 23 |
-
option_id: UUID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|