diff --git a/nicohack202002/uncloud/uncloud/settings.py b/nicohack202002/uncloud/uncloud/settings.py index d6cbb0e..be38f8f 100644 --- a/nicohack202002/uncloud/uncloud/settings.py +++ b/nicohack202002/uncloud/uncloud/settings.py @@ -38,7 +38,8 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', - 'uncloud_api' + 'uncloud_api', + 'uncloud_auth' ] MIDDLEWARE = [ @@ -101,11 +102,13 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] -# LDAP +################################################################################ +# AUTH/LDAP + import ldap from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion -AUTHENTICATION_BACKENDS = ["django_auth_ldap.backend.LDAPBackend"] + AUTH_LDAP_SERVER_URI = "ldaps://ldap1.ungleich.ch,ldaps://ldap2.ungleich.ch" AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=customer,dc=ungleich,dc=ch" @@ -114,6 +117,16 @@ AUTH_LDAP_USER_SEARCH = LDAPSearch( "ou=customer,dc=ungleich,dc=ch", ldap.SCOPE_SUBTREE, "(uid=%(user)s)" ) +################################################################################ +# AUTH/Django +AUTHENTICATION_BACKENDS = [ + "django_auth_ldap.backend.LDAPBackend", + "django.contrib.auth.backends.ModelBackend" +] + +AUTH_USER_MODEL = 'uncloud_auth.User' + + ################################################################################ # AUTH/REST REST_FRAMEWORK = { diff --git a/nicohack202002/uncloud/uncloud/urls.py b/nicohack202002/uncloud/uncloud/urls.py index e0a0b61..cb50432 100644 --- a/nicohack202002/uncloud/uncloud/urls.py +++ b/nicohack202002/uncloud/uncloud/urls.py @@ -27,6 +27,7 @@ router.register(r'groups', views.GroupViewSet) # Additionally, we include login URLs for the browsable API. urlpatterns = [ path('', include(router.urls)), + path('admin/', admin.site.urls), path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) ] diff --git a/nicohack202002/uncloud/uncloud_api/admin.py b/nicohack202002/uncloud/uncloud_api/admin.py index 8c38f3f..f9f5589 100644 --- a/nicohack202002/uncloud/uncloud_api/admin.py +++ b/nicohack202002/uncloud/uncloud_api/admin.py @@ -1,3 +1,6 @@ from django.contrib import admin -# Register your models here. +from .models import Product, Feature + +admin.site.register(Product) +admin.site.register(Feature) diff --git a/nicohack202002/uncloud/uncloud_api/migrations/0001_initial.py b/nicohack202002/uncloud/uncloud_api/migrations/0001_initial.py deleted file mode 100644 index 7248a66..0000000 --- a/nicohack202002/uncloud/uncloud_api/migrations/0001_initial.py +++ /dev/null @@ -1,34 +0,0 @@ -# Generated by Django 3.0.3 on 2020-02-21 09:40 - -from django.db import migrations, models -import django.db.models.deletion -import uuid - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ] - - operations = [ - migrations.CreateModel( - name='Product', - fields=[ - ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), - ('name', models.CharField(max_length=256)), - ('recurring_period', models.CharField(choices=[('per_year', 'Per Year'), ('per_month', 'Per Month'), ('per_week', 'Per Week'), ('per_day', 'Per Day'), ('per_hour', 'Per Hour'), ('not_recurring', 'Not recurring')], default='not_recurring', max_length=256)), - ], - ), - migrations.CreateModel( - name='Feature', - fields=[ - ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), - ('name', models.CharField(max_length=256)), - ('recurring_price', models.FloatField(default=0)), - ('one_time_price', models.FloatField()), - ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_api.Product')), - ], - ), - ] diff --git a/nicohack202002/uncloud/uncloud_api/models.py b/nicohack202002/uncloud/uncloud_api/models.py index 2dca8ea..9d4291a 100644 --- a/nicohack202002/uncloud/uncloud_api/models.py +++ b/nicohack202002/uncloud/uncloud_api/models.py @@ -1,6 +1,10 @@ -from django.db import models import uuid +from django.db import models +from django.contrib.auth import get_user_model + + + class Product(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField(max_length=256) @@ -17,6 +21,8 @@ class Product(models.Model): default="not_recurring" ) + def __str__(self): + return "{}".format(self.name) @@ -28,3 +34,25 @@ class Feature(models.Model): one_time_price = models.FloatField() product = models.ForeignKey(Product, on_delete=models.CASCADE) + + def __str__(self): + return "'{}' - '{}'".format(self.product, self.name) + + +class Order(models.Model): + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + + owner = models.ForeignKey(get_user_model(), + on_delete=models.CASCADE) + + product = models.ForeignKey(Product, + on_delete=models.CASCADE) + + +class OrderReference(models.Model): + """ + An order can references another product / relate to it. + This model is used for the relation + """ + + pass diff --git a/nicohack202002/uncloud/uncloud_api/views.py b/nicohack202002/uncloud/uncloud_api/views.py index 9310d8b..88e0543 100644 --- a/nicohack202002/uncloud/uncloud_api/views.py +++ b/nicohack202002/uncloud/uncloud_api/views.py @@ -1,8 +1,7 @@ from django.shortcuts import render +from django.contrib.auth import get_user_model +from django.contrib.auth.models import Group -# Create your views here. - -from django.contrib.auth.models import User, Group from rest_framework import viewsets, permissions from .serializers import UserSerializer, GroupSerializer @@ -12,7 +11,7 @@ class CreditCardViewSet(viewsets.ModelViewSet): """ API endpoint that allows credit cards to be listed """ - queryset = User.objects.all().order_by('-date_joined') + queryset = get_user_model().objects.all().order_by('-date_joined') serializer_class = UserSerializer permission_classes = [permissions.IsAuthenticated] @@ -23,7 +22,7 @@ class UserViewSet(viewsets.ModelViewSet): """ API endpoint that allows users to be viewed or edited. """ - queryset = User.objects.all().order_by('-date_joined') + queryset = get_user_model().objects.all().order_by('-date_joined') serializer_class = UserSerializer permission_classes = [permissions.IsAuthenticated] diff --git a/nicohack202002/uncloud/uncloud_api/migrations/__init__.py b/nicohack202002/uncloud/uncloud_auth/__init__.py similarity index 100% rename from nicohack202002/uncloud/uncloud_api/migrations/__init__.py rename to nicohack202002/uncloud/uncloud_auth/__init__.py diff --git a/nicohack202002/uncloud/uncloud_auth/admin.py b/nicohack202002/uncloud/uncloud_auth/admin.py new file mode 100644 index 0000000..f91be8f --- /dev/null +++ b/nicohack202002/uncloud/uncloud_auth/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin +from .models import User + +admin.site.register(User, UserAdmin) diff --git a/nicohack202002/uncloud/uncloud_auth/models.py b/nicohack202002/uncloud/uncloud_auth/models.py new file mode 100644 index 0000000..4c9c171 --- /dev/null +++ b/nicohack202002/uncloud/uncloud_auth/models.py @@ -0,0 +1,4 @@ +from django.contrib.auth.models import AbstractUser + +class User(AbstractUser): + pass