Skip to content

Commit

Permalink
chore: workspace char name and slug maximum length (#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablohashescobar authored Jul 4, 2023
1 parent e23e9cc commit e4ee6a5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
88 changes: 66 additions & 22 deletions apiserver/plane/api/views/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import date, datetime
from dateutil.relativedelta import relativedelta
from uuid import uuid4

# Django imports
from django.db import IntegrityError
from django.db.models import Prefetch
Expand Down Expand Up @@ -93,14 +94,35 @@ def get_queryset(self):
.annotate(count=Func(F("id"), function="Count"))
.values("count")
)
return self.filter_queryset(
super().get_queryset().select_related("owner")
).order_by("name").filter(workspace_member__member=self.request.user).annotate(total_members=member_count).annotate(total_issues=issue_count)
return (
self.filter_queryset(super().get_queryset().select_related("owner"))
.order_by("name")
.filter(workspace_member__member=self.request.user)
.annotate(total_members=member_count)
.annotate(total_issues=issue_count)
)

def create(self, request):
try:
serializer = WorkSpaceSerializer(data=request.data)

slug = request.data.get("slug", False)
name = request.data.get("name", False)

if not name or not slug:
return Response(
{"error": "Both name and slug are required"},
status=status.HTTP_400_BAD_REQUEST,
)

if len(name) > 80 or len(slug) > 48:
return Response(
{
"error": "The maximum length for name is 80 and for slug is 48"
},
status=status.HTTP_400_BAD_REQUEST,
)

if serializer.is_valid():
serializer.save(owner=request.user)
# Create Workspace member
Expand Down Expand Up @@ -160,14 +182,20 @@ def get(self, request):
)

workspace = (
Workspace.objects.prefetch_related(
Prefetch("workspace_member", queryset=WorkspaceMember.objects.all())
)
.filter(
workspace_member__member=request.user,
(
Workspace.objects.prefetch_related(
Prefetch(
"workspace_member", queryset=WorkspaceMember.objects.all()
)
)
.filter(
workspace_member__member=request.user,
)
.select_related("owner")
)
.select_related("owner")
).annotate(total_members=member_count).annotate(total_issues=issue_count)
.annotate(total_members=member_count)
.annotate(total_issues=issue_count)
)

serializer = WorkSpaceSerializer(self.filter_queryset(workspace), many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
Expand Down Expand Up @@ -216,9 +244,20 @@ def post(self, request, slug):
)

# check for role level
requesting_user = WorkspaceMember.objects.get(workspace__slug=slug, member=request.user)
if len([email for email in emails if int(email.get("role", 10)) > requesting_user.role]):
return Response({"error": "You cannot invite a user with higher role"}, status=status.HTTP_400_BAD_REQUEST)
requesting_user = WorkspaceMember.objects.get(
workspace__slug=slug, member=request.user
)
if len(
[
email
for email in emails
if int(email.get("role", 10)) > requesting_user.role
]
):
return Response(
{"error": "You cannot invite a user with higher role"},
status=status.HTTP_400_BAD_REQUEST,
)

workspace = Workspace.objects.get(slug=slug)

Expand Down Expand Up @@ -276,14 +315,17 @@ def post(self, request, slug):

# create the user if signup is disabled
if settings.DOCKERIZED and not settings.ENABLE_SIGNUP:
_ = User.objects.bulk_create([
User(
email=email.get("email"),
password=str(uuid4().hex),
is_password_autoset=True
)
for email in emails
], batch_size=100)
_ = User.objects.bulk_create(
[
User(
email=email.get("email"),
password=str(uuid4().hex),
is_password_autoset=True,
)
for email in emails
],
batch_size=100,
)

for invitation in workspace_invitations:
workspace_invitation.delay(
Expand Down Expand Up @@ -865,7 +907,9 @@ def get(self, request, slug):
)

state_distribution = (
Issue.issue_objects.filter(workspace__slug=slug, assignees__in=[request.user])
Issue.issue_objects.filter(
workspace__slug=slug, assignees__in=[request.user]
)
.annotate(state_group=F("state__group"))
.values("state_group")
.annotate(state_count=Count("state_group"))
Expand Down
4 changes: 2 additions & 2 deletions apiserver/plane/db/models/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@


class Workspace(BaseModel):
name = models.CharField(max_length=255, verbose_name="Workspace Name")
name = models.CharField(max_length=80, verbose_name="Workspace Name")
logo = models.URLField(verbose_name="Logo", blank=True, null=True)
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="owner_workspace",
)
slug = models.SlugField(max_length=100, db_index=True, unique=True)
slug = models.SlugField(max_length=48, db_index=True, unique=True)
company_size = models.PositiveIntegerField(default=10)

def __str__(self):
Expand Down

0 comments on commit e4ee6a5

Please sign in to comment.