Skip to content

Commit

Permalink
fixes for dynamic project model & layout
Browse files Browse the repository at this point in the history
  • Loading branch information
velis74 committed Dec 6, 2024
1 parent 4493b66 commit 3b92a35
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
32 changes: 28 additions & 4 deletions django_project_base/rest/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.conf import settings
from django.core.management import call_command
from django.db import transaction
from django.db.models import Model
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils.module_loading import import_string
Expand Down Expand Up @@ -83,6 +84,9 @@ def _get_queryset_for_request(request):
model_func = import_string(getattr(settings, ProjectViewSet.MODEL_FUNC_SETTING_NAME, None))
model = model_func(None, request)
except ImportError:
model = None

if not model:
model = swapper.load_model("django_project_base", "Project")

qs = model.objects
Expand Down Expand Up @@ -124,8 +128,8 @@ def get_permissions(self):
)
def get_current_project(self, request: Request, **kwargs) -> Response:
try:
request.selected_project.get_deferred_fields() # force immediate LazyObject evaluation
serializer = self.get_serializer(request.selected_project)
instance = self.get_instance(request.selected_project.pk)
serializer = self.get_serializer(instance)
except ProjectNotSelectedError as e:
raise NotFound(e.message)
return Response(serializer.data)
Expand All @@ -141,14 +145,34 @@ def get_current_project(self, request: Request, **kwargs) -> Response:
@get_current_project.mapping.put
def update_current_profile(self, request: Request, **kwargs) -> Response:
try:
serializer = self.get_serializer(request.selected_project, data=request.data, many=False)
instance = self.get_instance(request.selected_project.pk, create_if_not_exists=True)
serializer = self.get_serializer(instance, data=request.data, many=False)
serializer.is_valid(raise_exception=True)
serializer.save()
except ProjectNotSelectedError as e:
raise NotFound(e.message)

return Response(serializer.data)

def get_instance(self, pk, create_if_not_exists=False):
model = self.get_queryset().model
parent_model = list(model._meta.parents.keys())[0] if model._meta.parents else None

try:
return self.get_queryset().get(pk=pk)
except model.DoesNotExist:
if parent_model:
try:
parent = parent_model.objects.get(pk=pk)
if create_if_not_exists:
instance: Model = model(**{f"{parent_model._meta.model_name}_ptr": parent})
instance.save_base(raw=True)
return instance
return parent
except parent_model.DoesNotExist:
raise model.DoesNotExist()
raise model.DoesNotExist()

def get_object(self):
SLUG_FIELD_NAME: str = settings.DJANGO_PROJECT_BASE_SLUG_FIELD_NAME

Expand Down Expand Up @@ -176,7 +200,7 @@ def set_args(name: str) -> None:

def create(self, request, *args, **kwargs):
create_response = super().create(request, *args, **kwargs)
project = swapper.load_model("django_project_base", "Project").objects.get(slug=create_response.data["slug"])
project = self.get_queryset().model.objects.get(slug=create_response.data["slug"])
swapper.load_model("django_project_base", "ProjectMember").objects.create(project=project, member=request.user)
project_settings_model = swapper.load_model("django_project_base", "ProjectSettings")
project_settings_model.objects.create(
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"export-pdf": "press-export-pdf --debug export docs"
},
"peerDependencies": {
"@velis/dynamicforms": "^0.76.20",
"@velis/dynamicforms": "^0.77.1",
"axios": "^1.2.0",
"lodash": "^4.17.15",
"pinia": "^2.0.33",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
django
djangorestframework
dynamicforms>=0.76.20
dynamicforms>=0.77.1
swapper
django-rest-registration
drf-spectacular>=0.17.2, <0.26.0
Expand Down

0 comments on commit 3b92a35

Please sign in to comment.