Skip to content

Commit

Permalink
remove the owner from project
Browse files Browse the repository at this point in the history
  • Loading branch information
velis74 committed Dec 6, 2024
1 parent 3b92a35 commit 79db01c
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions django_project_base/rest/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))


Expand All @@ -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:
Expand Down Expand Up @@ -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"])
Expand Down

0 comments on commit 79db01c

Please sign in to comment.