backup webcode

This commit is contained in:
Vratika 2024-12-09 13:41:26 +05:30
parent 9102e54f73
commit ca3ed5d1cd
3051 changed files with 2251849 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
env

0
Accounts/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

5
Accounts/admin.py Normal file
View File

@ -0,0 +1,5 @@
from django.contrib import admin
from .models import *
admin.site.register(UserProfile)

6
Accounts/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'Accounts'

31
Accounts/forms.py Normal file
View File

@ -0,0 +1,31 @@
from django import forms
from django.contrib.auth.models import User
from .models import UserProfile
class SignUpForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = UserProfile
fields = ['email', 'password', 'phone_number', 'country', 'state', 'postal_code', 'full_address','company_name']
def clean_email(self):
email = self.cleaned_data['email']
if User.objects.filter(email=email).exists():
raise forms.ValidationError("This email is already registered.")
return email
def save(self, commit=True):
user_profile = super(SignUpForm, self).save(commit=False)
user = User.objects.create_user(username=self.cleaned_data['email'],
email=self.cleaned_data['email'],
password=self.cleaned_data['password'])
user_profile.user = user
if commit:
user.save()
user_profile.save()
return user_profile
class UserLoginForm(forms.Form):
email = forms.EmailField(label="Email")
password = forms.CharField(label="Password", widget=forms.PasswordInput)

View File

