Last active
April 10, 2025 13:26
-
-
Save ragusa87/565337380f12b06e4c5b3a8b876ddb51 to your computer and use it in GitHub Desktop.
Python learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "6a6ecead-7dbe-4e9f-a098-1eeb89bd21d5", | |
"metadata": {}, | |
"source": [ | |
"# Django\n", | |
"\n", | |
"Getting started template: <https://github.com/liip/django-template>\n", | |
"\n", | |
"Usefull links:\n", | |
"\n", | |
"- [Remote debugging](https://dev.to/doctorx/remote-debug-with-pydevd-pycharm-for-pycharm-1g3b)\n", | |
"- [Django debug toolbar](https://django-debug-toolbar.readthedocs.io/)\n", | |
"- [Django Extensions](https://django-extensions.readthedocs.io) ([RunServerPlus with stacktrace and console](https://werkzeug.palletsprojects.com/en/stable/))\n", | |
"- [Django Admin theme Jazzmin](https://github.com/farridav/django-jazzmin)\n", | |
"\n", | |
"# Tooling\n", | |
"\n", | |
"- [html2pdf with weasyprint](https://weasyprint.org/)\n", | |
"- [Web Mapping Services (WMS)](https://www.geo.admin.ch/fr/wms-services-et-donnees-disponibles) - Layer maps (Ex:ch.swisstopo.swissimage)\n", | |
"\n", | |
"# Snippets\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "504e8bea-f600-460e-8c3c-f6b517e4c3b9", | |
"metadata": {}, | |
"source": [ | |
"```python\n", | |
"\n", | |
"import environ\n", | |
"env = environ.Env()\n", | |
"\n", | |
"env.bool(\"DEBUG\", False)\n", | |
"env.str(\"BACKEND_URL\", default=\"http://localhost\")\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b12e8290-b067-4bb5-8713-c2f5b64187b2", | |
"metadata": {}, | |
"source": [ | |
"```python\n", | |
"\n", | |
"def get_project_root_path():\n", | |
" \"\"\"\n", | |
" Return the absolute path to the root of the project.\n", | |
" \"\"\"\n", | |
" return os.path.abspath(os.path.join(os.path.dirname(__file__), \"..\", \"..\"))\n", | |
"\n", | |
"```\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ffcf52a7-2706-40a5-99a3-839e2f76081a", | |
"metadata": {}, | |
"source": [ | |
"## Views\n", | |
"\n", | |
"<https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/>\n", | |
"\n", | |
"Views are \"assigned\" to an URL, they render a response, if you stick to MVC, a Django's view is in fact A CONTROLLER. As the \"view\" is often rendered via a template (Django template).\n", | |
"\n", | |
"A view forkflow is as below:\n", | |
"* setup()\n", | |
"* dispatch() Usually dispatch will render the \"method\" related to the HTTP verb (def get vs def post)\n", | |
"* http_method_not_allowed()\n", | |
"* options()\n", | |
"\n", | |
"Render a template:\n", | |
"- [TemplateView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#templateview) Render a template\n", | |
"- [DetailView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-display/) You can \"automate\" fetching an object for a given view (get_object)\n", | |
"- [ListView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-display/#listview) Also there is predefined views for lists\n", | |
"- [FormView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-editing/#formview) Form view:\n", | |
"- [CreateView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-editing/#createview)\n", | |
"- [updateview](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-editing/#updateview)\n", | |
"- [DeleteView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-editing/#deleteview)\n", | |
"- [ArchiveIndexView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-date-based/#archiveindexview)\n", | |
"- [yeararchiveview](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-date-based/#yeararchiveview)\n", | |
"- [MonthArchiveView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-date-based/#montharchiveview)\n", | |
"- Etc.\n", | |
"\n", | |
"Mixins:\n", | |
"<https://docs.djangoproject.com/en/5.1/topics/class-based-views/mixins/>\n", | |
"\n", | |
"\n", | |
" Simple mixins\n", | |
" Single object mixins\n", | |
" Multiple object mixins\n", | |
" Editing mixins\n", | |
" Date-based mixins\n", | |
"\n", | |
"Mixin are class that you can \"extends\" to add some behaviour into your view.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9017d008-6554-4d4b-9a7c-a13554e660b4", | |
"metadata": {}, | |
"source": [ | |
"## Settings\n", | |
"\n", | |
"```python\n", | |
"from distutils.util import strtobool\n", | |
"import os\n", | |
"strtobool(os.getenv(\"TEST_UPDATED_EXPECTED_IMAGES\", \"false\"))\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "aea1409b-d6a1-4146-a8d8-6550fc6a1915", | |
"metadata": {}, | |
"source": [ | |
"## Forms\n", | |
"\n", | |
"Forms are a way to generate forms based on a Model (just like in SF). \n", | |
"Based on the model's field, it can auto-map all fields to a given form.\n", | |
"\n", | |
"Each form field can be rendered differently (using a widget).\n", | |
"<https://docs.djangoproject.com/en/5.1/topics/forms/modelforms/#django.forms.ModelForm>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d46c101a-7a12-4e5c-bdf0-95451d20d0d1", | |
"metadata": {}, | |
"source": [ | |
"## Decorators\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e97ca53c-1553-4bfa-8864-3978d3c9b1ca", | |
"metadata": {}, | |
"source": [ | |
"```python\n", | |
"from freezegun import freeze_time\n", | |
"@freeze_time(make_aware(datetime(1985, 7, 4), get_default_timezone()))\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "91aa7043-98a4-43b1-bf9f-4b4cb704a9b5", | |
"metadata": {}, | |
"source": [ | |
"## Tests\n", | |
"\n", | |
"```python\n", | |
"from django.test import LiveServerTestCase\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "81bca4d2-a92b-41e6-952d-95d635514a73", | |
"metadata": {}, | |
"source": [ | |
"# Query\n", | |
"Il faut utiliser `select_related` plutôt que `prefetch_related` pour les relations définies sur le modèle en question. `prefetch_related` pour les relations inverses (aka qui sont référencées par leur `related_name`)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4f663540-3566-407c-a1d3-7780d0fd00bc", | |
"metadata": {}, | |
"source": [ | |
"## Misc" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "4643b19f-51a8-4353-ac8f-22f6c52e7620", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{<PermitStepType.VALIDATION: 'validation'>: 1,\n", | |
" <PermitStepType.PUBLIC_INQUIRY: 'public-inquiry'>: 2,\n", | |
" <PermitStepType.DECISION: 'decision'>: 3,\n", | |
" <PermitStepType.CONSTRUCTION: 'construction'>: 5,\n", | |
" <PermitStepType.VISITS: 'visits'>: 6,\n", | |
" <PermitStepType.PERMIT: 'permit'>: 7}" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import enum\n", | |
"class PermitStepType(enum.Enum):\n", | |
" VALIDATION = \"validation\"\n", | |
" PUBLIC_INQUIRY = \"public-inquiry\"\n", | |
" DECISION = \"decision\"\n", | |
" DECISION_DISPATCH = \"decision-dispatch\"\n", | |
" CONSTRUCTION = \"construction\"\n", | |
" VISITS = \"visits\"\n", | |
" PERMIT = \"permit\"\n", | |
"\n", | |
"def get_step_helper():\n", | |
" steps = {\n", | |
" PermitStepType.VALIDATION: 1,\n", | |
" PermitStepType.PUBLIC_INQUIRY: 2,\n", | |
" PermitStepType.DECISION: 3,\n", | |
" PermitStepType.DECISION_DISPATCH: None,\n", | |
" PermitStepType.CONSTRUCTION: 5,\n", | |
" PermitStepType.VISITS: 6,\n", | |
" PermitStepType.PERMIT: 7,\n", | |
" }\n", | |
"\n", | |
" return {step_type: step for step_type, step in steps.items() if step is not None}\n", | |
"\n", | |
"get_step_helper()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.13.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment