From 82ad14aef7a2ef319898728b3379ea99fa401525 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Wed, 29 Nov 2023 07:22:50 +0000 Subject: [PATCH] Basic working version --- .github/ISSUE_TEMPLATE/bug_report.md | 38 +++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 20 ++++++++++++ .github/renovate.json | 6 ++++ .github/workflows/build-container.yaml | 32 +++++++++++++++++++ .github/workflows/tests.yaml | 22 +++++++++++++ Dockerfile | 33 ++++++++++++++++++++ local_spaces/app.py | 3 +- local_spaces/local.py | 4 ++- local_spaces/templates/index.html | 23 ++++++++++---- poetry.lock | 14 ++++++++- pyproject.toml | 1 + renovate.json | 6 ---- 12 files changed, 187 insertions(+), 15 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md create mode 100644 .github/renovate.json create mode 100644 .github/workflows/build-container.yaml create mode 100644 .github/workflows/tests.yaml create mode 100644 Dockerfile delete mode 100644 renovate.json diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..dd84ea7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,38 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. iOS] + - Browser [e.g. chrome, safari] + - Version [e.g. 22] + +**Smartphone (please complete the following information):** + - Device: [e.g. iPhone6] + - OS: [e.g. iOS8.1] + - Browser [e.g. stock browser, safari] + - Version [e.g. 22] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..bbcbbe7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 0000000..29f1f6f --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,6 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": [ + "config:recommended" + ] +} \ No newline at end of file diff --git a/.github/workflows/build-container.yaml b/.github/workflows/build-container.yaml new file mode 100644 index 0000000..69dccc2 --- /dev/null +++ b/.github/workflows/build-container.yaml @@ -0,0 +1,32 @@ +name: Build Container + +"on": + push: + branches: + - main + tags: + - "[0-9]+.[0-9]+.[0-9]+" + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to GHCR + uses: docker/login-action@v3 + if: github.event_name != 'pull_request' + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push + id: docker_build + uses: docker/build-push-action@v5 + with: + push: true + tags: | + ghcr.io/${{ github.repository_owner }}/user-tweaker:${{ github.ref_name }} + ghcr.io/${{ github.repository_owner }}/user-tweaker:latest diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 0000000..b7bf7f5 --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,22 @@ +name: Run Tests + +on: + push: + branches: + - "main" + +jobs: + lint: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.11"] + steps: + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Lint with ruff + run: | + make lint \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ba75fbd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +FROM python:3.11.5-alpine3.18 AS base + +FROM base AS builder + +ENV PYTHONFAULTHANDLER=1 \ + PYTHONUNBUFFERED=1 \ + PYTHONHASHSEED=random \ + PIP_NO_CACHE_DIR=off \ + PIP_DISABLE_PIP_VERSION_CHECK=on \ + PIP_DEFAULT_TIMEOUT=100 \ + POETRY_NO_INTERACTION=1 \ + POETRY_VIRTUALENVS_CREATE=false \ + PATH="$PATH:/runtime/bin" \ + PYTHONPATH="$PYTHONPATH:/runtime/lib/python3.11/site-packages" \ + # Versions: + POETRY_VERSION=1.7.0 + +# System deps: +RUN apk add build-base unzip wget python3-dev libffi-dev +RUN pip install "poetry==$POETRY_VERSION" + +WORKDIR /src + +# Generate requirements and install *all* dependencies. +COPY pyproject.toml poetry.lock /src/ +RUN poetry export --dev --without-hashes --no-interaction --no-ansi -f requirements.txt -o requirements.txt +RUN pip install --prefix=/runtime --force-reinstall -r requirements.txt + +FROM base AS runtime +COPY --from=builder /runtime /usr/local +COPY . /app +WORKDIR /app +CMD ["/usr/local/bin/gunicorn", "local_spaces.app:create_app()", "--bind", "0.0.0.0:80"] \ No newline at end of file diff --git a/local_spaces/app.py b/local_spaces/app.py index 6c064c5..894522c 100644 --- a/local_spaces/app.py +++ b/local_spaces/app.py @@ -15,9 +15,10 @@ def create_app(): "SECRET_KEY": secrets.token_hex(64), "TESTING": False, "DEBUG": False, + "TEMPLATES_AUTO_RELOAD": True, "LOCALSPACES_SPACEAPI_ENDPOINT": "https://api.spaceapi.io", "LOCALSPACES_LOCAL_ENDPOINT": "https://api.leighhack.org/space.json", - "LOCALSPACES_DISTANCE": 300, + "LOCALSPACES_DISTANCE": 450, } ) app.config.from_prefixed_env() diff --git a/local_spaces/local.py b/local_spaces/local.py index a0744fb..ed5c671 100644 --- a/local_spaces/local.py +++ b/local_spaces/local.py @@ -1,6 +1,7 @@ from flask import Blueprint, render_template, current_app from math import radians, cos, sin, asin, sqrt import requests +from cachetools.func import ttl_cache local = Blueprint( "local", __name__, template_folder="templates", static_folder="static" @@ -27,6 +28,7 @@ def index(): @local.route("/local_spaces.json") +@ttl_cache(ttl=1800) def spaces(): # Get the local hackspace Space JSON resp = requests.get(current_app.config.get("LOCALSPACES_LOCAL_ENDPOINT")) @@ -63,4 +65,4 @@ def spaces(): space['distance'] = distance spaces.append(space) - return spaces + return sorted(spaces, key=lambda d: d['distance']) diff --git a/local_spaces/templates/index.html b/local_spaces/templates/index.html index 01314e8..52c472c 100644 --- a/local_spaces/templates/index.html +++ b/local_spaces/templates/index.html @@ -14,10 +14,10 @@ @@ -26,9 +26,12 @@ @@ -45,8 +48,16 @@

