﻿# -*- coding: UTF-8 -*-
from flask import Flask, render_template, request, jsonify
import json
import MySQLdb

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/addinfo', methods=['GET'])
def addinfo():
    name = request.args.get('name')
    #stdNum = request.args.get('stdNum')
    email = request.args.get('eaddress')
    name = name.encode('UTF-8')
    #stdNum = stdNum.encode('UTF-8')
    email = email.encode('UTF-8')

    namelist = getNameList()
    info = ""
    if (name not in namelist):
        info = "You have not joined our group, visit talkwithme.cn for checking in"
    else:
        try:
            connection = MySQLdb.connect(host="w.rdc.sae.sina.com.cn",port=3307, user="04n3nlznzl", passwd="4jj2w3jyyliz2iihwm3k3lzy0hzik3xw1izj41j1",db="app_udacity")
        except Exception, e:
            return str(e) + "xixishuiba"

        sql = "INSERT INTO Member VALUES (" + "'" + name + "','" + email + "');"
        #sql = sql.encode('UTF-8')
        cursor = connection.cursor()
        cursor.execute("SET NAMES utf8")
        cursor.execute("insert into Member values('1', '2');")
        info = "You have successfully submitted the form, waiting for the admin to sent invitation email"
        try:
            cursor.execute(sql)
        except Exception, e:
            info = str(e) + "Datebase error, retry later pls"

    #emailBook = open("record.txt","w+")
    #record = name+","+stdNum+","+email+"\n"
    #record = record.encode('UTF-8')
    #emailBook.write(record)
    #emailBook.close()

        cursor.close()
        connection.close()
    info = info.encode('UTF-8')

    return info

def getNameList():
    nameList = ['黄瓒', '林颖欣', '谢倩', '于悦洋', '王雅晨', '王世因', '陈文兴', '王远皓', '任崇旭', '周大业', '李佳蔚', '郭志梅', '张心之', '高浩', '王润喆', '郭铖浩', '李皓然', '郭耀华', '胡荣德', '胡壮飞', '张羿翔', '张宝河', '张显', '韩月军', '张海滨', '宋政钦', '杨亚东', '黄齐威', '张海滨', '张燕', '聂海东', '耿芳东', '吴昊南', '周睿', '张琳', '洪贤斌', '王璐']
    return nameList

if __name__ == '__main__':
    app.run(debug=True, port=80)
