Skip to content

Commit

Permalink
Merge pull request #21 from greenjune-ship-it/12-display-ed50-and-ed5…
Browse files Browse the repository at this point in the history
…0-thermal-threshold

12 display ed50, release v0.1.1
  • Loading branch information
greenjune-ship-it authored Feb 10, 2024
2 parents 4a7fb02 + a1f6240 commit 568e021
Show file tree
Hide file tree
Showing 51 changed files with 1,980 additions and 500 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ django_app/staticfiles/
pg_dump.sql
backup.pgdump

.~*
.~*
/django_app/users/management/commands/user_data.json
44 changes: 31 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,57 @@ CONTACT_EMAIL_ADDRESS=''

### Deploy

From backup:
#### Up the project from scratch

```commandline
bash deploy.sh
docker compose up -d
```

Up the project from scratch:
#### Create superuser

```commandline
sudo docker compose up -d
docker compose exec django-app python manage.py createsuperuser
```

Collect static files:
You can also prepare your `user_data.json` and populate your database automatically:

```commandline
sudo docker compose exec django-app python manage.py collectstatic --noinput
```
[
{"username": "user1", "password": "password123", "first_name": "John", "last_name": "Doe", "email": "[email protected]"},
{"username": "user2", "password": "password456", "first_name": "Jane", "last_name": "Smith", "email: "[email protected]"},
{"username": "admin", "password": "adminpassword", "first_name": "Admin", "last_name": "User", "email": "[email protected]"}
]
Create superuser:
```
And then run custom django-admin command:

```commandline
sudo docker compose exec django-app python manage.py createsuperuser
docker compose exec django-app python manage.py create_users path/to/user_data.json
```
Don't forget to replace the path to your `user_data.json` file.

#### Populate the database

Populate the database (let's say my superuser is `adm_iakovyu1`:
Let's say my superuser is `adm_iakovyu1`.

For complete datasets:

```commandline
sudo docker compose exec django-app python populate_db.py \
--owner iakovyu1 \
docker compose exec django-app python populate_db.py \
--owner adm_iakovyu1 \
--csv_path static/datasheets/cbass_84.csv
```

For incomplete datasets, use `--no-pam` argument:

```commandline
docker compose exec django-app python populate_db.py \
--owner adm_iakovyu1 \
--csv_path static/datasheets/redsea_gradient_study.csv \
--no-pam
```

## Database Backups

Create a database backup:
Expand Down
16 changes: 4 additions & 12 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#!/bin/bash

source .env

docker compose up -d

docker compose pause django-app

docker compose cp backup.pgdump database:backup.pgdump

echo "Drop the database and restore from backup"

docker compose exec database \
pg_restore --clean --dbname $DB_NAME -U $DB_USER backup.pgdump
docker compose exec django-app python manage.py create_users user_data.json

echo "Done!"
docker compose exec django-app python manage.py populate_db --owner voolsch1 --csv_path static/datasheets/cbass_84.csv
docker compose exec django-app python manage.py populate_db --owner voolsch1 --csv_path static/datasheets/redsea_gradient_study.csv --no-pam

docker compose unpause django-app
docker compose exec django-app python manage.py link_biosamples
3 changes: 0 additions & 3 deletions django_app/api/models.py

This file was deleted.

20 changes: 17 additions & 3 deletions django_app/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from projects.models import BioSample, Colony, Observation, Project
from rest_framework import serializers
from projects.models import BioSample, Colony, Observation


class BioSampleSerializer(serializers.ModelSerializer):
Expand All @@ -9,12 +9,26 @@ class Meta:


class ColonySerializer(serializers.ModelSerializer):
projects = serializers.SerializerMethodField()

def get_projects(self, obj):
# Get the related projects for the colony's biosamples
biosamples = obj.biosamples.all()
projects = Project.objects.filter(biosamples__in=biosamples).distinct()
# Assuming you want to serialize projects' names
return [project.name for project in projects]

class Meta:
model = Colony
fields = '__all__'

fields = ['id', 'name', 'species', 'country', 'latitude', 'longitude', 'ed50_value', 'projects']

class ObservationSerializer(serializers.ModelSerializer):
class Meta:
model = Observation
fields = '__all__'


class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = ['id', 'name', 'description']
5 changes: 3 additions & 2 deletions django_app/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# api/urls.py
from django.urls import path, include
from api.views import CheckAuthenticationApiView, UserCartApiView
from api.views import BioSamplesApiView, ColoniesApiView, ObservationsApiView
from api.views import BioSamplesApiView, ColoniesApiView, ObservationsApiView, ProjectsApiView

urlpatterns = [
path('auth/', include([
Expand All @@ -12,6 +12,7 @@
path('public/', include([
path('biosamples/', BioSamplesApiView.as_view()),
path('colonies/', ColoniesApiView.as_view()),
path('observations/', ObservationsApiView.as_view())
path('observations/', ObservationsApiView.as_view()),
path('projects/', ProjectsApiView.as_view())
]))
]
19 changes: 15 additions & 4 deletions django_app/api/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# api/views.py
from api.serializers import BioSampleSerializer, ColonySerializer, \
ObservationSerializer, ProjectSerializer
# Apps imports
from projects.models import BioSample, Colony, Observation, Project, UserCart
from rest_framework import generics, status
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
# Apps imports
from projects.models import BioSample, Colony, Observation, UserCart
from api.serializers import BioSampleSerializer, ColonySerializer, \
ObservationSerializer


class CheckAuthenticationApiView(APIView):
Expand All @@ -21,6 +21,17 @@ def get(self, request):
'username': request.user.username})


class ProjectsApiView(APIView):
"""
This endpoint allows users to retrieve a list of available Projects.
"""

def get(self, request):
projects = Project.objects.all()
serializer = ProjectSerializer(projects, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)


class BioSamplesApiView(APIView):
"""
This endpoint allows users to retrieve a list of BioSamples.
Expand Down
1 change: 1 addition & 0 deletions django_app/django_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# Installed pip packages
'bootstrap5',
'corsheaders',
'django_extensions',
'leaflet',
'rest_framework',
# Custom apps
Expand Down
Loading

0 comments on commit 568e021

Please sign in to comment.