@ -0,0 +1,36 @@
# Generated by Django 5.0.3 on 2024-03-06 10:06
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='UserProfile',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254, unique=True)),
('password', models.CharField(max_length=255)),
('shipping_address', models.CharField(max_length=100)),
('billing_address', models.CharField(max_length=100)),
('phone_number', models.CharField(max_length=15)),
('li_phy_card_identifier', models.CharField(max_length=20)),
('email_otp', models.CharField(blank=True, max_length=6, null=True)),
('mobile_code', models.CharField(max_length=50, null=True)),
('country', models.CharField(max_length=50, null=True)),
('state', models.CharField(max_length=50, null=True)),
('postal_code', models.CharField(max_length=10, null=True)),
('full_address', models.TextField(null=True)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -0,0 +1,21 @@
# Generated by Django 5.0.3 on 2024-03-08 06:15
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('Accounts', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='userprofile',
name='billing_address',
),
migrations.RemoveField(
model_name='userprofile',
name='shipping_address',
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-04-23 04:19
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('Accounts', '0002_remove_userprofile_billing_address_and_more'),
]
operations = [
migrations.AddField(
model_name='userprofile',
name='company_name',
field=models.CharField(blank=True, max_length=100, null=True),
),
]

View File

22
Accounts/models.py Normal file
View File

@ -0,0 +1,22 @@
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
email = models.EmailField(unique=True)
password = models.CharField(max_length=255)
phone_number = models.CharField(max_length=15)
li_phy_card_identifier = models.CharField(max_length=20)
email_otp = models.CharField(max_length=6, blank=True, null=True)
mobile_code = models.CharField(max_length=50, null=True)
country = models.CharField(max_length=50,null=True)
state = models.CharField(max_length=50,null=True)
postal_code = models.CharField(max_length=10,null=True)
full_address = models.TextField(null=True)
company_name = models.CharField(max_length=100,blank=True, null=True)
def get_user_id(self):
return self.user.id # Access the user ID
def __str__(self):
return f"{self.email}-{self.user.id}"

14
Accounts/serializers.py Normal file
View File

@ -0,0 +1,14 @@
# serializers.py
from rest_framework import serializers
from .models import UserProfile
from Device .models import Devices
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ['user', 'email', 'phone_number', 'company_name']
class DeviceSerializer(serializers.ModelSerializer):
class Meta:
model = Devices
fields = ['pod']

3
Accounts/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

13
Accounts/urls.py Normal file
View File

@ -0,0 +1,13 @@
from django.urls import path
from .import views
from .views import*
urlpatterns = [
path('signup',views.tech4biz_Signup,name="signup"),
path('',views.tech4biz_Login,name="login"),
path('logout',views.user_logout,name="logout"),
path('otp_verification',views.otp_verification,name="otp_verification"),
path('profile',views.profile,name="profile"),
path('user-details-and-device-pods/', get_user_details_and_device_pods, name='user_details_and_device_pods'),
]

254
Accounts/views.py Normal file
View File

@ -0,0 +1,254 @@
from django.shortcuts import render, redirect
import random
from django.contrib.auth import authenticate, login
from .forms import *
from Device .forms import DevicesForm
from django.db import IntegrityError
from django.conf import settings
from .models import*
from django.http import HttpResponse
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.contrib import messages
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.contrib.auth import logout
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from .models import UserProfile
from Device .models import Devices
from .serializers import UserProfileSerializer, DeviceSerializer
import os
#________________________________________________________Mail Functions____________________________________________
def tech4biz_generate_otp(length=6):
otp = ""
for _ in range(length):
otp += str(random.randint(0, 9))
return otp
def tech4biz_send_otp_email(email):
otp = tech4biz_generate_otp()
subject = 'Your OTP for Signup'
html_content = render_to_string('accounts/otp_template.html', {'otp': otp})
text_content = strip_tags(html_content)
email_message = EmailMultiAlternatives(subject, text_content, settings.EMAIL_HOST_USER, [email])
email_message.attach_alternative(html_content, "text/html")
try:
email_message.send()
return otp
except Exception as e:
return None
#________________________________________________________Signup Login___________________________________________________
# def tech4biz_Signup(request):
# if request.method == 'POST':
# form = SignUpForm(request.POST)
# if form.is_valid():
# request.session['form_data'] = form.cleaned_data
# email = form.cleaned_data['email']
# try:
# otp = tech4biz_send_otp_email(email)
# print(otp)
# request.session['sent_otp'] = otp
# return redirect('otp_verification')
# except Exception as e:
# return HttpResponse(f'Failed to send OTP email: {e}')
# else:
# form = SignUpForm()
# return render(request, 'accounts/signup.html', {'form': form})
# def tech4biz_Signup(request):
# if request.method == 'POST':
# form = SignUpForm(request.POST)
# if form.is_valid():
# user_profile = form.save() # Saves both the user and user profile
# request.session['user_id'] = user_profile.user.id # Assuming user_profile has a related User object
# return JsonResponse({
# "message": "User registered successfully",
# "user_id": user_profile.user.id # Include user.id in the response
# }, status=200)
# else:
# # Return a more user-friendly error message
# error_messages = form.errors.as_json() # Get errors in JSON format
# return JsonResponse({"error": error_messages}, status=400) # Return form errors as JSON
# else:
# form = SignUpForm() # Instantiate an empty form
# return render(request, 'accounts/signup.html', {'form': form})
def tech4biz_Signup(request):
mapbox_access_token = 'pk.eyJ1IjoiZmxleHhvbiIsImEiOiJjbHVtYzNoM2cwNXI2MnFveW51c2tyejVwIn0.ceqt6Ot6nU67CUmxVAWPEQ'
print("Received a request to tech4biz_Signup")
if request.method == 'POST':
print("Processing POST request")
form = SignUpForm(request.POST)
device_form = DevicesForm(request.POST)
print("SignUpForm and DevicesForm created")
if form.is_valid():
# Save the user profile from SignUpForm
user_profile = form.save()
print("User profile created:", user_profile)
user_id = user_profile.user.id # Access the ID of the newly created user from user_profile
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 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 = SignUpForm()
device_form = DevicesForm()
return render(request, 'accounts/signup.html', {
'form': form,
'device_form': device_form,
"mapbox_access_token": mapbox_access_token
})
def otp_verification(request):
if request.method == 'POST':
# Get individual OTP digits
entered_otp1 = request.POST.get('email_otp1')
entered_otp2 = request.POST.get('email_otp2')
entered_otp3 = request.POST.get('email_otp3')
entered_otp4 = request.POST.get('email_otp4')
entered_otp5 = request.POST.get('email_otp5')
entered_otp6 = request.POST.get('email_otp6')
# Combine OTP digits into a single string
entered_otp = entered_otp1 + entered_otp2 + entered_otp3 + entered_otp4 + entered_otp5 + entered_otp6
print(entered_otp, "otp") # Just for debugging
stored_otp = request.session.get('sent_otp')
if entered_otp == stored_otp:
form_data = request.session.get('form_data')
form = SignUpForm(form_data)
if form.is_valid():
form.save()
del request.session['form_data']
del request.session['sent_otp']
return redirect('login')
else:
messages.error(request, 'Form data is not valid.')
return redirect('signup')
else:
messages.error(request, 'Invalid OTP. Please try again.')
return redirect('otp_verification')
return render(request, 'accounts/otp_verification.html')
def tech4biz_Login(request):
if request.user.is_authenticated: # Check if the user is already logged in
return redirect('home')
if request.method == 'POST':
form = UserLoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = authenticate(request, username=email, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
return render(request, 'accounts/login.html', {'form': form, 'invalid_login': True})
else:
form = UserLoginForm()
return render(request, 'accounts/login.html', {'form': form})
def user_logout(request):
logout(request)
return redirect('login')
def profile(request):
return render(request, 'profile/profile.html')
#____________________________________________________________________________________________________________________
@api_view(['GET'])
@login_required # Ensures the user is logged in
def get_user_details_and_device_pods(request):
try:
# Get the logged-in user's UserProfile
user_profile = UserProfile.objects.get(user=request.user)
# Get the devices associated with the logged-in user
devices = Devices.objects.filter(used_by=user_profile)
# Construct the response data
response_data = {
"user_details": {
"user": user_profile.user.id,
"email": user_profile.email,
"phone_number": user_profile.phone_number,
"company_name": user_profile.company_name,
},
"device_pods": [{"pod": device.pod} for device in devices]
}
print('response data',response_data)
# Return data as JSON response
return JsonResponse(response_data, status=200)
except UserProfile.DoesNotExist:
return JsonResponse({"error": "User profile not found."}, status=404)
except Exception as e:
return JsonResponse({"error": str(e)}, status=500)

0
Dashboard/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

7
Dashboard/admin.py Normal file
View File

@ -0,0 +1,7 @@
from django.contrib import admin
from .models import*
# Register your models here.
# admin.site.register(Status)
admin.site.register(Number)
admin.site.register(SqlStatus)
admin.site.register(RestoreDatabase)

6
Dashboard/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class DashboardConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'Dashboard'

View File

@ -0,0 +1,21 @@
# Generated by Django 5.0.4 on 2024-04-24 08:08
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Status',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('number', models.CharField(max_length=15)),
],
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 5.0.4 on 2024-04-24 08:10
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('Dashboard', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Number',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('status', models.BooleanField(default=False)),
],
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 5.0.4 on 2024-07-06 08:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('Dashboard', '0002_number'),
]
operations = [
migrations.CreateModel(
name='SqlStatus',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', models.BooleanField(default=False)),
],
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-07-06 08:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('Dashboard', '0003_sqlstatus'),
]
operations = [
migrations.AlterField(
model_name='sqlstatus',
name='value',
field=models.CharField(default='0', max_length=15),
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 5.0.4 on 2024-07-08 10:54
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('Dashboard', '0004_alter_sqlstatus_value'),
]
operations = [
migrations.CreateModel(
name='RestoreDatabase',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', models.CharField(default='0', max_length=15)),
],
),
]

