def login():
    try:
        userid = request.form['user']
        userpass = request.form['pass']
    except:
        userid = request.cookies.get('user')
        userpass = request.cookies.get('pass')
    
    user_prof = my_func.sql_ALLuser_profile()
    # ユーザーのタイプ毎にリダイレクト
    if not (my_func.kakunin(userid, userpass)):# アカウントとパスワードの確認
        sentence = 'IDまたはPASSが違います。正しいパスワードを入力してください。'
        redirect_to_index = render_template('error.html',
                                            sentence = sentence)
    
    elif user_prof[userid]['type'] == -1:# 利用停止中のアカウント
        sentence = 'あなたのアカウントは現在利用できません。'
        redirect_to_index = render_template('error.html',
                                            sentence = sentence)
        
    elif user_prof[userid]['type'] == 0:# 管理者
        redirect_to_index = redirect('/admin/show', code=307)
        
    elif user_prof[userid]['type'] == 1:# 一般ユーザー
        redirect_to_index = redirect('/show', code=307)
        
    elif user_prof[userid]['type'] == 2:# 監督・コーチ
        redirect_to_index = redirect('/admin/show', code=307)
    
    resp = make_response(redirect_to_index)
    resp.set_cookie('user', userid)
    resp.set_cookie('pass', userpass)
    return resp
def hello():
    userid = request.cookies.get('user')
    userpass = request.cookies.get('pass')
    hantei = my_func.kakunin(userid,userpass)
    user_prof = my_func.sql_ALLuser_profile()
    
    if hantei:# lonin success
        # 11~3月のみ雪マークを追加
        weather = [{'num' : '{}'.format(i),
                    'moji' : tenki_dic[i]}
                     for i in tenki_dic.keys()
                         #if not(4 <= datetime.datetime.today().month <= 10) 
                         #    and i=='4' or i=='0' or i=='1' or i=='2' or i=='3'
                             ]
        # 飲水量の選択肢を追加
        water = ['{:.2f}'.format(round(i*0.05,2))\
                     for i in range(201)]
        
        return render_template('hello.html', 
                               title = 'taberube.jp', 
                               name = user_prof[userid]['rname'],
                               weather = weather,
                               water = water,
                               serverhost = server_address)
    else:# login fail
        sentence = '''You cannot log in on the website. 
                    Please try again from the start! 
                    (最初からやり直してください)'''
        return make_response(render_template('error.html',
                                             sentence = sentence))
Esempio n. 3
0
# -*- coding: utf-8 -*-
"""
Created on Fri Jan  3 12:41:56 2020

@author: azumi
"""

import my_function2_sql as my_func

user_name = 'azumi'
user_pass = '******'

print('get_user_dic():\n', 
      my_func.get_user_dic())

print('get_user_info():\n',
      my_func.get_user_info())

print('sql_ALLuser_profile():\n', 
      my_func.sql_ALLuser_profile(user_name, user_pass))

