Fakultas Ilmu Komputer UI

Commit b157ba8a authored by Annisa Devi Nurmalasari's avatar Annisa Devi Nurmalasari
Browse files

Merge branch 'annisa-grpc' into 'master'

Menambahkan grpc untuk GetResource user

See merge request !1
parents ec79d03d cf7a3c82
Pipeline #133858 passed with stage
in 2 minutes and 1 second
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"'
......
# 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/
```
Jika hanya menggunakan GitLab, kamu dapat menghapus berkas `tnd.yml`.
Jika hanya menggunakan GitHub, kamu dapat menghapus berkas `.gitlab-ci.yml`.
10. Buat direktori proyek kamu menjadi sebuah repositori Git dan buatlah
*commit* pertama.
```shell
git init
git add .
git commit -m "Initial commit"
```
11. Tambahkan *remote* baru bernama `origin` yang mengarah ke repositori yang
kamu buat di GitLab/GitHub, lalu push ke repositori tersebut.
```shell
git remote add origin https://gitlab.com/usernamekamu/project-name.git
git push -u origin master
```
12. Silakan cek **Pipelines** di GitLab atau Actions di GitHub. Jika
langkah-langkah diikuti dengan benar, maka proyek Django kamu akan berhasil
di-*deploy* ke Heroku.
> Contoh: https://django-template-heroku.herokuapp.com
13. Selamat! Sekarang, kamu hanya perlu fokus mengembangkan proyek web kamu
tanpa perlu pusing dengan masalah *deployment*. Untuk mengembangkan
proyek web kamu, silakan instal terlebih dahulu *package-package* yang
diperlukan dengan perintah berikut.
```shell
python -m pip install -r requirements.txt
```
14. Lanjutkan dengan membuat basis data lokal dan mengumpulkan
berkas *static* menjadi satu direktori dengan perintah-perintah berikut.
```shell
python manage.py migrate
python manage.py collectstatic
```
15. Jika sudah, kamu bisa menjalankan *web server* kamu secara lokal dengan
perintah berikut.
```shell
python manage.py runserver
```
16. Mulai dari sini, kamu cukup edit berkas-berkas proyek Django kamu
sesuai kebutuhan. Lalu, jangan lupa gunakan perintah `git add`,
`git commit`, dan `git push` untuk mengunggah perubahanmu ke GitLab/GitHub
(yang kemudian akan di-*deploy* ke Heroku). Jangan lupa untuk membuat
berkas-berkas *migrations* jika kamu mengubah berkas `models.py`.
```shell
python manage.py makemigrations
```
Berkas-berkas *migrations* yang dihasilkan **harus** dimasukkan ke dalam
repositori <sup><sub>(kecuali kamu mengubah konfigurasi templat sehingga
hal tersebut tidak diperlukan... tetapi mengapa?)</sub></sup>.
17. Untuk menjalankan *unit test*, kamu bisa gunakan perintah berikut.
```shell
python manage.py test --exclude-tag=functional
```
## Lanjutan
Setelah berhasil membuat proyek Django baru dengan templat ini, ada
langkah-langkah lanjutan yang sangat direkomendasikan untuk dikerjakan demi
kemudahan pengembangan web kamu ke depannya.
1. Tambahkan Django *super user* baru ke aplikasi kamu yang ada di Heroku.
Basis data yang digunakan di komputer lokal kamu **berbeda** dengan yang
digunakan di Heroku. Secara *default*, kamu menggunakan basis data SQLite
yang disimpan dalam berkas `db.sqlite3`, sedangkan Heroku menggunakan basis
data PostgreSQL yang di-*host* di *cloud*.
Untuk dapat mengakses situs administrasi Django milik proyek kalian yang ada
di Heroku, buka Heroku Dashboard. Lalu, klik aplikasi kalian dan klik tombol
**More > Run console** di pojok kanan atas. Ketik `bash` pada dialog
*pop-up* yang muncul. Jika sudah, masukkan perintah
`python manage.py createsuperuser` dan ikuti instruksinya.
> Psst, di dalam `bash`, kamu bisa lihat-lihat proyek Django kamu seperti
> dari cmd/terminal, lho! Artinya, kamu juga bisa memanggil perintah lain
> seperti `python manage.py migrate` atau bahkan perintah-perintah Linux
> *shell*.
2. Unduh `chromedriver` (atau *webdriver* lain yang kamu inginkan) untuk
komputer kamu agar bisa menjalankan *functional test* secara lokal.
**Ini akan sangat bermanfaat untuk salah satu *story*.**
Di Windows:\
Unduh [ChromeDriver][chromedriver] (`chromedriver_win32.zip`) untuk versi
Chrome yang kamu gunakan. Lalu, ekstrak berkas `.zip` tersebut dan letakkan
`chromedriver.exe` di direktori ini (setara dengan `manage.py`).
Di Linux:\
Beberapa distribusi Linux memiliki *package* khusus untuk ChromeDriver, atau
sebagian juga sudah menyertakannya di dalam *package* `chromium` (misal:
Arch Linux). Jika tidak ada *package* yang sesuai, kamu bisa unduh
`chromedriver_linux64.zip` melalui pranala di atas, lalu `unzip` dan
letakkan `chromedriver` ke salah satu direktori yang ada di `$PATH`.
Di macOS:\
Instal `chromedriver` dengan [Homebrew][homebrew]
(`brew cask install chromedriver` di Terminal). Silakan instal Homebrew
terlebih dahulu jika belum. Saya tidak punya perangkat dengan macOS, jadi
silakan cari solusinya apabila ada kendala yang muncul ;)
Untuk menjalankan *functional test* saja, gunakan perintah berikut:
```shell
python manage.py test --tag=functional
```