Skip to content

Commit

Permalink
Merge pull request #518 from MicroPyramid/new_release
Browse files Browse the repository at this point in the history
new release
  • Loading branch information
ashwin31 authored Dec 15, 2023
2 parents 3c4d14c + fbf2bd9 commit 5fc6e53
Show file tree
Hide file tree
Showing 16 changed files with 4,014 additions and 695 deletions.
14 changes: 7 additions & 7 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from leads.models import Lead
from leads.serializer import LeadSerializer

from common.external_auth import CustomDualAuthentication
#from common.external_auth import CustomDualAuthentication
from common.serializer import (
AttachmentsSerializer,
CommentSerializer,
Expand All @@ -57,7 +57,7 @@


class AccountsListView(APIView, LimitOffsetPagination):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
model = Account
serializer_class = AccountReadSerializer
Expand All @@ -67,7 +67,7 @@ def get_context_data(self, **kwargs):
queryset = self.model.objects.filter(org=self.request.profile.org).order_by("-id")
if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin:
queryset = queryset.filter(
Q(created_by=self.request.profile.user.user) | Q(assigned_to=self.request.profile)
Q(created_by=self.request.profile.user) | Q(assigned_to=self.request.profile)
).distinct()

if params:
Expand Down Expand Up @@ -215,7 +215,7 @@ def post(self, request, *args, **kwargs):


class AccountDetailView(APIView):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
serializer_class = AccountReadSerializer

Expand Down Expand Up @@ -496,7 +496,7 @@ def post(self, request, pk, **kwargs):

class AccountCommentView(APIView):
model = Comment
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
serializer_class = AccountCommentEditSwaggerSerializer

Expand Down Expand Up @@ -558,7 +558,7 @@ def delete(self, request, pk, format=None):

class AccountAttachmentView(APIView):
model = Attachments
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
serializer_class = AccountDetailEditSwaggerSerializer

Expand All @@ -585,7 +585,7 @@ def delete(self, request, pk, format=None):


class AccountCreateMailView(APIView):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
model = Account
serializer_class = EmailWriteSerializer
Expand Down
10 changes: 5 additions & 5 deletions cases/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from cases.tasks import send_email_to_assigned_user
from common.models import Attachments, Comment, Profile

from common.external_auth import CustomDualAuthentication
#from common.external_auth import CustomDualAuthentication
from common.serializer import AttachmentsSerializer, CommentSerializer
from common.utils import CASE_TYPE, PRIORITY_CHOICE, STATUS_CHOICE
from contacts.models import Contact
Expand All @@ -25,7 +25,7 @@


class CaseListView(APIView, LimitOffsetPagination):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
model = Case

Expand Down Expand Up @@ -148,7 +148,7 @@ def post(self, request, *args, **kwargs):


class CaseDetailView(APIView):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
model = Case

Expand Down Expand Up @@ -387,7 +387,7 @@ def post(self, request, pk, **kwargs):

class CaseCommentView(APIView):
model = Comment
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)

def get_object(self, pk):
Expand Down Expand Up @@ -450,7 +450,7 @@ def delete(self, request, pk, format=None):

class CaseAttachmentView(APIView):
model = Attachments
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)

@extend_schema(
Expand Down
22 changes: 9 additions & 13 deletions common/external_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
from common.models import Org,Profile,User
from django.conf import settings

def verify_jwt_token(token1):
def verify_jwt_token(token):
secret_key = (settings.SECRET_KEY) # Replace with your secret key used for token encoding/decoding
try:
token = token1.split(" ")[1] # getting the token value
payload = jwt.decode(token, (settings.SECRET_KEY), algorithms=[settings.JWT_ALGO])
return True, payload
except jwt.ExpiredSignatureError:
Expand All @@ -18,40 +17,37 @@ def verify_jwt_token(token1):

class CustomDualAuthentication(BaseAuthentication):

def authenticate(self, request: Request):

def authenticate(self, request):
jwt_user = None
profile = None

# Check JWT authentication
# Implement your JWT authentication logic here
# You might use a library like `python_jwt` to decode and verify the JWT token
# Example code assumes a method `verify_jwt_token` for JWT verification
jwt_token = request.META.get('HTTP_AUTHORIZATION')
jwt_token = request.headers.get('Authorization', '').split(' ')[1] if 'Authorization' in request.headers else None
if jwt_token:
is_valid, jwt_payload = verify_jwt_token(jwt_token)
if is_valid:
# JWT authentication successful
jwt_user = (User.objects.get(id=jwt_payload['user_id']),True)
jwt_user = (User.objects.get(id=jwt_payload['user_id']), True)
if jwt_payload['user_id'] is not None:
if request.headers.get("org"):
profile = Profile.objects.get(
user_id=jwt_payload['user_id'], org=request.headers.get("org"), is_active=True
)
if profile:
request.profile = profile

# Check API key authentication
api_key = request.headers.get('Token') # Get API key from request query params
if api_key:
try:
organization = Org.objects.get(api_key=api_key)
# API key authentication successful
api_key_user = organization
request.META['org'] = api_key_user.id
profile = Profile.objects.filter(org=api_key_user,role="ADMIN").first()
profile = Profile.objects.filter(org=api_key_user, role="ADMIN").first()
request.profile = profile
profile = (profile.user,True)
profile = (profile.user, True)
except Org.DoesNotExist:
raise AuthenticationFailed('Invalid API Key')

# Select the appropriate user based on authentication method
# Return the user if any authentication method succeeded
return jwt_user or profile
10 changes: 10 additions & 0 deletions common/middleware/get_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def process_request(self, request):
token = token1.split(" ")[1] # getting the token value
decoded = jwt.decode(token, (settings.SECRET_KEY), algorithms=[settings.JWT_ALGO])
user_id = decoded['user_id']
api_key = request.headers.get('Token') # Get API key from request query params
if api_key:
try:
organization = Org.objects.get(api_key=api_key)
api_key_user = organization
request.META['org'] = api_key_user.id
profile = Profile.objects.filter(org=api_key_user, role="ADMIN").first()
user_id = profile.user.id
except Org.DoesNotExist:
raise AuthenticationFailed('Invalid API Key')
if user_id is not None:
if request.headers.get("org"):
profile = Profile.objects.get(
Expand Down
12 changes: 6 additions & 6 deletions common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from django.views.decorators.http import require_http_methods
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiExample, OpenApiParameter, extend_schema
from common.external_auth import CustomDualAuthentication
#from common.external_auth import CustomDualAuthentication
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.pagination import LimitOffsetPagination
Expand Down Expand Up @@ -369,7 +369,7 @@ def get(self, request, format=None):


class OrgProfileCreateView(APIView):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)

model1 = Org
Expand Down Expand Up @@ -413,7 +413,7 @@ def post(self, request, format=None):
)

