From 15d61b8c3279e9cd4ffe92d3cc43298615242fde Mon Sep 17 00:00:00 2001 From: sosokker Date: Thu, 14 Sep 2023 23:34:45 +0700 Subject: [PATCH] Add setup file and Edit Installation Guide --- README.md | 18 +++++++++++++++- setup.ps1 | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 setup.ps1 create mode 100644 setup.py diff --git a/README.md b/README.md index 3e73308..c0c51cb 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,23 @@ An application to conduct online polls and surveys based on the [Django Tutorial project](https://docs.djangoproject.com/en/4.2/intro/tutorial01/), with additional features. ## Install and Run +### Run Setup.py Method -1. Install [Python 3.11.4 or later](https://www.python.org/downloads/) +Clone this repository and Run `setup.py` to install and run the project + +**Don't forget to answer the question from `setup.py` to setup the project** +```bash +git clone https://github.com/Sosokker/ku-polls +cd ku-polls +python setup.py +``` + +or run `setup.ps1` (For Windows User) + +---- + +### Manual +1. Install [Python 3.11 or later](https://www.python.org/downloads/) 2. Run these commands to clone and install requirements.txt ```bash git clone https://github.com/Sosokker/ku-polls @@ -93,5 +108,6 @@ All project documents are in the [Project Wiki](https://github.com/Sosokker/ku-p - [Requirements](https://github.com/Sosokker/ku-polls/wiki/Requirements) - [Iteration1](https://github.com/Sosokker/ku-polls/wiki/Iteration-1-Plan) - [Iteration2](https://github.com/Sosokker/ku-polls/wiki/Iteration-2-Plan) +- [Iteration3](https://github.com/Sosokker/ku-polls/wiki/Iteration-3-Plan) [django-tutorial](https://docs.djangoproject.com/en/4.2/intro/tutorial01/) diff --git a/setup.ps1 b/setup.ps1 new file mode 100644 index 0000000..c2b48af --- /dev/null +++ b/setup.ps1 @@ -0,0 +1,59 @@ +$python_command = (Get-Command python.exe -ErrorAction SilentlyContinue).Source + +if ($python_command -eq $null) { + Write-Host "Error: The Python interpreter 'python.exe' is not found in your PATH." + exit 1 +} + +if (-not (Test-Path $python_command)) { + Write-Host "Error: The specified Python executable path '$python_command' does not exist." + exit 1 +} + +if (-not (Test-Path .venv)) { + Write-Host "Creating a new virtual environment..." + python -m venv .venv + .\.venv\Scripts\Activate +} else { + Write-Host "Using existing virtual environment." +} + +if ($setup_venv -eq "yes") { + if (-not (Test-Path (Get-Command virtualenv -ErrorAction SilentlyContinue))) { + Write-Host "Error: virtualenv is not installed. Please install it and rerun this script." + exit 1 + } + + python -m venv .venv + .\.venv\Scripts\Activate +} + +python -m pip install -r requirements.txt + +$secret_key = (python manage.py shell -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())') +@" +SECRET_KEY=$secret_key +DEBUG=False +ALLOWED_HOSTS=*.ku.th,localhost,127.0.0.1,::1 +TIME_ZONE=Asia/Bangkok +EMAIL_HOST_PASSWORD=ineedmorebullets +"@ | Set-Content -Path .env + +$text = @" +Django is now running in insecure mode for the static files gathering reason. +You can stop the server and run it again +"@ +$boxWidth = ($text | Measure-Object -Property Length -Maximum).Maximum + 4 +$topBorder = '+' + ('-' * ($boxWidth - 2)) + '+' +$sideBorder = '| ' + $text + (' ' * ($boxWidth - $text.Length - 4)) + ' |' +$bottomBorder = '+' + ('-' * ($boxWidth - 2)) + '+' + +Write-Host $topBorder +Write-Host $sideBorder +Write-Host $bottomBorder + + +python manage.py migrate +python manage.py loaddata data/users.json +python manage.py loaddata data/polls.json +python manage.py runserver --insecure \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..290e9ac --- /dev/null +++ b/setup.py @@ -0,0 +1,62 @@ +import os +import subprocess +import sys + +is_windows = os.name == 'nt' +is_posix = os.name == 'posix' + +def check_python_command(): + python_commands = ["python", "py", "python3"] + + for command in python_commands: + try: + subprocess.check_output([command, "--version"]) + return command + except FileNotFoundError: + continue + + return None + +python_command = check_python_command() + +if python_command is None: + print("Error: Python interpreter not found. Please specify the Python command (e.g., python, py, python3).") + sys.exit(1) + +setup_venv = input("Do you want to set up a virtual environment? (yes/no): ").lower() +if setup_venv == "yes": + if not os.path.exists(".venv"): + print("Creating a new virtual environment...") + subprocess.run([python_command, "-m", "venv", ".venv"]) + else: + print("Using an existing virtual environment.") + + if is_posix: + activate_command = os.path.join(".venv", "bin", "activate") + elif is_windows: + activate_command = os.path.join(".venv", "Scripts", "activate") + subprocess.run([activate_command], shell=True) + +subprocess.run([python_command, "-m", "pip", "install", "-r", "requirements.txt"]) + +secret_key = subprocess.check_output([python_command, "manage.py", "shell", "-c", + 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())']).decode().strip() + +with open(".env", "w") as env_file: + env_file.write(f"""SECRET_KEY={secret_key} +DEBUG=False +ALLOWED_HOSTS=*.ku.th,localhost,127.0.0.1,::1 +TIME_ZONE=Asia/Bangkok +EMAIL_HOST_PASSWORD=temppassword +""") + +subprocess.run([python_command, "manage.py", "migrate"]) +subprocess.run([python_command, "manage.py", "loaddata", "data/users.json"]) +subprocess.run([python_command, "manage.py", "loaddata", "data/polls.json"]) + +start_server = input("Do you want to start the Django server? (yes/no): ").lower() +if start_server == "yes": + print("=================================================") + print("Django run in --insecure mode to load Static File") + print("==================================================") + subprocess.run([python_command, "manage.py", "runserver", "--insecure"])