main
parent
30e894fb86
commit
b75cd6a09a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,9 +1,8 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
from . import views
|
from .views import *
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('login/', views.login_user),
|
path("users/", UserProfilesAPIView.as_view(), name="users-with-profiles"),
|
||||||
path('register-device/', views.register_device),
|
path("utilities/", BusinessDataAPIView.as_view(), name="utilities"),
|
||||||
path('update-device/', views.update_device, name='update_device'),
|
|
||||||
]
|
]
|
@ -1,72 +1,59 @@
|
|||||||
|
|
||||||
|
|
||||||
from osinacore.models import *
|
from osinacore.models import *
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework import status
|
||||||
|
from django.contrib.auth.models import User
|
||||||
from .serializers import *
|
from .serializers import *
|
||||||
from rest_framework.decorators import api_view
|
|
||||||
from .utils import *
|
class UserProfilesAPIView(APIView):
|
||||||
import calendar
|
def get(self, request):
|
||||||
from django.contrib.auth.hashers import check_password
|
data = []
|
||||||
from datetime import datetime
|
|
||||||
import datetime as datetime2
|
users = User.objects.all()
|
||||||
from fcm_django.models import FCMDevice
|
|
||||||
|
for user in users:
|
||||||
@api_view(['POST'])
|
user_data = {
|
||||||
def login_user(request):
|
"user": UserSerializer(user).data,
|
||||||
try:
|
"customer_profile": None,
|
||||||
username = request.data.get('username')
|
"staff_profile": None
|
||||||
password = request.data.get('password')
|
}
|
||||||
if(not password or not username):
|
|
||||||
raise ValueError("Missing fields")
|
# Check for customer profile
|
||||||
user = User.objects.get(username=username)
|
customer = CustomerProfile.objects.filter(user=user).first()
|
||||||
serial = LoginSerializer(user)
|
if customer:
|
||||||
passwordValid = check_password(password, serial.data['password'])
|
user_data["customer_profile"] = CustomerProfileSerializer(customer).data
|
||||||
if passwordValid and StaffProfile.objects.filter(user=user):
|
|
||||||
future = datetime.utcnow() + datetime2.timedelta(days=183)
|
# Check for staff profile
|
||||||
futuretime = calendar.timegm(future.timetuple())
|
staff = StaffProfile.objects.filter(user=user).first()
|
||||||
encoded_jwt = jwt.encode({"username": username, "exp": futuretime, "is_superuser": serial.data['is_superuser'], "userid":serial.instance.id}, "ibiye4700", algorithm="HS256")
|
if staff:
|
||||||
#encoded_jwt = encoded_jwt.decode('utf-8')
|
user_data["staff_profile"] = StaffProfileSerializer(staff).data
|
||||||
return successRes(msg={"token": encoded_jwt,"first_name":user.first_name,"last_name":user.last_name,"email":user.email, "id":user.id})
|
|
||||||
else:
|
data.append(user_data)
|
||||||
raise ValueError("Incorrect password!")
|
|
||||||
except User.DoesNotExist:
|
return Response(data, status=status.HTTP_200_OK)
|
||||||
return errorRes(msg="User doesn't exist!")
|
|
||||||
except ValueError as err:
|
|
||||||
return errorRes(msg=str(err))
|
def get_all_business_related_data():
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
business_types = BusinessType.objects.all()
|
||||||
return errorRes(msg=str(e))
|
departments = Department.objects.all()
|
||||||
|
job_positions = JobPosition.objects.all()
|
||||||
|
|
||||||
@api_view(['POST'])
|
business_types_data = BusinessTypeSerializer(business_types, many=True).data
|
||||||
def register_device(request):
|
departments_data = DepartmentSerializer(departments, many=True).data
|
||||||
try:
|
job_positions_data = JobPositionSerializer(job_positions, many=True).data
|
||||||
registration_token = request.data.get('registration_token')
|
|
||||||
device_type = request.data.get('device_type')
|
return {
|
||||||
# Check if the device already exists
|
'business_types': business_types_data,
|
||||||
existing_device = FCMDevice.objects.get(registration_id=registration_token)
|
'departments': departments_data,
|
||||||
return errorRes(msg='Device already registered')
|
'job_positions': job_positions_data,
|
||||||
except FCMDevice.DoesNotExist:
|
}
|
||||||
# Create a new device
|
|
||||||
device = FCMDevice(registration_id=registration_token, type=device_type)
|
|
||||||
device.save()
|
class BusinessDataAPIView(APIView):
|
||||||
return successRes(msg="Device registered successfully.")
|
|
||||||
except Exception as e:
|
def get(self, request):
|
||||||
return errorRes(str(e))
|
data = get_all_business_related_data()
|
||||||
|
return Response(data)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST'])
|
|
||||||
def update_device(request):
|
|
||||||
try:
|
|
||||||
registration_token = request.data.get('registration_token')
|
|
||||||
token_data = verify(request.headers.get("Authorization"))
|
|
||||||
user_id = token_data['userid']
|
|
||||||
user = User.objects.get(id=user_id)
|
|
||||||
existing_device = FCMDevice.objects.get(registration_id=registration_token)
|
|
||||||
existing_device.user = user
|
|
||||||
existing_device.save()
|
|
||||||
return successRes(msg='Device updated')
|
|
||||||
except FCMDevice.DoesNotExist:
|
|
||||||
return errorRes(msg='Device is not registered')
|
|
||||||
except TokenError as terr:
|
|
||||||
return errorRes(msg=str(terr), status=450)
|
|
||||||
except Exception as e:
|
|
||||||
return errorRes(str(e))
|
|
Loading…
Reference in New Issue