@extend_schema(
description="Just Pass the token, will return ORG list, associated with user",
description="Just Pass the token, will return ORG list, associated with user"
)
def get(self, request, format=None):
"""
Expand Down Expand Up @@ -441,7 +441,7 @@ def get(self, request, format=None):
return Response(context, status=status.HTTP_200_OK)

class DocumentListView(APIView, LimitOffsetPagination):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
model = Document

Expand Down Expand Up @@ -576,7 +576,7 @@ def post(self, request, *args, **kwargs):


class DocumentDetailView(APIView):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)

def get_object(self, pk):
Expand Down Expand Up @@ -815,7 +815,7 @@ def post(self, request, *args, **kwargs):

class DomainDetailView(APIView):
model = APISettings
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)

@extend_schema(
Expand Down
18 changes: 18 additions & 0 deletions contacts/migrations/0004_alter_contact_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.1 on 2023-11-23 05:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('contacts', '0003_alter_contact_secondary_number_and_more'),
]

operations = [
migrations.AlterField(
model_name='contact',
name='address',
field=models.TextField(blank=True, null=True),
),
]
20 changes: 20 additions & 0 deletions contacts/migrations/0005_alter_contact_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.1 on 2023-11-23 05:59

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('common', '0007_org_is_active'),
('contacts', '0004_alter_contact_address'),
]

operations = [
migrations.AlterField(
model_name='contact',
name='address',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='adress_contacts', to='common.address'),
),
]
10 changes: 5 additions & 5 deletions contacts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from common.utils import COUNTRIES

from common.external_auth import CustomDualAuthentication
#from common.external_auth import CustomDualAuthentication
from contacts import swagger_params1
from contacts.models import Contact, Profile
from contacts.serializer import *
Expand All @@ -27,7 +27,7 @@


class ContactsListView(APIView, LimitOffsetPagination):
authentication_classes = (CustomDualAuthentication,)
#authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
model = Contact

Expand Down Expand Up @@ -140,7 +140,7 @@ def post(self, request, *args, **kwargs):


class ContactDetailView(APIView):
# authentication_classes = (CustomDualAuthentication,)
# #authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)
model = Contact

Expand Down Expand Up @@ -390,7 +390,7 @@ def post(self, request, pk, **kwargs):

class ContactCommentView(APIView):
model = Comment
# authentication_classes = (CustomDualAuthentication,)
# #authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)

def get_object(self, pk):
Expand Down Expand Up @@ -452,7 +452,7 @@ def delete(self, request, pk, format=None):

class ContactAttachmentView(APIView):
model = Attachments
# authentication_classes = (CustomDualAuthentication,)
# #authentication_classes = (CustomDualAuthentication,)
permission_classes = (IsAuthenticated,)

@extend_schema(
Expand Down
11 changes: 9 additions & 2 deletions crm/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"corsheaders.middleware.CorsMiddleware",
"crum.CurrentRequestUserMiddleware",
# "common.middleware.get_company.GetProfileAndOrg",
# "common.external_auth.CustomDualAuthentication"
"common.middleware.get_company.GetProfileAndOrg",
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
]

Expand Down Expand Up @@ -256,6 +257,7 @@
"EXCEPTION_HANDLER": "rest_framework.views.exception_handler",
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework_simplejwt.authentication.JWTAuthentication",
"common.external_auth.CustomDualAuthentication"
# "rest_framework.authentication.SessionAuthentication",
# "rest_framework.authentication.BasicAuthentication",
),
Expand All @@ -282,7 +284,12 @@
SWAGGER_SETTINGS = {
"DEFAULT_INFO": "crm.urls.info",
"SECURITY_DEFINITIONS": {
"api_key": {"type": "apiKey", "name": "Authorization", "in": "header"},
"Bearer": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
"description": "Enter 'Bearer <token>'",
},
},
}

Expand Down
Loading

0 comments on commit 5fc6e53

Please sign in to comment.