print('kakunin():')
def admin_message():
    admin = request.cookies.get('user')
    adminpass = request.cookies.get('pass')
    user_prof = my_func.sql_ALLuser_profile()
    
    #if my_func.admin_coach_kakunin(admin, adminpass):
    if my_func.admin_kakunin(admin, adminpass):
        pass
    else:
        sentence = '改修中です。監督・コーチの掲示板の機能のご利用はもうしばらくお待ちください。'
        index = render_template('error.html',
                                sentence = sentence)
        return make_response(index)
    
    messages = my_func.sql_message_get(
        admin,
        adminpass,
        max_messages = 10
    )
    
    posts = []
    for d in messages:
        posts.append({
            'day'     :d['day'],
            'rname'   :user_prof[d['userid']]['rname'],
            'group'   :d['group'],
            'title'   :d['title'],
            'contents':d['contents']}
        )
    
    if request.args.get('status') == 'first':
        try:
            my_func.kakunin(admin, adminpass)
        except Exception as error:
            return 'NG: '+error.__str__()
        index = render_template('admin_message.html',
                                serverhost = server_address,
                                posts = posts)
        resp = make_response(index)
        
        return resp
    
    try:
        if len(admin) == 0 or len(adminpass) == 0:
            return 'Cannot access message'
        
        # you have to add form of group below
        group = 'ALL'
        title = str(request.form['title'])
        contents = str(request.form['contents'])
        
        my_func.sql_message_send(
            admin, 
            adminpass, 
            group,
            title, 
            contents,
        )
        
        messages = my_func.sql_message_get(
                admin,
                adminpass,
                max_messages = 10
                )
        
        posts = []
        for d in messages:
            posts.append({
                'day'     :d['day'],
                'rname'   :user_prof[d['userid']]['rname'],
                'group'   :d['group'],
                'title'   :d['title'],
                'contents':d['contents']}
            )
        
        return render_template(
                'admin_message.html', 
                 title = 'Message',
                 user = admin,
                 posts = posts,
                 serverhost = server_address
                 )
    except Exception as error:
        return error.__str__()
def admin_register():
    admin = request.cookies.get('user')
    adminpass = request.cookies.get('pass')
    text = ''
    
    if not (my_func.admin_kakunin(admin, adminpass)):
        sentence = '機能制限: ユーザー管理機能は管理者のみ利用可能です。'
        index = render_template('error.html',
                                sentence = sentence)
        return make_response(index)
    
    if len(admin) == 0 or len(adminpass) == 0:
        return 'NG1: cannot access'
    
    if request.args.get('resgs') == 'org':
        if len(request.form['org_id']) == 0 \
            or len(request.form['org_name']) == 0:
            sentence = 'ERROR : Fill in the blank!: すべての空欄を埋めてください。'
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        
        if request.form['org_id'] in my_func.get_org().keys():
            sentence = '''
                        NG: 新しい組織を登録できません。
                        組織ID[{}]は使われています。違う組織名を指定してください。
                        '''.format(request.form['org_id'])
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        
        try:
            info = {'org_id':request.form['org_id'],
                    'org_name':request.form['org_name']}
            my_func.addorg(admin, adminpass, info)
            
        except Exception as error:
            sentence = 'ERROR: ' + error.__str__()
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
    
    if request.args.get('resgs') == 'user':
        # ユーザーの登録
        info = {'newuser':request.form['newuser'],
                'newpass':request.form['newpass'],
                'rname'  :request.form['rname'],
                'type'   :request.form['type'],
                'org'    :request.form['org'],
                'year'   :request.form['year']
                }
        
        if len(request.form['newuser']) == 0 or len(request.form['newpass']) == 0 or \
            len(request.form['rname']) == 0 or len(request.form['org']) == 0:
            
            sentence = 'ERROR : Fill in the blank!: すべての空欄を埋めてください。'
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        
        if request.form['newuser'] in my_func.sql_ALLuser_profile().keys():
            sentence = '''
                        NG: 新しいユーザーを登録できません。
                        ユーザー名[{}]は使われています。違うユーザー名を指定してください。
                        '''.format(request.form['newuser'])
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        
        try:
            if my_func.adduser(admin, adminpass, info):
                text = request.form['rname'] + 'さんを登録しました.',

            else:
                return 'NG'
        except Exception as error:
            return 'Fail: SQL Server Error' + error.__str__()
    
    user_prof = my_func.sql_ALLuser_profile()
    org_dic = my_func.get_org()
    
    posts = []; posts_admin = [] 
    posts_coach = []; posts_unusable = []
    posts_org = []
    
    for name in user_prof.keys():
        dic = {'name':user_prof[name]['rname'],
                'org':org_dic[user_prof[name]['org']]['org_name'],
                'year':user_prof[name]['year'],
                'id':name,
                'keyword':str(user_prof[name]['year']) \
                     + user_prof[name]['org'] + name,
               }
        
        if user_prof[name]['type'] == 0:
            posts_admin.append(dic)
        elif user_prof[name]['type'] == 1:
            posts.append(dic)
        elif user_prof[name]['type'] == 2:
            posts_coach.append(dic)
        elif user_prof[name]['type'] == -1:
            posts_unusable.append(dic)
    
    
    for p in org_dic.keys():
        dic = {'org_id'  :p,
               'org_name':org_dic[p]['org_name']}
        
        posts_org.append(dic)
        
    posts = reversed(sorted(posts, key = lambda x:x['keyword']))
    
    index = render_template('admin_register.html',
                            text = text,
                            serverhost = server_address,
                            posts = posts,
                            posts_admin = posts_admin,
                            posts_coach = posts_coach,
                            posts_unusable = posts_unusable,
                            posts_org = posts_org,
                            year = datetime.datetime.now().year)
    
    resp = make_response(index)
    
    return resp
