File size: 4,274 Bytes
ba558e6
 
4114b0e
 
 
 
 
 
 
 
43c3bf3
4114b0e
d67a770
 
 
 
 
1c1ec0f
 
 
4114b0e
f72f77b
4114b0e
1c1ec0f
 
 
 
 
 
d67a770
4114b0e
1c1ec0f
 
 
4114b0e
d67a770
4114b0e
1c1ec0f
 
 
4114b0e
d67a770
 
 
 
 
 
 
 
 
9a1de81
d67a770
 
78b97ed
d67a770
 
 
 
 
43c3bf3
 
 
9a1de81
43c3bf3
 
 
 
d67a770
 
 
4114b0e
 
f28be2b
4114b0e
f28be2b
 
 
4114b0e
f28be2b
 
 
4114b0e
f28be2b
10521cb
f28be2b
 
 
1c1ec0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://github.com/codespaces/new?hide_repo_select=true&repo=pamelafox%2Fdjango-quiz-app)

# Quizzes app

An example Django app that serves quizzes and lets people know how they scored.
Quizzes and their questions are stored in a PostGreSQL database.
There is no user authentication or per-user data stored.

## Local development

Install the requirements and Git hooks:

This project has devcontainer support, so you can open it in Github Codespaces or local VS Code with the Dev Containers extension. If you're unable to open the devcontainer,
then it's best to first [create a Python virtual environment](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) and activate that.

1. Install the requirements:

    ```shell
    python3 -m pip install -r requirements-dev.txt
    ```

2. Create an `.env` file using `.env.sample` as a guide. Set the value of `DBNAME` to the name of an existing database in your local PostgreSQL instance. Set the values of `DBHOST`, `DBUSER`, and `DBPASS` as appropriate for your local PostgreSQL instance. If you're in the devcontainer, copy the values exactly from `.env.sample`.

3. Fill in a secret value for `SECRET_KEY`. You can use this command to generate an appropriate value.

    ```shell
    python -c 'import secrets; print(secrets.token_hex())'
    ```

3. Run the migrations:

    ```
    python3 manage.py migrate
    ```

4. Run the local server:

    ```
    python3 manage.py runserver
    ```

5. Navigate to "/quizzes" (since no "/" route is defined) to verify server is working.

### Admin

This app comes with the built-in Django admin.

1. Create a superuser:

```
python3 manage.py createsuperuser
```

2. Restart the server and navigate to "/admin"

3. Login with the superuser credentials.

### Testing

Run tests:

```
python3 manage.py collectstatic
coverage run --source='.' manage.py test quizzes
coverage report
```

The same tests are also run as a Github action.


## Deployment

This repository is set up for deployment on Azure App Service (w/PostGreSQL flexible server) using the configuration files in the `infra` folder.

1. Sign up for a [free Azure account](https://azure.microsoft.com/free/?WT.mc_id=python-79461-pamelafox)
2. Install the [Azure Dev CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd?WT.mc_id=python-79461-pamelafox). (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.)
3. Provision and deploy all the resources:

```
azd up
```

4. To be able to access `/admin`, you'll need a Django superuser. Navigate to the Azure Portal for the App Service, select SSH, and run this command:

```
python manage.py createsuperuser
```

## CI/CD pipeline

This project includes a Github workflow for deploying the resources to Azure
on every push to main. That workflow requires several Azure-related authentication secrets
to be stored as Github action secrets. To set that up, run:

```shell
azd pipeline config
```

### Costs

Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage.

You can try the Azure pricing calculator for the resources:

- Azure App Service: Basic Tier with 1 CPU core, 1.75GB RAM. Pricing is hourly. [Pricing](https://azure.microsoft.com/pricing/details/app-service/linux/)
- PostgreSQL Flexible Server: Burstable Tier with 1 CPU core, 32GB storage. Pricing is hourly. [Pricing](https://azure.microsoft.com/pricing/details/postgresql/flexible-server/)
- Virtual Network: Pricing based on data transfer. [Pricing](https://azure.microsoft.com/en-us/pricing/details/virtual-network/)
- Private DNS Zone: Pricing based on number of zones per region per month. [Pricing](https://azure.microsoft.com/en-in/pricing/details/dns/)
- Log analytics: Pay-as-you-go tier. Costs based on data ingested. [Pricing](https://azure.microsoft.com/pricing/details/monitor/)

⚠️ To avoid unnecessary costs, remember to take down your app if it's no longer in use,
either by deleting the resource group in the Portal or running `azd down`.

## Getting help

If you're working with this project and running into issues, please post in **Discussions**.