From 3a672b555b0163d67d493a0413d3284278f200a7 Mon Sep 17 00:00:00 2001 From: Vratika Date: Tue, 10 Dec 2024 15:46:09 +0530 Subject: [PATCH] this is updated --- Accounts/forms.py | 8 +- Accounts/urls.py | 1 + Accounts/views.py | 97 ++++++++- templates/accounts/login.html | 9 +- templates/accounts/loginwithAdd.html | 295 +++++++++++++++++++++++++++ 5 files changed, 406 insertions(+), 4 deletions(-) create mode 100644 templates/accounts/loginwithAdd.html diff --git a/Accounts/forms.py b/Accounts/forms.py index a8bd238..9ed42be 100644 --- a/Accounts/forms.py +++ b/Accounts/forms.py @@ -28,4 +28,10 @@ class SignUpForm(forms.ModelForm): class UserLoginForm(forms.Form): email = forms.EmailField(label="Email") - password = forms.CharField(label="Password", widget=forms.PasswordInput) \ No newline at end of file + password = forms.CharField(label="Password", widget=forms.PasswordInput) + + + +class UserForm(forms.Form): + email = forms.EmailField(label="Email") + \ No newline at end of file diff --git a/Accounts/urls.py b/Accounts/urls.py index a1cf394..1447dcd 100644 --- a/Accounts/urls.py +++ b/Accounts/urls.py @@ -4,6 +4,7 @@ from .views import* urlpatterns = [ path('signup',views.tech4biz_Signup,name="signup"), + path('Login_with_Device',views.tech4biz_loginwithAdd,name="Login_with_Device"), path('',views.tech4biz_Login,name="login"), path('logout',views.user_logout,name="logout"), path('otp_verification',views.otp_verification,name="otp_verification"), diff --git a/Accounts/views.py b/Accounts/views.py index b83c498..72c2be7 100644 --- a/Accounts/views.py +++ b/Accounts/views.py @@ -22,8 +22,8 @@ from rest_framework import status from .models import UserProfile from Device .models import Devices from .serializers import UserProfileSerializer, DeviceSerializer +from rest_framework.authtoken.models import Token import os - #________________________________________________________Mail Functions____________________________________________ def tech4biz_generate_otp(length=6): @@ -251,4 +251,97 @@ def get_user_details_and_device_pods(request): except UserProfile.DoesNotExist: return JsonResponse({"error": "User profile not found."}, status=404) except Exception as e: - return JsonResponse({"error": str(e)}, status=500) \ No newline at end of file + return JsonResponse({"error": str(e)}, status=500) + + + + + +def tech4biz_loginwithAdd(request): + mapbox_access_token = 'pk.eyJ1IjoiZmxleHhvbiIsImEiOiJjbHVtYzNoM2cwNXI2MnFveW51c2tyejVwIn0.ceqt6Ot6nU67CUmxVAWPEQ' + print("Received a request inside") + + if request.method == 'POST': + print("Processing POST request") + + form = UserForm(request.POST) + device_form = DevicesForm(request.POST) + + print("UserForm and DevicesForm created") + + if form.is_valid(): + # Check if the user exists or create them if not + email = form.cleaned_data['email'] + + try: + # Try to get the user based on the email + user_profile = UserProfile.objects.get(user__email=email) + print("User exists:", user_profile) + except UserProfile.DoesNotExist: + # If the user does not exist, create a new user + print("Creating new user profile") + user = User.objects.create_user(username=email, email=email) + user_profile = UserProfile.objects.create(user=user) + print("New user profile created:", user_profile) + + user_id = user_profile.user.id # Access the ID of the user + print("Extracted user ID:", user_id) + + # Set the user_id in the session + request.session['user_id'] = user_id + + # Set 'used_by' field directly in device_form's data before validation + device_form.data = device_form.data.copy() + device_form.data['used_by'] = user_profile + if device_form.is_valid(): + # Create device instance from device_form without saving it yet + device_instance = device_form.save(commit=False) + device_instance.used_by = user_profile # Set the user as 'used_by' + + print('Device instance prepared for saving:', device_instance) + try: + # Save the device instance + device_instance.save() + print("Device instance saved successfully") + # Set the device ID in the environment variable + os.environ['DEVICE_ID'] = str(device_instance.id) + print("Device ID set in OS environment:", os.environ['DEVICE_ID']) + + device_id = device_instance.id + + return JsonResponse({ + "message": "User and device registered successfully", + "user_id": user_profile.user.id, + "device_id": device_id + }, status=200) + except IntegrityError as e: + error_message = str(e) + print("IntegrityError occurred:", error_message) + form.add_error(None, error_message) + device_form.add_error(None, error_message) + else: + # Print device form errors if it is not valid + print("Device form errors:", device_form.errors) + return JsonResponse({ + "device_errors": device_form.errors.as_json() + }, status=400) + else: + # Return form errors if any exist + error_messages = form.errors.as_json() + device_error_messages = device_form.errors.as_json() + print("Form errors:", error_messages) + print("Device form errors:", device_error_messages) + return JsonResponse({ + "error": error_messages, + "device_errors": device_error_messages + }, status=400) + else: + print("Request method is not POST") + form = UserForm() + device_form = DevicesForm() + + return render(request, 'accounts/loginwithAdd.html', { + 'form': form, + 'device_form': device_form, + "mapbox_access_token": mapbox_access_token + }) diff --git a/templates/accounts/login.html b/templates/accounts/login.html index 4114154..8433c6b 100644 --- a/templates/accounts/login.html +++ b/templates/accounts/login.html @@ -44,7 +44,14 @@

Forgot Password?

- + + {% if invalid_login %} diff --git a/templates/accounts/loginwithAdd.html b/templates/accounts/loginwithAdd.html new file mode 100644 index 0000000..8c36f8b --- /dev/null +++ b/templates/accounts/loginwithAdd.html @@ -0,0 +1,295 @@ +{% load static %} + + + + + adddevice + + + + + + + + + +
+
+
+ {% comment %}

X-SYS

{% endcomment %} + + +
+ + + + + +
+ {% csrf_token %} +
+ +
+
+ >. Email + ... +
+ +
+ +
+
+ >. OTP Verification + ... +
+ +
+ + + +
+
+ >. Identifier name + ... +
+ +
+
+
+ >. Device name + ... +
+ +
+
+
+ >. Operating system + ... +
+ +
+
+
+ >. Department + ... +
+ +
+
+
+
+ >. Zone + ... +
+ +
+
+
+ >. Continents + ... +
+ + +
+
+
+ >. Region + ... +
+ +
+
+
+ >. Cluster + ... +
+ +
+
+
+ >. Pod + ... +
+ +
+
+ +
+ +
+ + +
+ + + +
+
+ + + + {% comment %} {% endcomment %} + + + + +