Fakultas Ilmu Komputer UI

Commit 5fe4a7fa authored by annisadevin's avatar annisadevin
Browse files

[CHORES] Initial commit

parents
Pipeline #123149 passed with stages
in 3 minutes and 55 seconds
# Meta
django-template-heroku-template.*
# Virtual environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Python cache files
__pycache__/
# SQLite database
db.sqlite3
db.sqlite3-journal
# User-uploaded media
media
# Collected static files
staticfiles/
# Coverage
.coverage
.coverage.*
coverage.*
htmlcov/
# Chromedriver
chromedriver
chromedriver.exe
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows shortcuts
*.lnk
image: python:3.8
stages:
- test
- deploy
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
paths:
- .cache/pip
# Feel free to remove this in your generated project.
.disable_on_template: &disable_on_template
rules:
- if: '$CI_COMMIT_REF_NAME != "template"'
.django_before_script: &django_before_script |-
pip install -r requirements.txt
python manage.py collectstatic --no-input
.coverage_artifacts: &coverage_artifacts
artifacts:
paths:
- .coverage*
UnitTest:
<<: *disable_on_template
<<: *coverage_artifacts
stage: test
before_script:
- *django_before_script
script:
- coverage run --parallel --include="./*" --omit="manage.py,auth_remindme/*"
manage.py test --exclude-tag=functional
FunctionalTest:
<<: *disable_on_template
<<: *coverage_artifacts
stage: test
before_script:
- apt-get update -q && apt-get install -yq chromium-driver
- *django_before_script
script:
- coverage run --parallel --include="./*" --omit="manage.py,auth_remindme/*"
manage.py test --tag=functional
Coverage:
<<: *disable_on_template
stage: deploy
before_script:
- pip install coverage
- coverage combine
script:
- coverage report -m
- coverage html
artifacts:
paths:
- htmlcov/
Deployment:
rules:
- if: '$CI_COMMIT_REF_NAME == "master"'
image: ruby:2.7
stage: deploy
before_script:
- gem install dpl
- wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh
script:
- dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_API_KEY
- heroku run --app $HEROKU_APP_NAME migrate
environment:
name: production
url: https://$HEROKU_APP_NAME.herokuapp.com
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <https://unlicense.org>
migrate: python manage.py migrate
web: gunicorn auth_remindme.wsgi
# django-template-heroku
[![Test and Deploy][actions-badge]][commits-gh]
[![pipeline status][pipeline-badge]][commits-gl]
[![coverage report][coverage-badge]][commits-gl]
This repository contains a template for creating a Django project ready to be
deployed to Heroku via GitHub Actions or GitLab CI.
*Untuk melihat berkas ini dalam bahasa Indonesia,
[klik di sini][readme-id].*
## Table of contents
- [Table of contents](#table-of-contents)
- [Usage instructions](#usage-instructions)
- [Additional steps](#additional-steps)
- [Development tips](#development-tips)
- [What kind of magic is this?](#what-kind-of-magic-is-this)
- [Contributing](#contributing)
- [License](#license)
## Usage instructions
1. Create a directory for the project that you want to create (example:
`auth_remindme`), then open a Command Prompt (cmd) or Terminal in
that directory.
2. Create a Python virtual environment in it.
```shell
python -m venv venv
```
> Note: please adjust the command with the `python` executable on your
> computer, because sometimes (example: on Ubuntu or macOS) Python 3
> can only be executed using `python3`, not `python`.
3. Activate the virtual environment that was just created.
On Windows:
```shell
venv\Scripts\activate
```
On Linux/macOS:
```shell
source venv/bin/activate
```
If successful, there should be `(venv)` in your cmd/terminal prompt.
4. Install Django in the virtual environment.
```shell
python -m pip install Django
```
5. Create a new Django project with this template.
```shell
django-admin startproject --template="https://codeload.github.com/laymonage/django-template-heroku/zip/template" --extension="py,yml,md" --name="Procfile" auth_remindme .
```
> Note: rename `auth_remindme` with the desired project name. Notice
> that there is a period (`.`) at the end of the command. If there's an
> error when downloading the template from GitHub, please visit the link
> manually and point the `--template` argument to the location of your
> `.zip` file that you downloaded to your computer.
6. Sign in to [Heroku Dashboard][heroku-dashboard] and create a new Heroku
application. The application name **does not have to be the same** as the
name of your Django project. Then, open up the **Settings** of the
application. In the **Config Vars** section, click the **Reveal Config
Vars** button. Add a variable named `HEROKU_APP_NAME` with the name of your
Heroku app **without `.herokuapp.com`** as its value. Then, add another
variable named `SECRET_KEY` using a value generated by the
[Djecrety][djecrety] site.
7. Create a new project/repository on GitLab/GitHub (choose one). **Do not**
select the options to initialize the repository with `README.md`, license,
or `.gitignore`. Just leave them blank.
8. If you use GitLab, go to the **Settings** menu in the left sidebar. In the
**Variables** section, add a variable named `HEROKU_API_KEY` with the API
key of your Heroku account as its value. Then, add another variable named
`HEROKU_APP_NAME` with your Heroku app name **without `.herokuapp.com`** as
its value.
If you're using GitHub, do the same through the **Settings > Secrets** menu.
> Note: Your Heroku account's API key can be viewed on [**Account
> settings**][account-settings].
9. **If you use GitHub**, move the `tnd.yml` file to the `.github/workflows`
directory with the following commands.
On Windows:
```shell
mkdir ".github\workflows"
move tnd.yml ".github\workflows\"
```
On Linux/macOS:
```shell
mkdir -p .github/workflows
mv tnd.yml .github/workflows/
```
If you only use GitLab, you can delete the `tnd.yml` file. If you only use
GitHub, you can delete the `.gitlab-ci.yml` file.
10. Turn your project directory into a Git repository and create an initial
commit.
```shell
git init
git add .
git commit -m "Initial commit"
```
11. Add a new remote named `origin` that points to the repository you created
on GitLab/GitHub, then push to that repository.
```shell
git remote add origin https://gitlab.com/yourusername/project-name.git
git push -u origin master
```
12. Please check **Pipelines** on GitLab or Actions on GitHub. If the steps are
followed correctly, then your Django project will be successfully deployed
to Heroku.
> Example: https://django-template-heroku.herokuapp.com
13. Congratulations! Now, you just need to focus on developing your web project
without having to worry about deployment problems. Before developing your
web project, install the required packages with the following command.
```shell
python -m pip install -r requirements.txt
```
14. Continue by creating a local database and collecting static files into a
single directory with the following commands.
```shell
python manage.py migrate
python manage.py collectstatic
```
15. After that, you can run your web server locally with the following command.
```shell
python manage.py runserver
```
16. From here, you just need to edit your Django project files as necessary.
Then, don't forget to use the `git add`, `git commit`, and `git push`
commands to upload your changes to GitLab/GitHub (which will then be
deployed to Heroku). Don't forget to make migration files if you change the
`models.py` file.
```shell
python manage.py makemigrations
```
The generated migration files should be committed into the repository
<sup><sub>(unless you change the template configuration so that it's not
necessary... but why?)</sub></sup>.
17. To run the unit tests, you can use the following command.
```shell
python manage.py test --exclude-tag=functional
```
## Additional steps
After successfully creating a new Django project with this template, there are
some highly recommended additional steps you can do in order to ease your web
development in the future.
1. Add a new Django super user to your Heroku app.
The database used on your local computer is **different** to the one that's
used on Heroku. By default, you use the SQLite database stored in the
`db.sqlite3` file, while Heroku uses the PostgreSQL database hosted in the
cloud.
To be able to access your existing Django administration site on Heroku,
open the Heroku Dashboard. Then, click on your app and click the **More >
Run console** option in the top right corner. Type `bash` in the pop-up
dialog that appears. Then, enter the command `python manage.py
createsuperuser` and follow the instructions.
> Psst, inside `bash`, you can browse your Django projects just like from
> the cmd/terminal! That means, you can also use other commands such as
> `python manage.py migrate` or even Linux shell commands.
2. Download `chromedriver` (or any other webdriver you want) to your computer
in order to run functional tests locally. **This will be very useful for one
of the stories.**
On Windows:\
Download [ChromeDriver][chromedriver] (`chromedriver_win32.zip`) for the
Chrome version that you use. Then, extract the `.zip` file and place the
`chromedriver.exe` in this directory (same level as `manage.py`).
On Linux:\
Some Linux distributions have their own ChromeDriver packages, or some have
also included it in the chromium package (e.g. Arch Linux). If there is no
suitable package, you can download `chromedriver_linux64.zip` from the link
above, then `unzip` and put `chromedriver` into one of the existing
directories in your `$PATH`.
On macOS:\
Install `chromedriver` with [Homebrew][homebrew] (`brew cask install
chromedriver` on the Terminal). Please install Homebrew first if you
haven't already. I don't have a macOS device, so please find a solution on
your own if there's any problem ;)
To run the functional tests only, use the following command:
```shell
python manage.py test --tag=functional
```
> Note: if you want to use a different webdriver, please customize the
> `tests.py` file and install the webdriver accordingly. If you want to see
> the functional tests in action on your computer, turn off the `headless`
> option in `tests.py`. However, don't forget to turn it back on because
> the option is required by GitLab CI/GitHub Actions. Make sure you've run
> the `collectstatic` command before running the functional tests.
> Note for Windows users: when you run functional tests, an error such as
> `ConnectionResetError: [WinError 10054] An existing connection was
> forcibly closed by the remote host` may appear. As long as the tests
> finish successfully, just ignore the error. This is a [bug][ticket-21227]
> which has not been fixed for Django on Windows.
3. Set the code coverage configuration on GitLab.
Code coverage is a useful metric for measuring how much of your code is
covered by tests. GitLab has features for capturing code coverage from a
job log in the Pipelines. To set it up, go to **CI/CD > General
pipelines**. In the **Test coverage parsing** section, fill in the
following regular expressions: `^TOTAL.*s+(d+%)$` (what is this for?), and
save it.
Later, in the **Coverage report** section there will be a coverage badge of
your project. You can add this badge into the `README.md` file just like
this one.
To run the tests with coverage on your local computer, use the following
command.
```shell
coverage run --include="./*" --omit="venv/*,manage.py,auth_remindme/*" manage.py test
```
> Note: this command runs both unit tests and functional tests at once,
> so make sure you've run the `collectstatic` command before running the tests.
## Development tips
1. From now on, get used to reloading your browser with
<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>R</kbd> (you can also use
<kbd>Shift</kbd>+<kbd>F5</kbd> on some browsers), not just
<kbd>Ctrl</kbd>+<kbd>R</kbd> or <kbd>F5</kbd>. Otherwise, when you develop
the web locally, you may often find that the static files that are loaded on
your browser do not change after you modify it. This happens because the
browser caches the static files for faster access. By pressing
<kbd>Shift</kbd>, the browser will [bypass the cached static
files][bypass-cache].
2. Get used to writing quality tests for your web projects, especially for the
logic parts such as those in the views layer. Writing tests will make it
easier for you to find bugs in your program early on. Besides, tests can
also prevent you from creating new bugs when you develop your web project.
3. 100% code coverage does not mean your project is bug-free. However, having
tests that assess your project as a whole will certainly be very useful.
Start writing tests every time you create a new functionality to maintain
your code coverage.
4. This template intentionally does not include linting configurations such as
using [`flake8`][flake8] or [`pylint`][pylint]. This is done to avoid
warnings from appearing on your GitHub Actions or GitLab CI if your code
does not comply with the rules applied by the linter. If you don't want to
be bothered with code style, I suggest using [`black`][black] and
[`isort`][isort]. It's a good idea to create a configuration to run the
linters on GitHub Actions or GitLab CI for your project.
## What kind of magic is this?
The `django-admin` tool provides the [`--template`][template] option for the
`startproject` command that accepts a path to a template of a Django project.
The path can be the location of a local file/directory or the URL for an
archive file. GitLab/GitHub provides the option to download repositories as
`.zip` archives... you can figure out the rest :)
This template will then be rendered with variables that can be applied when
running the `startproject` command. It works similarly to Django templates and
context variables that can be rendered into it.
Please use this template as a **learning resource as much as possible**. It is
true that this template can make it easier for you to create Django projects
that are ready to be deployed without having to deal with many configurations.
However, it will be very beneficial for you if you understand how the
configurations work in this template.
## Contributing
If you would like to contribute to this template, please create an issue or
submit a pull request to the repository for this template at
[**GitHub**][repo-gh]. This repository is also mirrored to [GitLab][repo-gl]
for demonstration purposes.
## License
This template is distributed under [The Unlicense][license] license. Projects
generated with this template may be distributed under different licenses.
[actions-badge]: https://github.com/laymonage/django-template-heroku/workflows/Test%20and%20Deploy/badge.svg
[commits-gh]: https://github.com/laymonage/django-template-heroku/commits/master
[pipeline-badge]: https://gitlab.com/laymonage/django-template-heroku/badges/master/pipeline.svg
[coverage-badge]: https://gitlab.com/laymonage/django-template-heroku/badges/master/coverage.svg
[commits-gl]: https://gitlab.com/laymonage/django-template-heroku/-/commits/master
[readme-id]: README.md
[heroku-dashboard]: https://dashboard.heroku.com
[djecrety]: https://djecrety.ir
[account-settings]: https://dashboard.heroku.com/account
[chromedriver]: https://chromedriver.chromium.org/downloads
[homebrew]: https://brew.sh
[ticket-21227]: https://code.djangoproject.com/ticket/21227
[bypass-cache]: https://en.wikipedia.org/wiki/Wikipedia:Bypass_your_cache
[flake8]: https://pypi.org/project/flake8
[pylint]: https://pypi.org/project/pylint
[black]: https://pypi.org/project/black
[isort]: https://pypi.org/project/isort
[template]: https://docs.djangoproject.com/en/4.0/ref/django-admin/#cmdoption-startproject-template
[repo-gh]: https://github.com/laymonage/django-template-heroku
[repo-gl]: https://gitlab.com/laymonage/django-template-heroku
[license]: LICENSE
# auth_remindme
[![Test and Deploy][actions-badge]][commits-gh]
[![pipeline status][pipeline-badge]][commits-gl]
[![coverage report][coverage-badge]][commits-gl]
Repositori ini berisi sebuah templat untuk membuat proyek Django yang siap
di-*deploy* ke Heroku melalui GitHub Actions atau GitLab CI.
*To view this file in English, [click here][readme-en].*
## Daftar isi
- [Daftar isi](#daftar-isi)
- [Instruksi penggunaan](#instruksi-penggunaan)
- [Lanjutan](#lanjutan)
- [Tips pengembangan](#tips-pengembangan)
- [Sihir macam apa ini?](#sihir-macam-apa-ini)
- [Berkontribusi](#berkontribusi)
- [Lisensi](#lisensi)
## Instruksi penggunaan
1. Buat direktori untuk proyek yang akan kamu buat (contoh: `project_name`), lalu
buka Command Prompt (cmd) atau Terminal di dalam direktori tersebut.
2. Buat Python *virtual environment* di dalamnya.
```shell
python -m venv venv
```
> Catatan: sesuaikan dengan *executable* `python` yang ada di komputer kamu,
> karena terkadang (misal: di Ubuntu atau macOS) Python 3 hanya bisa
> dipanggil dengan `python3`, bukan `python`.
3. Aktifkan *virtual environment* yang telah dibuat.\
Di Windows:
```shell
venv\Scripts\activate
```
Di Linux/macOS:
```shell
source venv/bin/activate
```
Jika berhasil, akan muncul `(venv)` pada *prompt* cmd/terminal kamu.
4. Instal Django pada *virtual environment* tersebut.
```shell
python -m pip install Django
```
5. Buat proyek Django baru dengan templat ini.
```shell
django-admin startproject --template="https://codeload.github.com/laymonage/django-template-heroku/zip/template" --extension="py,yml,md" --name="Procfile" project_name .
```
> Catatan: ubah `project_name` dengan nama proyek yang kamu inginkan.
> Perhatikan bahwa terdapat tanda titik (`.`) di akhir perintah.
> Apabila muncul *error* terkait pengunduhan templat dari GitHub, silakan
> kunjungi pranala tersebut secara manual dan arahkan argumen `--template`
> ke lokasi berkas `.zip` yang sudah kamu unduh ke komputer kamu.
6. Masuk ke [Heroku Dashboard][heroku-dashboard] dan buat aplikasi Heroku baru.
Nama aplikasi ini **tidak harus sama** dengan nama proyek Django kamu. Lalu,
buka bagian **Settings** dari aplikasi tersebut. Pada bagian **Config
Vars**, klik tombol **Reveal Config Vars**. Tambahkan variabel bernama
`HEROKU_APP_NAME` yang bernilai nama aplikasi Heroku kamu **tanpa
`.herokuapp.com`**. Lalu, tambahkan juga variabel bernama `SECRET_KEY`
dengan nilai berupa sebuah kunci yang dihasilkan oleh situs
[Djecrety][djecrety].
7. Buat *project*/repositori baru di GitLab/GitHub (pilih salah satu).
**Jangan** pilih opsi untuk menginisialisasi repositori dengan `README.md`,
`LICENSE`, ataupun `.gitignore`. Biarkan kosong saja.
8. Jika menggunakan GitLab, buka menu **Settings > CI/CD** di *sidebar*
sebelah kiri. Pada bagian **Variables**, tambahkan variabel bernama
`HEROKU_API_KEY` yang bernilai API *key* akun Heroku kamu. Lalu, tambahkan
juga variabel bernama `HEROKU_APP_NAME` yang bernilai nama aplikasi Heroku
kamu **tanpa `.herokuapp.com`**.
Jika menggunakan GitHub, lakukan hal yang sama melalui menu **Settings >
Secrets**.
> Catatan: API *key* akun Heroku kamu dapat dilihat melalui
> [**Account settings**][account-settings].
9. **Jika menggunakan GitHub**, pindahkan berkas `tnd.yml` ke direktori
`.github/workflows` dengan cara berikut.
Di Windows:
```shell
mkdir ".github\workflows"
move tnd.yml ".github\workflows\"
```
Di Linux/macOS:
```shell
mkdir -p .github/workflows
mv tnd.yml .github/workflows/
```