Skip to content

Commit

Permalink
Merge pull request #68 from whatTeamNaeTeam/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
fnzksxl authored Jul 25, 2024
2 parents 295e360 + 7ac1914 commit 9f8a85d
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 46 deletions.
27 changes: 26 additions & 1 deletion wtnt/admin/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,32 @@ class Meta:
class ApproveTeamSerializer(serializers.ModelSerializer):
is_approved = serializers.BooleanField(write_only=True)
genre = serializers.CharField(read_only=True)
leader_info = serializers.SerializerMethodField(read_only=True)

class Meta:
model = Team
fields = ["id", "title", "created_at", "is_approved", "genre"]
fields = ["id", "title", "created_at", "is_approved", "genre", "leader_info"]

def get_leader_info(self, obj):
return {
"name": obj.leader.name,
"id": obj.leader.id,
"image_url": obj.leader.image if "github" in obj.leader.image else obj.leader.image + "thumnail.jpg",
"student_num": obj.leader.student_num,
"position": obj.leader.position,
}


class AdminTeamManageDetailSerializer(serializers.ModelSerializer):
image_url = serializers.SerializerMethodField()
image = serializers.CharField(write_only=True)

class Meta:
model = User
fields = ["id", "name", "student_num", "position", "image", "image_url"]

def get_image_url(self, obj):
if "github" not in obj.image:
return obj.image + "thumnail.jpg"
else:
return obj.image
21 changes: 19 additions & 2 deletions wtnt/admin/team/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import core.exception.notfound as notfound_exception
import core.exception.team as team_exception
from admin.serializers import ApproveTeamSerializer
from admin.serializers import ApproveTeamSerializer, AdminTeamManageDetailSerializer
from core.pagenations import ListPagenationSize10
from core.service import BaseService
from core.utils.s3 import S3Utils
from team.models import Team
from core.utils.admin import TeamResponse
from team.models import Team, TeamUser

User = get_user_model()

Expand Down Expand Up @@ -69,3 +70,19 @@ def serach_teams(self):
serializer = ApproveTeamSerializer(paginated, many=True)

return self.get_paginated_response(serializer.data)

def detailed_team(self):
team_id = self.kwargs.get("team_id")

try:
team = Team.objects.get(id=team_id)
except Team.DoesNotExist:
raise notfound_exception.TeamNotFoundError()

member_ids = TeamUser.objects.filter(team_id=team_id).values_list("user_id", flat=True)
members = User.objects.filter(id__in=member_ids)
serializer = AdminTeamManageDetailSerializer(members, many=True)

data = TeamResponse.make_team_manage_detail_data(serializer.data, team)

return data
2 changes: 2 additions & 0 deletions wtnt/admin/team/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
TeamManageView,
TeamDeleteView,
TeamSearchView,
TeamManageDetailView,
)

urlpatterns = [
path("manage", TeamManageView.as_view(), name="admin-team-manage"),
path("list", TeamDeleteView.as_view(), name="admin-team-list"),
path("search", TeamSearchView.as_view(), name="admin-team-search"),
path("manage-detail/<int:team_id>", TeamManageDetailView.as_view(), name="admin-team-manage-detail"),
]
10 changes: 10 additions & 0 deletions wtnt/admin/team/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def delete(self, request, *args, **kwargs):
return Response(data, status=status.HTTP_204_NO_CONTENT)


class TeamManageDetailView(APIView):
permission_classes = [IsAdminUser]

def get(self, request, *args, **kwargs):
admin_service = AdminTeamService(request, **kwargs)
data = admin_service.detailed_team()

return Response(data, status=status.HTTP_200_OK)


class TeamDeleteView(APIView):
permission_classes = [IsAdminUser]

Expand Down
17 changes: 17 additions & 0 deletions wtnt/core/utils/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class TeamResponse:
@staticmethod
def make_team_manage_detail_data(members, team):
leader_id = team.leader.id

for i, member in enumerate(members):
if member["id"] == leader_id:
leader_info = members.pop(i)
break

return {
"title": team.title,
"leader_info": leader_info,
"members_info": members,
"member_count": len(members) + 1,
"team_id": team.id,
}
17 changes: 17 additions & 0 deletions wtnt/core/utils/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,20 @@ def make_tech_data(tech):
@staticmethod
def make_activity_data(teams, user_id, owner_id):
return {"team": teams, "is_owner": user_id == owner_id}

@staticmethod
def make_team_manage_detail_data(members, team):
leader_id = team.leader.id

for i, member in enumerate(members):
if member["id"] == leader_id:
leader_info = members.pop(i)
break

return {
"title": team.title,
"leader_info": leader_info,
"members_info": members,
"member_count": len(members) + 1,
"team_id": team.id,
}
5 changes: 3 additions & 2 deletions wtnt/core/utils/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

