Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup update and API Token Authentication #137

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SENSORSAFRICA_CELERY_SLACK_WEBHOOK=""
SENSORSAFRICA_CELERY_SLACK_WEBHOOK_FAILURES_ONLY=""
SENSORSAFRICA_DATABASE_URL=""
SENSORSAFRICA_DEBUG=""
SENSORSAFRICA_FLOWER_ADMIN_PASSWORD=""
SENSORSAFRICA_FLOWER_ADMIN_USERNAME=""
SENSORSAFRICA_RABBITMQ_URL=""
SENSORSAFRICA_SENTRY_DSN=""
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ VOLUME [ "/src/logs" ]
# Copy the current directory contents into the container at sensorsafrica
ADD . /src/

# Upgrade pip from trusted hosts
RUN python -m pip install --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --trusted-host pypi.org --upgrade pip

# Upgrade pip and setuptools
RUN pip install -q -U pip setuptools
Comment on lines +16 to 20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 2 ways of upgrading pip?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽 the upgrade flag will be removed to just install the setup tools


Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ CREATE DATABASE sensorsafrica;
CREATE USER sensorsafrica WITH ENCRYPTED PASSWORD 'sensorsafrica';
GRANT ALL PRIVILEGES ON DATABASE sensorsafrica TO sensorsafrica;
```

- Migrate the database; `python manage.py migrate`
- Create super user for admin login; `python manage.py createsuperuser`
username: `sensorsafrica`
email: blank
password: `sensorsafrica`
Comment on lines +31 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must username/password be sensorsafrica?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. Let's say it a convention. Any thoughts?


- Run the server; `python manage.py runserver`

### Docker
Expand Down
29 changes: 17 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ version: '3.3'

services:
rabbitmq:
image: rabbitmq:3.5.1
image: rabbitmq:3.12.7-management
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 3.12 the version used in PROD as well?

ports:
- 4369:4369
- 5672:5672
- 25672:25672
- 15672:15672
- "5672:5672"
# GUI port
- "15672:15672"
environment:
- RABBITMQ_USERNAME=sensorsafrica
- RABBITMQ_PASSWORD=sensorsafrica
- RABBITMQ_DEFAULT_USER=sensorsafrica
- RABBITMQ_DEFAULT_PASS=sensorsafrica
healthcheck:
test: [ "CMD-SHELL", "rabbitmq-diagnostics -q ping" ]
interval: 10s
timeout: 5s
retries: 2

postgres:
image: postgres:13.7
ports:
Expand All @@ -21,15 +26,15 @@ services:
- POSTGRES_DB=sensorsafrica
volumes:
- postgres_data:/var/lib/postgresql/data/

api:
build:
context: .
environment:
SENSORSAFRICA_DATABASE_URL: postgres://sensorsafrica:sensorsafrica@postgres:5432/sensorsafrica
SENSORSAFRICA_READ_DATABASE_URLS: postgres://sensorsafrica:sensorsafrica@postgres:5432/sensorsafrica
SENSORSAFRICA_RABBITMQ_URL: amqp://sensorsafrica:sensorsafrica@rabbitmq//
SENSORSAFRICA_FLOWER_ADMIN_USERNAME: admin
SENSORSAFRICA_FLOWER_ADMIN_PASSWORD: password
SENSORSAFRICA_DATABASE_URL: ${SENSORSAFRICA_DATABASE_URL}
SENSORSAFRICA_RABBITMQ_URL: ${SENSORSAFRICA_RABBITMQ_URL}
SENSORSAFRICA_FLOWER_ADMIN_USERNAME: ${SENSORSAFRICA_FLOWER_ADMIN_USERNAME}
SENSORSAFRICA_FLOWER_ADMIN_PASSWORD: ${SENSORSAFRICA_FLOWER_ADMIN_PASSWORD}
Comment on lines +34 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a default for any of these?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was previously.

  SENSORSAFRICA_DATABASE_URL: postgres://sensorsafrica:sensorsafrica@postgres:5432/sensorsafrica
  SENSORSAFRICA_RABBITMQ_URL: amqp://sensorsafrica:sensorsafrica@rabbitmq:5672
  SENSORSAFRICA_FLOWER_ADMIN_USERNAME: admin
  SENSORSAFRICA_FLOWER_ADMIN_PASSWORD: password```

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be updated in the README for local setup.