View File

View File

@ -0,0 +1,54 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="{% static '/accounts/css/login.css' %}">
<title>Login</title>
</head>
<body>
<div class="container background-login">
<div class="right-part">
{% comment %} <img src="{% static 'accounts/media/loginlocker.png' %}" /> {% endcomment %}
</div>
<div class="left-part">
<div class="box-header">
<h3>X-SYS</h3>
<img src="{% static '/assets/x-phy-new-logo-white.png' %}" alt="" id="logo"/>
<div>
<span class="Encryption-title">>. Encryption</span>
<span class="email-details">>. Support</span>
</div>
</div>
<div class="login-title-box">
<h2 class="login-text">Login</h2>
<p class="login-desc">Don't have an account yet? <a href="{% url 'signup' %}">Sign up</a></p>
</div>
<form method="post" class="login-form">
{% csrf_token %}
<div class="email-block">
<div>
<span class="email-details">>. Email</span>
<span class="email-details">...</span>
</div>
<input id="id_email" name="email" type="text" required>
</div>
<div class="password-block">
<div>
<span class="email-details">>. Password</span>
<span class="email-details">...</span>
</div>
<input type="password" name='password' id="id_password" required>
</div>
<div class="form-footer">
<button class="login-button" type="submit">>. Login</button>
<p class=""><a href="{% url 'signup' %}">Forgot Password?</a></p>
</div>
</form>
{% if invalid_login %}
<p class="error-message">Invalid email or password. Please try again.</p>
{% endif %}
</div>
</div>
</body>
</html>

Some files were not shown because too many files have changed in this diff Show More