Skip to content

Commit

Permalink
feat: removed displayall url and checks for changing owner
Browse files Browse the repository at this point in the history
  • Loading branch information
rb-25 committed Apr 25, 2024
1 parent 4821144 commit 34d19d2
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 24 deletions.
2 changes: 2 additions & 0 deletions gibspons/spons_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Sponsorship(models.Model):
objects = models.Manager()

class Event(models.Model):

""" Model for Event details """

class Meta:
Expand Down Expand Up @@ -89,6 +90,7 @@ def money_raised(self):


class Leaderboard(models.Model):

"""Model for leaderboard data"""

class Meta:
Expand Down
8 changes: 7 additions & 1 deletion gibspons/spons_app/views/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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})
Expand Down
6 changes: 6 additions & 0 deletions gibspons/spons_app/views/leaderboard_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@


class LeaderboardView(APIView):

""" View to display leaderboard by event """

permission_classes=[IsAuthenticated]
authentication_classes=[JWTAuthentication]
def get(self,request):
Expand All @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions gibspons/spons_app/views/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@


class DisplayOrganisationView(APIView):

""" View to display an organisation """

permission_classes = [IsAuthenticated]
authentication_classes=[JWTAuthentication]

Expand Down
4 changes: 2 additions & 2 deletions gibspons/spons_app/views/sponsorship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions gibspons/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'),
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion gibspons/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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--------

Expand Down
4 changes: 1 addition & 3 deletions gibspons/users/urls.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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/<int:user_id>',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"),
Expand Down
53 changes: 39 additions & 14 deletions gibspons/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -33,6 +36,9 @@ def get(self,request):
return Response(serializer.data)

class LoginView(APIView):

"""View for users to login"""

permission_classes = []
authentication_classes = []

Expand Down Expand Up @@ -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')
Expand All @@ -99,6 +108,9 @@ def post(self, request):
return response

class ResetPasswordView(APIView):

"""View for resetting password"""

permission_classes = []
authentication_classes = []

Expand Down Expand Up @@ -132,6 +144,9 @@ def post(request):


class VerifyResetPasswordOTPView(APIView):

""" View to veryify otp of reset password """

authentication_classes = []
permission_classes = []

Expand Down Expand Up @@ -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]

Expand All @@ -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):
Expand All @@ -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()

Expand Down

0 comments on commit 34d19d2

Please sign in to comment.