from django.shortcuts import render_to_response
from lrntkr.models import Course, University, Student
import datetime


def index(request):
    courses = Course.objects.courseinfo_courses().order_by('-id')
    #universitys=University.objects.all()
    ctx = {'courses': courses}  #'universitys':universitys}
    return render_to_response('homepage/index.html', ctx)


def about(request):
    return render_to_response('homepage/about.html')


def contact(request):
    return render_to_response('homepage/contact.html')


def university(request):
    return render_to_response('homepage/university.html')


def blog(request):
    return render_to_response('homepage/blog.html')


def login(request):
    return render_to_response('homepage/login.html')


def register(request):
    """

    :param request:
    :return:
    """
    success = False
    name_error = ""
    emailid_error = ""
    username_error = ""
    password_error = ""
    rpassword_error = ""

    address_error = ""
    contact_error = ""

    pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"
    if request.POST:
        name = request.POST['name']
        emailid = request.POST['emailid']
        username = request.POST['username']
        password = request.POST['password']
        rpassword = request.POST['rpassword']
        month = request.POST['month']
        day = request.POST['day']
        year = request.POST['year']
        gender = request.POST['sex']
        address = request.POST['address']
        contact = request.POST['contact']
        dob = year + "-" + month + "-" + day
        birth_date = datetime.date(*map(int, dob.split('-')))
        student = Student()
        if 8 >= username >= 6:
            username_error = "User Name Must Contain 6 to 8 Character Only"
        if 8 <= password <= 15:
            password_error = "Password Must Contain 8-15 Character Only"
        if password != rpassword:
            rpassword_error = "Password's are mis match"

        if 9 <= contact > 11:
            contact_error = "enter 10 digit mobile no. with 0 "

        if name_error == "" and emailid_error == "" and username_error == "" and password_error and rpassword_error == "" and address_error == "" and contact_error == "":
            success = True
            student = Student(name=name, email_id=emailid, user_name=username, user_pass=password,
                              user_repass=rpassword,
                              birth_date=birth_date, gender=gender, address=address, contact_no=contact)
        student.save()
    return render_to_response('homepage/register.html', {'name_error': name_error, 'emailid_error': emailid_error,
                                                         'username_error': username_error,
                                                         'password_error': password_error,
                                                         'rpassword_error': rpassword_error,
                                                         'address_error': address_error, 'contact_error': contact_error,
                                                         'success': success}, )



