gcp-hockey-results/dbWrite.py
Jonny Ervine 1ffa319b1e new file: app.py
new file:   appengine_config.py
	new file:   dbWrite.py
	new file:   forms.py
	new file:   main.py
	new file:   readSettings.py
	new file:   requirements.txt
	new file:   routes/__init__.py
	new file:   routes/_convenor.py
	new file:   routes/_hkfcD_motm.py
	new file:   routes/_matches.py
	new file:   routes/_search.py
	new file:   routes/dashboard.py
	new file:   static/css/bootstrap-theme.css
	new file:   static/css/bootstrap-theme.css.map
	new file:   static/css/bootstrap-theme.min.css
	new file:   static/css/bootstrap-theme.min.css.map
	new file:   static/css/bootstrap.css
	new file:   static/css/bootstrap.css.map
	new file:   static/css/bootstrap.min.css
	new file:   static/css/bootstrap.min.css.map
	new file:   static/css/dashboard.css
	new file:   static/css/dashboard.css.orig
	new file:   static/fonts/glyphicons-halflings-regular.eot
	new file:   static/fonts/glyphicons-halflings-regular.svg
	new file:   static/fonts/glyphicons-halflings-regular.ttf
	new file:   static/fonts/glyphicons-halflings-regular.woff
	new file:   static/fonts/glyphicons-halflings-regular.woff2
	new file:   static/js/bootstrap.js
	new file:   static/js/bootstrap.min.js
	new file:   static/js/dashboard.js
	new file:   static/js/npm.js
	new file:   tables.py
	new file:   templates/_about.html
	new file:   templates/_clubPlayingRecordResults.html
	new file:   templates/_clubPlayingRecords.html
	new file:   templates/_convenorClubAdd.html
	new file:   templates/_convenorClubAddResults.html
	new file:   templates/_convenorClubList.html
	new file:   templates/_convenorEditPlayerResults.html
	new file:   templates/_convenorEditSquadList.html
	new file:   templates/_convenorEditSquadListTeamSelect.html
	new file:   templates/_convenorFixtureList.html
	new file:   templates/_convenorPlayerAdd.html
	new file:   templates/_convenorPlayerAddResults.html
	new file:   templates/_convenorPlayerDbCreate.html
	new file:   templates/_convenorPlayerDbCreateResults.html
	new file:   templates/_convenorPlayerEdit.html
	new file:   templates/_convenorSquadList.html
	new file:   templates/_convenorSquadListResults.html
	new file:   templates/_convenorTeamAdd.html
	new file:   templates/_convenorTeamAddResults.html
	new file:   templates/_error.html
	new file:   templates/_goalsAssistsAdmin.html
	new file:   templates/_hkfcDAdminThanks.html
	new file:   templates/_hkfcDGoalsThanks.html
	new file:   templates/_hkfcDMatchComments.html
	new file:   templates/_hkfcDMatchSquad.html
	new file:   templates/_hkfcDMatchSquadReset.html
	new file:   templates/_hkfcDMatchSquadSelected.html
	new file:   templates/_hkfcDMotmAdmin.html
	new file:   templates/_hkfcDMotmVote.html
	new file:   templates/_hkfcDPlayerRemoved.html
	new file:   templates/_hkfcDPotYChart.html
	new file:   templates/_hkfcDVoteChart.html
	new file:   templates/_hkfcDVoteThanks.html
	new file:   templates/_hkfcPlayerDeleted.html
	new file:   templates/_matchDetails.html
	new file:   templates/_playerCheck.html
	new file:   templates/_playerCheckResults.html
	new file:   templates/_playerRecordResults.html
	new file:   templates/_playerRecords.html
	new file:   templates/_search.html
	new file:   templates/_searchResults.html
	new file:   templates/_teamRecordResults.html
	new file:   templates/_teamRecords.html
	new file:   templates/dashboard.html
	new file:   templates/results.html
	new file:   templates/search.html
Initial commit
2020-02-19 06:58:08 +00:00

165 lines
7.2 KiB
Python

