Skip to content

Commit

Permalink
Merge pull request #6 from adamjmcgrath/feature/1-change-average-scor…
Browse files Browse the repository at this point in the history
…e-calculation

Feature/1 change average score calculation
  • Loading branch information
adamjmcgrath committed Jun 13, 2015
2 parents 4b5936c + 006495e commit 4429d81
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 21 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# EditorConfig

# top-most EditorConfig file
root = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py}]
charset = utf-8
indent_style = space
indent_size = 2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ src/pylib
src/secrets.py
npm-debug.log
node_modules
.idea
.env

4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ python:
env:
global:
- secure: KTWs8jE9COxyuULlAiynxrI61wKfZhz56B99zT75e4tdcCSGRIIWut8Skm1thAUSQsEymXhvhhmgND3qbjChyw90F/hAiA8xgo/QxpESvI48AXi9CLPfB9kyaf9SPqXokKxj0yYqCCYLLfmCu/glO66S69+5iQbwn8M+9WHdeHwYffJrFJonUt/E0HAuaNdmJIDCd3nuAp7Ol7JAuUsg8971SoeDqk+wBPNzN4UWYtpWGvxUas51w7YjIPwG7cr6C3XvP/G1x2YPU6yP0gQBtgKt3X3+W6FJqO1GZaiQ1iTK+nRhaU1SecjmkHJJKQjQDX+0CLVWW5Y98SrbtKYht/Ez3/3GG4NyHTtmBZ67F4cC3ikdTRDp3ixTAoay7M4osIgvkdNsCeiYGz8dXvIbOsh+bebCPHwYfdsVk0656XS3V2G5EnXMhqaOzDqdwdCMCUR/LUm4GOmZ2BqNAQT/CnsWugE7BZwyN9I8S1bVMfU0a4soXNAlXbr6Kzr01cdX7Po43y7GOe4QgQHMJuNrFzbJ24IT/OOKJKkgRRfUQoX3LQHH++EHUnKpeTDrT72MRp095jiRW5gjUVjidTtYG0YruZ4PFv66ICKHEmVEm57ATxjupea/goESWslp1/Vj2XV3mKhVcGwJ52WHV2czh8EEu/G16XZMHqwFDjIOg6c=
- APPENGINE_SDK=google_appengine_1.9.19
- APPENGINE_SDK=google_appengine_1.9.21

cache:
directories:
- node_modules
- bower_components

before_install:
- openssl aes-256-cbc -K $encrypted_aac91bacce06_key -iv $encrypted_aac91bacce06_iv -in src/secrets.py.enc -out src/secrets.py -d
- openssl aes-256-cbc -K $encrypted_1d9196d3864d_key -iv $encrypted_1d9196d3864d_iv -in src/secrets.py.enc -out src/secrets.py -d

install:
- pip install -r requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
from fabric.api import *

APPENGINE_PATH = os.environ['APPENGINE_SRC']
APPENGINE_PATH = os.path.abspath(os.environ['APPENGINE_SRC'])
APPENGINE_DEV_APPSERVER = os.path.join(APPENGINE_PATH, 'dev_appserver.py')
APPENGINE_APP_CFG = os.path.join(APPENGINE_PATH, 'appcfg.py')

Expand Down
63 changes: 63 additions & 0 deletions scripts/fix_average_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python
# coding=utf-8
#
# Copyright Friday Film Club. All Rights Reserved.

"""Update average score so that archive questions aren't included."""

import os
import sys
import getpass
import datetime

APPENGINE_PATH = os.path.abspath(os.environ['APPENGINE_SRC'])
APPENGINE_DEV_APPSERVER = os.path.join(APPENGINE_PATH, 'dev_appserver.py')
APPENGINE_APP_CFG = os.path.join(APPENGINE_PATH, 'appcfg.py')

sys.path.append(APPENGINE_PATH)
import dev_appserver
dev_appserver.fix_sys_path()

from google.appengine.ext import ndb
from google.appengine.ext.remote_api import remote_api_stub

import models

APP_NAME = 's~ffcapp'
os.environ['AUTH_DOMAIN'] = 'gmail.com'
os.environ['USER_EMAIL'] = '[email protected]'


def auth_func():
return os.environ['USER_EMAIL'], getpass.getpass('Password:')


def fix_user(u):
uqs = [uq for uq in models.UserQuestion.query(
models.UserQuestion.user==u.key,
models.UserQuestion.complete==True,
ndb.OR(models.UserQuestion.score==0, models.UserQuestion.score==None))]

correction = 0

for uq in uqs:
if (uq.created and
(uq.created - uq.question.get().posed) > datetime.timedelta(days=7)):
correction += 1

u.active_questions_answered = u.questions_answered - correction
print '%s: old: %d, new: %d' % (u.username, u.questions_answered, u.active_questions_answered)
if correction > 1:
u.put()


def main():
# Use 'localhost:8080' for dev server.
remote_api_stub.ConfigureRemoteDatastore(APP_NAME, '/_ah/remote_api',
auth_func, servername='ffcapp.appspot.com')

for u in models.User.query():
fix_user(u)

if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions src/api/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_google_contacts(self):
if refresh_token:

