Make UserInfo contain User, add views for UserInfo, Client, Provider
This commit is contained in:
parent
6992114ce6
commit
170878441e
|
@ -1,28 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.1 on 2017-05-27 02:53
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='UserInfo',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('phone_number', models.CharField(blank=True, max_length=16, validators=[django.core.validators.RegexValidator(message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.", regex='^\\+?1?\\d{9,15}$')])),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -1,22 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.1 on 2017-05-27 03:38
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('api', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='userinfo',
|
||||
name='user',
|
||||
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='user_info', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
]
|
|
@ -1,22 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.1 on 2017-05-27 04:39
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('api', '0002_auto_20170527_0338'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='userinfo',
|
||||
name='user',
|
||||
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
]
|
|
@ -1,34 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.1 on 2017-05-27 07:30
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('api', '0003_auto_20170527_0439'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Client',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('business_number', models.CharField(blank=True, max_length=16)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Provider',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('sin', models.CharField(blank=True, max_length=16)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -1,65 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.1 on 2017-05-27 22:47
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('api', '0004_client_provider'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Manage',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('note', models.CharField(blank=True, max_length=500)),
|
||||
('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Client')),
|
||||
('provider', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Provider')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Price',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('amount', models.DecimalField(decimal_places=2, max_digits=8)),
|
||||
('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Client')),
|
||||
('provider', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Provider')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Shift',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('set_start', models.DateTimeField()),
|
||||
('set_end', models.DateTimeField()),
|
||||
('amount', models.DecimalField(decimal_places=2, max_digits=8)),
|
||||
('description', models.CharField(blank=True, max_length=100)),
|
||||
('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Client')),
|
||||
('provider', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Provider')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Work',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('color', models.CharField(blank=True, max_length=16)),
|
||||
('label', models.CharField(blank=True, max_length=100)),
|
||||
('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Client')),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='shift',
|
||||
name='work',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Work'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='price',
|
||||
name='work',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Work'),
|
||||
),
|
||||
]
|
|
@ -7,6 +7,8 @@ class UserInfo(models.Model):
|
|||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
|
||||
phone_number = models.CharField(validators=[phone_regex], max_length=16, blank=True)
|
||||
USER_TYPES = (('C', 'Client'), ('P', 'Provider'))
|
||||
user_type = models.CharField(max_length=1, choices=USER_TYPES, blank=True)
|
||||
|
||||
class Client(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
|
|
20
caremyway/api/permissions.py
Normal file
20
caremyway/api/permissions.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from pprint import pprint
|
||||
from rest_framework import permissions
|
||||
from caremyway.api.models import Client, Provider
|
||||
|
||||
class UserTypePermission(permissions.BasePermission):
|
||||
"""
|
||||
Disallow creation of client or provider if one already exists
|
||||
"""
|
||||
message = "User is already either a client or a provider."
|
||||
|
||||
def has_permission(self, request, view):
|
||||
user = request.data.get('user', None)
|
||||
|
||||
if request.method != 'POST' or user is None:
|
||||
return True
|
||||
elif not Client.objects.filter(user=user).exists() \
|
||||
and not Provider.objects.filter(user=user).exists():
|
||||
return True
|
||||
else:
|
||||
return False
|
|
@ -5,24 +5,28 @@ from caremyway.api.models import UserInfo, Client, Provider
|
|||
class UserInfoSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = UserInfo
|
||||
fields = ('phone_number',)
|
||||
fields = ('user', 'user_type', 'phone_number')
|
||||
|
||||
class ChosenUserInfoSerializer(UserInfoSerializer):
|
||||
class Meta(UserInfoSerializer.Meta):
|
||||
read_only_fields = ('user_type',)
|
||||
|
||||
class ClientSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Client
|
||||
fields = ('business_number',)
|
||||
fields = ('user', 'business_number')
|
||||
|
||||
class ProviderSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Provider
|
||||
fields = ('sin',)
|
||||
fields = ('user', 'sin')
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
userinfo = UserInfoSerializer()
|
||||
client = ClientSerializer()
|
||||
provider = ProviderSerializer()
|
||||
userinfo = UserInfoSerializer(read_only=True, allow_null=True)
|
||||
client = ClientSerializer(allow_null=True)
|
||||
provider = ProviderSerializer(allow_null=True)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('id', 'username', 'is_active', 'first_name', 'last_name', 'email', 'userinfo', 'client', 'provider')
|
||||
fields = ('username', 'is_active', 'first_name', 'last_name', 'email', 'userinfo', 'client', 'provider')
|
||||
depth = 1
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
from django.contrib.auth.models import User, Group
|
||||
from caremyway.api.models import UserInfo, Client, Provider
|
||||
from rest_framework import viewsets, permissions, status
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
from caremyway.api.serializers import UserSerializer, UserInfoSerializer, ClientSerializer, ProviderSerializer
|
||||
from caremyway.api.permissions import UserTypePermission
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = UserSerializer
|
||||
lookup_field = 'username'
|
||||
serializer_class = UserSerializer
|
||||
|
||||
# Disallow POSTing from /users/ route. Only rest-auth can add users.
|
||||
http_method_names = ['get', 'head', 'put', 'options']
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
|
@ -19,6 +22,47 @@ class UserViewSet(viewsets.ModelViewSet):
|
|||
else:
|
||||
return User.objects.filter(username=user)
|
||||
|
||||
class UserInfoViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "user__username"
|
||||
serializer_class = UserInfoSerializer
|
||||
|
||||
http_method_names = ['get', 'post', 'head', 'put', 'options']
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
if user.is_staff:
|
||||
return UserInfo.objects.all().order_by('-user__date_joined')
|
||||
else:
|
||||
return UserInfo.objects.filter(user__username=user)
|
||||
|
||||
class ClientViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "user__username"
|
||||
serializer_class = ClientSerializer
|
||||
|
||||
http_method_names = ['get', 'post', 'head', 'put', 'options']
|
||||
permission_classes = (UserTypePermission,)
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
if user.is_staff:
|
||||
return Client.objects.all().order_by('-user__date_joined')
|
||||
else:
|
||||
return Client.objects.filter(user__username=user)
|
||||
|
||||
class ProviderViewSet(viewsets.ModelViewSet):
|
||||
lookup_field = "user__username"
|
||||
serializer_class = ProviderSerializer
|
||||
|
||||
http_method_names = ['get', 'post', 'head', 'put', 'options']
|
||||
permission_classes = (UserTypePermission,)
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
if user.is_staff:
|
||||
return Provider.objects.all().order_by('-user__date_joined')
|
||||
else:
|
||||
return Provider.objects.filter(user__username=user)
|
||||
|
||||
@api_view()
|
||||
def null_view(request):
|
||||
return Response(status=status.HTTP_400_BAD_REQUEST)
|
||||
|
|
|
@ -18,7 +18,10 @@ from rest_framework import routers
|
|||
from caremyway.api import views
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
router.register(r'users', views.UserViewSet, 'user')
|
||||
router.register(r'user', views.UserViewSet, 'user')
|
||||
router.register(r'userinfo', views.UserInfoViewSet, 'userinfo')
|
||||
router.register(r'client', views.ClientViewSet, 'client')
|
||||
router.register(r'provider', views.ProviderViewSet, 'provider')
|
||||
|
||||
# Wire up our API using automatic URL routing.
|
||||
# Additionally, we include login URLs for the browsable API.
|
||||
|
|
Loading…
Reference in New Issue
Block a user