Demo entry 3540719

hashlib

   

Submitted by anonymous on Jan 11, 2016 at 12:07
Language: Lua. Code size: 3.5 kB.

from django.conf import settings
from django.core.mail import send_mail
from django.shortcuts import render
from .models import SignUp
import hashlib


from .forms import SignUpForm, ContactForm

# Create your views here.
def home(request):
	title = "Signup Now"
	# if request.user.is_authenticated():
	# 	title = "My Title %s" %(request.user) #this request.user dynamically displays the name of the user who is logged in. That is looged in our App.
	# if request.method == "POST":
	# 	print request.POST
	form = SignUpForm(request.POST or None) #request.POST means that if we have data then send it through and None means if not then dont check for the validation. If I dont use NONE and i open 127.0.0.1:8000 then it will show that email field not flled and it is required.
	context = {
		"title" : title,
		"form" : form
	}

	
	
	if form.is_valid():
		# form.save()
		print request.POST['email'] #Not suggested to use as it doesnt passes through validations and is printed.

		instance = form.save(commit=False) # allows to skip saving and do some stuff.

		full_name = form.cleaned_data.get("full_name")
		if not full_name:
			full_name = "New full name"
		instance.full_name = full_name

		# if not instance.full_name:
		# 	instance.full_name = "Karan"
		
		# print instance.email
		# print instance.timestamp 
		instance.key = hashlib.sha224(instance.full_name).hexdigest()
		instance.save()
		context = {
			"title" : "Thank you for Signing Up"
		}

		
		print instance.key

	if request.user.is_authenticated():
		print "user:", request.user
		queryset = SignUp.objects.all().order_by('full_name').order_by('email')
		context = {
			"queryset": queryset
		}
	if request.user.is_authenticated() and request.user.is_staff:
		# print (SignUp.objects.all())
		
		# i = 1
		# for instance in SignUp.objects.all():
		# 	print i,
		# 	print instance.full_name
		# 	i += 1

		queryset = SignUp.objects.all().order_by('-timestamp')#.filter(full_name__icontains="Karan")
		context = {
			"queryset": queryset
			}
	
	return render(request, "home.html", context)



def contact(request):

	form = ContactForm(request.POST or None)


	if form.is_valid():


		#  to show that if we have a lot of fields to be printed we can use a loop to print them all instead of doing the way done below.
		
		# for key, value in form.cleaned_data.iteritems():
		# 	print key, value
		#  to run through the keys and values in dictionary

		form_email = form.cleaned_data.get('email')
		form_message = form.cleaned_data.get('message')
		form_full_name = form.cleaned_data.get('full_name')
		form_subject = form.cleaned_data.get('subject')
		
		#print email, message, full_name

		#print form.cleaned_data
		subject = form_subject
		from_email = settings.EMAIL_HOST_USER
		to_email =  [from_email]
		contact_message = "%s -----> %s via %s" %( 
				form_message,
				form_full_name, 
				form_email)

		send_mail(subject, 
				contact_message, 
				from_email, 
				to_email, 
				fail_silently=False)
		
 
		# send_mail('Subject here', 'Here is the message.', 'from@example.com',
 		#   ['to@example.com'], fail_silently=False)


		
	title = "Contact Us"
	title_align_center = True
		#print form.cleaned_data.get('message')
	context = {
		"form" : form,
		"title": title,
		"title_align_center": title_align_center

	}

	return render(request, "contact.html", context)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).