DOKKU_APP_NAME: sensorsafrica
depends_on:
- postgres
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ckanapi==4.1

celery-slack==0.3.0

urllib3<1.25,>=1.21.1 #requests 2.21.0
urllib3<2

django-cors-headers==3.0.2

Expand Down
11 changes: 3 additions & 8 deletions sensorsafrica/api/v1/router.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# The base version is entirely based on feinstaub
from feinstaub.main.views import UsersView
from feinstaub.sensors.views import (
SensorView,
StatisticsView,
SensorDataView,
)

from .views import (
FilterView,
NodeView,
NowView,
PostSensorDataView,
SensorsAfricaSensorDataView,
VerboseSensorDataView,
SensorView,
StatisticsView,
UsersView,
)

from rest_framework import routers
Expand Down
38 changes: 28 additions & 10 deletions sensorsafrica/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@


from django.conf import settings
from django.db.models import ExpressionWrapper, F, FloatField, Max, Min, Sum, Avg, Q
from django.db.models.functions import Cast, TruncDate
from dateutil.relativedelta import relativedelta
from django.db.models import Q
from django.utils import timezone
from rest_framework import mixins, pagination, viewsets
from rest_framework import mixins,viewsets
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated

from feinstaub.main.views import UsersView as FeinstaubUsersView
from feinstaub.sensors.models import Node, SensorData
from feinstaub.sensors.serializers import NowSerializer
from feinstaub.sensors.views import SensorDataView, StandardResultsSetPagination
from feinstaub.sensors.views import (
SensorDataView,
StandardResultsSetPagination,
SensorView as FeinstaubSensorView,
StatisticsView as FeinstaubStatisticsView

)
from feinstaub.sensors.authentication import NodeUidAuthentication

from .filters import NodeFilter, SensorFilter
Expand Down Expand Up @@ -72,8 +76,8 @@ def get_queryset(self):

class NowView(mixins.ListModelMixin, viewsets.GenericViewSet):
"""Show all public sensors active in the last 5 minutes with newest value"""

permission_classes = []
authentication_classes=[SessionAuthentication, TokenAuthentication]
permission_classes = [IsAuthenticated]
serializer_class = NowSerializer
queryset = SensorData.objects.none()

Expand All @@ -92,9 +96,11 @@ class PostSensorDataView(mixins.CreateModelMixin,
permission_classes = tuple()
serializer_class = LastNotifySensorDataSerializer
queryset = SensorData.objects.all()


class VerboseSensorDataView(SensorDataView):
authentication_classes=[SessionAuthentication, TokenAuthentication]
permission_classes=[IsAuthenticated]
filter_class = SensorFilter

class SensorsAfricaSensorDataView(mixins.ListModelMixin, viewsets.GenericViewSet):
Expand All @@ -110,3 +116,15 @@ def get_queryset(self):
.prefetch_related("sensordatavalues")
)


class UsersView(FeinstaubUsersView):
authentication_classes=[SessionAuthentication, TokenAuthentication]
permission_classes = [IsAuthenticated]

class SensorView(FeinstaubSensorView):
authentication_classes=[SessionAuthentication, TokenAuthentication]
permission_classes = [IsAuthenticated]

class StatisticsView(FeinstaubStatisticsView):
authentication_classes=[SessionAuthentication, TokenAuthentication]
permission_classes = [IsAuthenticated]
5 changes: 1 addition & 4 deletions sensorsafrica/migrations/0004_auto_20190509_1145.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

class Migration(migrations.Migration):

dependencies = [
('sensors', '0020_auto_20190314_1232'),
('sensorsafrica', '0003_auto_20190222_1137'),
]
dependencies = [('sensorsafrica', '0003_auto_20190222_1137')]

operations = [
migrations.CreateModel(
Expand Down
7 changes: 0 additions & 7 deletions sensorsafrica/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@

DATABASES = {"default": dj_database_url.parse(DATABASE_URL), }

READ_DATABASE_URLS = os.getenv("SENSORSAFRICA_READ_DATABASE_URLS", DATABASE_URL).split(",")

for index, read_database_url in enumerate(READ_DATABASE_URLS,start=1):
DATABASES[f"read_replica_{index}"] = dj_database_url.parse(read_database_url)

DATABASE_ROUTERS = ["sensorsafrica.router.ReplicaRouter", ]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

Expand Down