#import MySQLdb
import pymysql
import os
import json
# These environment variables are configured in app.yaml.
CLOUDSQL_CONNECTION_NAME = "hk-hockey:asia-east2:hk-hockey-sql"
CLOUDSQL_USER = "root"
CLOUDSQL_WRITE_USER = "hockeyWrite"
CLOUDSQL_READ_USER = "hockeyRead"
CLOUDSQL_PASSWORD = "P8P1YopMlwg8TxhE"
CLOUDSQL_WRITE_PASSWORD = "1URYcxXXlQ6xOWgj"
CLOUDSQL_READ_PASSWORD = "o4GWrbbkBKy3oR6u"
CLOUDSQL_DATABASE = "2019_hockeyResults"
CLOUDSQL_DATABASE_STATIC = "hockeyResults"
CLOUDSQL_CHARSET = "utf8"
def write_cloudsql():
# When deployed to App Engine, the `SERVER_SOFTWARE` environment variable
# will be set to 'Google App Engine/version'.
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
# Connect using the unix socket located at
# /cloudsql/cloudsql-connection-name.
cloudsql_unix_socket = os.path.join('/cloudsql', CLOUDSQL_CONNECTION_NAME)
# db = MySQLdb.connect(unix_socket=cloudsql_unix_socket, user=CLOUDSQL_WRITE_USER, passwd=CLOUDSQL_WRITE_PASSWORD, db=CLOUDSQL_DATABASE, charset=CLOUDSQL_CHARSET)
db = pymysql.connect(unix_socket=cloudsql_unix_socket, user=CLOUDSQL_WRITE_USER, passwd=CLOUDSQL_WRITE_PASSWORD, db=CLOUDSQL_DATABASE, charset=CLOUDSQL_CHARSET)
# If the unix socket is unavailable, then try to connect using TCP. This
# will work if you're running a local MySQL server or using the Cloud SQL
# proxy, for example:
#
# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
#
else:
# db = MySQLdb.connect(host='db.ipa.champion', user=CLOUDSQL_WRITE_USER, passwd=CLOUDSQL_WRITE_PASSWORD, db=CLOUDSQL_DATABASE, charset=CLOUDSQL_CHARSET)
db = pymysql.connect(host='db.ipa.champion', user=CLOUDSQL_WRITE_USER, passwd=CLOUDSQL_WRITE_PASSWORD, db=CLOUDSQL_DATABASE, charset=CLOUDSQL_CHARSET)
return db
def write_cloudsql_static():
# When deployed to App Engine, the `SERVER_SOFTWARE` environment variable
# will be set to 'Google App Engine/version'.
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
# Connect using the unix socket located at
# /cloudsql/cloudsql-connection-name.
cloudsql_unix_socket = os.path.join('/cloudsql', CLOUDSQL_CONNECTION_NAME)
# db = MySQLdb.connect(unix_socket=cloudsql_unix_socket, user=CLOUDSQL_WRITE_USER, passwd=CLOUDSQL_WRITE_PASSWORD, db=CLOUDSQL_DATABASE_STATIC, charset=CLOUDSQL_CHARSET)
db = pymysql.connect(unix_socket=cloudsql_unix_socket, user=CLOUDSQL_WRITE_USER, passwd=CLOUDSQL_WRITE_PASSWORD, db=CLOUDSQL_DATABASE_STATIC, charset=CLOUDSQL_CHARSET)
# If the unix socket is unavailable, then try to connect using TCP. This
# will work if you're running a local MySQL server or using the Cloud SQL
# proxy, for example:
#
# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
#
else:
# db = MySQLdb.connect(host='db.ipa.champion', user=CLOUDSQL_WRITE_USER, passwd=CLOUDSQL_WRITE_PASSWORD, db=CLOUDSQL_DATABASE_STATIC, charset=CLOUDSQL_CHARSET)
db = pymysql.connect(host='db.ipa.champion', user=CLOUDSQL_WRITE_USER, passwd=CLOUDSQL_WRITE_PASSWORD, db=CLOUDSQL_DATABASE_STATIC, charset=CLOUDSQL_CHARSET)
return db
def read_cloudsql():
# When deployed to App Engine, the `SERVER_SOFTWARE` environment variable
# will be set to 'Google App Engine/version'.
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
# Connect using the unix socket located at
# /cloudsql/cloudsql-connection-name.
cloudsql_unix_socket = os.path.join('/cloudsql', CLOUDSQL_CONNECTION_NAME)
# db = MySQLdb.connect(unix_socket=cloudsql_unix_socket, user=CLOUDSQL_READ_USER, passwd=CLOUDSQL_READ_PASSWORD, db=CLOUDSQL_DATABASE, charset=CLOUDSQL_CHARSET)
db = pymysql.connect(unix_socket=cloudsql_unix_socket, user=CLOUDSQL_READ_USER, passwd=CLOUDSQL_READ_PASSWORD, db=CLOUDSQL_DATABASE, charset=CLOUDSQL_CHARSET)
# If the unix socket is unavailable, then try to connect using TCP. This
# will work if you're running a local MySQL server or using the Cloud SQL
# proxy, for example:
#
# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
#
else:
# db = MySQLdb.connect(host='db.ipa.champion', user=CLOUDSQL_READ_USER, passwd=CLOUDSQL_READ_PASSWORD, db=CLOUDSQL_DATABASE, charset=CLOUDSQL_CHARSET)
db = pymysql.connect(host='db.ipa.champion', user=CLOUDSQL_READ_USER, passwd=CLOUDSQL_READ_PASSWORD, db=CLOUDSQL_DATABASE, charset=CLOUDSQL_CHARSET)
return db
def read_cloudsql_static():
# When deployed to App Engine, the `SERVER_SOFTWARE` environment variable
# will be set to 'Google App Engine/version'.
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
# Connect using the unix socket located at
# /cloudsql/cloudsql-connection-name.
cloudsql_unix_socket = os.path.join('/cloudsql', CLOUDSQL_CONNECTION_NAME)
# db = MySQLdb.connect(unix_socket=cloudsql_unix_socket, user=CLOUDSQL_READ_USER, passwd=CLOUDSQL_READ_PASSWORD, db=CLOUDSQL_DATABASE_STATIC, charset=CLOUDSQL_CHARSET)
db = pymysql.connect(unix_socket=cloudsql_unix_socket, user=CLOUDSQL_READ_USER, passwd=CLOUDSQL_READ_PASSWORD, db=CLOUDSQL_DATABASE_STATIC, charset=CLOUDSQL_CHARSET)
# If the unix socket is unavailable, then try to connect using TCP. This
# will work if you're running a local MySQL server or using the Cloud SQL
# proxy, for example:
#
# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
#
else:
# db = MySQLdb.connect(host='db.ipa.champion', user=CLOUDSQL_READ_USER, passwd=CLOUDSQL_READ_PASSWORD, db=CLOUDSQL_DATABASE_STATIC, charset=CLOUDSQL_CHARSET)
db = pymysql.connect(host='db.ipa.champion', user=CLOUDSQL_READ_USER, passwd=CLOUDSQL_READ_PASSWORD, db=CLOUDSQL_DATABASE_STATIC, charset=CLOUDSQL_CHARSET)
return db
def sql_write(sql_cmd):
try:
db = write_cloudsql()
# cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute(sql_cmd)
db.commit()
except Exception as e:
print(e)
finally:
cursor.close()
db.close()
return db
def sql_write_static(sql_cmd):
try:
db = write_cloudsql_static()
# cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute(sql_cmd)
db.commit()
except Exception as e:
print(e)
finally:
cursor.close()
db.close()
return db
def sql_read(sql_cmd):
try:
db = read_cloudsql()
# cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute(sql_cmd)
rows = cursor.fetchall()
except Exception as e:
print(e)
rows = ''
finally:
cursor.close()
db.close()
return rows
def sql_read_static(sql_cmd):
try:
db = read_cloudsql_static()
# cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute(sql_cmd)
rows = cursor.fetchall()
except Exception as e:
print(e)
rows = ''
finally:
cursor.close()
db.close()
return rows