-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from CadeiraCuidadora/define_users
Define users
- Loading branch information
Showing
8 changed files
with
180 additions
and
24 deletions.
There are no files selected for viewing
Binary file not shown.
62 changes: 62 additions & 0 deletions
62
umiss_project/umiss_auth/migrations/0005_auto_20170515_2101.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.10.2 on 2017-05-15 21:01 | ||
from __future__ import unicode_literals | ||
|
||
from django.conf import settings | ||
import django.contrib.auth.models | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('umiss_auth', '0004_auto_20170422_2244'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='MonitorUser', | ||
fields=[ | ||
('customuser_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)), | ||
('android_token', models.CharField(max_length=512)), | ||
], | ||
options={ | ||
'abstract': False, | ||
'verbose_name_plural': 'users', | ||
'verbose_name': 'user', | ||
}, | ||
bases=('umiss_auth.customuser',), | ||
managers=[ | ||
('objects', django.contrib.auth.models.UserManager()), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='PatientUser', | ||
fields=[ | ||
('customuser_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'abstract': False, | ||
'verbose_name_plural': 'users', | ||
'verbose_name': 'user', | ||
}, | ||
bases=('umiss_auth.customuser',), | ||
managers=[ | ||
('objects', django.contrib.auth.models.UserManager()), | ||
], | ||
), | ||
migrations.RemoveField( | ||
model_name='customuser', | ||
name='monitor_users', | ||
), | ||
migrations.RemoveField( | ||
model_name='customuser', | ||
name='user_type', | ||
), | ||
migrations.AddField( | ||
model_name='patientuser', | ||
name='monitor_users', | ||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='monitors', to=settings.AUTH_USER_MODEL), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from rest_framework import permissions | ||
|
||
class IsAnonCreate(permissions.BasePermission): | ||
def has_permission(self, request, view): | ||
print("ho") | ||
if request.method == "POST" and not request.user.is_authenticated(): | ||
return True | ||
elif not request.user.is_authenticated() and request.method != "POST": | ||
return False | ||
elif request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return False | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if not request.user.is_authenticated(): | ||
return False | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return obj.username == request.user.username | ||
|
||
class IsOwnerOrReadOnly(permissions.BasePermission): | ||
""" | ||
Custom permission to only allow owners of an object to edit it. | ||
""" | ||
|
||
def has_object_permission(self, request, view, obj): | ||
# Read permissions are allowed to any request | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
# Write permissions are only allowed to the owner of the snippet | ||
return obj.owner == request.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,28 @@ | ||
from rest_framework import serializers | ||
from body_sign.models import HeartBeats, BodySignal | ||
from .models import CustomUser | ||
from .models import CustomUser, MonitorUser, PatientUser | ||
|
||
|
||
class UserSerializer(serializers.HyperlinkedModelSerializer): | ||
|
||
class Meta: | ||
model = CustomUser | ||
fields = ('url', 'username') | ||
|
||
|
||
class MonitorUserSerializer(serializers.HyperlinkedModelSerializer): | ||
class Meta: | ||
model = MonitorUser | ||
fields = ('url', 'username', 'password') | ||
|
||
def create(self, validated_data): | ||
user = MonitorUser.objects.create_user(**validated_data) | ||
return user | ||
|
||
class PatientUserSerializer(serializers.HyperlinkedModelSerializer): | ||
class Meta: | ||
model = PatientUser | ||
fields = ('url', 'username', 'password') | ||
|
||
def create(self, validated_data): | ||
user = PatientUser.objects.create_user(**validated_data) | ||
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import ugettext_lazy as _ | ||
from umiss_auth.models import CustomUser | ||
import hashlib | ||
|
||
|
||
def validate_token(token): | ||
patients = CustomUser.objects.filter(user_type='patient') | ||
patient_tokens = [user.token for user in patients] | ||
hash_token = hashlib.sha512( | ||
self.token.encode('utf-8') | ||
).hexdigest() | ||
|
||
if hash_token not in patient_tokens: | ||
raise ValidationError( | ||
_("The monitor must have a token from a patient." + | ||
" The token %(token) is wrong"), | ||
params={'token': token}, | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
from django.shortcuts import render | ||
from .models import CustomUser | ||
from .serializers import UserSerializer | ||
from .serializers import UserSerializer, MonitorUserSerializer, PatientUserSerializer | ||
from .models import MonitorUser, PatientUser | ||
from rest_framework import viewsets | ||
from rest_framework import permissions | ||
from .permissions import IsAnonCreate | ||
|
||
|
||
class UserViewSet(viewsets.ModelViewSet): | ||
class UserViewSet(viewsets.ReadOnlyModelViewSet): | ||
queryset = CustomUser.objects.all() | ||
serializer_class = UserSerializer | ||
write_only_fields = ('password',) | ||
|
||
permission_classes = (permissions.AllowAny, permissions.BasePermission) | ||
permission_classes = (IsAnonCreate,) | ||
|
||
class MonitorViewSet(viewsets.ModelViewSet): | ||
queryset = MonitorUser.objects.all() | ||
serializer_class = MonitorUserSerializer | ||
write_only_fields = ('password') | ||
|
||
permission_classes = (IsAnonCreate,) | ||
|
||
class PatienteViewSet(viewsets.ModelViewSet): | ||
queryset = PatientUser.objects.all() | ||
serializer_class = PatientUserSerializer | ||
write_only_fields = ('password',) | ||
|
||
permission_classes = (IsAnonCreate,) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters