Commit 710a5417 authored by Florent Chehab's avatar Florent Chehab

feat(documentation): more documentation on app specific things

* Documentation of backend specific things
* Docuentation of the use of redux in the frontend
* Cleaned documentation elsewhere

Related to #74
parent 4720be82
Pipeline #37098 passed with stages
in 5 minutes and 59 seconds
......@@ -3,35 +3,60 @@ Words relative to *config files*
<!-- This file is referenced in the config_files, if you modify its location, please update those files too. -->
There exist different kinds of *config files* throughout the app and all of them are absolutely necessary, so it's good to understand what is their purpose.
## Django config files
!> TODO
Django's main config file in `backend/base_app/settings.py`. This file is a standard Django one and defines many important aspects of the app such as:
- Which packages should be bundled with the app,
- What Middleware to use,
- Custom behaviors depending on if we are in testing, developing or production environment,
- Config to serve the frontend files from the backend,
- App specific settings,
- Database settings,
- Etc.
## Custom config files
To centralize the definition of certain certain Models and (all) ViewSets attributes, we centralized a lot of information in few configuration files. Some of those files are also read at compile time when the frontend is built to automatically generate the required `Redux` actions and reducers.
To centralize the definition of certain certain Models and (all) viewsets attributes, we gathered a lot of information in few configuration files.
-----
?> For your information, some of those files are also read at *frontend-*compile time to automatically generate the required `Redux` actions and reducers.
!> :warning: Models are imported in Django as soon as (and only if) a corresponding viewset is registered in the config files. :warning:
!> TODO more on actions and reducers for the frontend.
*A viewset is (most often) associated with a serializer class and a serializer class is associated with a model, so we can transparently go up the chain.*
Below is the documentation about those files.
?> If for some reason you need to bypass this behavior, then you will need to follow the standard way of using Django and register the models in `backend/backend_app/admin.py` and the viewsets in `backend/backend_app/urls.py`.
*NB: all those files use the `yaml` syntax.*
-----
Below is the documentation about those custom config files that are located in the folder `backend/backend_app/config`, along we some python scripts to help handle them.
*NB: all those files use the awesome `yaml` syntax.*
### defaults.yaml
Contains default values, nothing special to say here.
Contains default values use across the app, nothing special to say here.
### viewsets_config.yml
This file contains the configuration of the (all) the app viewsets. It has a key/value structure with key the ViewSet name.
This file contains the configuration of the (all) the app viewsets. It has a key/value (dict) structure with key the viewset name.
Each viewset has the following required required attributes:
Each viewset entry is defined with the following required required attributes:
* `api_end_pont`: it is the main part of the url for making request to the api. This string will also be used for naming variables in JS, so keep it simple and consistent please. :smile:
* `import_location`: in what file path (dot like notation) is the viewset available ? You should only specify the part that comes after `backend_app.models.`. *NB: the associated model should be available from the same path.*
* `import_location`: in what file path (dot like notation - just like any python import) is the viewset available ? You should only specify the part that comes after `backend_app.models.`. *NB: the associated model should be available from the same path.*
!> **If you change an `api_end_pont` you will most likely need to update some JS files (see [here](Application/Frontend/redux.md)).**
TODO direct link to section
There can also be optional parameters:
......@@ -47,18 +72,16 @@ If no value is taken, then the default one is added in the `load_viewsets_config
### models_config.yml
This file contains the configuration of the app models. It has a key/value structure with key the model name.
This file contains the configuration of the app models. It has a key/ (dict) structure with key the model name.
For now, there is only one attribute which can be set: `moderation_level`. This attribute tells how moderation should be applied to each model.
Moderation levels are defined as follow :
* `0` : moderation will never be applied,
* `1` : moderation will be on if the global settings for moderation is turned on,
* `2` : (default for security reasons) moderation will always be on no matter what.
Moderation levels are defined as follow:
*NB: staff members, dri members and moderators won't be subject to moderation.*
* `0`: moderation will never be applied,
* `1`: moderation will be on if the global settings for moderation is turned on,
* `2`: (default for security reasons) moderation will always be on no matter what.
*NB: moderation can be moreover enforced at the object level. But that's for another documentation part.*
*NB: staff members, dri members and moderators won't be subject to (model-level) moderation.*
!> TODO doc about this.
*NB: moderation can be moreover enforced at the object level. But that's for [another documentation part](Application/Backend/moderation_and_versioning.md).*
......@@ -4,7 +4,7 @@ Models, Serializers and ViewSets
## Basics
Our backend app is based on Django. If you are familiar with Django then you will understand what is a Model.
The general idea is that Django is used to handle the data and nothing else. Lets say that we are only using the "M" of "MVC" of a standard Django architecture.
For this project, the general idea is that Django is used to handle the data and nothing else. Lets say that we are only using the "M" of "MVC" of a standard Django architecture.
To be able to transfer data to the frontend, we use the famous Django extension [django-rest-framework](https://www.django-rest-framework.org/).
......@@ -13,9 +13,15 @@ To be able to transfer data to the frontend, we use the famous Django extension
To put it into a nutshell, we have:
- Models that handle all data and the exchanges with the database,
- Serializers that handle the conversion from models' data to JSON and from JSON to models' data,
- ViewSets that handle communication with the frontend.
- **Models** that handle all data and the exchanges with the database,
- **Serializers** that handle the conversion from models' data to JSON and from JSON to models' data,
- **ViewSets** that handle communication with the frontend.
!> :warning: **Before moving to the details of the models/serializers/viewsets you must now that a lot is automated in the REX-DRI backend. Especially regarding *importing* those classes in Django admin and registering the viewsets with their configuration. All is centralized in *config files* (make sure to read the [doc about them](Application/Backend/config_files.md): if you add a Model/Viewset you WILL need to *register* them in those config files for them to be imported at runtime.** :warning:
*NB: as it is explained in the previously cited [documentation page](Application/Backend/config_files.md), models are registered "through" the associated viewset.*
## In details
......@@ -23,18 +29,19 @@ Since the process of creating the API was a bit repetitive, we have used a lot o
Below are the things to know about what has been implemented.
### Models
There exist different *abstract* models the "real" models can inherit from and have different behaviors.
#### App specific
There exist different *abstract* models from which the *concrete* models can inherit from. Those *abstract* models have different behaviors and properties built in directly.
- `MyModel`: contains attribute that should be shared by all models in the app such as for storing who updating something, etc.
- `MyModelVersionned`: inherits from `MyModel` and make it possible to version the data contain in the models that inherit from it.
- `BasicModule`: inherits from `MyModelVersionned` and provides a set of attributes such a title, a comment, a custom useful links field and an importance level.
Below will be outdated soon.
?> :warning: what is said below will soon be unnecessary: https://gitlab.utc.fr/rex-dri/rex-dri/issues/85
!> Models that inherit from `MyModelVersionned` (and therefore `BasicModule`) must have the following calls method:
Models that inherit from `MyModelVersionned` (and therefore `BasicModule`) must have the following class method that returns the associated serializer:
```python
class MyAwesomeClass(MyModelVersionned):
......@@ -45,3 +52,152 @@ class MyAwesomeClass(MyModelVersionned):
```
This method is used in the `VersionSerializer` class to convert version data back to a model instance.
#### General idea
So to define a model for the app, you should (in most cases) use one of the three base classes defined above. What remains is to define a model just like you would do for any Django Model. Here is an example (you have tons of them in app already):
```python
from django.db import models
from backend_app.models.abstract.my_model import MyModel
class Country(MyModel):
name = models.CharField(max_length=200)
iso_alpha2_code = models.CharField(primary_key=True, max_length=2)
iso_alpha3_code = models.CharField(
unique=True, max_length=3, default="", blank=True
)
region_name = models.CharField(max_length=200)
region_un_code = models.CharField(max_length=3)
sub_region_name = models.CharField(max_length=200, default="", blank=True)
sub_region_un_code = models.CharField(max_length=3, default="", blank=True)
intermediate_region_name = models.CharField(max_length=200, default="", blank=True)
intermediate_region_un_code = models.CharField(max_length=3, default="", blank=True)
```
### Serializers
#### App specific
To go along the previously mentioned *abstract* models, we have matching serializers that handle all the logic behind rendering the models' field to JSON, validating the data before creating/updating a model instance and handling the specific behaviors (such as moderation — that comes with all models that inherit from `MyModel` — and versioning — that comes with all models that inherit from `MyModelVersionned`).
:warning: **95% of the *magic* behind those not that simple behaviors are handled in a subtle, sometimes *hacky*, manner.**
:information_desk_person: All those specific behaviors should have been correctly unit-tested at this point.
?> You can find more information about moderation and versioning [here](Application/Backend/moderation_and_versioning.md).
#### General idea
To create a serializer for your model, you need to create a class that inherits from the matching parent class of your model. For instance the `Country` model inherits from the `MyModel` *abstract* class, so the `CountrySerializer` inherits from `MyModelSerializer`.
Then you must define a `Meta` class inside the serializer that countains information about what model is the serializer for and what fields (attribute) should be taken into account. For instance for the country model we have the following serializer:
```python
class CountrySerializer(MyModelSerializer):
class Meta:
model = Country
fields = "__all__"
```
So the serialization of all fields will be handled automatically by `django-rest-framework`.
You may also decide to exclude some fields:
```python
class UniversitySerializer(MyModelSerializer):
class Meta:
model = University
exclude = ("utc_id",)
```
And even include custom serializer to make some fields read only, or add data to send to the client:
```python
class MyModelSerializer(MySerializerWithJSON):
# ...
# Extract of the definition of the class
# This field is "smartly" set by the serializer on save
# and should be considered as readonly.
updated_by = serializers.CharField(read_only=True)
# For easier handling on the client side, we force an id field
# this is useful when a model has a dedicated primary key
id = serializers.SerializerMethodField()
def get_id(self, obj: MyModel):
"""
Serializer for the id field.
"""
return obj.pk
class Meta:
model = MyModel
# ...
```
As shown above, when using a `custom_attr = serializers.SerializerMethodField()`, you need to define a method in the class that is named `get_custom_attr` and returns whatever you want (as soon as it can be converted to JSON).
?> Django-rest-framework is full a functionalities, and only a subset of them have been presentend here. So feel free to have a look at [the package documentation](https://www.django-rest-framework.org/).
### Viewsets
#### App specific
The custom work performed on the generic viewsets is fairly straightforward, you can have a look at it. Anyway, just like with the serializer, you should use the corresponding viewset when inheriting: `MyModelViewset` or `MyModelVersionnedViewset` or `BasicModuleViewSet`.
The general idea is that a good chunk of the parametrization of the viewsets have been moved away from the class definition to the [config files](Application/Backend/config_files.md).
#### General idea
Most often a wiewset class will look like this, with a simple reference to the associated serializer class:
```python
class MyModelVersionnedViewSet(MyModelViewSet):
"""
Viewset for the versionned models
"""
serializer_class = MyModelVersionnedSerializer
```
Sometimes you will also need to filter the data against some attribute found in the client request:
```python
class CountryDriViewSet(BasicModuleViewSet):
queryset = CountryDri.objects.all() # pylint: disable=E1101
serializer_class = CountryDriSerializer
def get_queryset(self):
country_id = self.kwargs["country_id"]
return super().get_queryset().filter(countries__pk=country_id).distinct()
```
Or also prefetch some related attributes for optimization purposes:
```python
# Extract of the class definition
class MyModelViewSet(viewsets.ModelViewSet):
def get_queryset(self):
"""
Extended default rest framework behavior
to prefetch some table and enhance performances
"""
return self.queryset.prefetch_related("moderated_by", "updated_by")
```
?> If you are familiar with `REST` api, you will surely be familiar with filtering elements directly in the request with a syntaxe such as `url/?username=bob&isOld=true`. This syntax is not yet supported, since we didn't need it up until now.
## Keeping things consistent
!> :warning: To keep things consistent the general idea is that if a model is called `Blabla` than it's serializer class will be named `BlablaSerializer` and the associated viewset class will be named `BlablaViewSet`.
:information_desk_person: Sometimes it will be useful to have different viewsets for the same serializer, for instance if you want an api endpoint that returns the data filtered in a certain way.
!> You should avoid having multiple serializers for the same model since it may brake things related to deserializing previous versions of models.
Moderation & Versionning in the app
=================================
## Moderation
### Application level
!> TODO
### Model level
!> TODO
### Object/instance level
!> TODO
## Versioning
### General idea
!> TODO
Few words on optimization
===============
TODO optimization of the request.
Quick intro to React
===============
?> :information_desk_person: `React` is a JavaScript library to build user interface and it was introduced by Facebook.
## General idea
When doing web development, you should be familiar with the *DOM* ([Document Object Model](https://fr.wikipedia.org/wiki/Document_Object_Model)) which is basically a tree translation of the HTML file you send to the browser for rendering.
React is built on a *virtual DOM* (you can explore it with the [React Developer Tools](https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi) extension for chromium based browsers). The elements of this *virtual DOM* are no longer HTML tags; they are real *living* objects called **components** that can be modified et that can perform multiple actions. Every time an update to is performed, the DOM in your browser is smartly updated by react based on its own internal *virtual DOM*.
Components being elements of tree like structure, they must have a parent (or be the root node) and they can have as many *children* components as they want.
## TODO more
This diff is collapsed.
......@@ -3,11 +3,21 @@ Frontend tests
*Rex-DRI* comes in with several *tests* that you can perform locally using the commands `make test_backend` and `make test_frontend` once the project is up and running (`make up`).
?> :information_desk_person: As of now, the frontend tests only concern specific functions and not react components.
## General words
The frontend tests are performed with `jest` framework and are contained in the `frontend/tests` directory. For your tests to be taken into account, the file name must end with `.test.js`.
Testing the frontend is done with `jest`.
To create tests, the syntaxe is fairly simple:
## Documentation
```js
test("parse empty string", () => {
const str = "";
expect(parseMoney(str).length).toBe(0);
});
```
- [`jest` documentation](https://jestjs.io/docs/en/getting-started)
You simply need to wrap your test in the `test` function (no need to import it, it will be provided automatically when testing). This function takes the name of your test as first argument and a function to execute as second argument.
Inside the second argument function you can use the wrapper `expect` and then use the `toBe` attribute to check that what is returned is something specific.
Don't miss the [`jest` documentation](https://jestjs.io/docs/en/getting-started) for more examples.
......@@ -14,12 +14,16 @@
* [Architecture](Application/Backend/architecture.md)
* [Models, Serializers and ViewSets](Application/Backend/models_serializers_viewsets.md)
* [Config files](Application/Backend/config_files.md)
* [Moderation & versioning](Application/Backend/moderation_and_versioning.md)
* [Tags](Application/Backend/tags.md)
* [API](Application/Backend/API.md)
* [Tests](Application/Backend/tests.md)
* [Optimization](Application/Backend/optimization.md)
- Frontend
* [React basics](Application/Frontend/react.md)
* [Use of Redux](Application/Frontend/redux.md)
* [Tests](Application/Frontend/tests.md)
* [Notifications](Application/Frontend/notifications.md)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment