Skip to content

Commit

Permalink
Restart history
Browse files Browse the repository at this point in the history
  • Loading branch information
dansahagian committed Jul 22, 2024
0 parents commit 13106f4
Show file tree
Hide file tree
Showing 142 changed files with 5,489 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
DOMAIN=
CONTACT=
VENMO=

ENV=

SECRET_KEY=

DJANGO_SETTINGS_MODULE=fbsurvivor.settings

DATABASE=
PG_USER=
PG_PASSWORD=
PG_HOST=
PG_PORT=

SMTP_SERVER=
SMTP_SENDER=
SMTP_USER=
SMTP_PASSWORD=
SMTP_PORT=

TWILIO_SID=
TWILIO_KEY=
TWILIO_NUM=
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI

on:
push:
paths:
- "fbsurvivor/**"
- "requirements/**"

jobs:
check:
name: Continuous Integration
runs-on: ubuntu-latest
env:
ENV: dev
DOMAIN: http://127.0.0.1:8000
SECRET_KEY: abcdefghijklmnop
PGDATABASE: ci
PGUSER: postgres
PGPASSWORD: password
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: ci
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: 3.12
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements/dev.txt --no-deps
- name: Ruff Linting Check
run: ruff check .
- name: Ruff Imports Check
run: ruff check --select I
- name: Ruff Format Check
run: ruff format --check
- name: Run Tests
run: pytest -p no:warnings .
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
.env
.idea/
data
.data/
__pycache__
venv
.venv
*.pid
.pytest_cache
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.5.1
hooks:
# Run the linter.
- id: ruff
# Run the import sorter
- id: ruff
args: [ --select, I, --fix]
# Run the formatter.
- id: ruff-format
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Dan Sahagian

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