def admin_latest():
    admin = request.cookies.get('user')
    adminpass = request.cookies.get('pass')
    
    if admin == '' or adminpass == '':
        sentence = 'ログアウトしています。'
        index = render_template('error.html',
                                sentence = sentence)
        return make_response(index)
    
    if not (my_func.admin_coach_kakunin(admin, adminpass)):
        sentence = '初めからやり直してください。'
        index = render_template('error.html',
                                sentence = sentence)
        return make_response(index)
    
    try:
        user_prof = my_func.sql_ALLuser_profile()
        try:
            data = my_func.sql_data_get_latest_all(user_prof[admin]['type'],
                                                   user_prof[admin]['org'])
            posts = []
            for d in reversed(data):
                neccessary1_tmp \
                    = round(float(d['wb']*0.01) + float(d['moi']),1)
                if neccessary1_tmp <= 0:
                    neccessary1_tmp = 0
                shitsudo = d['shitsudo']
                temp = d['temp']
                if int(shitsudo) == 1111:
                    shitsudo = ' '
                if int(temp) == 1111:
                    temp = ' '
                posts.append({
                  'date'         :d['day'],#日
                  'bweight'      :d['wb'],#運動前体重
                  'aweight'      :d['wa'],#運動後体重
                  'training'     :d['contents'][0:10],#トレーニング内容
                  'period'       :d['time'],#運動時間
                  'intake'       :d['moi'],#飲水量
                  'dehydraterate':my_func.dassui_ritu(d['wb'],d['wa']),#脱水率
                  'necessary'    :round(my_func.hakkann_ryo(d['wb'], d['wa'], d['moi']), 1),
                  'tenki'        :tenki_dic[str(d['tenki'])],#天気
                  'shitsudo'     :shitsudo,#湿度
                  'temp'         :temp,
                  'username'     :user_prof[d['username']]['rname'],
                  'w1'           :round(d['wb']*0.99,1),
                  'necessary1'   :neccessary1_tmp}# ユーザの本名
                )
            
            posts = reversed(sorted(posts, key=lambda x:x['date']))
            index = render_template('admin_latest.html', 
                                    title = 'taberube.jp', 
                                    posts = posts,
                                    serverhost = server_address)
            return make_response(index)
            
        except Exception as error:
            sentence = 'ERROR1: ' + error.__str__()
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        
    except Exception as error:
            sentence = 'ERROR2: '+error.__str__()
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
def admin_watch_show():
    admin = request.cookies.get('user')# クッキーを保存
    adminpass = request.cookies.get('pass')# クッキーを保存
    
    if not(my_func.admin_coach_kakunin(admin, adminpass)):
        sentence = '初めからやり直してください。'
        index = render_template('error.html',
                                sentence = sentence)
        return make_response(index)
    
    try:
        user_prof = my_func.sql_ALLuser_profile()
        uid_get = request.args.get('name')# 見たいユーザ名
        real_name = user_prof[uid_get]['rname']# ユーザの本名
        
        if user_prof[admin]['type'] == 2 and user_prof[admin]['org'] != user_prof[uid_get]['org']:
            sentence = '機能制限: このユーザーのデータは閲覧できません。'
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        
        data = my_func.sql_data_get(uid_get)
        posts = []
        for d in reversed(data):# dataは辞書形式
            neccessary1_tmp \
                = round(float(d['wb']*0.01) + float(d['moi']), 1)
            
            if neccessary1_tmp <= 0:
                neccessary1_tmp = 0
            shitsudo = d['shitsudo']
            temp = d['temp']
            if int(shitsudo) == 1111:
                shitsudo = ' '
            if int(temp) == 1111:
                temp = ' '
            
            posts.append({
              'date'          :d['day'],#日
              'bweight'       :d['wb'],#運動前体重
              'aweight'       :d['wa'],#運動後体重
              'training'      :d['contents'][0:10],#トレーニング内容
              'period'        :d['time'],#運動時間
              'intake'        :d['moi'],#飲水量
              'dehydraterate' :my_func.dassui_ritu(d['wb'],d['wa']),#脱水率
              'necessary'    :round(my_func.hakkann_ryo(d['wb'], d['wa'], d['moi']), 1),
              'tenki'         :tenki_dic[str(d['tenki'])],#天気
              'shitsudo'      :shitsudo,#湿度
              'temp'          :temp,
              'w1'            :round(d['wb']*0.99,1),
              'necessary1'    :neccessary1_tmp
            })
        
        index = render_template('admin_show.html',
                                title = 'taberube.jp',
                                user = real_name,
                                posts = posts,
                                userid = uid_get,
                                serverhost = server_address)
        
        resp = make_response(index)
        
        resp.set_cookie('user', admin)# クッキーの再設定
        resp.set_cookie('pass', adminpass)# クッキーの再設定
        
        return resp
    except Exception as error:# SQLなどのエラー
        sentence = error.__str__()
        index = render_template('error.html',
                                sentence = sentence)
        return make_response(index)
