|
| 1 | +# eGSIM - Maintenance & Operations (web app) |
| 2 | + |
| 3 | +## Install for development |
| 4 | + |
| 5 | +Follow installation on github workflow, remember to add -e editable |
| 6 | +in the last command: `pip install -e ...` |
| 7 | + |
| 8 | +## Run tests (smtk lib only) |
| 9 | +```bash |
| 10 | +pytest -vvv ./tests/smtk |
| 11 | +``` |
| 12 | + |
| 13 | +## Run tests (web app) |
| 14 | + |
| 15 | +> Note: the value of `DJANGO_SETTINGS_MODULE` in the examples below |
| 16 | +> must be changed in production |
| 17 | +
|
| 18 | +Move in the `egsim directory` and type: |
| 19 | + |
| 20 | +```bash |
| 21 | +export DJANGO_SETTINGS_MODULE=egsim.settings.test; pytest -xvvv ./tests/ |
| 22 | +``` |
| 23 | +(x=stop at first error, v*=increase verbosity). |
| 24 | + |
| 25 | +with coverage report: |
| 26 | + |
| 27 | +```bash |
| 28 | +export DJANGO_SETTINGS_MODULE=egsim.settings.test; pytest --cov=egsim --cov-report=html -xvvv ./tests/ |
| 29 | +``` |
| 30 | + |
| 31 | +<details> |
| 32 | +<summary>Configure PyCharm</summary> |
| 33 | +For PyCharm developers, you need to configure the environment variable |
| 34 | +for all tests. Go to: |
| 35 | + |
| 36 | +- Run |
| 37 | + - Edit Configurations Templates ... |
| 38 | + - Python tests |
| 39 | + - pytest |
| 40 | + |
| 41 | +And then under **Environment variables:** add: |
| 42 | + |
| 43 | +`DJANGO_SETTINGS_MODULE=egsim.settings.test` |
| 44 | + |
| 45 | +(type several env vars separated by ;) |
| 46 | + |
| 47 | +</details> |
| 48 | + |
| 49 | +## Test GUI in local browser |
| 50 | + |
| 51 | +> Note: the value of `DJANGO_SETTINGS_MODULE` in the examples below |
| 52 | +> must be changed in production |
| 53 | +
|
| 54 | +> Note: the DB should have been created befoirehand(see dedicated section below) |
| 55 | +
|
| 56 | +Type: |
| 57 | + |
| 58 | +```bash |
| 59 | +export DJANGO_SETTINGS_MODULE="egsim.settings.dev";python manage.py runserver |
| 60 | +``` |
| 61 | + |
| 62 | +<details> |
| 63 | +<summary>Configure PyCharm</summary> |
| 64 | +For PyCharm developers, you can implement a service, which can be run as any |
| 65 | +PyCharm configuration in debug mode, allowing to open the browser |
| 66 | +and stop at specific point in the code (the PyCharm window will popup |
| 67 | +automatically in case). To implement a service, go to: |
| 68 | + |
| 69 | +- Run |
| 70 | + - Edit Configurations |
| 71 | + - Add new configuration |
| 72 | + |
| 73 | +then under **Run**: |
| 74 | + - between `script` and `module` (should be a combo box) choose `script`, |
| 75 | + and in the next text field put `manage.py` |
| 76 | + - script parameters: `runserver` |
| 77 | + - And then under **Environment variables:** add: |
| 78 | + `DJANGO_SETTINGS_MODULE=egsim.settings.dev` |
| 79 | + (type several env vars separated by ;) |
| 80 | + |
| 81 | +You should see in the `Services` tab appearing the script name, so you can |
| 82 | +run / debug it normally |
| 83 | + |
| 84 | +</details> |
| 85 | + |
| 86 | + |
| 87 | +## Packages upgrade |
| 88 | + |
| 89 | +**WHEN:** OpenQuake upgrade (e.g., new models to be made available) |
| 90 | + |
| 91 | +- Create a new virtual env, decide which Python version you want |
| 92 | + to use for the server, and install it if needed. |
| 93 | + |
| 94 | +- Look at the workflows run, and follow installation from creating |
| 95 | + a virtual env up to installing the desired OpenQuake version. |
| 96 | + **STOP after installing OpenQuake** and type: |
| 97 | + ```console |
| 98 | + pip freeze >./requirements.lib.txt && pip install pytest |
| 99 | + ``` |
| 100 | + |
| 101 | +- Run tests for smtk (see above). Fix ecode as needed |
| 102 | + |
| 103 | +- Install eGSIM web app, upgrading its dependencies: |
| 104 | + ```console |
| 105 | + pip install -U --upgrade-strategy eager ".[web]" |
| 106 | + pip freeze > ./requirements.txt |
| 107 | + ``` |
| 108 | + the upgrade strategy tries to upgrade stuff to the latest |
| 109 | + version (needed for e.g., Django), there might be conflicts |
| 110 | + though, fix conflicts in case |
| 111 | + |
| 112 | +- Run tests for web app (see above). Fix ecode as needed |
| 113 | + |
| 114 | +- Change `setup.py` and set the current OpenQuake version in |
| 115 | + `install_requires` (uncomment it if commented). Optionally, |
| 116 | + remove egsim from requirements.txt |
| 117 | + (it might interfere with Django web?*). |
| 118 | + |
| 119 | +- Eventually, **commit and push** |
| 120 | + |
| 121 | + |
| 122 | +## New data |
| 123 | + |
| 124 | +**WHEN:** New selectable flatfile or regionalization to be added |
| 125 | +to the platform. |
| 126 | + |
| 127 | +- Consult `README.md` in the `egsim-data` Nextcloud directory |
| 128 | + |
| 129 | +- Copy the newly created file(s) and the `media_files.yml` file in the project |
| 130 | + media directory (`MEDIA_ROOT` in the used Django settings file) |
| 131 | + |
| 132 | + |
| 133 | +## Django Database (sqlite DB) |
| 134 | + |
| 135 | +> Note: the value of `DJANGO_SETTINGS_MODULE` in the examples below |
| 136 | +> must be changed in production |
| 137 | +
|
| 138 | +**WHEN**: |
| 139 | + |
| 140 | + - **The DB needs to be emptied and repopulated** |
| 141 | + (e.g., OpenQuake is upgraded, or new regionalization or |
| 142 | + flatfile was added) |
| 143 | + |
| 144 | + - **The DB schema (columns, tables, constraints) |
| 145 | + has changed** (see `egsim.api.models.py`) |
| 146 | + |
| 147 | +To do so: |
| 148 | + |
| 149 | +- Delete or rename the database of the settings file |
| 150 | + |
| 151 | + (path is in the settings file variable `DATABASES['default']['NAME']`) |
| 152 | + |
| 153 | +- **If the DB Schema HAS CHANGED** (otherwise skip): |
| 154 | + |
| 155 | + - Delete the (only) migration file |
| 156 | + |
| 157 | + (should be `egsim/api/migrations/0001_initial.py`, if more than |
| 158 | + one migration file in the directory, delete all of them) |
| 159 | + |
| 160 | + - Recreate migration file (file to auto-populate the DB): |
| 161 | + ```bash |
| 162 | + export DJANGO_SETTINGS_MODULE="egsim.settings.dev";python manage.py makemigrations && python manage.py migrate && python manage.py egsim-init |
| 163 | + ``` |
| 164 | + |
| 165 | + - `git add` the newly created migration file |
| 166 | + |
| 167 | + (should be `egsim/api/migrations/0001_initial.py`) |
| 168 | + |
| 169 | +- Migrate (populate DB): |
| 170 | + ```bash |
| 171 | + export DJANGO_SETTINGS_MODULE="egsim.settings.dev";python manage.py migrate && python manage.py egsim-init |
| 172 | + ``` |
| 173 | + |
| 174 | +> Note: |
| 175 | +> eGSIM DB is extremely simple and rarely modified, recreating DB file all the time is cheaper than |
| 176 | +> managing migration files |
| 177 | + |
| 178 | +## Modify eGSIM DB data from the command line |
| 179 | + |
| 180 | +> Note: the value of `DJANGO_SETTINGS_MODULE` in the examples below |
| 181 | +> must be changed in production |
| 182 | + |
| 183 | +**WHEN**: mostly when you want to hide a flatfile, model or |
| 184 | +regionalization from the program usually temporarily (more complex |
| 185 | +modifications are possible but do it at your own risk) |
| 186 | + |
| 187 | +Execute the interactive command: |
| 188 | + ```bash |
| 189 | + export DJANGO_SETTINGS_MODULE="egsim.settings.dev";python manage.py egsim-db |
| 190 | + ``` |
| 191 | + |
| 192 | +> NOTE: |
| 193 | +> this Django command replaces the very expensive admin panel |
| 194 | +> (again, our DB is very simple) |
| 195 | + |
| 196 | + |
| 197 | +## Django utilities |
| 198 | + |
| 199 | +### Starting a Python terminal shell |
| 200 | + |
| 201 | +> Note: the value of `DJANGO_SETTINGS_MODULE` in the examples below |
| 202 | +> must be changed in production |
| 203 | + |
| 204 | +Typing `python` on the terminal does not work as one needs to |
| 205 | +initialize Django settings. The Django `shell` command does this: |
| 206 | + |
| 207 | +```bash |
| 208 | +export DJANGO_SETTINGS_MODULE="egsim.settings.dev";python manage.py shell |
| 209 | +``` |
| 210 | + |
| 211 | +### Get Django uploaded files directory |
| 212 | + |
| 213 | +If you did not set it explicitly in `settings.FILE_UPLOAD_TEMP_DIR` |
| 214 | +(by default is missing), then Django will put uploaded files |
| 215 | +in the standard temporary directory which you can get easily by |
| 216 | +typing: |
| 217 | + |
| 218 | +```bash |
| 219 | +python -c "import tempfile;print(tempfile.gettempdir())" |
| 220 | +``` |
0 commit comments