Skip to content

Commit

Permalink
Merge pull request #217 from Cal-CS-61A-Staff/version_download
Browse files Browse the repository at this point in the history
better downloading from version
  • Loading branch information
sharadmv committed Sep 30, 2014
2 parents d1a23bf + 46c53bc commit 08789fa
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 25 deletions.
2 changes: 1 addition & 1 deletion client/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.12'
__version__ = 'v1.0.12'
11 changes: 5 additions & 6 deletions client/cli/ok.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ def send_to_server(access_token, messages, name, server,
except error.HTTPError as ex:
# print("Error while sending to server: {}".format(ex))
try:
#response_json = json.loads(response)
if ex.code == 403:
get_latest_version(server)
response = ex.read().decode('utf-8')
response_json = json.loads(response)
get_latest_version(response_json['data']['download_link'])
#message = response_json['message']
#indented = '\n'.join('\t' + line for line in message.split('\n'))
#print(indented)
Expand All @@ -86,17 +87,15 @@ def send_to_server(access_token, messages, name, server,
# Software Updating #
#####################

def get_latest_version(server):
def get_latest_version(download_link):
"""Check for the latest version of ok and update this file accordingly.
"""
#print("We detected that you are running an old version of ok.py: {0}".format(VERSION))

# Get server version
address = "https://" + server + "/api/v1" + "/version/ok/download"

try:
#print("Updating now...")
req = request.Request(address)
req = request.Request(download_link)
response = request.urlopen(req)

zip_binary = response.read()
Expand Down
3 changes: 2 additions & 1 deletion server/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
DEBUG = (os.environ['SERVER_SOFTWARE'].startswith('Dev')
if 'SERVER_SOFTWARE' in os.environ
else True)
if len(list(models.Course.query().filter(models.Course.name == 'CS 61A'))) == 0:
TESTING = os.environ["FLASK_CONF"] == "TEST" if "FLASK_CONF" in os.environ else False
if DEBUG and not TESTING and len(list(models.Course.query().filter(models.Course.name == 'CS 61A'))) == 0:
seed()

if DEBUG:
Expand Down
25 changes: 24 additions & 1 deletion server/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from flask.views import View
from flask.app import request, json
from flask import session, make_response
from flask import session, make_response, redirect
from webargs import Arg
from webargs.flaskparser import FlaskParser

Expand Down Expand Up @@ -557,6 +557,12 @@ class VersionAPI(APIResource):
'current': BooleanArg()
}
},
'download': {
'methods': set(['GET']),
'web_args': {
'version': Arg(str)
}
},
'current': {
'methods': set(['GET']),
'web_args': {
Expand Down Expand Up @@ -587,11 +593,28 @@ def new(self, obj, user, data):
return obj

def current(self, obj, user, data):
need = Need('get')
if not obj.can(user, need, obj):
raise need.exception()
if not obj.current_version:
raise BadValueError("Invalid version resource. Contact an administrator.")
return obj.current_version

def download(self, obj, user, data):
need = Need('get')
if not obj.can(user, need, obj):
raise need.exception()
if 'version' not in data:
download_link = obj.download_link()
else:
download_link = obj.download_link(data['version'])
return redirect(download_link)


def set_current(self, obj, user, data):
need = Need('update')
if not obj.can(user, need, obj):
raise need.exception()
current_version = data['version']
if current_version not in obj.versions:
raise BadValueError("Invalid version. Cannot update to current.")
Expand Down
5 changes: 3 additions & 2 deletions server/app/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ def __init__(self, supplied_version, correct_version):
def message(self):
return ("Incorrect client version. Supplied version was {}. "
"Correct version is {}.".format(self.supplied_version,
self.correct_version))
self.correct_version.current_version))

@property
def data(self):
return {
'supplied': self.supplied_version,
'correct': self.correct_version
'correct': self.correct_version.current_version,
'download_link': self.correct_version.download_link()
}

15 changes: 12 additions & 3 deletions server/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

from app import app
from app.needs import Need
from app.exceptions import *
from flask import json
from flask.json import JSONEncoder as old_json

from google.appengine.ext import db, ndb

BadValueError = db.BadValueError

# To deal with circular imports
class APIProxy(object):
Expand Down Expand Up @@ -408,11 +408,20 @@ class Version(Base):
current_version = ndb.StringProperty()
base_url = ndb.StringProperty(required=True)

def download_link(self, version=None):
if version is None:
if not self.current_version:
raise BadValueError("current version doesn't exist")
return '/'.join((self.base_url, self.current_version,
self.name))
if version not in self.versions:
raise BadValueError("specified version %s doesn't exist" % version)
return '/'.join((self.base_url, version, self.name))

def to_json(self, fields=None):
json = super(Version, self).to_json(fields)
if self.current_version:
json['current_download_link'] = '/'.join((
self.base_url, self.current_version, self.name))
json['current_download_link'] = self.download_link()

return json

Expand Down
7 changes: 4 additions & 3 deletions server/app/seed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def make_fake_submission(assignment, submitter):

def make_version(current_version):
return models.Version(
name='okpy',
base_url='github.com',
name='ok',
id='ok',
base_url='https://github.com/Cal-CS-61A-Staff/ok/releases/download',
versions=[current_version],
current_version=current_version
)
Expand All @@ -72,7 +73,7 @@ def make_version(current_version):
login="albert",
role="admin"
)
version = make_version('1.0.11')
version = make_version('v1.0.11')
version.put()
c.put()
course = make_fake_course(c)
Expand Down
13 changes: 5 additions & 8 deletions server/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import traceback
import collections

import werkzeug
from flask import render_template, session, request, Response

from google.appengine.api import users
Expand Down Expand Up @@ -53,12 +54,8 @@ def check_version(client):

if latest is None or latest.current_version is None:
raise APIException('Current version of ok not found')
latest = latest.current_version

# If it returned a response, and not a string
if not isinstance(latest, (str, unicode)):
raise RuntimeError(latest)
if client != latest:
if client != latest.current_version:
raise IncorrectVersionError(client, latest)


Expand Down Expand Up @@ -88,13 +85,13 @@ def api_wrapper(*args, **kwds):

rval = view(*args, **kwds)

if isinstance(rval, list):
if isinstance(rval, Response) or isinstance(rval, werkzeug.wrappers.Response):
pass
elif isinstance(rval, list):
rval = utils.create_api_response(200, message, rval)
elif (isinstance(rval, collections.Iterable)
and not isinstance(rval, dict)):
rval = utils.create_api_response(*rval)
elif isinstance(rval, Response):
pass
else:
rval = utils.create_api_response(200, message, rval)

Expand Down
13 changes: 13 additions & 0 deletions server/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ indexes:
properties:
- name: created

- kind: Submission
properties:
- name: assignment
- name: created
direction: desc

- kind: Submission
properties:
- name: assignment
- name: submitter
- name: created
direction: desc

- kind: Submission
properties:
- name: submitter
Expand Down

0 comments on commit 08789fa

Please sign in to comment.