def admin_watch():# ユーザリスト ユーザを選び -> admin_watch_show()
    admin = request.cookies.get('user')
    adminpass = request.cookies.get('pass')
    
    try:
        if not(my_func.admin_coach_kakunin(admin, adminpass)):
            sentence = '初めからやり直してください。'
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        user_prof = my_func.sql_ALLuser_profile()
        org_dic = my_func.get_org()
        
    except Exception as error:
        sentence = 'do not connect sql server by your username \
                \n or occur making html error:\n{}'.format(error.__str__())
        index = render_template('error.html',
                                sentence = sentence)
        resp = make_response(index)
    
    posts = []; posts_admin = [] 
    posts_coach = []; posts_unusable = []
    
    for name in user_prof.keys():
        dic = {'name'     :user_prof[name]['rname'],
                'org'     :org_dic[user_prof[name]['org']]['org_name'],
                'year'    :user_prof[name]['year'],
                'id'      :name,
                'keyword' :str(user_prof[name]['year']) \
                     + user_prof[name]['org'] + name,
               }
        if user_prof[admin]['type'] == 2 \
            and dic['org'] == org_dic[user_prof[admin]['org']]['org_name']:
            
            if user_prof[name]['type'] == 1:
                posts.append(dic)
            elif user_prof[name]['type'] == 2:
                posts_coach.append(dic)
            
        if user_prof[admin]['type'] == 0:
            
            if user_prof[name]['type'] == 0:
                posts_admin.append(dic)
            elif user_prof[name]['type'] == 1:
                posts.append(dic)
            elif user_prof[name]['type'] == 2:
                posts_coach.append(dic)
            elif user_prof[name]['type'] == -1:
                posts_unusable.append(dic)
    
    if user_prof[admin]['type'] == 2:
        posts_admin =[{'name':'非表示',
                       'org' :'XXXX',
                       'year':'XXXX',
                       'id'  :'XXXX'}]; 
    
    posts = reversed(sorted(posts, 
                            key = lambda x : x['keyword'])
            )
    resp = make_response(render_template(
            'admin_watch.html',
            serverhost = server_address,
            posts = posts,
            posts_admin = posts_admin,
            posts_coach = posts_coach,
            posts_unusable = posts_unusable))
    
    return resp
