<commit_before># -*- coding: utf-8 -*-
from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django import forms
from django.contrib.auth import authenticate, login
import logging
from ntulgapp.user import ntulgUser
from ntulgapp.user import ntulgUserForm
from ntulgapp.globals import CURRENT_STAGE
from ntulgapp.globals import APP_URL
from django.contrib.auth.models import User
import string
import random
from google.appengine.api import mail
from django.shortcuts import redirect

USER_INPUT_LEN_MIN = 1
USER_INPUT_LEN_MAX = 100

logger = logging.getLogger(__name__)

def auto_fill(post_data):
    new_post_data = post_data.copy()
    new_post_data['stage_no'] = 99
    new_post_data['cap_no'] = 889
    new_post_data['name_cht'] = u"陳田富"
    new_post_data['name_eng'] = u"CHEN,TIENFU"
    new_post_data['nationality']= u"TW"
    #new_post_data['identify_number']= u"K123123123"
    new_post_data['email']=u"<EMAIL>"
    new_post_data['birthday']=u"1980-2-2"
    new_post_data['sex']=u"male"
    new_post_data['tel_mobile']=u"0937019827"
    new_post_data['tel']=u"(02)22222222"
    new_post_data['address']=u"12313 基隆市中正區正義路40號"
    new_post_data['height']=177
    new_post_data['weight']=65
    new_post_data['tshirt_size']=u"M"
    new_post_data['if_present_ntu']=True
    new_post_data['if_vegetarian']=False
    new_post_data['emergency_contact_name']=u"我"
    new_post_data['emergency_contact_mobile']=u"0928371231"
    new_post_data['beneficiary']=u"他"
    new_post_data['beneficiary_relationship']=u"兄弟"
    new_post_data['medical_history']=u"無"
    new_post_data['source']=u"faceook看到的"
    #new_post_data['comment']=
    #new_post_data['facebook_id']=
    #new_post_data['ptt_id']=
    #new_post_data['ptt2_id']=
    
    return new_post_data


def create_user(user_title, user_name, email):
    password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
    logging.info("user_name=%s, password=%s" % (user_name, password))

    if User.objects.filter(username=user_name).count():
        return None

    user = User.objects.create_user(user_name, email, password)
    user.save()
    body = u"%s 你好，\n你的密碼是：%s\n\n管理系統：%s" % (user_title, password, APP_URL)
    #mail.send_mail(sender="<EMAIL>",to=email,subject=u"謝謝使用台大救生班隊員資料管理系統", body=body)

    return user

class loginForm(forms.Form):
    login_id = forms.CharField(required=False, label=u'帳號(account)', help_text=u'你的身份證字號/居留證號碼(your ID.)', max_length=USER_INPUT_LEN_MAX)
    login_pw = forms.CharField(required=False, label=u'密碼(password)', max_length=USER_INPUT_LEN_MAX)


def signup_view(request, if_training):

    if request.method == 'POST': # If the form has been submitted...
        new_post = request.POST.copy()
        
        #new_post = auto_fill(request.POST)
        if if_training:
            new_post["stage_no"] = CURRENT_STAGE["no"]

        form = ntulgUserForm(new_post) # A form bound to the POST data
        if if_training:
            form.fields['stage_no'].widget = forms.HiddenInput()
            form.fields['cap_no'].widget = forms.HiddenInput()

        post_keys = request.POST.keys()

        if u"confirm" in post_keys:
            if form.is_valid(): # All validation rules pass
                user = create_user(new_post['name_cht'], new_post['identify_number'], new_post['email'])
                if user is not None:
                    n = form.save()
                    return render_to_response('signup_feedback.html', {'Email': new_post["email"]},context_instance=RequestContext(request) )
                else:
                    return render(request, 'signup.html', {
                        'form':form,
                        'if_training':if_training,
                        'error':u'你已經註冊過了，請直接登入，或再次檢查你所輸入的身分證字號，或聯絡管理員！'},
                        context_instance=RequestContext(request))

            else:
                return render(request, 'signup.html', {
                    'form':form,
                    'if_training':if_training,
                    'error':u'資料輸入有誤，請檢查欄位，完成後請按\"確定\"！'},
                    context_instance=RequestContext(request))

        elif u"cancel" in post_keys:
            return redirect("/")

    else:
        form = ntulgUserForm() # An unbound form
        if if_training:
            form.fields['stage_no'].widget = forms.HiddenInput()
            form.fields['cap_no'].widget = forms.HiddenInput()
        return render_to_response('signup.html',{
            'form':form,
            'if_training':if_training
            },context_instance=RequestContext(request))

