Skip to content

Commit

Permalink
update entities script
Browse files Browse the repository at this point in the history
  • Loading branch information
raylu committed Apr 8, 2015
1 parent a0bed8a commit 55abba4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
42 changes: 6 additions & 36 deletions outlauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ccp_pls
import config
import db
import update_entities

app = flask.Flask(__name__)
app.secret_key = config.secret_key
Expand All @@ -42,8 +43,10 @@ def step_2():
# step 1
return flask.render_template('register.html')
else:
key_info = check_api_key(request.form['key_id'], request.form['vcode'])
if not key_info:
try:
key_info = update_entities.check_api_key(request.form['key_id'], request.form['vcode'])
except InvalidAPI as e:
flask.flash(e.message)
return flask.redirect(flask.url_for('register'))
char_id = request.form.get('character_id')
username = request.form.get('username')
Expand All @@ -67,8 +70,7 @@ def step_2():
char_id = int(char_id)
for char in key_info['characters']:
if char['character_id'] == char_id:
update_db_for_char(char)
db.session.commit()
update_entities.update_for_char(char)
break
else:
flask.abort(400)
Expand All @@ -91,38 +93,6 @@ def step_2():
session['user_id'] = user.id
return flask.redirect(flask.url_for('home'))

def check_api_key(key_id, vcode):
key_info = ccp_pls.key_info(key_id, vcode)

error = None
if not key_info:
error = 'API key ID and vCode combination were not valid.'
else:
if key_info['type'] != 'Account':
error = 'API key must be account-wide, not character-specific.'
if key_info['accessmask'] & 65405275 != 65405275:
error = 'API key has insufficient permissions. Please use the create link.'

if error:
flask.flash(error)
else:
return key_info

def update_db_for_char(char):
if char['alliance_id']:
db.session.merge(db.Entity(
id=char['alliance_id'], type='alliance', name=char['alliance_name']))

corporation = db.Entity(
id=char['corporation_id'], type='corporation', name=char['corporation_name'])
if char['alliance_id']:
corporation.parent_id = char['alliance_id']
db.session.merge(corporation)

db.session.merge(db.Entity(
id=char['character_id'], type='character',
name=char['character_name'], parent_id=char['corporation_id']))

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
Expand Down
61 changes: 61 additions & 0 deletions update_entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3

import time

import ccp_pls
import db

def check_api_key(key_id, vcode):
key_info = ccp_pls.key_info(key_id, vcode)

if not key_info:
raise InvalidAPI('API key ID and vCode combination were not valid.')
if key_info['type'] != 'Account':
raise InvalidAPI('API key must be account-wide, not character-specific.')
if key_info['accessmask'] & 65405275 != 65405275:
raise InvalidAPI('API key has insufficient permissions. Please use the create link.')

return key_info

def update_for_char(char):
if char['alliance_id']:
db.session.merge(db.Entity(
id=char['alliance_id'], type='alliance', name=char['alliance_name']))
db.session.commit()

corporation = db.Entity(
id=char['corporation_id'], type='corporation', name=char['corporation_name'])
if char['alliance_id']:
corporation.parent_id = char['alliance_id']
db.session.merge(corporation)
db.session.commit()

db.session.merge(db.Entity(
id=char['character_id'], type='character',
name=char['character_name'], parent_id=char['corporation_id']))
db.session.commit()

class InvalidAPI(Exception):
def __init__(self, message):
self.message = message

def main():
for user in db.session.query(db.User):
try:
key_info = check_api_key(user.apikey_id, user.apikey_vcode)
except InvalidAPI as e:
user.character.parent_id = None
print('%s: %s' % (user.username, e.message))
continue
for char in key_info['characters']:
if char['character_id'] == user.character_id:
update_for_char(char)
break
else:
# couldn't find the character
user.character.parent_id = None
print(user.username + ": Key didn't have the character we were looking for.")
time.sleep(1)

if __name__ == '__main__':
main()

0 comments on commit 55abba4

Please sign in to comment.