def show():
    userid = request.cookies.get('user')
    userpass = request.cookies.get('pass')
    
    user_prof = my_func.sql_ALLuser_profile()
    
    if not (my_func.kakunin(userid, userpass)):
        sentence = 'IDまたはPASSが違います。正しいパスワードを入力してください。'
        return make_response(render_template('error.html',
                                         sentence = sentence))
    
    try:
        data = my_func.sql_data_get(userid)
        posts = []
        for d in reversed(data):
            neccessary1_tmp \
                = round(float(d['wb'] * 0.01) + float(d['moi']), 1)
            
            if neccessary1_tmp <= 0:
                neccessary1_tmp = 0
            
            shitsudo = d['shitsudo']
            temp = d['temp']
            if int(shitsudo) == 1111:
                shitsudo = '??'
            if int(temp) == 1111:
                temp = '??'
            posts.append({
                  'date'         :d['day'],# 日
                  'bweight'      :d['wb'],# 運動前体重
                  'aweight'      :d['wa'],# 運動後体重
                  'training'     :d['contents'][0:10],# トレーニング内容
                  'period'       :d['time'],# 運動時間
                  'intake'       :d['moi'],# 飲水量
                  'dehydraterate':my_func.dassui_ritu(d['wb'], d['wa']),# 脱水率
                  'tenki'        :str(tenki_dic[str(d['tenki'])]),# 天気
                  'shitsudo'     :shitsudo,# 湿度
                  'temp'         :temp,# 気温
                  'dassui1'      :round(my_func.hakkann_ritu_ex1(d['wb'], d['wa'], d['time']), 1),
                  'necessary'    :round(my_func.hakkann_ryo(d['wb'], d['wa'], d['moi']), 1),
                  'necessary1'   :neccessary1_tmp,
                  'w1'           :round(d['wb'] * 0.99, 1)
                })
        if len(posts) > 0:
            latest = posts.pop(0)
            data = my_func.generateComment(latest)
            comment = data['sentence']
            img = data['img']
            
        else:
            latest = {
                  'date'         :'今回',   #日
                  'bweight'      :'No data',# 運動前体重
                  'aweight'      :'No data',# 運動後体重
                  'training'     :'No data',# トレーニング内容
                  'period'       :'No data',# 運動時間
                  'intake'       :'No data',# 飲水量
                  'dehydraterate':'No data',# 脱水率
                  # 'dehydrateval' :'No data',# 脱水量
                  'tenki'        :'No data',# 天気
                  'shitsudo'     :'No data',# 湿度
                  'temp'         :'No data',
                  'dassui1'      :'No data',
                  'necessary'    :'No data',
                  'necessary1'   :'No data',
                  'w1'           :'No data'}
            
            comment = '''初めまして。このアプリでは、
                     日々のトレーニング後の脱水量を記録していきます。
                     最初のデータを入力しましょう。
                     下の「データ入力」ボタンから結果を登録できます。
                     また、「アスリートのみなさんへ」は、
                     このアプリを利用している全員向けのコメントです。
                     '''
            
            img = 'suzuki1.png'
        messages = my_func.sql_message_get(
                 userid,
                 userpass,
                 max_messages = 3)
        
        texts = []
        for d in messages:
            texts.append({
                'day'     :d['day'],
                'rname'   :user_prof[d['userid']]['rname'],
                'group'   :d['group'],
                'title'   :d['title'],
                'contents':d['contents']
                }
            )
        
        resp = make_response(render_template('main.html',
                                             title = 'taberube.jp',
                                             user = userid,
                                             posts = posts,
                                             latest = latest,
                                             comment = comment,
                                             texts = texts,
                                             img = img,
                                             rname = user_prof[userid]['rname'],
                                             serverhost = server_address))
        resp.set_cookie('user', userid)
        resp.set_cookie('pass', userpass)
        
        return resp
    except Exception as error:
        sentence = '''
            エラー: 結果の画面が取得できません。
            SQLサーバーが停止している、または、
            表データに不正な文字が含まれているため表示できません。 
            サーバー側に問題があるので、管理者にお問い合わせください。
            (detail:'''+error.__str__()+')'
        return make_response(render_template('error.html',
                                             sentence=sentence))