class TeamResponse:
@staticmethod
def get_team_list_response(team_data, user_id, is_manage=None):
def get_team_list_response(team_data, user_id, is_manage=None, count=None):
if is_manage:
for d in team_data:
for d, c in zip(team_data, count):
if d["leader_info"]["id"] == user_id:
d["leader_info"]["is_leader"] = True
d["member_count"] = c
else:
team_ids = []
for d in team_data:
Expand Down
20 changes: 10 additions & 10 deletions wtnt/team/apply/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_applies(self):
raise notfound_exception.TeamNotFoundError()
self.check_leader(user_id, team.leader.id)

queryset = TeamApply.objects.filter(is_approved=False, team_id=team_id)
queryset = TeamApply.objects.filter(team_id=team_id, is_approved=False)

if queryset:
serializer = TeamApplySerializer(queryset, many=True)
Expand All @@ -31,7 +31,7 @@ def post_apply(self):
tech_id = self.kwargs.get("team_id")
try:
teamTech = TeamTech.objects.get(id=tech_id)
except TeamTech.DoesNotExist():
except TeamTech.DoesNotExist:
raise notfound_exception.TechNotFoundError()

if teamTech.need_num <= teamTech.current_num:
Expand Down Expand Up @@ -67,16 +67,16 @@ def approve_apply(self):

self.check_leader(user_id, team.leader.id)

serializer = TeamApplySerializer(apply, data={"is_approved": True}, partial=True)
if serializer.is_valid():
serializer.save()
team_tech.current_num += 1
team_tech.save()
team_tech.current_num += 1
team_tech.save()

teamUser = TeamUser(team_id=team.id, user_id=apply.user_id, tech=apply.tech)
teamUser.save()

teamUser = TeamUser(team_id=team.id, user_id=user_id)
teamUser.save()
apply.is_approved = True
apply.save()

return {"detail": "Success to update apply"}
return {"detail": "Success to update apply"}

def reject_apply(self):
apply_id = self.kwargs.get("team_id")
Expand Down
4 changes: 4 additions & 0 deletions wtnt/team/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ class Likes(TimestampedModel):
class TeamUser(models.Model):
team = models.ForeignKey(Team, on_delete=models.CASCADE)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
tech = models.CharField(max_length=15)

class Meta:
constraints = [models.UniqueConstraint(fields=["user", "team"], name="teamuser_unique")]
3 changes: 1 addition & 2 deletions wtnt/team/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@ def update(self, instance, validated_data):
class TeamApplySerializer(serializers.ModelSerializer):
team_id = serializers.IntegerField()
user_id = serializers.IntegerField()
is_approved = serializers.BooleanField(write_only=True)

class Meta:
model = TeamApply
fields = ["id", "team_id", "user_id", "is_approved", "created_at", "bio", "tech"]
fields = ["id", "team_id", "user_id", "created_at", "bio", "tech"]


class TeamListSerializer(LeaderInfoIncludedSerializer):
Expand Down
3 changes: 1 addition & 2 deletions wtnt/team/team/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ def create_team(self):
if serializer.is_valid():
team = serializer.save()

teamUser = TeamUser(team_id=team.id, user_id=user_id)
teamUser = TeamUser(team_id=team.id, user_id=user_id, tech="팀장")
teamUser.save()

return TeamResponse.get_detail_response(serializer.data, user_id)
print("말도안돼 왜 validation 통과 못함?")

def update_team(self):
user_id = self.request.user.id
Expand Down
77 changes: 51 additions & 26 deletions wtnt/user/profile/service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from django.contrib.auth import get_user_model
from django.db.models import Count

import core.exception.notfound as notfound_exception
import core.exception.team as team_exception
from core.service import BaseServiceWithCheckOwnership
from core.service import BaseServiceWithCheckOwnership, BaseServiceWithCheckLeader
from user.models import UserUrls, UserTech
from team.models import Team, TeamApply, TeamUser, Likes, TeamTech
from user.serializers import UserUrlSerializer, UserTechSerializer, UserProfileSerializer
from user.serializers import (
UserUrlSerializer,
UserTechSerializer,
UserProfileSerializer,
UserSerializerOnTeamManageDetail,
)
from team.serializers import TeamListSerializer, TeamManageActivitySerializer
from core.utils.profile import ProfileResponse
from core.utils.team import TeamResponse
Expand Down Expand Up @@ -133,44 +139,63 @@ def get_like_activity(self):
return data


class MyTeamManageService(BaseServiceWithCheckOwnership):
class MyTeamManageService(BaseServiceWithCheckOwnership, BaseServiceWithCheckLeader):
def get_my_teams(self):
owner_id = self.kwargs.get("user_id")
user_id = self.request.user.id

