Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raylu committed Oct 16, 2014
1 parent 2c0201f commit 8586cc1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 deletions.
32 changes: 15 additions & 17 deletions ccp_pls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,27 @@ def key_info(key_id, vcode):
})
return info

rs = requests.Session()

def alliance_contact_list(key_id, key_vcode, char_id=None):
contacts = []
xml = query('/char/ContactList.xml.aspx', 1188900,'cJvDc3cuvWAuQUKGpDB1Dh44r8bHUTnN6E8SQ9laUIUoMhxstLyyeNbIGg2MqUfv', 92301442)
def alliance_contact_list(key_id, key_vcode, char_id):
xml = query('/char/ContactList.xml.aspx', key_id, key_vcode, char_id)
if not xml:
return
cached_until = datetime.strptime(xml.find('cachedUntil').text, '%Y-%m-%d %H:%M:%S')
current_time = datetime.strptime(xml.find('currentTime').text, '%Y-%m-%d %H:%M:%S')
if timedelta(minutes=15) == (cached_until - current_time):
result = xml.find('result')
for row in result.findall('./rowset[@name="allianceContactList"]/'):
contacts.append({
'id' : int(row.get('contactID')),
'contact_name': row.get('contactName'),
'standing': float(row.get('standing')),
'type_id': int(row.get('contactTypeID')),
})
return contacts
return None
if timedelta(minutes=15) != (cached_until - current_time):
return None
result = xml.find('result')
contacts = []
for row in result.findall('./rowset[@name="allianceContactList"]/'):
contacts.append({
'id' : int(row.get('contactID')),
'contact_name': row.get('contactName'),
'standing': float(row.get('standing')),
'type_id': int(row.get('contactTypeID')),
})
return contacts

rs = requests.Session()
def query(endpoint, key_id, vcode, char_id=None):
response = rs.get(base_url + endpoint, params={'keyID': key_id, 'vCode': vcode, 'characterID': char_id})
xml = ElementTree.fromstring(response.content)
return xml

8 changes: 4 additions & 4 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ def __ne__(self, other):
def init_db():
Base.metadata.create_all(bind=engine)
session.add_all([
Group(Group.diplo),
Group(Group.ilaw),
Group(Group.militia),
Group(Group.allies),
Group.diplo,
Group.ilaw,
Group.militia,
Group.allies,
])
session.commit()
def drop_db():
Expand Down
52 changes: 24 additions & 28 deletions outlauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ def contacts():
if db.Group.diplo not in groups:
return flask.redirect(flask.url_for('home'))
if request.method == 'GET':
contacts = []
for x in db.session.query(db.Contact):
contacts.append(x)
contacts = db.session.query(db.Contact)
if request.method == 'POST':
save_contacts(request.form)
return flask.redirect(flask.url_for('contacts'))
Expand All @@ -219,15 +217,12 @@ def contacts():


def save_contacts(form):
db_contacts = []
form_contacts = defaultdict(list)
changed = []
for contact in db.session.query(db.Contact):
db_contacts.append(contact)
for contact in form:
form_contacts[int(contact)].append(form[contact])
for contact in db_contacts:
if (form_contacts[contact.id][0] != contact.comments):
for contact in db.session.query(db.Contact):
if form_contacts[contact.id][0] != contact.comments:
changed.append(db.Contact(
id=contact.id,
name=contact.name,
Expand All @@ -236,48 +231,49 @@ def save_contacts(form):
comments=form_contacts[contact.id][0],
))


for contact in changed:
db.session.merge(db.Contact(
id=int(contact.id),
comments=contact.comments,
))
db.session.commit()



@app.route('/update_contacts', methods=(['POST']))
def update_contacts():
user=get_current_user()
changed=[]
removed=[]
db_standings = {}
api_standings = {}
user = get_current_user()
api_contacts = ccp_pls.alliance_contact_list(user.apikey_id, user.apikey_vcode, user.character_id)
if api_contacts == None:
if api_contacts is None:
return flask.redirect(flask.url_for('contacts'))

api_standings = {}
for contact in api_contacts:
api_standings[contact['id']].append(db.Contact(
api_standings[contact['id']] = db.Contact(
id=contact['id'],
name=contact['contact_name'],
standing=contact['standing'],
type_id=contact['type_id']
))
db_contacts = db.session.query(db.Contact)
for contact in db_contacts:
db_standings[contact.id].append(contact)
for contact in db_standings.items():
if contact != api_standings[contact[id]]:
if (api_standings[contact].id == []):
removed.append(contact)
else:
)

changed = []
removed = []
for contact in db.session.query(db.Contact):
try:
api_standing = api_standings[contact.id].standing
except KeyError:
removed.append(contact)
else:
if contact.standing != api_standing:
contact.standing = api_standing
changed.append(contact)
del api_standings[contact.id]

db.session.add_all(api_standings.values())
for contact in changed:
db.session.merge(api_standings[contact.id])
for contact in removed:
pass
db.session.delete(contact)
db.session.commit()

return flask.redirect(flask.url_for('contacts'))

def get_current_user():
Expand Down

0 comments on commit 8586cc1

Please sign in to comment.