Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Backend

MeisterSeSe edited this page Nov 6, 2023 · 26 revisions

This the backend page for prototype v0.2. You may like the backend now.

General Structure

If you want to create a new app for the core component (which is also an app), you need to navigate into the core folder and create a new app as usual (e.g. python manage.py startapp newapp). Afer this you need to alter newapp/apps.py like this:

from django.apps import AppConfig

class NewAppConfig(AppConfig):
    name = 'core.newapp'
    label = 'core_newapp'

And also the newapp/__init__.py

default_app_config = 'core.newapp.apps.NewAppConfig'

Now you can add core.newapp to INSTALLED_APPS in settings.py AFTER the core entry. Please avoid using print for printing information. Use logging instead.

Env

Some variables e.g. for database connection are loaded from a .env file. You can find an example file in backend/ddueruemweb/.env.example. Here you need to edit EMAIL_HOST, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, ... and then rename the file to .env before starting. There also is a pre-configured file called .env.testing. Note that before running dockerized you need to create backend/ddueruemweb/.env.production.

Django-Rest framework

For providing API endpotins we use Django REST framework and therefore we need serizalizers.py (see details here). We use django built in views only for the admin panel. For every other app, we use viewsets.

Backend Endpoints

Authentication

  • /auth/login: POST request for login.
  • /auth/register: POST request for registration.
  • /auth/register/confirm/:token: Confirmation of registration.
  • /auth/refresh: Refreshing authentication token.

File Operations

  • /files: File upload endpoint (POST request).
  • /files/uploaded/confirmed: Confirmed uploaded files (retrieve, list).
  • /files/uploaded/private: Private uploaded files (retrieve, list).
  • /files/uploaded/unconfirmed: Unconfirmed files (retrieve, list).
  • /files/uploaded/unconfirmed/confirm/:token: Confirmation of an unconfirmed file.
  • /files/uploaded/unconfirmed/delete/:token: Delete an unconfirmed file.
  • /bulk-upload/: Bulk upload API (Also used for Single Upload).
  • /zip-upload/: ZIP upload API.
  • /private-upload/: Private file upload API.

Other Entities

  • /tags: Uploaded Tags.
  • /licenses: Uploaded Licenses.
  • /families: Uploaded Families.
  • /analyses: Analyses endpoint.
  • /analysisresults: Analysis results endpoint.
  • /api-auth/: Authentication and authorization views for Django REST Framework.
  • /user-info/: User information API.

Admin Panel

We chose to have one admin.py file which controls all apps of the core component. Note that creating a new File in the admin panel does not trigger the transpiler to create a transpiled_file! This is only triggerd when uploading the file via the API.

User Model

In General there are four ways to extend the existing User model. You could use a proxy model, One-to-One Link or extend the AbstractUser or AbstractBaseUser (more details, see here). We chose to extend the AbstractBaseUser because it is an entirely new user model and the only good way to customize the authentication process to use the email adress instead of the username for validation. This was a mandatory requirement. To overwrite the default user model, reference the new user model in AUTH_USER_MODEL in settings.py. When working with the new model, always ensure that you do not import the default User from django.contrib.auth.models to avoid problems. We also created a new manager class for the user which extends BaseUserManager to control the creation of a new user, staffuser or superuser.

Forms.py

The forms.py in backend/core/user is only for overwriting the existings forms which are used by default in the admin panel. We can't use the existings forms because they all require an user attribute username which we don't have.

Viewsets.py

This holds the view ActivateUserViewSet which is accessd when an inactive user tries to activate the account by clicking on the link in the received activation email.

File Upload

In backend/core/fileupload the models File and Tag are defined. An important class is in backend/core/fileupload/serializers.py. Here you can find FileSerializer. This serializer handles the file upload as multipart/form-data and also is able to handle the JSON part which defines which Tags belong to this file. Note that there always should be at least one accessible (public) Tag when uploading a file via the frontend.

File Mirroring

It is possible to automatically mirror a uploaded file to GitHub. For this you need a GitHub account which is only used for uploading files and creating branches on GitHub. With this account, create a new repository for file storing and create an access token. Copy the access token into the .env file (GITHUB_TOKEN=). Every other mirror configuration (like configuration of used repo) can be done in backend/fileupload/githubmirror/github_manager.py.

Transpiler

To translate a feature model in XML format (you can find example feature models in backend/transpiler/xmlExamples) to JSON you can use backend/transpiler/xml_parser.py. If you want to translate to ant G6 format (which is used for feature model visulaization) you can use backend/transpiler/g6_transpiler.py.

Third Party Libs

Other third party libs used:

Simple JWT

Handling of authorization with JWT access and refresh tokens with REST. You can find more details, here: Django-Rest-Framework-SimpleJWT.

Django-Extension

Since performing jobs on hourly/daily/weekly/... basis is not build-in in django, we use Django-Extension for job scheduling in python. It is located in backend/core/jobs

Jobs do not run automatically! You must either run a job manually specifying the exact time on which the command is to be run, or use crontab

Migrations

Currently all migrations have been auto generated with the makemigrate command. The advantage of having these migration files in Github is, being able to generate the default license database entry as soon as the default migrations are done. This is achieved by the file 0003_add_default_license.py. Unfortunatly we need to push ALL migration files (also for user and analysis) because they all depend on each other. Consider deleting the migration files if you change models but you should assure that the license file is still created afterwards.

Tests

Django test are located in backend/core/tests.py and API tests are currently located in backend/core/fileupload/tests.py