from django.urls import reverse
from django.contrib.auth.models import User
from PIL import Image
# from package.models import Package
# from .models import BabyInfo,Profile
# from user_profile.models import BabyInfo
# Create your models here.
class UserOTP(models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE)
time_st = models.DateTimeField(auto_now = True)
otp = models.SmallIntegerField()
def upload_profile_to(instance,filename):
return f'profile_picture/{instance}/{filename}'
class BabyInfo(models.Model):
MALE = 'male'
FEMALE = 'female'
BOTH = 'both'
GENDER = (
('MALE','Male'),
('FEMALE','Female'),
('BOTH','both'),
)
Y='yes'
N='no'
CHOICES = (
('Y', 'Yes'),
('N', 'No'),
)
born_status = models.CharField(max_length=1, choices=CHOICES)
gender = models.CharField(max_length=10, choices=GENDER, verbose_name="gender")
story_name = models.CharField(max_length=100)
baby_date = models.DateField()
profile_pic = models.ImageField(upload_to = upload_profile_to, default = 'defaults/profile_pic.jpg')
# package=models.ForeignKey(Package,on_delete=models.CASCADE)
created = models.DateTimeField(verbose_name=('Created'),auto_now_add=True)
updated = models.DateTimeField(verbose_name=('Updated'),auto_now=True)
class Meta:
verbose_name = ('BabyInfo')
verbose_name_plural = ('BabyInfos')
ordering = ['-created']
def __str__(self):
return self.story_name
class Profile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE,)
profile_pic = models.ImageField(upload_to = upload_profile_to, null=True)
# cover_image = models.ImageField(upload_to = 'upload_cover_to', null = True, default= 'defaults/cover_image.jpg')
name=models.CharField(max_length=250)
mail_id=models.EmailField()
relationship=models.CharField(max_length=100)
phone_no=models.IntegerField()
is_lead_user=models.BooleanField(('Is LEAD USER'),help_text='button to toggle employee lead and aditional',default=False)
babyinfo=models.ForeignKey(BabyInfo,on_delete=models.CASCADE,null=True)
created = models.DateTimeField(verbose_name=('Created'),auto_now_add=True,null=True)
updated = models.DateTimeField(verbose_name=('Updated'),auto_now=True,null=True)
class Meta:
verbose_name = ('Profile')
verbose_name_plural = ('Profiles')
ordering = ['-created']
def __str__(self):
return self.mail_id
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.profile_pic.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.profile_pic.path)
img2 = Image.open(self.cover_image.path)
if img2.height > 500 or img2.width > 500:
output_size = (500, 500)
img2.thumbnail(output_size)
img2.save(self.cover_image.path)
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
# Create your views here.
# from .forms import Profile
from userapp.models import BabyInfo,Profile,UserOTP
from userapp.forms import BabyForm1,BabyForm2
from django.contrib import messages
import random
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
def page(request):
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)#########################################
def detail(request):
if request.method == 'POST':
baby_form1 = BabyForm1(data=request.POST)
baby_form2 = BabyForm2(data=request.POST)
if baby_form1.is_valid() and baby_form2.is_valid():
baby1 = baby_form1.save(commit=False)
baby1.save()
print(baby1)
baby2 = baby_form2.save(commit=False)
baby2.baby1 = baby1
baby2.save()
print(baby2)
baby_form1 = BabyForm1()
baby_form2 = BabyForm2()
context = {'baby_form1':baby_form1, 'baby_form2':baby_form2,}
return render(request,'userapp_temp/profile.html', context)
# registered = False
#
#
#
# if request.method == "POST":
# baby_form1 = BabyForm1(data=request.POST)
#
#
# if baby_form1.is_valid():
# baby1 = baby_form1.save(commit=True)
# baby_id = baby1.id
#
# print(baby1.id)
#
#
# # babyinfo = BabyInfo.objects.get(pk=103)
# # print(babyinfo)
# baby_form2 = BabyForm2(data=request.POST)
# print(baby_form2)
# # baby2 = baby_form2.save()
# if baby_form2.is_valid():
# print("form2 start")
# baby2 = baby_form2.save(commit=False)
# baby2.babyinfo_id=baby_id
# baby2.user_id=8
# baby2.save()
# # baby2.name = baby1
#
#
# registered =True
#
# else:
# print(baby_form1.errors,baby_form2.errors)
#
#
# else:
# return render(request,'userapp_temp/landing_page.html')
##################################
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_active = True
usr.save()
messages.success(request, f'Account is Created For {usr.username}')
return redirect('userapp:login')
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_active = 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})
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 ")
def login_view(request):
if request.user.is_authenticated:
return redirect('userapp:login')
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_active = True
usr.save()
login(request, usr)
return redirect('userapp:login')
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']
user = authenticate(request, username = usrname, password = passwd) #None
if user is not None:
login(request, user)
return redirect('userapp:login')
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('userapp: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('userapp:login')
form = AuthenticationForm()
return render(request, 'userapp_temp/login.html', {'form': form})
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.core.exceptions import ValidationError
from .models import BabyInfo,Profile
from django.contrib.auth.forms import UserCreationForm
class BabyForm1(forms.ModelForm):
class Meta:
model = BabyInfo
fields = ['story_name', 'baby_date', 'profile_pic']
labels = {
'story_name': 'story_name',
'baby_date': 'baby_date',
'profile_pic': 'profile_pic'
}
class BabyForm2(forms.ModelForm):
class Meta:
model = Profile
fields = ['name', 'mail_id', 'relationship']
labels = {
'name': 'name',
'mail_id': 'mail_id',
'relationship': 'relationship'
}
# class UserRegisterForm(UserCreationForm):
# email = forms.EmailField()
# phone_no = forms.CharField(max_length = 20)
# first_name = forms.CharField(max_length = 20)
# last_name = forms.CharField(max_length = 20)
# class Meta:
# model = User
# fields = ['username', 'email', 'phone_no', 'password1', 'password2']
class SignUpForm(UserCreationForm):
name = forms.CharField(label = ("Username"))
username = forms.EmailField(label = ("Email"))
class Meta:
model = User
fields = ('name', 'username', 'password1', 'password2')
Hii all,
I hv been struck in last 4 days please fix this error , i'm new to django
-- 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/c753a468-0b45-4266-9094-213719986069n%40googlegroups.com.
No comments:
Post a Comment