Make UserInfo contain User, add views for UserInfo, Client, Provider

This commit is contained in:
Tanner Collin 2017-06-04 06:20:41 +00:00
parent 6992114ce6
commit 170878441e
11 changed files with 83 additions and 181 deletions

View File

@ -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)),
],
),
]

View File

@ -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),
),
]

View File

@ -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),
),
]

View File

@ -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)),
],
),
]

View File

@ -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'),
),
]

View File

@ -7,6 +7,8 @@ class UserInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) 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_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) 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): class Client(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) user = models.OneToOneField(User, on_delete=models.CASCADE)

View 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

View File

@ -5,24 +5,28 @@ from caremyway.api.models import UserInfo, Client, Provider
class UserInfoSerializer(serializers.ModelSerializer): class UserInfoSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = UserInfo 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 ClientSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Client model = Client
fields = ('business_number',) fields = ('user', 'business_number')
class ProviderSerializer(serializers.ModelSerializer): class ProviderSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Provider model = Provider
fields = ('sin',) fields = ('user', 'sin')
class UserSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer):
userinfo = UserInfoSerializer() userinfo = UserInfoSerializer(read_only=True, allow_null=True)
client = ClientSerializer() client = ClientSerializer(allow_null=True)
provider = ProviderSerializer() provider = ProviderSerializer(allow_null=True)
class Meta: class Meta:
model = User 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 depth = 1

View File

@ -1,16 +1,19 @@
from django.shortcuts import render from django.shortcuts import render
# Create your views here.
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from caremyway.api.models import UserInfo, Client, Provider from caremyway.api.models import UserInfo, Client, Provider
from rest_framework import viewsets, permissions, status from rest_framework import viewsets, permissions, status
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
from rest_framework.response import Response from rest_framework.response import Response
from caremyway.api.serializers import UserSerializer, UserInfoSerializer, ClientSerializer, ProviderSerializer from caremyway.api.serializers import UserSerializer, UserInfoSerializer, ClientSerializer, ProviderSerializer
from caremyway.api.permissions import UserTypePermission
class UserViewSet(viewsets.ModelViewSet): class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
lookup_field = 'username' 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): def get_queryset(self):
user = self.request.user user = self.request.user
@ -19,6 +22,47 @@ class UserViewSet(viewsets.ModelViewSet):
else: else:
return User.objects.filter(username=user) 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() @api_view()
def null_view(request): def null_view(request):
return Response(status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_400_BAD_REQUEST)

View File

@ -18,7 +18,10 @@ from rest_framework import routers
from caremyway.api import views from caremyway.api import views
router = routers.DefaultRouter() 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. # Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API. # Additionally, we include login URLs for the browsable API.