# Convert the access token to refresh_token
app_id, app_secret, app_scope = secrets.AUTH_CONFIG['google']
app_id, app_secret, app_scope = secrets.get_auth_config('google', 'prod')
data = urllib.urlencode({
'client_id': app_id,
'client_secret': app_secret,
Expand Down Expand Up @@ -99,7 +99,7 @@ def get_google_contacts(self):

def get_facebook_friends(self):
u = self.current_user
app_id, app_secret, app_scope = secrets.AUTH_CONFIG['facebook']
app_id, app_secret, app_scope = secrets.get_auth_config('facebook', 'prod')
# facebook_token = u.facebook_token
facebook_token = app_id + '|' + app_secret
facebook_uid = u.facebook_uid
Expand Down
6 changes: 4 additions & 2 deletions src/api/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ def update_question(question):
deferred.defer(leaderboard.delete_leaderboard_cache)


def update_users_score(user, score, num_guesses):
def update_users_score(user, score, num_guesses, is_current):
user.overall_score += score
user.overall_clues += num_guesses - 1
user.questions_answered += 1
if is_current:
user.active_questions_answered += 1


def update_users_season_score(user_season, score, num_guesses):
Expand Down Expand Up @@ -118,7 +120,7 @@ def get_or_post(self, question_id):
user_season = None

# Update the users score and stats.
update_users_score(user, score, num_guesses)
update_users_score(user, score, num_guesses, question.is_current)

# Update the question answer count and reset the leaderboard cache.
update_question(question)
Expand Down
3 changes: 2 additions & 1 deletion src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def _callback_uri_for(self, provider):

def _get_consumer_info_for(self, provider):
"""Returns a tuple (key, secret) for auth init requests."""
return secrets.AUTH_CONFIG[provider]
return secrets.get_auth_config(
provider, settings.get_environment(self.request.host))

def _to_user_model_attrs(self, data, provider, new_user):
attrs_map = self.USER_ATTRS[provider]
Expand Down
4 changes: 2 additions & 2 deletions src/cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ cron:
url: /tasks/cleanupanonymoususers
schedule: every 24 hours

- description: delete obsolete anonymous users
url: /tasks/cleanupanonymoususers
- description: clean up obsolete user tokens
url: /tasks/cleanupusertokens
schedule: every monday 05:00

- description: weekly backup
Expand Down
1 change: 1 addition & 0 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class User(AuthUser):
overall_score = ndb.IntegerProperty(default=0)
overall_clues = ndb.IntegerProperty(default=0)
questions_answered = ndb.IntegerProperty(default=0)
active_questions_answered = ndb.IntegerProperty(default=0)
invited_by = ndb.KeyProperty(kind='User')
joined = ndb.DateTimeProperty(auto_now_add=True)
leagues = ndb.KeyProperty(repeated=True)
Expand Down
9 changes: 5 additions & 4 deletions src/secrets.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
'twitter': (TWITTER_CONSUMER_KEY_STAGING, TWITTER_CONSUMER_SECRET_STAGING),
}

AUTH_CONFIG = ({
'local': _CONFIG_DEV,
'staging': _CONFIG_STAGING,
}).get(settings.ENVIRONMENT, _CONFIG)
def get_auth_config(provider, environment):
return ({
'local': _CONFIG_DEV,
'staging': _CONFIG_STAGING,
}).get(environment, _CONFIG)[provider]
Binary file modified src/secrets.py.enc
Binary file not shown.
12 changes: 7 additions & 5 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
DEBUG = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev')
FMJ_EMAIL_SHORT = '[email protected]'
FMJ_EMAIL = 'Film Master Jack <%s>' % FMJ_EMAIL_SHORT
ENVIRONMENT = ({
'www.fridayfilmclub.com': 'prod',
'ffcapp.appspot.com': 'prod',
'dev.ffcapp.appspot.com': 'staging',
}).get(os.environ.get('HTTP_HOST'), 'local')

def get_environment(host):
return ({
'www.fridayfilmclub.com': 'prod',
'ffcapp.appspot.com': 'prod',
'dev.ffcapp.appspot.com': 'staging',
}).get(host, 'local')
1 change: 1 addition & 0 deletions src/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def user(id=None,
overall_score=0,
overall_clues=0,
questions_answered=0,
active_questions_answered=0,
invited_by=None,
joined=None,
leagues=None):
Expand Down
4 changes: 3 additions & 1 deletion src/tests/test_api_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,13 @@ def testUpdateUserScore(self):
overall_score=10,
overall_clues=10,
questions_answered=10,
active_questions_answered=5,
)
question_api.update_users_score(user, 5, 5)
question_api.update_users_score(user, 5, 5, True)
self.assertEqual(user.overall_score, 15)
self.assertEqual(user.overall_clues, 14)
self.assertEqual(user.questions_answered, 11)
self.assertEqual(user.active_questions_answered, 6)

def testUpdateUsersLeagueScore(self):
user = helpers.user()
Expand Down
3 changes: 2 additions & 1 deletion src/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from google.appengine.api import urlfetch


consumer_token, consumer_token_secret = secrets.AUTH_CONFIG['twitter']
# TODO (adamjmcgrath) don't hardcode environment.
consumer_token, consumer_token_secret = secrets.get_auth_config('twitter', 'prod')


def timestamp():
Expand Down

0 comments on commit 4429d81

Please sign in to comment.