diff --git a/django_project_base/rest/project.py b/django_project_base/rest/project.py index 5fa6d735..897d4c51 100644 --- a/django_project_base/rest/project.py +++ b/django_project_base/rest/project.py @@ -45,21 +45,15 @@ class ProjectSerializer(DynamicModelSerializerMixin, ModelSerializer): MODEL_FUNC_SETTING_NAME = "DJANGO_PROJECT_BASE_PROJECT_MODEL_AT_RUNTIME" LAYOUT_FUNC_SETTING_NAME = "DJANGO_PROJECT_BASE_PROJECT_LAYOUT_AT_RUNTIME" - def __init__(self, *args, is_filter: bool = False, **kwds): - super().__init__(*args, is_filter=is_filter, **kwds) - - if self.context.get("view") and self.context["view"].format_kwarg == "componentdef": - self.fields.fields["owner"].display_table = DisplayMode.SUPPRESS - if self.context["view"].detail and self.instance.pk is None: - # we are rendering new form - self.fields.fields["owner"].display_form = DisplayMode.HIDDEN - # logo = fields.FileField(display=DisplayMode.SUPPRESS, required=False) # todo: not implemented UI - owner = fields.AutoGeneratedField(display_table=DisplayMode.SUPPRESS, display_form=DisplayMode.HIDDEN) class Meta: model = swapper.load_model("django_project_base", "Project") - exclude = ("logo",) # TODO we currently don't support logos well. see DPB #3 + + # we can remove the owner, because they're set at project creation and there will be a separate API for changing + # TODO we currently don't support logos well. see DPB #3 + exclude = ("logo", "owner") + layout = Layout(Row("name"), Row("slug"), Row("description")) @@ -68,15 +62,6 @@ class ProjectViewSet(DynamicModelMixin, ModelViewSet): permission_classes = (IsProjectOwnerOrReadOnly | CreateAny,) MODEL_FUNC_SETTING_NAME = "DJANGO_PROJECT_BASE_PROJECT_MODEL_AT_RUNTIME" - def new_object(self: ModelViewSet): - new_object = super().new_object() - if self.request and self.request.user and self.request.user.is_authenticated: - new_object.owner = getattr( - self.request.user, swapper.load_model("django_project_base", "Profile")._meta.model_name - ) - - return new_object - @staticmethod def _get_queryset_for_request(request): try: @@ -198,6 +183,13 @@ def set_args(name: str) -> None: return super().get_object() return super().get_object() + def perform_create(self, serializer): + serializer.save( + owner=self.request.user + if self.request and self.request.user and self.request.user.is_authenticated + else None + ) + def create(self, request, *args, **kwargs): create_response = super().create(request, *args, **kwargs) project = self.get_queryset().model.objects.get(slug=create_response.data["slug"])