from django.shortcuts import render
from django.shortcuts import redirect
from django.urls import reverse
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django import forms
from django.contrib.auth.forms import UserCreationForm
# Create your views here.
# from .forms import Profile
from userapp.models import BabyInfo,Profile,UserOTP
from userapp.forms import BabyForm1,BabyForm2,BabyupdateForm1,BabyupdateForm2
import string
from django.core.mail import send_mail
from django.contrib import messages
from django.contrib.messages import constants
import random
from django.template.loader import render_to_string
from .forms import SignUpForm
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.conf import settings
from django.http import HttpResponse, Http404, JsonResponse
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.core import serializers
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from package import views
import itertools
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(8))
def page(request):
print(User.objects.all().values())
return render(request,'userapp_temp/landing_page.html')
def home(request):
return render(request,'userapp_temp/home.html',)
# def index(request):
# return render(request, 'userapp_temp/index.html', {'title':'index'})
# def login(request):
# return render(request,'userapp_temp/login.html')
#
# def register(request):
# return render(request,'userapp_temp/signup.html')
# def profile(request):
# return render(request,'userapp_temp/profile.html')
#########################(BABY_PROFILE_DETAIL(detail))################################################################################
@login_required()
def detail(request):
# print(request.POST)
if request.method == 'POST':
# print(request.POST.getlist('name'))
baby_form1 = BabyForm1(data=request.POST)
baby_form2 = BabyForm2(data=request.POST)
story_name= request.POST['story_name']
born_status= request.POST['born_status']
baby_date= request.POST['baby_date']
profile_pic= request.POST['profile_pic']
gender= request.POST['gender']
name= request.POST.getlist('name')
mail_id= request.POST.getlist('mail_id')
relationship= request.POST.getlist('relationship')
# if request.method == 'POST' and 'profile_pic' in request.FILES:
# pic = request.FILES['profile_pic']
# pic_name = pic['profile_pic']
# if 'profile_pic' in request.POST:
# profile_pic= request.POST['profile_pic']
#
# else:
# profile_pic = False
# print(story_name,baby_date,profile_pic,name,mail_id,relationship)
for (name,mail_id,relationship) in zip(name,mail_id,relationship):
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(8))
# print(request.user.username)
if User.objects.filter(email=mail_id).exists():
messages.add_message(request, constants.SUCCESS, 'user name already taken')
print('already taken')
else:
newuser = User.objects.create_user(username=mail_id,email=mail_id,password=result_str)
newuser.save()
template = render_to_string("userapp_temp/newusermail.html",{'email':mail_id,'password':result_str})
send_mail(
'New user',
template,
'mohansaravanan111@gmail.com',
[mail_id],
fail_silently=False,
)
babyinfo= BabyInfo.objects.create(user=request.user,story_name=story_name,born_status=born_status,baby_date=baby_date,profile_pic=profile_pic,gender=gender)
if 'profile_pic' in request.FILES:
babyinfo.profile_pic = request.FILES['profile_pic']
babyinfo.save()
profile= Profile.objects.create(user=request.user,name=name,mail_id=mail_id,relationship=relationship,babyinfo=babyinfo)
profile.save()
# b = profile(babyinfo=babyinfo.id)
# b.save()
return redirect('babyinfo')
else:
baby_form1 = BabyForm1()
baby_form2 = BabyForm2()
return render(request,'userapp_temp/profile.html', {'baby_form1':baby_form1, 'baby_form2':baby_form2,})
##########################(BABY_PROFILE_DETAIL(babyinfo))########################################################################################
def babydetail(request):
# print(Profile.objects.all())
profileinfo = Profile.objects.filter(user=request.user.id).values()
for babyinfo_id in profileinfo:
# print(babyinfo_id)
b_id = babyinfo_id['babyinfo_id']
# print(b_id)
babyinfo = BabyInfo.objects.filter(id=b_id).values()
# print(babyinfo)
context = {
'babyinfo':babyinfo,
'profileinfo':profileinfo,
}
return render(request,'userapp_temp/profile_detail.html',context)
##########################(BABY_PROFILE_DETAIL(babyinfoupdate))########################################################################################
def babyupdate(request):
if request.method == 'POST':
baby_form1 = BabyupdateForm1(data=request.POST)
baby_form2 = BabyupdateForm2(data=request.POST)
story_name= request.POST['story_name']
born_status= request.POST['born_status']
baby_date= request.POST['baby_date']
profile_pic= request.POST['profile_pic']
gender= request.POST['gender']
# name= request.POST['name']
# mail_id= request.POST['mail_id']
# relationship= request.POST['relationship']
profileinfo = Profile.objects.filter(user=request.user.id).values()
for babyinfo_id in profileinfo:
b_id = babyinfo_id['babyinfo_id']
babyinfo= BabyInfo.objects.filter(id=b_id).update(story_name=story_name,born_status=born_status,baby_date=baby_date,profile_pic=profile_pic,gender=gender)
# profile= Profile.objects.filter(user=request.user.id).update(name=name,mail_id=mail_id,relationship=relationship)
return redirect('babyinfo')
else:
baby_form1 = BabyupdateForm1()
baby_form2 = BabyupdateForm2()
return render(request,"userapp_temp/profile_update.html", {'baby_form1':baby_form1, 'baby_form2':baby_form2})
##########################(BABY_PROFILE_END)########################################################################################
##########################(PACKAGE_BGN)#############################################################################################
# def package(request):
#
# babyinfo = BabyInfo.objects.filter(user=request.user.id)
# print(babyinfo )
# for package_id in babyinfo:
# print(package_id)
#
# pck_id = package_id['package_id']
# # print(b_id)
# pack = Package.objects.filter(id=pck_id)
# print(pack)
# return redirect('page')
# # print(babyinfo)
##########################(PACKAGE_END)#############################################################################################
##########################(REGISTER & LOG)##########################################################################################
def signup(request):
if request.method == 'POST':
get_otp = request.POST.get('otp') #213243 #None
if get_otp:
get_usr = request.POST.get('usr')
usr = User.objects.get(username=get_usr)
if int(get_otp) == UserOTP.objects.filter(user = usr).last().otp:
usr.is_staff = True
usr.save()
messages.success(request, f'Account is Created For {usr.username}')
return redirect('login')
# return redirect('detail')
else:
messages.warning(request, f'You Entered a Wrong OTP')
return render(request, 'userapp_temp/signup.html', {'otp': True, 'usr': usr})
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
name = form.cleaned_data.get('name').split(' ')
usr = User.objects.get(username=username)
usr.email = username
usr.first_name = name[0]
if len(name) > 1:
usr.last_name = name[1]
usr.is_staff = False
usr.save()
usr_otp = random.randint(100000, 999999)
UserOTP.objects.create(user = usr, otp = usr_otp)
mess = f"Hello {usr.first_name},\nYour OTP is {usr_otp}\nThanks!"
send_mail(
"Welcome to KuttyStory - Verify Your Email",
mess,
settings.EMAIL_HOST_USER,
[usr.email],
fail_silently = False
)
return render(request, 'userapp_temp/signup.html', {'otp': True, 'usr': usr})
# if user.is_authenticated:
# redirectURL ="https://www.instagram.com/"
# print(redirectURL)
# return('babyinfo')
# login(request, user)
# if user. == 'username':
# return redirect('babyinfo')
# return render(request, 'userapp_temp/signup.html', {'otp': True, 'usr': usr})
else:
form = SignUpForm()
return render(request, 'userapp_temp/signup.html', {'form':form})
def resend_otp(request):
if request.method == "GET":
get_usr = request.GET['usr']
if User.objects.filter(username = get_usr).exists() and not User.objects.get(username = get_usr).is_active:
usr = User.objects.get(username=get_usr)
usr_otp = random.randint(100000, 999999)
UserOTP.objects.create(user = usr, otp = usr_otp)
mess = f"Hello {usr.first_name},\nYour OTP is {usr_otp}\nThanks!"
send_mail(
"Welcome to KuttyStory - Verify Your Email",
mess,
settings.EMAIL_HOST_USER,
[usr.email],
fail_silently = False
)
return HttpResponse("Resend")
return HttpResponse("Can't Send ")
##########################(REGISTER(END))###########################################################################################
##########################(LOGIN)###################################################################################################
def login_view(request):
if request.user.is_authenticated:
# if last_login == 20210621
return redirect('babyinfo')
# if last_login is not None:
# return redirect('detail')
# else:
# return redirect('babyinfo')
if request.method == 'POST':
get_otp = request.POST.get('otp') #213243 #None
if get_otp:
get_usr = request.POST.get('usr')
usr = User.objects.get(username=get_usr)
if int(get_otp) == UserOTP.objects.filter(user = usr).last().otp:
usr.is_staff = True
usr.save()
login(request, usr)
return redirect('detail')
# if last_login is not None:
# return redirect('detail')
# else:
# return redirect('babyinfo')
else:
messages.warning(request, f'You Entered a Wrong OTP')
return render(request, 'userapp_temp/login.html', {'otp': True, 'usr': usr})
usrname = request.POST['username']
passwd = request.POST['password']
# lst = request.POST['last_login']
user = authenticate(request, username = usrname, password = passwd) #None
if user is not None:
# print(request.user)
# var = BabyInfo.objects.filter(user_id=request.user.id).values()
# print(var)
login(request, user)
# print(request.user.id)
var = BabyInfo.objects.filter(user_id=request.user.id).values()
# print(var)
try:
var1 = Profile.objects.filter(mail_id=usrname).values('user_id')
profileinfo = Profile.objects.filter(user=var1[0]['user_id']).values()
for babyinfo_id in profileinfo:
# print(babyinfo_id)
b_id = babyinfo_id['babyinfo_id']
# print(b_id)
babyinfo = BabyInfo.objects.filter(id=b_id).values()
# print(babyinfo)
context = {
'babyinfo':babyinfo,
'profileinfo':profileinfo,
}
return render(request,'userapp_temp/profile_detail.html',context)
except:
pass
try:
if var[0]['last_login'] is not None:
return redirect('page')
else:
pass
# return redirect('detail')
except:
return redirect('detail')
# return redirect('detail')
elif not User.objects.filter(username = usrname).exists():
messages.warning(request, f'Please enter a correct username and password. Note that both fields may be case-sensitive.')
return redirect('login')
elif not User.objects.get(username=usrname).is_active:
usr = User.objects.get(username=usrname)
usr_otp = random.randint(100000, 999999)
UserOTP.objects.create(user = usr, otp = usr_otp)
mess = f"Hello {usr.first_name},\nYour OTP is {usr_otp}\nThanks!"
send_mail(
"Welcome to KuttyStory - Verify Your Email",
mess,
settings.EMAIL_HOST_USER,
[usr.email],
fail_silently = False
)
return render(request, 'userapp_temp/login.html', {'otp': True, 'usr': usr})
else:
messages.warning(request, f'Please enter a correct username and password. Note that both fields may be case-sensitive.')
return redirect('login')
form = AuthenticationForm()
return render(request, 'userapp_temp/login.html', {'form': form})
##########################(REGISTER_&_LOGIN(END))#################################################################################################
please check below view Function --
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8d5c1a31-8781-4eec-8840-bc6b58969c97n%40googlegroups.com.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment