Skip to content

Commit

Permalink
Merge pull request #118 from fga-eps-mds/103/TestesUnitarios
Browse files Browse the repository at this point in the history
103/testes unitarios
  • Loading branch information
DaviMatheus authored Apr 19, 2021
2 parents 0a6bfed + 2d25432 commit dade502
Show file tree
Hide file tree
Showing 22 changed files with 302 additions and 113 deletions.
5 changes: 5 additions & 0 deletions .envs/.postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_HOST=db
POSTGRES_DB=postgres
POSTGRES_USER=postgres
POSTGRES_PORT=5432
POSTGRES_PASSWORD=postgres
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

17 changes: 11 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
FROM python:3

ENV PYTHONUNBUFFERED 1
EXPOSE 8000

RUN mkdir /src

WORKDIR /src

RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
COPY src /src

RUN pip3 install --upgrade pip && \
pip3 install -r requirements.txt

ENV PYTHONUNBUFFERED 1
25 changes: 17 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ version: "3"

services:
web:
build: .
command: /bin/sh ./startup.sh
volumes:
- .:/code
build:
context: .
dockerfile: ./Dockerfile
command: ["./startup.sh"]
ports:
- 8000:8000
volumes:
- ./src/:/src
- pip_cache:/usr/local/lib/python3/site-packages
depends_on:
- db
env_file:
- ./.envs/.postgres

db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
restart: unless-stopped
ports:
- 5432:5432
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.envs/.postgres

volumes:
postgres_data:
postgres_data:
pip_cache:
21 changes: 12 additions & 9 deletions src/CheerUp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,28 @@
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
},
# 'sqlite3': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
}

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],

'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
]
# 'DEFAULT_AUTHENTICATION_CLASSES': [
# 'rest_framework_simplejwt.authentication.JWTAuthentication',
# ],
# 'DEFAULT_PERMISSION_CLASSES': [
# 'rest_framework.permissions.IsAuthenticated',
# ]
}

SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(days=5),
'REFRESH_TOKEN_LIFETIME': timedelta(days=10),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': True,
}
}

# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
Expand Down
25 changes: 17 additions & 8 deletions src/CheerUp/urls.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
from django.contrib import admin
from django.urls import path, include
# from paciente.views import PacienteViewSet
from rest_framework import routers
from rest_framework_nested import routers
from rest_framework.authtoken.views import obtain_auth_token

from paciente.urls import router as paciente_router
from users.urls import router as psicologo_router
from users.views import PsicologoModelViewSet
from paciente.views import PacienteModelViewSet

router = routers.DefaultRouter()

router.register(r'psicologos', PsicologoModelViewSet)

router = routers.DefaultRouter()
router.registry.extend(paciente_router.registry)
router.registry.extend(psicologo_router.registry)
psicologo_router = routers.NestedDefaultRouter(
router,
r'psicologos',
lookup='psicologo'
)

psicologo_router.register(r'pacientes', PacienteModelViewSet)

# router.registry.extend(psicologo_router.registry)

urlpatterns = [
path('', include(router.urls)),
path('api/', include(router.urls)),
path('api/', include(psicologo_router.urls)),

path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('api/', include(router.urls)),
path('api-token-auth/', obtain_auth_token, name='api_token_auth'),
path('login/', include('users.urls')),
# path('api/register/', include('users.urls')),
Expand Down
6 changes: 5 additions & 1 deletion src/paciente/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Generated by Django 3.1.5 on 2021-04-07 00:26
# Generated by Django 3.1.5 on 2021-04-18 01:20

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('users', '0001_initial'),
]

