From 34d19d226f0a0f2618eee26f913d1c860e23393e Mon Sep 17 00:00:00 2001 From: rb-25 Date: Fri, 26 Apr 2024 01:13:57 +0530 Subject: [PATCH] feat: removed displayall url and checks for changing owner --- gibspons/spons_app/models.py | 2 + gibspons/spons_app/views/ai.py | 8 ++- gibspons/spons_app/views/leaderboard_views.py | 6 +++ gibspons/spons_app/views/organisation.py | 3 ++ gibspons/spons_app/views/sponsorship.py | 4 +- gibspons/users/models.py | 11 ++-- gibspons/users/serializers.py | 2 +- gibspons/users/urls.py | 4 +- gibspons/users/views.py | 53 ++++++++++++++----- 9 files changed, 69 insertions(+), 24 deletions(-) diff --git a/gibspons/spons_app/models.py b/gibspons/spons_app/models.py index f4ea8da..1a4f971 100644 --- a/gibspons/spons_app/models.py +++ b/gibspons/spons_app/models.py @@ -59,6 +59,7 @@ class Sponsorship(models.Model): objects = models.Manager() class Event(models.Model): + """ Model for Event details """ class Meta: @@ -89,6 +90,7 @@ def money_raised(self): class Leaderboard(models.Model): + """Model for leaderboard data""" class Meta: diff --git a/gibspons/spons_app/views/ai.py b/gibspons/spons_app/views/ai.py index e12d70e..59097e8 100644 --- a/gibspons/spons_app/views/ai.py +++ b/gibspons/spons_app/views/ai.py @@ -11,6 +11,9 @@ from spons_app.config import model class EmailGeneratorView(APIView): + + """ This generates an email based on the information provided """ + permission_classes=[IsAuthenticated] authentication_classes=[JWTAuthentication] def post(self,request): @@ -27,6 +30,9 @@ def post(self,request): return Response({"message":response_html}) class LinkedInGeneratorView(APIView): + + """ View to generate linkedin request """ + permission_classes=[IsAuthenticated] authentication_classes=[JWTAuthentication] def post(self,request): @@ -37,7 +43,7 @@ def post(self,request): organisation=get_object_or_404(Organisation,id=company.organisation.id) event=get_object_or_404(Event, id=serializer.validated_data['event_id']) user=get_object_or_404(User,id=request.user.id) - prompt = f"Ignore all previous prompts. Here is the information you are provided with. {organisation.name} is an organisation from {organisation.location} and they are organising an event {event.name}. The event is being hosted from {event.start_date} to {event.end_date} with expected registrations {event.expected_reg}. Additional information about the event is {event.description}. Assume you are {user.name}, a manager at {organisation.name} Write me a professional linkedin request inviting the POC {poc.name} from {company.name} with the designation {poc.designation} to sponsor the event hosted by our organisation. The company is in the {company.industry} industry. Our organisation is in the {organisation.industry}. Additional information given is {serializer.validated_data['additional']} Write this linkedin in 50 to 100 words. " + prompt = f"Ignore all previous prompts. Here is the information you are provided with. {organisation.name} is an organisation from {organisation.location} and they are organising an event {event.name}. The event is being hosted from {event.start_date} to {event.end_date} with expected registrations {event.expected_reg}. Additional information about the event is {event.description}. Assume you are {user.name}, a manager at {organisation.name} Write me a professional linkedin request inviting the POC {poc.name} from {company.name} with the designation {poc.designation} to sponsor the event hosted by our organisation. The company is in the {company.industry} industry. Our organisation is in the {organisation.industry}. Additional information given is {serializer.validated_data['additional']} Write a linkedin request in 50 to 100 words for this. " response = model.generate_content(prompt) response_html=markdown.markdown(response.text).replace('\n','') return Response({"message":response_html}) diff --git a/gibspons/spons_app/views/leaderboard_views.py b/gibspons/spons_app/views/leaderboard_views.py index ba0bd0b..770e03a 100644 --- a/gibspons/spons_app/views/leaderboard_views.py +++ b/gibspons/spons_app/views/leaderboard_views.py @@ -11,6 +11,9 @@ class LeaderboardView(APIView): + + """ View to display leaderboard by event """ + permission_classes=[IsAuthenticated] authentication_classes=[JWTAuthentication] def get(self,request): @@ -20,6 +23,9 @@ def get(self,request): return Response(serializer.data, status=status.HTTP_200_OK) class StatusPieChartView(APIView): + + """ View to display pie chart of all status by event """ + permission_classes=[IsAuthenticated] authentication_classes=[JWTAuthentication] def get(self,request): diff --git a/gibspons/spons_app/views/organisation.py b/gibspons/spons_app/views/organisation.py index 98a867a..b80b4e3 100644 --- a/gibspons/spons_app/views/organisation.py +++ b/gibspons/spons_app/views/organisation.py @@ -15,6 +15,9 @@ class DisplayOrganisationView(APIView): + + """ View to display an organisation """ + permission_classes = [IsAuthenticated] authentication_classes=[JWTAuthentication] diff --git a/gibspons/spons_app/views/sponsorship.py b/gibspons/spons_app/views/sponsorship.py index ec1ec70..025bde6 100644 --- a/gibspons/spons_app/views/sponsorship.py +++ b/gibspons/spons_app/views/sponsorship.py @@ -16,9 +16,9 @@ class UpdateSponsorView(APIView): - """ View to update """ + """ View to update sponsor """ - permission_classes=[IsAuthenticated,IsCompanyCreator,IsApproved] + permission_classes=[IsAuthenticated,IsApproved] authentication_classes=[JWTAuthentication] @staticmethod diff --git a/gibspons/users/models.py b/gibspons/users/models.py index 02c6ae7..9ea0050 100644 --- a/gibspons/users/models.py +++ b/gibspons/users/models.py @@ -6,6 +6,9 @@ from spons_app.models import Event class Organisation(models.Model): + + """ Model for storing organisation information """ + name=models.CharField(max_length=255) invite_code = models.CharField(max_length=8, unique=True, blank=True) industry=models.CharField(max_length=255) @@ -27,6 +30,9 @@ def total_money_raised(self): return total class User(AbstractUser): + + """ Model for storing user information """ + ROLE_CHOICES = [ ('user', 'User'), ('owner', 'Owner'), @@ -48,9 +54,8 @@ def get_expiry_date() -> datetime: return timezone.now() + timedelta(minutes=5) class OTP(models.Model): - """ - Model for storing OTPs - """ + + """ Model for storing OTPs """ class Meta: verbose_name = "OTP" diff --git a/gibspons/users/serializers.py b/gibspons/users/serializers.py index eb962c8..1c8611d 100644 --- a/gibspons/users/serializers.py +++ b/gibspons/users/serializers.py @@ -23,7 +23,7 @@ def create(self,validated_data): class ChangeRoleSerializer(serializers.Serializer): id = serializers.IntegerField() role = serializers.CharField() - is_approved=serializers.BooleanField() + is_approved=serializers.BooleanField(source='User.is_approved',required=False) #-----------ORGANISATION SERIALIZERS-------- diff --git a/gibspons/users/urls.py b/gibspons/users/urls.py index 9b68756..8463dce 100644 --- a/gibspons/users/urls.py +++ b/gibspons/users/urls.py @@ -1,6 +1,5 @@ from django.urls import path -from django.contrib.auth import views as auth_views -from .views import RegisterView,LoginView,LogoutView,CreateOrganisationView,JoinOrganisationView,DeleteUserView,ChangeRoleView,UpdateDisplayUserView,ApproveView, CheckView,DisplayAllUsersView,ResetPasswordView,VerifyResetPasswordOTPView +from .views import RegisterView,LoginView,LogoutView,CreateOrganisationView,JoinOrganisationView,DeleteUserView,ChangeRoleView,UpdateDisplayUserView,ApproveView, CheckView,ResetPasswordView,VerifyResetPasswordOTPView from rest_framework_simplejwt.views import TokenRefreshView urlpatterns = [ @@ -13,7 +12,6 @@ path("verify_reset_password_otp/",VerifyResetPasswordOTPView.as_view(), name="verify_reset_password_otp"), path('user/',UpdateDisplayUserView.as_view(),name="update_user"), path('user/',DeleteUserView.as_view(),name="delete_user"), - path('displayall/',DisplayAllUsersView.as_view(),name="display_all_users"), path('approve/',ApproveView.as_view(),name="approve_user"), path('changerole/',ChangeRoleView.as_view(),name="change_role"), path('createorg/',CreateOrganisationView.as_view(),name="create_organisation"), diff --git a/gibspons/users/views.py b/gibspons/users/views.py index 17398e1..4c5b9fa 100644 --- a/gibspons/users/views.py +++ b/gibspons/users/views.py @@ -22,6 +22,9 @@ def get(self,request): #---------------AUTH VIEWS--------------------- class RegisterView(APIView): + + """ View to register users """ + permission_classes = [AllowAny] def post(self, request): serializer=UserSerializer(data=request.data) @@ -33,6 +36,9 @@ def get(self,request): return Response(serializer.data) class LoginView(APIView): + + """View for users to login""" + permission_classes = [] authentication_classes = [] @@ -89,7 +95,10 @@ def post(request): ) class LogoutView(APIView): - permission_classes = [AllowAny] + + """ View for users to logout """ + + permission_classes = [] def post(self, request): response = Response() response.delete_cookie('jwt') @@ -99,6 +108,9 @@ def post(self, request): return response class ResetPasswordView(APIView): + + """View for resetting password""" + permission_classes = [] authentication_classes = [] @@ -132,6 +144,9 @@ def post(request): class VerifyResetPasswordOTPView(APIView): + + """ View to veryify otp of reset password """ + authentication_classes = [] permission_classes = [] @@ -167,6 +182,9 @@ def post(request): #---------------USER VIEWS--------------- class UpdateDisplayUserView(APIView): + + """ View to update or display a user/s """ + permission_classes=[IsAuthenticated] authentication_classes=[JWTAuthentication] @@ -183,27 +201,28 @@ def patch(request): @staticmethod def get(request): - users=User.objects.filter(id=request.user.id) - user_serializer = UserSerializer(users, many=True) - return Response(user_serializer.data, status=status.HTTP_200_OK) - -class DisplayAllUsersView(APIView): - permission_classes=[IsAuthenticated,IsApproved] - authentication_classes=[JWTAuthentication] - def get(self, request): - organisation_id = request.query_params.get('org') + + organisation_id = request.query_params.get('org') + if organisation_id is None: + users=User.objects.filter(id=request.user.id) + user_serializer = UserSerializer(users, many=True) + return Response(user_serializer.data, status=status.HTTP_200_OK) + if int(request.user.organisation.id) != int(organisation_id): return Response({'detail': 'Permission denied.'}, status=status.HTTP_403_FORBIDDEN) - if organisation_id is None: - return Response({'detail': 'Organisation ID is required'}, status=status.HTTP_400_BAD_REQUEST) + if request.user.role == 'admin' or request.user.role == 'owner': users = User.objects.filter(organisation=organisation_id) else: users = User.objects.filter(organisation=organisation_id, is_approved=True) user_serializer = UserSerializer(users, many=True) return Response(user_serializer.data, status=status.HTTP_200_OK) - + + class DeleteUserView(APIView): + + """ View to delete user. Can be done by admin """ + permission_classes = [IsAuthenticated,IsApproved] authentication_classes=[JWTAuthentication] def delete(self, request,user_id): @@ -217,13 +236,19 @@ def delete(self, request,user_id): #allowing only owner to assign admins class ChangeRoleView(APIView): - permission_classes = [IsAuthenticated, IsOwner,IsApproved] + + """ Allowing owner to change roles """ + + permission_classes = [IsAuthenticated, IsApproved] authentication_classes=[JWTAuthentication] def post(self, request): serializer = ChangeRoleSerializer(data=request.data) serializer.is_valid(raise_exception=True) user_to_change = get_object_or_404(User,id=serializer.validated_data['id']) + all_owners = User.objects.filter(role="owner",organisation=request.user.organisation) + if user_to_change.role == "owner" and len(all_owners)==1: + return Response({'detail' : 'Please set another owner before changing'}) user_to_change.role = serializer.data['role'].lower() user_to_change.save()