def management_view(request):
    logging.info("management_view")
    #q_user = request.session["q_user"]
    #logging.info(q_user.id)
    #logging.info(q_user.identify_number)
    
    q_user = request.session.get("q_user")

    if request.method == 'POST' and q_user is not None: # If the form has been submitted...
        logging.info(q_user.id)
        logging.info(q_user.identify_number)

        post_keys = request.POST.keys()
        if u"update_profile" in post_keys:
            form = ntulgUserForm(instance=q_user)
            return render_to_response('update_data.html', {'form':form}, context_instance=RequestContext(request))

        elif u"update_password" in post_keys:
            return render_to_response('update_password.html', context_instance=RequestContext(request))
    else:
        return render_to_response('home.html', {"form":loginForm()},context_instance=RequestContext(request))

def login_view(request):
    error = ""
    if request.method == 'POST': # If the form has been submitted...
        form = loginForm(request.POST) # A form bound to the POST data
        post_keys = request.POST.keys()
        logging.info(form)
        logging.info(request.POST)
        if u"signin" in post_keys:
            if form.is_valid(): # All validation rules pass
                login_id = request.POST['login_id']
                login_pw = request.POST['login_pw']

                user = authenticate(username=login_id, password=<PASSWORD>)<commit_msg>redirect to login page if user view management directly<commit_after># -*- coding: utf-8 -*-
from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django import forms
from django.contrib.auth import authenticate, login
import logging
from ntulgapp.user import ntulgUser
from ntulgapp.user import ntulgUserForm
from ntulgapp.globals import CURRENT_STAGE
from ntulgapp.globals import APP_URL
from django.contrib.auth.models import User
import string
import random
from google.appengine.api import mail
from django.shortcuts import redirect

USER_INPUT_LEN_MIN = 1
USER_INPUT_LEN_MAX = 100

logger = logging.getLogger(__name__)

def auto_fill(post_data):
    new_post_data = post_data.copy()
    new_post_data['stage_no'] = 99
    new_post_data['cap_no'] = 889
    new_post_data['name_cht'] = u"陳田富"
    new_post_data['name_eng'] = u"CHEN,TIENFU"
    new_post_data['nationality']= u"TW"
    #new_post_data['identify_number']= u"K123123123"
    new_post_data['email']=u"<EMAIL>"
    new_post_data['birthday']=u"1980-2-2"
    new_post_data['sex']=u"male"
    new_post_data['tel_mobile']=u"0937019827"
    new_post_data['tel']=u"(02)22222222"
    new_post_data['address']=u"12313 基隆市中正區正義路40號"
    new_post_data['height']=177
    new_post_data['weight']=65
    new_post_data['tshirt_size']=u"M"
    new_post_data['if_present_ntu']=True
    new_post_data['if_vegetarian']=False
    new_post_data['emergency_contact_name']=u"我"
    new_post_data['emergency_contact_mobile']=u"0928371231"
    new_post_data['beneficiary']=u"他"
    new_post_data['beneficiary_relationship']=u"兄弟"
    new_post_data['medical_history']=u"無"
    new_post_data['source']=u"faceook看到的"
    #new_post_data['comment']=
    #new_post_data['facebook_id']=
    #new_post_data['ptt_id']=
    #new_post_data['ptt2_id']=
    
    return new_post_data