operations = [
Expand All @@ -21,6 +23,8 @@ class Migration(migrations.Migration):
('regiao', models.CharField(choices=[('AC', 'Águas Claras'), ('AS', 'Asa Sul'), ('AN', 'Asa Norte'), ('CI', 'Ceilândia'), ('GA', 'Gama'), ('GA', 'Guará'), ('RE', 'Recanto das Emas'), ('RF', 'Riacho Fundo'), ('SA', 'Samambaia'), ('SO', 'Sobradinho'), ('TA', 'Taguatinga'), ('EO', 'Entre outros')], max_length=3)),
('situacao', models.CharField(choices=[('C', 'Controlada'), ('M', 'Moderada'), ('G', 'Grave')], default=True, max_length=2)),
('descricao', models.TextField(blank=True, null=True)),
('cpf', models.CharField(default=False, max_length=11, unique=True)),
('psicologo', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='users.psicologo')),
],
),
]
35 changes: 30 additions & 5 deletions src/paciente/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,39 @@ class Paciente(models.Model):
('AC', 'Águas Claras'),
('AS', 'Asa Sul'),
('AN', 'Asa Norte'),
('AR', 'Arniqueira'),
('BZ', 'Brazilandia'),
('CA', 'Candangolândia'),
('CI', 'Ceilândia'),
('CZ', 'Cruzeiro'),
('FE', 'Fercal'),
('GA', 'Gama'),
('GA', 'Guará'),
('GR', 'Guará'),
('IT', 'Itapoã'),
('JB', 'Jardim Botânico'),
('LS', 'Lago Sul'),
('LN', 'Lago Norte'),
('NB', 'Núcleo Bandeirante'),
('PW', 'Park Way'),
('PA', 'Paranoá'),
('Pl', 'Planaltina'),
('PP', 'Plano Piloto'),
('RE', 'Recanto das Emas'),
('RF', 'Riacho Fundo'),
('SA', 'Samambaia'),
('SM', 'Santa Maria'),
('SB', 'São Sabastião'),
('SCIA', 'SCIA'),
('RF', 'Riacho Fundo'),
('RF', 'Riacho Fundo II'),
('SI', 'SIA'),
('SO', 'Sobradinho'),
('SO', 'Sobradinho II'),
('SN', 'Sol Nascente'),
('SD','Sudoeste'),
('TA', 'Taguatinga'),
('VA', 'Varjão'),
('VP', 'Vicente Pires'),
('EO', 'Entre outros'),
)
SITUACAO = (
Expand All @@ -30,14 +55,14 @@ class Paciente(models.Model):
)
nome = models.CharField('Nome completo',max_length=90)
data_nascimento = models.DateField()
psicologo = models.ForeignKey(Psicologo, null=False, on_delete=models.CASCADE)
psicologo = models.ForeignKey(Psicologo, on_delete=models.CASCADE)
genero = models.CharField(default = True, max_length=1, choices=GENERO)
regiao = models.CharField(max_length=3, choices=REGIAO)
regiao = models.CharField(max_length=4, choices=REGIAO)
situacao = models.CharField(default = True,blank=False, max_length=2, choices=SITUACAO)
descricao = models.TextField(blank=True, null=True)
cpf = models.CharField(max_length=11, unique=True, default=False)


def __str__(self):
return self.nome

4 changes: 2 additions & 2 deletions src/paciente/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .models import Paciente
from rest_framework import serializers
from users.serializers import PsicologoSerializer

class PacienteSerializer(serializers.ModelSerializer):
class Meta():
model = Paciente
fields = ['nome', 'cpf', 'data_nascimento', 'psicologo', 'genero', 'regiao', 'situacao', 'descricao']
fields = ['nome', 'cpf', 'data_nascimento', 'genero', 'regiao',
'situacao', 'descricao']

22 changes: 14 additions & 8 deletions src/paciente/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from .views import PacienteViewSet
from rest_framework import routers
from .views import PacienteViewSet, PacienteRegistrationAPIView ,PacienteDelete, PacienteUpdate
from rest_framework_nested import routers

# from .views import PacienteViewSet, PacienteRegistrationAPIView ,PacienteDelete, PacienteUpdate

app_name = 'paciente'

