Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Features #3

Merged
merged 17 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/build_ghcr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ env:
IMAGE_NAME: ${{ github.repository }}

jobs:
# Test Django app
test:
name: Run Tests
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.13"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pipenv
pipenv install --dev # Install dev dependencies from Pipfile

- name: Run tests
run: |
pipenv run python manage.py test

formatblack:
name: Black Formatter
runs-on: ubuntu-latest
Expand All @@ -34,6 +58,9 @@ jobs:
build-and-publish-image:
name: Build and Publish Registry Image
runs-on: ubuntu-latest
needs: [test]

if: github.ref != 'refs/heads/main' || needs.test.result == 'success' # Prevent push on 'main' if tests fail

permissions:
contents: read
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pillow = "*"
dj-database-url = "*"
psycopg2 = "*"
gunicorn = "*"
qrcode = "*"

[dev-packages]

Expand Down
11 changes: 10 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
https://battery.frc1721.org/

Django server to keep track of the state of batteries.
Made by dublu.
Made by [@dublUayaychtee ](https://github.com/dublUayaychtee), [@kenwoodfox ](https://github.com/kenwoodfox).

### TODO

Sort and filter
Make it look better
# Development

## Development (using pipenv)

Expand All @@ -21,6 +18,14 @@ pipenv install
pipenv shell
```

It can be handy to place a `.env` at the root of this repo for connection details

```
DATABASE_URL=postgres://battery_user:[email protected]:5432/battery_db
DEBUG=true
SECRET_KEY="fortestingonly"
```

Spawn a development django server

```shell
Expand Down Expand Up @@ -52,3 +57,20 @@ export DATABASE_URL=postgres://battery_user:[email protected]:5432/bat
python manage.py migrate
python manage.py runserver
```

# Maintainence tasks

There are a few custom maintenance tasks outlined in `log/management/commands`.

## addretrobattery

Useful command will walk you though adding a system-generated record to retroactively include
an older battery.

```shell
# Local
pipenv run python manage.py addretrobattery

# On prod
docker exec -it <id> ./manage.py addretrobattery
```
3 changes: 2 additions & 1 deletion battery/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [BASE_DIR / "static"]

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
Expand Down
Empty file.
60 changes: 60 additions & 0 deletions log/management/commands/addretrobattery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import random

from django.core.management.base import BaseCommand
from django.db import connection

from log.models import Entry

from datetime import datetime


class Command(BaseCommand):
help = "Joe Wrote This. Adds a dummy battery entry for recordkeeping purposes"

def handle(self, *args, **kwargs):
# Prompt the user for inputs
battery_id = input("Enter the battery ID (e.g., 22A): ").strip()
year = input("Enter the year (e.g., 2022): ").strip()
month = input("Enter the month (1-12): ").strip()

try:
# Convert year and month to integers
year = int(year)
month = int(month)

# Validate the inputs
if month < 1 or month > 12:
raise ValueError("Month must be between 1 and 12.")
if year < 1900 or year > datetime.now().year:
raise ValueError("Year must be a valid past or present year.")

# Generate a random day in the given month
day = random.randint(1, 28) # (Safe for all months)
date = datetime(year, month, day)

# Insert the record directly using raw SQL
with connection.cursor() as cursor:
cursor.execute(
"""
INSERT INTO log_entry (battery, ready, condition, charge, rint, memo, "user", date)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
""",
[
battery_id,
True, # ready
0, # condition n/a
100.0, # charge
25.0, # rint
"System added entry for recordkeeping.",
"system",
date,
],
)

self.stdout.write(
self.style.SUCCESS(
f"Successfully added entry for battery '{battery_id}' dated {date.strftime('%Y-%m-%d')}."
)
)
except Exception as e:
self.stderr.write(self.style.ERROR(f"Error: {e}"))
23 changes: 20 additions & 3 deletions log/templates/battery_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,29 @@
<meta name="twitter:image" content="{% url 'get_cover_image' battery_id %}" />

<script src="{% static 'js/battery_format.js' %}"></script>
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>

<body>
<h1>Battery: {{ battery_id }}</h1>
<header>
<h1>Battery {{ battery_id }}</h1>
<nav>
<a href="{% url 'battery_label' battery_id=battery_id %}">
<button>Print Label For This Battery</button>
</a>
or
<a href="{% url 'submit_entry' %}{{ battery_id}}">
<button>Record entry for {{ battery_id }}</button>
</a>
<hr>
</nav>
</header>

<table>
<thead>
<th scope="col">DATE</th>
<th scope="col">USER</th>
<th scope="col">BATTERY</th>
<th scope="col">READY</th>
<th scope="col">CONDITION</th>
<th scope="col">CHARGE</th>
Expand All @@ -42,7 +57,9 @@ <h1>Battery: {{ battery_id }}</h1>
<tr>
<td class="date">{{ entry.date }}</td>
<td class="user">{{ entry.user }}</td>
<td class="battery">{{ entry.battery }}</td>
<td class="battery">
<a href="{% url 'battery_detail' entry.battery %}">{{ entry.battery }}</a>
</td>
<td class="ready">{{ entry.ready }}</td>
<td class="condition">{{ entry.condition }}</td>
<td class="charge">{{ entry.charge }}</td>
Expand Down Expand Up @@ -73,7 +90,7 @@ <h1>Battery: {{ battery_id }}</h1>
<a href="/">Back to index</a>

<footer>
<p>Version: {{ version }}</p>
<a href="https://github.com/FRC-1721/battery.frc1721.org">Version: {{ version }}</a>
</footer>
</body>

Expand Down
68 changes: 25 additions & 43 deletions log/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,35 @@
<meta charset="UTF-8" />

<script src="{% static 'js/battery_format.js' %}"></script>
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>

<body>
{% if user.is_authenticated %} Hello, {{ user.first_name }}
<form action="{% url 'logout' %}" method="post" style="display: inline-block">
{% csrf_token %}
<button type="submit">Log Out</button>
</form>
{% else %} You are not logged in
<a href="{% url 'login' %}"><button>Log In</button></a>
{% endif %} {% if bad_key %} Security Token not found on server! Not
recommended to log in. {% endif %}
<hr />

<form method="post">
{% csrf_token %}
<table>
<tr>
<td>
<label for="battery">Battery:</label>
</td>
<td>
<input list="batteries" id="battery" name="battery" placeholder="Enter or select a battery ID" />
<datalist id="batteries">
{% for battery in available_batteries %}
<option value="{{ battery }}">
{% endfor %}
</datalist>
</td>

{% for field in form %}
{% if field.name != "battery" %}
<tr>
<td>{{ field.label_tag }}</td>
<td>{{ field }}</td>
</tr>
<header>
<h1>1721 Battery Management</h1>
<nav>
{% if user.is_authenticated %}
<span>Hello, {{ user.first_name }}</span>
<a href="{% url 'submit_entry' %}">Submit New Entry</a>
<a href="{% url 'logout' %}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Log Out
</a>
<form id="logout-form" action="{% url 'logout' %}" method="post" style="display: none;">
{% csrf_token %}
</form>
{% else %}
<span>You are not logged in.</span>
<a href="{% url 'login' %}">Log In</a>
{% endif %}
{% endfor %}

<td colspan="2">
<button type="submit">Log Entry</button>
</td>
</tr>
</table>
</form>
{% if bad_key %}
<div style="color: red; font-weight: bold;">
Security Token not found on server! Not recommended to log in.
</div>
{% endif %}
</nav>
</header>

<hr />
<hr>
<table>
<thead>
<th scope="col">DATE</th>
Expand Down Expand Up @@ -102,7 +84,7 @@
{% endif %}

<footer>
<p>Version: {{ version }}</p>
<a href="https://github.com/FRC-1721/battery.frc1721.org">Version: {{ version }}</a>
</footer>
</body>

Expand Down
51 changes: 38 additions & 13 deletions log/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>1721 Battery Log</title>
</head>
<body>
Enter your login information. Contact Dylan for a login.<br />
<hr />
<form method="post">
{% csrf_token %} {{ form.as_table }}
<button type="submit">Login</button>
</form>
</body>
</html>

{% load static %}

<head>
<title>1721 Battery Log</title>
<meta charset="UTF-8" />

<script src="{% static 'js/battery_format.js' %}"></script>
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>

<body>
<header>
<h1>Login</h1>
<nav>
{% if bad_key %}
<div style="color: red; font-weight: bold;">
Security Token not found on server! Not recommended to log in.
</div>
{% endif %}
</nav>
</header>

<hr />
Enter your login information. Contact Dylan for a login.
<hr />

<form method="post" , style="max-width: 40vw;">
{% csrf_token %} {{ form.as_table }}
<button type="submit">Login</button>
</form>

<footer>
<a href="https://github.com/FRC-1721/battery.frc1721.org">Version: {{ version }}</a>
</footer>
</body>

</html>
Loading
Loading