def create_user(user_title, user_name, email):
    password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
    logging.info("user_name=%s, password=%s" % (user_name, password))

    if User.objects.filter(username=user_name).count():
        return None

    user = User.objects.create_user(user_name, email, password)
    user.save()
    body = u"%s 你好，\n你的密碼是：%s\n\n管理系統：%s" % (user_title, password, APP_URL)
    #mail.send_mail(sender="<EMAIL>",to=email,subject=u"謝謝使用台大救生班隊員資料管理系統", body=body)

    return user

class loginForm(forms.Form):
    login_id = forms.CharField(required=False, label=u'帳號(account)', help_text=u'你的身份證字號/居留證號碼(your ID.)', max_length=USER_INPUT_LEN_MAX)
    login_pw = forms.CharField(required=False, label=u'密碼(password)', max_length=USER_INPUT_LEN_MAX)


def signup_view(request, if_training):

    if request.method == 'POST': # If the form has been submitted...
        new_post = request.POST.copy()
        
        #new_post = auto_fill(request.POST)
        if if_training:
            new_post["stage_no"] = CURRENT_STAGE["no"]

        form = ntulgUserForm(new_post) # A form bound to the POST data
        if if_training:
            form.fields['stage_no'].widget = forms.HiddenInput()
            form.fields['cap_no'].widget = forms.HiddenInput()

        post_keys = request.POST.keys()

        if u"confirm" in post_keys:
            if form.is_valid(): # All validation rules pass
                user = create_user(new_post['name_cht'], new_post['identify_number'], new_post['email'])
                if user is not None:
                    n = form.save()
                    return render_to_response('signup_feedback.html', {'Email': new_post["email"]},context_instance=RequestContext(request) )
                else:
                    return render(request, 'signup.html', {
                        'form':form,
                        'if_training':if_training,
                        'error':u'你已經註冊過了，請直接登入，或再次檢查你所輸入的身分證字號，或聯絡管理員！'},
                        context_instance=RequestContext(request))

            else:
                return render(request, 'signup.html', {
                    'form':form,
                    'if_training':if_training,
                    'error':u'資料輸入有誤，請檢查欄位，完成後請按\"確定\"！'},
                    context_instance=RequestContext(request))

        elif u"cancel" in post_keys:
            return redirect("/")

    else:
        form = ntulgUserForm() # An unbound form
        if if_training:
            form.fields['stage_no'].widget = forms.HiddenInput()
            form.fields['cap_no'].widget = forms.HiddenInput()
        return render_to_response('signup.html',{
            'form':form,
            'if_training':if_training
            },context_instance=RequestContext(request))

def management_view(request):
    logging.info("management_view")
    #q_user = request.session["q_user"]
    #logging.info(q_user.id)
    #logging.info(q_user.identify_number)
    
    q_user = request.session.get("q_user")

    if request.method == 'POST' and q_user is not None: # If the form has been submitted...
        logging.info(q_user.id)
        logging.info(q_user.identify_number)

        post_keys = request.POST.keys()
        if u"update_profile" in post_keys:
            form = ntulgUserForm(instance=q_user)
            return render_to_response('update_data.html', {'form':form}, context_instance=RequestContext(request))

        elif u"update_password" in post_keys:
            return render_to_response('update_password.html', context_instance=RequestContext(request))
    else:
        return redirect("/login")

def update_password_view(request):
    pass

def update_data_view(request):
    pass

def login_view(request):
    error = ""
    if request.method == 'POST': # If the form has been submitted...
        form = loginForm(request.POST) # A form bound to the POST data
        post_keys = request.POST.keys()
        logging.info(form)
        logging.info(request.POST)
        if u"signin" in post_keys:
            if form.is_valid(): # All validation rules pass
                login_id = request.POST['login_id']
                login_pw = request.POST['login_pw']

                user = authenticate(username=login_id, password=<PASSWORD>)
{'fraction_non_alphanumeric': 0.11836475994295674, 'fraction_numerical': 0.010933291079068293, 'mean_word_length': 3.2444519166106254, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1256612', 'n_tokens_mistral': 4373, 'n_tokens_neox': 4212, 'n_words': 718}