Skip to content

Commit

Permalink
Merge pull request #107 from neuroforgede/wip/tenant-api#main
Browse files Browse the repository at this point in the history
Wip/tenant api#main
  • Loading branch information
s4ke authored Mar 28, 2024
2 parents 512bfe2 + c27a606 commit 04cc6ae
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 162 deletions.
2 changes: 1 addition & 1 deletion skipper/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name = "pypi"

[packages]
setuptools = "==68.2.2"
djangorestframework = "==3.14.0"
djangorestframework = "==3.15.1"
djangorestframework-simplejwt = { version = "==5.3.1" }
# at least required for simplejwt crypto
cryptography = "==42.0.5"
Expand Down
319 changes: 160 additions & 159 deletions skipper/Pipfile.lock

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion skipper/skipper/core/components/tenant/tenant_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

from django.contrib.auth.models import User
from django.db.models import Model, QuerySet
from django_filters.rest_framework import FilterSet, CharFilter # type: ignore
from rest_framework.request import Request
from rest_framework.viewsets import ModelViewSet
from rest_framework import permissions, serializers
from rest_framework.generics import get_object_or_404
from rest_framework.serializers import ValidationError

from skipper.core import constants
from skipper.core.serializers.base import BaseSerializer
Expand All @@ -31,6 +33,15 @@ class GenericTenantUsertHyperlinkedIdentityField(MultipleParameterHyperlinkedIde
def get_extra_lookup_url_kwargs(self, obj: Model, view_name: str, request: Request, format: str) -> Dict[str, Any]:
tenant_user = cast(Tenant_User, obj)
return {'tenant_id': tenant_user.tenant.id}


class UserFilterSet(FilterSet): # type: ignore
username = CharFilter(
field_name="username", method='username_equal', label='Username'
)

def username_equal(self, qs: 'QuerySet[Any]', name: str, value: str) -> 'QuerySet[Any]':
return qs.filter(user__username=value)


class TenantUserViewSet(
Expand All @@ -41,10 +52,11 @@ class TenantUserViewSet(
skipper_base_name = constants.core_tenant_user_view_set_name
kwargs: Dict[str, Any]

filterset_class = UserFilterSet

def get_serializer_class(self) -> Any:

class GenericTenantUserSerializer(BaseSerializer):

url = GenericTenantUsertHyperlinkedIdentityField(view_name=constants.core_tenant_user_view_set_name + '-detail')
user = serializers.HyperlinkedRelatedField(
view_name=constants.core_user_view_set_name + '-detail',
Expand All @@ -53,6 +65,15 @@ class GenericTenantUserSerializer(BaseSerializer):
tenant_manager = serializers.BooleanField()
system = serializers.BooleanField()

def to_representation(self, obj: Any) -> Any:
repr = super().to_representation(obj)
repr['username'] = obj.user.username
return repr

def validate_user(self, value: User) -> User:
if Tenant_User.objects.filter(user=value).exists():
raise ValidationError("User is already assigned to a tenant")

def create(self, validated_data: Any) -> Any:
kwargs = self.context.get('view').kwargs # type: ignore
validated_data['tenant'] = Tenant.objects.get(id=kwargs['tenant_id'])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.2 on 2024-03-28 19:19

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dataseries', '0097_alter_consumerevent_retries'),
]

operations = [
migrations.AlterField(
model_name='consumer',
name='health',
field=models.CharField(choices=[('UNKNOWN', 'UNKNOWN'), ('UNHEALTHY', 'UNHEALTHY'), ('HEALTHY', 'HEALTHY'), ('RATE_LIMIT', 'RATE_LIMIT')], default='UNKNOWN', max_length=100),
),
]
1 change: 1 addition & 0 deletions skipper/skipper/environment_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"SKIPPER_S3_STATIC_BUCKET_NAME": 'skipper-static',
"SKIPPER_SESSION_INSECURE": "true",
"SKIPPER_FLOW_DEFAULT_SYSTEM_SECRET": 'QAeV8ESByNqyXNA4bUNMqEVvbhR7B4ftCEWTGM2ujbVtMfuHL7YnjhWUzEaUWDW9',
"SKIPPER_FEATURE_FLAG_ALL": "true",
# keep anything that was overridden
**os.environ
})
Expand Down
2 changes: 1 addition & 1 deletion skipper/skipper/templates/rest_framework/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<ul class="nav navbar-nav pull-right">
{% block userlinks %}
{% if user.is_authenticated %}
{% optional_logout request user %}
{% optional_logout request user csrf_token %}
{% else %}
{% optional_login request %}
{% endif %}
Expand Down

0 comments on commit 04cc6ae

Please sign in to comment.