Other Local Hackspaces

spaces.forEach(function (val, indx) { var obj = $($("template#space-block").html()); obj.find('#space-name').html(val['data']['space']); - obj.find('#space-distance').html(val['distance']); + obj.find('#space-distance').html(Math.round(val['distance'] * 100) / 100); + + let status = 'Closed'; + if (val['data']['state']['open']) { + status = 'Open'; + } + obj.find('#space-status').html(status); + obj.find('#space-logo').attr('src', val['data']['logo']); + obj.fin $('div#spaces').append(obj); }); }); diff --git a/poetry.lock b/poetry.lock index 165dff3..f61ee7f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -12,6 +12,18 @@ files = [ {file = "blinker-1.7.0.tar.gz", hash = "sha256:e6820ff6fa4e4d1d8e2747c2283749c3f547e4fee112b98555cdcdae32996182"}, ] +[[package]] +name = "cachetools" +version = "5.3.2" +description = "Extensible memoizing collections and decorators" +category = "main" +optional = false +python-versions = ">=3.7" +files = [ + {file = "cachetools-5.3.2-py3-none-any.whl", hash = "sha256:861f35a13a451f94e301ce2bec7cac63e881232ccce7ed67fab9b5df4d3beaa1"}, + {file = "cachetools-5.3.2.tar.gz", hash = "sha256:086ee420196f7b2ab9ca2db2520aca326318b68fe5ba8bc4d49cca91add450f2"}, +] + [[package]] name = "certifi" version = "2023.11.17" @@ -367,4 +379,4 @@ watchdog = ["watchdog (>=2.3)"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "0c7b4f12c03b1d4625f2523340c76ab95841188992018cf057ce28759062989b" +content-hash = "08da0510edfd8e6171711552c8b42d04547b760ca0b685fc7f46801f12946021" diff --git a/pyproject.toml b/pyproject.toml index f5d38c3..5ad0c9f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ python = "^3.11" Flask = "^3.0.0" requests = "^2.31.0" prometheus-flask-exporter = "^0.23.0" +cachetools = "^5.3.2" [build-system] diff --git a/renovate.json b/renovate.json deleted file mode 100644 index 5db72dd..0000000 --- a/renovate.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "$schema": "https://docs.renovatebot.com/renovate-schema.json", - "extends": [ - "config:recommended" - ] -}