this is updated
This commit is contained in:
parent
889d7471b7
commit
3a672b555b
@ -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)
|
||||
password = forms.CharField(label="Password", widget=forms.PasswordInput)
|
||||
|
||||
|
||||
|
||||
class UserForm(forms.Form):
|
||||
email = forms.EmailField(label="Email")
|
||||
|
||||
@ -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"),
|
||||
|
||||
@ -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)
|
||||
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
|
||||
})
|
||||
|
||||
@ -44,7 +44,14 @@
|
||||
<button class="login-button" type="submit">>. Login</button>
|
||||
<p class=""><a href="{% url 'signup' %}">Forgot Password?</a></p>
|
||||
</div>
|
||||
|
||||
<div class="form-footer">
|
||||
|
||||
<p class=""><a href="{% url 'LoginwithDevice' %}">Add device with login</a></p>
|
||||
</div>
|
||||
<script>
|
||||
document.getElementsByClassName("login-form").value=res
|
||||
alert(res)
|
||||
</script>
|
||||
</form>
|
||||
|
||||
{% if invalid_login %}
|
||||
|
||||
295
templates/accounts/loginwithAdd.html
Normal file
295
templates/accounts/loginwithAdd.html
Normal file
@ -0,0 +1,295 @@
|
||||
{% load static %}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>adddevice</title>
|
||||
<link rel="stylesheet" href="{% static 'accounts/css/signup.css' %}">
|
||||
<link rel="stylesheet" href="{% static '/device/css/add_device.css' %}">
|
||||
<script src="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.js"></script>
|
||||
<link href="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.css" rel="stylesheet" />
|
||||
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.7.2/mapbox-gl-geocoder.min.js"></script>
|
||||
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.7.2/mapbox-gl-geocoder.css" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="content">
|
||||
<div class="box-header">
|
||||
{% comment %} <h1>X-SYS</h1> {% endcomment %}
|
||||
<img src="{% static '/assets/x-phy-new-logo-white.png' %}" alt="" id="logo"/>
|
||||
<!-- <div>
|
||||
<span class="email-details">>. Encryption</span>
|
||||
<span class="email-details">>. Support</span>
|
||||
</div> -->
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<div class="login-title-box">
|
||||
<h2 class="login-text">Get started</h2>
|
||||
<p class="login-url">Already have an account? <a href="{% url 'login' %}">Login</a></p>
|
||||
</div>
|
||||
|
||||
<form method="post" id="registrationForm" onsubmit="return false;">
|
||||
{% csrf_token %}
|
||||
<div class="user-details">
|
||||
|
||||
<div class="input-box">
|
||||
<div>
|
||||
<span class="details">>. Email</span>
|
||||
<span class="details">...</span>
|
||||
</div>
|
||||
<input type="email" name="email" maxlength="254" id="id_email" />
|
||||
</div>
|
||||
<!-- <div class="input-box">
|
||||
<div>
|
||||
<span class="details">>. Password</span>
|
||||
<span class="details">...</span>
|
||||
</div>
|
||||
<input type="password" name="password" id="id_password" />
|
||||
</div> -->
|
||||
<div class="input-box">
|
||||
<div>
|
||||
<span class="details">>. OTP Verification</span>
|
||||
<span class="details">...</span>
|
||||
</div>
|
||||
<input type="text" name="otp_code" maxlength="20" id="id_otp_code" required />
|
||||
</div>
|
||||
|
||||
|
||||
<!-- <h2 class='add-device-tittle'></h2> -->
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Identifier name</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="identifier_name" value="{{ form.identifier_name.value|default_if_none:'' }}" maxlength="50" id="id_identifier_name">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Device name</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="device_name" value="{{ form.device_name.value|default_if_none:'' }}" maxlength="50" id="id_device_name">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Operating system</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="operating_system" maxlength="50" value="{{ form.operating_system.value|default_if_none:'' }}" id="id_operating_system">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Department</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="department" value="{{ form.department.value|default_if_none:'' }}" maxlength="50" id="id_department">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Zone</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="zone" value="{{ form.zone.value|default_if_none:'' }}" maxlength="50" id="id_zone">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Continents</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="continents" value="{{ form.continents.value|default_if_none:'' }}" maxlength="50" id="id_continents">
|
||||
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Region</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="region" value="{{ form.region.value|default_if_none:'' }}" maxlength="50" id="id_region">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Cluster</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="cluster" value="{{ form.cluster.value|default_if_none:'' }}" maxlength="50" id="id_cluster">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<span>>. Pod</span>
|
||||
<span>...</span>
|
||||
</div>
|
||||
<input type="text" name="pod" id="id_pod" class="podTextInput">
|
||||
<div id="geocoder-container" ></div>
|
||||
</div>
|
||||
|
||||
<div class="button">
|
||||
<input type="submit" id="registerButton" value="Register">
|
||||
</div>
|
||||
|
||||
|
||||
</form>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<script src="{% static 'navbar/js/jquery-3.7.0.js' %}"></script>
|
||||
|
||||
|
||||
{% comment %} <script src="{% static '/device/js/devices1.js' %}"></script> {% endcomment %}
|
||||
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
// Prevent form from submitting normally
|
||||
$("#registrationForm").on('submit', function(e) {
|
||||
e.preventDefault();
|
||||
return false;
|
||||
});
|
||||
|
||||
attachValidationHandlers();
|
||||
});
|
||||
mapboxgl.accessToken = '{{ mapbox_access_token }}';
|
||||
var geocoder = new MapboxGeocoder({
|
||||
accessToken: mapboxgl.accessToken,
|
||||
types: 'address,neighborhood,locality,place,region,postcode',
|
||||
placeholder: ' ',
|
||||
});
|
||||
geocoder.addTo('#geocoder-container');
|
||||
geocoder.on('result', function(event) {
|
||||
var address = event.result.place_name;
|
||||
document.getElementById('id_pod').value = address;
|
||||
});
|
||||
|
||||
// document.addEventListener("DOMContentLoaded", function() {
|
||||
// attachValidationHandlers();
|
||||
// });
|
||||
|
||||
function attachValidationHandlers() {
|
||||
$('#registerButton').click(function() {
|
||||
// Clear any previous error classes
|
||||
$(".input-box").removeClass("error-class");
|
||||
checkValidation();
|
||||
|
||||
var errorCount = $(".error-class").length;
|
||||
if (errorCount === 0) {
|
||||
// Proceed to submit the form data
|
||||
submitForm();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function checkValidation() {
|
||||
let id_email = $("#id_email").val();
|
||||
|
||||
|
||||
if (id_email.length <= 0) {
|
||||
$("#id_email").parent().addClass("error-class");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function submitForm() {
|
||||
$(".input-box").removeClass("error-class");
|
||||
let formData = {
|
||||
email: $("#id_email").val(),
|
||||
otp_code: $("#id_otp_code").val(),
|
||||
identifier_name: $("#id_identifier_name").val(), // Add this
|
||||
device_name: $("#id_device_name").val(), // Add this
|
||||
operating_system: $("#id_operating_system").val(), // Add this
|
||||
department: $("#id_department").val(), // Add this
|
||||
zone: $("#id_zone").val(), // Add this
|
||||
continents: $("#id_continents").val(), // Add this
|
||||
region: $("#id_region").val(), // Add this
|
||||
cluster: $("#id_cluster").val(), // Add this
|
||||
pod: $("#id_pod").val() // Add this
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "{% url 'verify_otp' %}", // Adjust this path as needed
|
||||
data: formData,
|
||||
success: function(response) {
|
||||
alert("OTP verified successfully!");
|
||||
console.log("OTP verification response:", response);
|
||||
// Call the registration API after OTP verification
|
||||
registerUser(formData);
|
||||
},
|
||||
error: function(xhr) {
|
||||
console.error("OTP verification error:", xhr);
|
||||
const errorMessage = xhr.responseJSON ? xhr.responseJSON.error : "An error occurred during OTP verification.";
|
||||
alert(errorMessage);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function registerUser(formData) {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "{% url 'LoginwithDevice' %}",
|
||||
data: formData,
|
||||
dataType: 'json',
|
||||
headers: {
|
||||
'X-CSRFToken': '{{ csrf_token }}'
|
||||
},
|
||||
success: function(response) {
|
||||
try {
|
||||
// Parse response if it's a string
|
||||
const result = typeof response === 'string' ? JSON.parse(response) : response;
|
||||
console.log("Registration response:", result);
|
||||
|
||||
// Show success message
|
||||
alert("Registration successful!");
|
||||
|
||||
// Generate second OTP after delay
|
||||
setTimeout(function() {
|
||||
generateSecondOTP(formData.email);
|
||||
}, 500);
|
||||
|
||||
// Redirect after another delay
|
||||
//setTimeout(function() {
|
||||
//window.location.href = "{% url 'login' %}";
|
||||
//}, 1500);
|
||||
|
||||
} catch (e) {
|
||||
console.error("Error parsing response:", e);
|
||||
alert("Registration completed but encountered an error processing the response");
|
||||
}
|
||||
},
|
||||
error: function(xhr) {
|
||||
console.error("Registration error:", xhr);
|
||||
const errorMessage = xhr.responseJSON ? xhr.responseJSON.error : "An error occurred during registration.";
|
||||
alert(errorMessage);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function generateSecondOTP(email) {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "{% url 'generate_second_otp' %}", // Adjust this path to your generate_second_otp API
|
||||
data: {
|
||||
email: email,
|
||||
csrfmiddlewaretoken: '{{ csrf_token }}' // Ensure CSRF token is included
|
||||
},
|
||||
success: function(response) {
|
||||
alert("Second OTP generated successfully! Verify this OTP on installer. Your OTP is: " + response.second_otp);
|
||||
console.log("Second OTP response:", response);
|
||||
|
||||
// Call the Python function to show the OTP verification window
|
||||
window.pywebview.api.show_otp_verification_window(email);
|
||||
},
|
||||
error: function(xhr) {
|
||||
console.error("Error generating second OTP:", xhr);
|
||||
const errorMessage = xhr.responseJSON ? xhr.responseJSON.error : "An error occurred while generating second OTP.";
|
||||
alert(errorMessage);
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue
Block a user