[![Python 3.12](https://img.shields.io/badge/python-3.12-blue.svg)](https://www.python.org/downloads/release/python-3111/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v1.json)](https://github.com/charliermarsh/ruff)

# FB Survivor
This project is used to run a family and friends NFL Survivor League. The league was originally managed using email and Excel. The first version of the application was written in Python, Flask, and used MongoDB. The second version switched to using Postgres and raw SQL queries, but continued to use Flask. This version, technically the third (but with 2 in the name), is a ground up rewrite using Django and leveraging Django's ORM.

## About the Deployment
- Runs on a single [Linode](https://www.linode.com/) running Debian Linux
- Deployed with a simple deploy script
5 changes: 5 additions & 0 deletions bin/deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/zsh

echo "\nDeploying...\n"
ssh linode "cd /opt/fbsurvivor2 && git pull origin main"
ssh linode "/opt/fbsurvivor2/bin/deploy_on_server"
16 changes: 16 additions & 0 deletions bin/deploy_on_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/zsh

echo "\nInstalling Dependencies...\n"
cd /opt/fbsurvivor2
.venv/bin/uv pip install --upgrade pip
.venv/bin/uv pip sync requirements/prod.txt

echo "\nStopping services...\n"
sudo systemctl stop wsgi-server-fbsurvivor.service

.venv/bin/python manage.py migrate
.venv/bin/python manage.py collectstatic --no-input
.venv/bin/python manage.py check --deploy

echo "\nStarting services...\n"
sudo systemctl start wsgi-server-fbsurvivor.service
4 changes: 4 additions & 0 deletions bin/format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/zsh

.venv/bin/ruff check --select I --fix
.venv/bin/ruff format
17 changes: 17 additions & 0 deletions bin/init-db
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/zsh

ssh dan@linode "pg_dump -U fbsurvivor2 fbsurvivor2 > /tmp/fbsurvivor2_dump"
scp dan@linode:/tmp/fbsurvivor2_dump ~/d/code/fbsurvivor2/data/fbsurvivor2_dump
ssh dan@linode "rm /tmp/fbsurvivor2_dump"

psql -U postgres -d fbsurvivor2 -c "DROP SCHEMA IF EXISTS public CASCADE;"
psql -U postgres -d postgres -c "DROP DATABASE fbsurvivor2;"
psql -U postgres -d postgres -c "DROP ROLE fbsurvivor2;"
psql -U postgres -d postgres -c "CREATE DATABASE fbsurvivor2;"
psql -U postgres -d postgres -c "CREATE ROLE fbsurvivor2 WITH PASSWORD 'password';"
psql -U postgres -d postgres -c "ALTER ROLE fbsurvivor2 WITH CREATEDB LOGIN;"
psql -U postgres -d postgres -c "GRANT ALL ON DATABASE fbsurvivor2 to fbsurvivor2;"
psql -U postgres -d fbsurvivor2 -c "DROP SCHEMA IF EXISTS public;"
psql -U postgres -d fbsurvivor2 -c "CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION fbsurvivor2;"

psql fbsurvivor2 < ~/d/code/fbsurvivor2/data/fbsurvivor2_dump
5 changes: 5 additions & 0 deletions bin/load-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/zsh

.venv/bin/python3 ./manage.py load_weeks 2024 ./uploads/2024_weeks.txt
.venv/bin/python3 ./manage.py load_teams 2024 ./uploads/2024_teams.txt
.venv/bin/python3 ./manage.py load_game_locks 2024 ./uploads/2024_game_locks.txt
6 changes: 6 additions & 0 deletions bin/update-requirements
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/zsh

uv pip compile --upgrade --generate-hashes --output-file requirements/prod.txt requirements/in/prod.in
uv pip compile --upgrade --generate-hashes --output-file requirements/dev.txt requirements/in/dev.in

uv pip sync requirements/dev.txt
1 change: 1 addition & 0 deletions fbsurvivor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .wsgi import application # noqa
16 changes: 16 additions & 0 deletions fbsurvivor/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for fbsurvivor project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fbsurvivor.settings")

application = get_asgi_application()
Empty file added fbsurvivor/core/__init__.py
Empty file.
91 changes: 91 additions & 0 deletions fbsurvivor/core/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from django.contrib import admin

from fbsurvivor.core.models import (
Lock,
Payout,
Pick,
Player,
PlayerStatus,
Season,
Team,
Week,
)

admin.AdminSite.enable_nav_sidebar = False


@admin.register(Player)
class PlayerAdmin(admin.ModelAdmin):
list_display = [
"username",
"email",
"is_admin",
"has_email_reminders",
]

def get_queryset(self, request):
qs = super(PlayerAdmin, self).get_queryset(request)
return qs.order_by("username")


@admin.register(PlayerStatus)
class PlayerStatusAdmin(admin.ModelAdmin):
list_display = ["player", "season", "is_paid", "is_retired", "is_survivor"]
list_editable = ["is_paid", "is_retired", "is_survivor"]
list_filter = ["season"]

def get_queryset(self, request):
qs = super(PlayerStatusAdmin, self).get_queryset(request)
return qs.order_by("player__username")


@admin.register(Season)
class SeasonAdmin(admin.ModelAdmin):
list_display = ["year", "is_locked", "is_current"]
list_editable = ["is_locked", "is_current"]


@admin.register(Week)
class WeekAdmin(admin.ModelAdmin):
list_display = ["week_num", "season", "lock_datetime"]
list_filter = ["season"]

def get_queryset(self, request):
qs = super(WeekAdmin, self).get_queryset(request)
return qs.order_by("-season", "week_num")


@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
list_display = ["team_code", "season", "bye_week"]
list_filter = ["season"]

def get_queryset(self, request):
qs = super(TeamAdmin, self).get_queryset(request)
return qs.order_by("-season", "team_code")


@admin.register(Pick)
class PickAdmin(admin.ModelAdmin):
list_display = ["week", "team", "player", "result"]
list_editable = ["result"]
list_filter = ["week__season", "player"]

def get_queryset(self, request):
qs = super(PickAdmin, self).get_queryset(request)
return qs.order_by("-week__season", "-week__week_num")


@admin.register(Payout)
class PayoutAdmin(admin.ModelAdmin):
list_display = ["player", "season", "amount"]
list_filter = ["season", "player"]

def get_queryset(self, request):
qs = super(PayoutAdmin, self).get_queryset(request)
return qs.order_by("-season", "player")


@admin.register(Lock)
class LockAdmin(admin.ModelAdmin):
list_display = ["week", "team", "lock_datetime"]
5 changes: 5 additions & 0 deletions fbsurvivor/core/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CoreConfig(AppConfig):
name = "fbsurvivor.core"
42 changes: 42 additions & 0 deletions fbsurvivor/core/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django import forms

from fbsurvivor.core.models import Pick, Team


class EmailForm(forms.Form):
email = forms.EmailField(label="email")


class PlayerForm(forms.Form):
username = forms.CharField(label="username", max_length=20)
email = forms.EmailField(label="email (won't be displayed)")


class PickForm(forms.Form):
def __init__(self, player, season, week, *args, **kwargs):
super().__init__(*args, **kwargs)

picks = Pick.objects.filter(
player=player, week__season=season, team__isnull=False
).values_list("team__team_code", flat=True)

choices = [("", "--")]
choices.extend(
[
(team.team_code, f"{team.team_code} ({team.name})")
for team in (
Team.objects.filter(season=season)
.exclude(bye_week=week.week_num)
.exclude(team_code__in=picks)
.order_by("team_code")
)
if not team.is_locked(week)
]
)

self.fields["team"] = forms.ChoiceField(choices=choices, required=False)


class MessageForm(forms.Form):
subject = forms.CharField(label="subject", max_length=25)
message = forms.CharField(label="message", widget=forms.Textarea)
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions fbsurvivor/core/management/commands/cache_current_board.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.core.management.base import BaseCommand

from fbsurvivor.core.utils.helpers import cache_current_board


class Command(BaseCommand):
help = "Cache the board for the current season."

def handle(self, *args, **options):
cache_current_board()
Loading

0 comments on commit 13106f4

Please sign in to comment.