chore: workspace char name and slug maximum length (#1453)

This commit is contained in:
pablohashescobar 2023-07-04 13:54:48 +05:30 committed by GitHub
parent e23e9ccdbb
commit e4ee6a5bfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 24 deletions

View File

@ -3,6 +3,7 @@ import jwt
from datetime import date, datetime from datetime import date, datetime
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from uuid import uuid4 from uuid import uuid4
# Django imports # Django imports
from django.db import IntegrityError from django.db import IntegrityError
from django.db.models import Prefetch from django.db.models import Prefetch
@ -93,14 +94,35 @@ class WorkSpaceViewSet(BaseViewSet):
.annotate(count=Func(F("id"), function="Count")) .annotate(count=Func(F("id"), function="Count"))
.values("count") .values("count")
) )
return self.filter_queryset( return (
super().get_queryset().select_related("owner") 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) .order_by("name")
.filter(workspace_member__member=self.request.user)
.annotate(total_members=member_count)
.annotate(total_issues=issue_count)
)
def create(self, request): def create(self, request):
try: try:
serializer = WorkSpaceSerializer(data=request.data) 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(): if serializer.is_valid():
serializer.save(owner=request.user) serializer.save(owner=request.user)
# Create Workspace member # Create Workspace member
@ -160,14 +182,20 @@ class UserWorkSpacesEndpoint(BaseAPIView):
) )
workspace = ( workspace = (
(
Workspace.objects.prefetch_related( Workspace.objects.prefetch_related(
Prefetch("workspace_member", queryset=WorkspaceMember.objects.all()) Prefetch(
"workspace_member", queryset=WorkspaceMember.objects.all()
)
) )
.filter( .filter(
workspace_member__member=request.user, 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) serializer = WorkSpaceSerializer(self.filter_queryset(workspace), many=True)
return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.data, status=status.HTTP_200_OK)
@ -216,9 +244,20 @@ class InviteWorkspaceEndpoint(BaseAPIView):
) )
# check for role level # check for role level
requesting_user = WorkspaceMember.objects.get(workspace__slug=slug, member=request.user) requesting_user = WorkspaceMember.objects.get(
if len([email for email in emails if int(email.get("role", 10)) > requesting_user.role]): workspace__slug=slug, member=request.user
return Response({"error": "You cannot invite a user with higher role"}, status=status.HTTP_400_BAD_REQUEST) )
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) workspace = Workspace.objects.get(slug=slug)
@ -276,14 +315,17 @@ class InviteWorkspaceEndpoint(BaseAPIView):
# create the user if signup is disabled # create the user if signup is disabled
if settings.DOCKERIZED and not settings.ENABLE_SIGNUP: if settings.DOCKERIZED and not settings.ENABLE_SIGNUP:
_ = User.objects.bulk_create([ _ = User.objects.bulk_create(
[
User( User(
email=email.get("email"), email=email.get("email"),
password=str(uuid4().hex), password=str(uuid4().hex),
is_password_autoset=True is_password_autoset=True,
) )
for email in emails for email in emails
], batch_size=100) ],
batch_size=100,
)
for invitation in workspace_invitations: for invitation in workspace_invitations:
workspace_invitation.delay( workspace_invitation.delay(
@ -865,7 +907,9 @@ class UserWorkspaceDashboardEndpoint(BaseAPIView):
) )
state_distribution = ( 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")) .annotate(state_group=F("state__group"))
.values("state_group") .values("state_group")
.annotate(state_count=Count("state_group")) .annotate(state_count=Count("state_group"))

View File

@ -15,14 +15,14 @@ ROLE_CHOICES = (
class Workspace(BaseModel): 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) logo = models.URLField(verbose_name="Logo", blank=True, null=True)
owner = models.ForeignKey( owner = models.ForeignKey(
settings.AUTH_USER_MODEL, settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name="owner_workspace", 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) company_size = models.PositiveIntegerField(default=10)
def __str__(self): def __str__(self):