def newaccount():
    text = ''
    org_dic = my_func.get_org()
    if request.args.get('resgs') == 'user':
        # ユーザーの登録
        info = {'newuser':request.form['newuser'],
                'newpass':request.form['newpass'],
                'rname'  :request.form['rname'],
                'type'   :request.form['type'],
                'org'    :request.form['org'],
                'year'   :request.form['year'],
                'mail'   :request.form['mail']
                }
        
        if len(request.form['newuser']) == 0 or len(request.form['newpass']) == 0 or \
            len(request.form['rname']) == 0 or len(request.form['org']) == 0:
            
            sentence = 'ERROR : Fill in the blank!: すべての空欄を埋めてください。'
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        
        if request.form['newuser'] in my_func.sql_ALLuser_profile().keys():
            sentence = '''
                        NG: 新しいユーザーを登録できません。
                        ユーザー名[{}]は使われています。違うユーザー名を指定してください。
                        '''.format(request.form['newuser'])
            index = render_template('error.html',
                                    sentence = sentence)
            return make_response(index)
        
        try:
            if my_func.adduser_general(info):
                text = request.form['rname'] + 'さんを登録しました.'

                try:
                    my_func_mail.send_mail_newaccount(info, org_dic)
                    text = text + '登録完了メールが送信されました。'
                except Exception as error:
                    text = text + 'メールアドレス入力ミスなどにより、登録完了メールは送信されませんでした。エラー内容:' + error.__str__()
                
                index = render_template('registered.html',
                            text = text,
                            serverhost = server_address,
                            newuser = info['newuser'],
                            newpass = info['newpass'],
                            rname = info['rname'],
                            org = org_dic[info['org']]['org_name'],
                            year = info['year'],
                            mail  = info['mail'])
                resp = make_response(index)
                
                return resp
            else:
                return 'NG'
        except Exception as error:
            return 'Fail: SQL Server Error or mail error' + error.__str__()

    
    user_prof = my_func.sql_ALLuser_profile()
    
    posts = []; posts_admin = [] 
    posts_coach = []; posts_unusable = []
    posts_org = []
    
    for name in user_prof.keys():
        dic = {'name':user_prof[name]['rname'],
                'org':org_dic[user_prof[name]['org']]['org_name'],
                'year':user_prof[name]['year'],
                'id':name,
                'keyword':str(user_prof[name]['year']) \
                     + user_prof[name]['org'] + name,
               }
        
        if user_prof[name]['type'] == 0:
            posts_admin.append(dic)
        elif user_prof[name]['type'] == 1:
            posts.append(dic)
        elif user_prof[name]['type'] == 2:
            posts_coach.append(dic)
        elif user_prof[name]['type'] == -1:
            posts_unusable.append(dic)
    
    
    for p in org_dic.keys():
        dic = {'org_id'  :p,
               'org_name':org_dic[p]['org_name']}
        
        posts_org.append(dic)
        
    posts = reversed(sorted(posts, key = lambda x:x['keyword']))
    
    index = render_template('register.html',
                            text = text,
                            serverhost = server_address,
                            posts = posts,
                            posts_admin = posts_admin,
                            posts_coach = posts_coach,
                            posts_unusable = posts_unusable,
                            posts_org = posts_org,
                            year = datetime.datetime.now().year)
    
    resp = make_response(index)
    
    return resp