team_ids = TeamUser.objects.filter(user_id=owner_id).values_list("team_id", flat=True)
team_users = TeamUser.objects.filter(user_id=owner_id).values("team_id").annotate(member_count=Count("team_id"))
team_ids = [team["team_id"] for team in team_users]
member_counts = [team["member_count"] for team in team_users]

team_data = Team.objects.filter(id__in=team_ids)
serializer = TeamManageActivitySerializer(team_data, many=True)
data = TeamResponse.get_team_list_response(serializer.data, user_id, is_manage=True)
data = TeamResponse.get_team_list_response(serializer.data, user_id, is_manage=True, count=member_counts)

return data

def delete_or_leave_team(self):
def delete_team(self):
team_id = self.kwargs.get("user_id")
user_id = self.request.user.id

try:
team = Team.objects.get(id=team_id)
except Team.DoesNotExist:
raise notfound_exception.TeamNotFoundError()
self.check_leader(user_id, team.leader.id)
S3Utils.delete_team_image_on_s3(team.uuid)
team.delete()

if team.leader.id == user_id:
S3Utils.delete_team_image_on_s3(team.uuid)
team.delete()
return {"detail": "Success to delete team"}
else:
try:
team_apply = TeamApply.objects.get(team_id=team_id, user_id=user_id)
team_user = TeamUser.objects.get(team_id=team_id, user_id=user_id)
team_tech = TeamTech.objects.get(tech=team_apply.tech, team_id=team_id, user_id=user_id)
except TeamApply.DoesNotExist:
raise notfound_exception.ApplyNotFoundError()
except TeamUser.DoesNotExist:
raise notfound_exception.TeamUserNotFoundError()

team_apply.delete()
team_user.delete()
team_tech.current_num -= 1
team_tech.save()

return {"detail": "Success to leave team"}
return {"detail": "Success to delete team"}

def leave_team(self):
team_id = self.kwargs.get("user_id")
user_id = self.request.user.id

try:
team_user = TeamUser.objects.get(team_id=team_id, user_id=user_id)
team_tech = TeamTech.objects.get(tech=team_user.tech, team_id=team_id, user_id=user_id)
except TeamUser.DoesNotExist:
raise notfound_exception.TeamUserNotFoundError()

team_user.delete()
team_tech.current_num -= 1
team_tech.save()

return {"detail": "Success to leave team"}

def get_my_team_detail(self):
team_id = self.kwargs.get("team_id")

try:
team = Team.objects.get(id=team_id)
except Team.DoesNotExist:
raise notfound_exception.TeamNotFoundError()

member_ids = TeamUser.objects.filter(team_id=team_id).values_list("user_id", flat=True)
members = User.objects.filter(id__in=member_ids)
serializer = UserSerializerOnTeamManageDetail(members, many=True)

data = ProfileResponse.make_team_manage_detail_data(serializer.data, team)

return data
2 changes: 2 additions & 0 deletions wtnt/user/profile/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
UserUrlView,
UserMyActivityView,
UserManageActivityView,
UserManageActivityDetailView,
UserLikeTeamView,
)

Expand All @@ -15,4 +16,5 @@
path("activity/<int:user_id>", UserMyActivityView.as_view(), name="user-my-activity"),
path("team-manage/<int:user_id>", UserManageActivityView.as_view(), name="profile-team-manage"),
path("like/<int:user_id>", UserLikeTeamView.as_view(), name="like-team-list"),
path("team-manage/detail/<int:team_id>", UserManageActivityDetailView.as_view(), name="profile-team-manage-detail"),
]
18 changes: 17 additions & 1 deletion wtnt/user/profile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,29 @@ def get(self, request, *args, **kwargs):

return Response({"team": data}, status=status.HTTP_200_OK)

def patch(self, request, *args, **kwargs):
myteam_service = MyTeamManageService(request, **kwargs)
data = myteam_service.leave_team()

return Response(data, status=status.HTTP_204_NO_CONTENT)

def delete(self, request, *args, **kwargs):
myteam_service = MyTeamManageService(request, **kwargs)
data = myteam_service.delete_or_leave_team()
data = myteam_service.delete_team()

return Response(data, status=status.HTTP_204_NO_CONTENT)


class UserManageActivityDetailView(APIView):
permission_classes = [IsApprovedUser]

def get(self, request, *args, **kwargs):
myteam_service = MyTeamManageService(request, **kwargs)
data = myteam_service.get_my_team_detail()

return Response(data, status=status.HTTP_200_OK)


class UserLikeTeamView(APIView):
permission_classes = [IsApprovedUser]

Expand Down
Loading

0 comments on commit 9f8a85d

Please sign in to comment.