# router = routers.DefaultRouter()
# router.register(r'create', PacienteRegistrationAPIView)
# router.register(r'delete', PacienteDelete)
# router.register(r'update', PacienteUpdate)






router = routers.DefaultRouter()
router.register(r'paciente', PacienteViewSet)
router.register(r'create', PacienteRegistrationAPIView)
router.register(r'delete', PacienteDelete)
router.register(r'update', PacienteUpdate)
53 changes: 32 additions & 21 deletions src/paciente/views.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
from django.shortcuts import render
from rest_framework import viewsets, generics
from rest_framework.views import APIView
from rest_framework import viewsets

from users.models import Psicologo
from .models import Paciente
from .serializers import PacienteSerializer
from rest_framework.viewsets import GenericViewSet
from rest_framework import mixins, permissions
from rest_framework.decorators import api_view, permission_classes

class PacienteViewSet(GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):

queryset = Paciente.objects.all()
serializer_class = PacienteSerializer

class PacienteRegistrationAPIView(GenericViewSet, mixins.CreateModelMixin):
# class PacienteViewSet(GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):

serializer_class = PacienteSerializer
queryset = Paciente.objects.all()
# queryset = Paciente.objects.all()
# serializer_class = PacienteSerializer

class PacienteDelete(GenericViewSet, mixins.DestroyModelMixin):
# class PacienteRegistrationAPIView(GenericViewSet, mixins.CreateModelMixin):
# serializer_class = PacienteSerializer
# queryset = Paciente.objects.all()

serializer_class = PacienteSerializer
queryset = Paciente.objects.all()
lookup_field = 'cpf'
# class PacienteDelete(GenericViewSet, mixins.DestroyModelMixin):

class PacienteUpdate(GenericViewSet, mixins.UpdateModelMixin):
# serializer_class = PacienteSerializer
# queryset = Paciente.objects.all()
# lookup_field = 'cpf'

serializer_class = PacienteSerializer
# class PacienteUpdate(GenericViewSet, mixins.UpdateModelMixin):

# serializer_class = PacienteSerializer
# queryset = Paciente.objects.all()
# lookup_field = 'cpf'


class PacienteModelViewSet(viewsets.ModelViewSet):
queryset = Paciente.objects.all()
serializer_class = PacienteSerializer
lookup_field = 'cpf'



def get_psicologo(self):
return Psicologo.objects.get(nCRP=self.kwargs['psicologo_nCRP'])

def get_queryset(self):
psicologo = self.get_psicologo()
return Paciente.objects.filter(psicologo=psicologo)

def perform_create(self, serializer):
psicologo = self.get_psicologo()
paciente = serializer.save(psicologo=psicologo)
3 changes: 2 additions & 1 deletion requirements.txt → src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ django==3.1.5
djangorestframework==3.12.2
psycopg2-binary>=2.8
django-cors-headers==3.7.0
djangorestframework-simplejwt
djangorestframework-simplejwt
drf-nested-routers==0.93.*
37 changes: 37 additions & 0 deletions src/startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# Exporting all environment variables to use in crontab
env | sed 's/^\(.*\)$/ \1/g' > /root/env

function_postgres_ready() {
python << END
import socket
import time
import os
port = int(os.environ["POSTGRES_PORT"])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('db', port))
s.close()
END
}

echo '======= CHECKING FOR UNINSTALLED PKGs AND INSTALLING'
pip freeze || pip install -r requirements.txt

until function_postgres_ready; do
>&2 echo "======= POSTGRES IS UNAVAILABLE, WAITING"
sleep 1
done
echo "======= POSTGRES IS UP, CONNECTING"

echo '======= MAKING MIGRATIONS'
python3 manage.py makemigrations

echo '======= RUNNING MIGRATIONS'
python3 manage.py migrate

echo '======= RUNNING SERVER'
python3 manage.py runserver 0.0.0.0:8000
Loading

0 comments on commit dade502

Please sign in to comment.