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

use django management commands for tools #526

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 2 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,17 @@ jobs:
python -m venv env
source env/bin/activate
curl -sSL https://install.python-poetry.org | python3 - --version 1.3.2
- name: Install dependencies
- name: Install dependencies and prepare files
# always install all -E extras to use a single cache
run: |
sudo apt-get install gettext
source env/bin/activate
poetry run python -m pip install setuptools -U
poetry install -E mysql -E pgsql
python tools/install_bootstrap.py
- name: Prepare files for test run
continue-on-error: true
run: |
cp .env.example .env
source env/bin/activate
jeriox marked this conversation as resolved.
Show resolved Hide resolved
poetry run python manage.py install_bootstrap
poetry run python manage.py compilemessages --settings myhpi.settings
poetry run python manage.py collectstatic --settings myhpi.settings

- name: Setup postgres
uses: harmon758/postgresql-action@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ RUN pip install "poetry==$POETRY_VERSION" "uwsgi==$UWSGI_PIP_VERSION" "psycopg2=
RUN poetry self add "poetry-dynamic-versioning[plugin]"
ADD . /app
RUN poetry install --no-dev
RUN python tools/install_bootstrap.py -u
RUN python tools/install_bootstrap.py -u # FIXME
jeriox marked this conversation as resolved.
Show resolved Hide resolved
RUN chmod +x /app/entrypoint.sh
ENTRYPOINT ["/app/entrypoint.sh"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ To set up a development version on your local machine, you need to execute the f
1. Set up a virtualenv for the project with Python >=3.11 and activate it (e.g., `python3 -m venv venv` and `source venv/bin/activate`)
1. Install poetry (if not already installed): `curl -sSL https://install.python-poetry.org/ | python -`
1. Install dependencies with `poetry install`
1. Install bootstrap with `python tools/install_bootstrap.py`
1. Create env file by copying the `.env.example` file to `.env`, e.g. `cp .env.example .env` (Notice that for some functionality like OIDC some settings must be changed)
1. Migrate the database with `python manage.py migrate`
1. Compile translations with `python manage.py compilemessages` (does not work on Windows, recommended to skip this step or see [docs](https://docs.djangoproject.com/en/4.0/topics/i18n/translation/#gettext-on-windows))
1. Install bootstrap with `python manage.py install_bootstrap`
1. Optionally: Compile translations with `python manage.py compilemessages` (does not work on Windows, recommended to skip this step or see [docs](https://docs.djangoproject.com/en/4.0/topics/i18n/translation/#gettext-on-windows))
1. Optionally: Create test data with `python manage.py create_test_data`
1. Create a local superuser with `python manage.py createsuperuser`
1. Start the development server with `python manage.py runserver`
1. Optionally: Create test data with `python tools/create_test_data.py`
1. Open your web browser, visit `http://localhost:8000/admin` and log in with the user you just created

### Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import os
from datetime import date

import django

from myhpi.polls.models import PollList, RankedChoiceOption, RankedChoicePoll

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myhpi.settings")
django.setup()
import random
from datetime import date

from django.contrib.auth.models import Group, Permission, User
from django.core.files import File
from django.db import IntegrityError
from wagtail.contrib.redirects.models import Redirect
from wagtail.documents.models import Document
from wagtail.models import Collection, GroupCollectionPermission
from django.contrib.auth.models import Group, User
from django.core.management import BaseCommand

from myhpi.core.models import (
AbbreviationExplanation,
BasePage,
FirstLevelMenuItem,
InformationPage,
Minutes,
Expand All @@ -27,6 +14,7 @@
RootPage,
SecondLevelMenuItem,
)
from myhpi.polls.models import PollList, RankedChoiceOption, RankedChoicePoll
from myhpi.tests.core.setup import create_collections, create_documents


Expand Down Expand Up @@ -354,14 +342,18 @@ def create_some_pages(users, groups, documents):
slash_1999_poll.options.add(option_charlie)


def main():
# Superuser is created manually via createsuperuser command
users, groups = create_users_and_groups()
collections = list(create_collections(groups))
documents = create_documents([collections[1], collections[2]])
create_abbreviation_explanations()
create_some_pages(users, groups, documents)

class Command(BaseCommand):
help = "Creates test data (user, pages, etc.) for myHPI."

if __name__ == "__main__":
main()
def handle(self, *args, **options):
# Superuser is created manually via createsuperuser command
users, groups = create_users_and_groups()
jeriox marked this conversation as resolved.
Show resolved Hide resolved
collections = list(create_collections(groups))
documents = create_documents([collections[1], collections[2]])
create_abbreviation_explanations()
create_some_pages(users, groups, documents)
self.stdout.write(
self.style.SUCCESS(
'Test data created succesfully. Remember that you need to create a superuser manually with "python manage.py createsuperuser".'
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import zipfile

import requests
from django.core.management import BaseCommand

TEMP_DIRECTORY = "tmp"
ZIP_FILENAME = "bootstrap.zip"
Expand All @@ -13,8 +14,6 @@


def ensure_correct_directory():
if os.getcwd().endswith("tools"):
os.chdir("..")
try:
with open("pyproject.toml") as toml_file:
return "myHPI" in toml_file.readlines()[1]
Expand Down Expand Up @@ -95,50 +94,40 @@ def is_bootstrap_installed():
)


def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
usage="%(prog)s [-u]", description="Install bootstrap for myHPI."
)
parser.add_argument(
"-u",
"--update",
action="store_true",
help="Update (Remove and reinstall) current bootstrap installation.",
)
parser.add_argument(
"-r", "--remove", action="store_true", help="Remove current bootstrap installation."
)
parser.add_argument(
"--is-installed", action="store_true", help="Check if bootstrap is installed."
)
return parser


def install_bootstrap():
logger.setLevel(level=logging.INFO)
logger.addHandler(logging.StreamHandler())
parser = init_argparse()
args = parser.parse_args()
correct_dir = ensure_correct_directory()
if not correct_dir:
logger.error(
"The program was not executed in the correct directory!\n\
Ensure that it is run in the top directory of the repository."
class Command(BaseCommand):
help = "Install bootstrap for myHPI."

def add_arguments(self, parser):
parser.add_argument(
"-u",
"--update",
action="store_true",
help="Update (Remove and reinstall) current bootstrap installation.",
)
exit(1)
if args.is_installed:
is_installed = is_bootstrap_installed()
exit(0 if is_installed else 1)
if args.update or args.remove:
remove_current_bootstrap()
if args.remove:
exit(0)
file_path = download_zip()
extract_zip(file_path)
move_files()
remove_temporary_directory()
print("Installed bootstrap")


if __name__ == "__main__":
install_bootstrap()
parser.add_argument(
"-r", "--remove", action="store_true", help="Remove current bootstrap installation."
)
parser.add_argument(
"--is-installed", action="store_true", help="Check if bootstrap is installed."
)

def handle(self, *args, **options):
correct_dir = ensure_correct_directory()
if not correct_dir:
logger.error(
"The program was not executed in the correct directory!\n\
Ensure that it is run in the top directory of the repository."
)
exit(1)
if options["is_installed"]:
is_installed = is_bootstrap_installed()
exit(0 if is_installed else 1)
if options["update"] or options["remove"]:
remove_current_bootstrap()
if options["remove"]:
exit(0)
file_path = download_zip()
extract_zip(file_path)
move_files()
remove_temporary_directory()
self.stdout.write(self.style.SUCCESS("Installed bootstrap"))
Loading