diff --git a/app/index.html b/app/index.html index 69ffe87..870e71c 100755 --- a/app/index.html +++ b/app/index.html @@ -210,6 +210,7 @@ Hello, {{name}}! +
Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.
Sketch ID | +File Owner | +Sketch Version | +Parent Sketch | +Sketch Name | +Sketch Description | +||
{{files.data.sketchId}} | @@ -366,16 +377,16 @@
Sketch ID | +File Owner | +Sketch Version | +Parent Sketch | +Sketch Name | +Sketch Description | +
{{files.data.sketchId}} | diff --git a/app/js/angular/sketch-controller.js b/app/js/angular/sketch-controller.js index 1a85895..4d717c5 100644 --- a/app/js/angular/sketch-controller.js +++ b/app/js/angular/sketch-controller.js @@ -54,7 +54,7 @@ function SketchController($scope,$resource){ $scope.item.data = {"sketchId":"", "version":"", "original":"", "owner":"", "fileName":"", "fileData":"", "changeDescription":"", "permissions":""}; $scope.item.data.sketchId = ""; - $scope.item.data.version = parseInt("1", 10); + //$scope.item.data.version = parseInt("1", 10); $scope.item.data.original = $scope.sketchId + ":" + $scope.version; @@ -77,7 +77,7 @@ function SketchController($scope,$resource){ $scope.item.data = {"sketchId":"", "version":"", "original":"", "owner":"", "fileName":"", "fileData":"", "changeDescription":"", "permissions":""}; $scope.item.data.sketchId = $scope.sketchId; - $scope.item.data.version = parseInt($scope.version, 10) + 1; + //$scope.item.data.version = parseInt($scope.version, 10) + 1; $scope.item.data.original = $scope.sketchId + ":" + $scope.version; $scope.item.data.owner = $scope.owner; $scope.item.data.fileName = $scope.fileName; @@ -107,6 +107,14 @@ function SketchController($scope,$resource){ $scope.name = l.displayName; $scope.etag = l.result; } + + $scope.logout = function() { + $scope.name = "Anonymous User"; + $scope.etag = "" + + var authorizeButton = document.getElementById('authorize-button'); + authorizeButton.style.visibility = ''; + } /* General Add/List (pass "model" to m_type) diff --git a/backend.py b/backend.py index 8e2e5db..f18428d 100644 --- a/backend.py +++ b/backend.py @@ -46,13 +46,24 @@ def add(apikey, model, data): else: modelCount = ModelCount(en_type='Backend', apikey=apikey, model=model, count=1) modelCount.put() - - #For sketch files saved through "Save As" + + #For sketch files saved through "Save As" if jsonData['sketchId'] == '': jsonData['sketchId'] = str(modelCount.count) - #For sketch files saved through "Save As" that were not derived from another file + #For sketch files saved through "Save As" that were not derived from another file if jsonData['original'] == ':': jsonData['original'] = 'original' + + #update VersionCount when adding + versionCount = VersionCount.all().filter('sketchId', int(jsonData['sketchId'])).get() + if versionCount: + versionCount.lastVersion += 1 + versionCount.put() + else: + versionCount = VersionCount(sketchId=int(jsonData['sketchId']), lastVersion=1) + versionCount.put() + + jsonData['version'] = str(versionCount.lastVersion) entity = Backend(apikey=apikey, model=model, @@ -143,187 +154,32 @@ def clearapikey(apikey): #You can't name it delete since db.Model already has a delete method @staticmethod def remove(apikey, model, model_id): - #update model count when deleting - entity = Backend.get_by_id(int(model_id)) - - if entity and entity.apikey == apikey and entity.model == model: - entity.delete() - - result = {'method':'delete_model_success', - 'apikey': apikey, - 'model': model, - 'id': model_id - } - else: - result = {'method':'delete_model_not_found'} - - modelCount = ModelCount.all().filter('en_type','Backend').filter('apikey',apikey).filter('model', model).get() - if modelCount: - modelCount.count -= 1 - modelCount.put() - - return result - - #data is a dictionary that must be merged with current json data and stored. - @staticmethod - def edit_entity(apikey, model, model_id, data): - jsonString = data + #update model count when deleting entity = Backend.get_by_id(int(model_id)) - entity.jsonString = jsonString - entity.put() - if entity.jsonString: - data = json.loads(entity.jsonString) - else: - data = {} - result = {'model':model, - 'apikey': apikey, - 'id': entity.key().id(), - 'data': data #this would also check if the json submitted was valid - } - return result - -""" -Attributes and functions for User entity -class User(db.Model): - apikey = db.StringProperty(required=True,default='Default-APIKey') - model = db.StringProperty(required=True,default='Default-Model') - #Use backend record id as the model id for simplicity - jsonString = db.TextProperty(required=True,default='{}') - created = db.DateTimeProperty(auto_now_add=True) #The time that the model was created - modified = db.DateTimeProperty(auto_now=True) - - def to_dict(self): - d = dict([(p, unicode(getattr(self, p))) for p in self.properties()]) - d["id"] = self.key().id() - return d - - - @staticmethod - def add(apikey, model, data): - #update ModelCount when adding - jsonString = data - entity = User(apikey=apikey, - model=model, - jsonString=jsonString) - - entity.put() - modelCount = ModelCount.all().filter('en_type','User').filter('apikey',apikey).filter('model', model).get() - if modelCount: - modelCount.count += 1 - modelCount.put() - else: - modelCount = ModelCount(en_type='User', apikey=apikey, model=model, count=1) - modelCount.put() - - result = {'model':model, - 'apikey': apikey, - 'id': entity.key().id(), - 'data': json.loads(jsonString)} #this would also check if the json submitted was valid - - return result - - @staticmethod - def get_entities(apikey, model=None, offset=0, limit=50): - #update ModelCount when adding - theQuery = User.all().filter('apikey',apikey) - if model: - theQuery = theQuery.filter('model', model) - - objects = theQuery.fetch(limit=limit, offset=offset) - - entities = [] - for object in objects: - entity = {'model':object.model, - 'apikey': apikey, - 'id': object.key().id(), - 'created': object.created, - 'modified': object.modified, - 'data': json.loads(object.jsonString)} - entities.append(entity) - count = 0 - modelCount = ModelCount.all().filter('en_type','User').filter('apikey',apikey).filter('model', model).get() - if modelCount: - count = modelCount.count - result = {'method':'get_entities', - 'apikey': apikey, - 'en_type': 'User', - 'model': model, - 'count': count, - 'offset': offset, - 'limit':limit, - 'entities': entities} - return result + if entity and entity.apikey == apikey and entity.model == model: + entity.delete() - @staticmethod - def get_entity(apikey,model,model_id): - theobject = User.get_by_id(int(model_id)) - - result = {'method':'get_model', + result = {'method':'delete_model_success', 'apikey': apikey, 'model': model, - 'id': model_id, - 'data': json.loads(theobject.jsonString) + 'id': model_id } - return result - - @staticmethod - def clear(apikey, model): - #update model count when clearing model on api - count = 0 - for object in User.all().filter('apikey',apikey).filter('model', model): - count += 1 - object.delete() - - modelCount = ModelCount.all().filter('en_type','User').filter('apikey',apikey).filter('model', model).get() + else: + result = {'method':'delete_model_not_found'} + + modelCount = ModelCount.all().filter('en_type','Backend').filter('apikey',apikey).filter('model', model).get() if modelCount: - modelCount.delete() - result = {'items_deleted': count} - return result + modelCount.count -= 1 + modelCount.put() - @staticmethod - def clearapikey(apikey): - #update model count when clearing model on api - count = 0 - for object in User.all().filter('apikey',apikey): - count += 1 - object.delete() - - modelCount = ModelCount.all().filter('en_type','User').filter('apikey',apikey).get() - if modelCount: - modelCount.delete() - result = {'items_deleted': count} return result - - #You can't name it delete since db.Model already has a delete method - @staticmethod - def remove(apikey, model, model_id): - #update model count when deleting - entity = User.get_by_id(int(model_id)) - - if entity and entity.apikey == apikey and entity.model == model: - entity.delete() - - result = {'method':'delete_model_success', - 'apikey': apikey, - 'model': model, - 'id': model_id - } - else: - result = {'method':'delete_model_not_found'} - - modelCount = ModelCount.all().filter('en_type','User').filter('apikey',apikey).filter('model', model).get() - if modelCount: - modelCount.count -= 1 - modelCount.put() - - return result #data is a dictionary that must be merged with current json data and stored. @staticmethod def edit_entity(apikey, model, model_id, data): jsonString = data - entity = User.get_by_id(int(model_id)) + entity = Backend.get_by_id(int(model_id)) entity.jsonString = jsonString entity.put() if entity.jsonString: @@ -335,9 +191,8 @@ def edit_entity(apikey, model, model_id, data): 'id': entity.key().id(), 'data': data #this would also check if the json submitted was valid } - return result -""" - + return result + #Quick retrieval for supported models metadata and count stats class ModelCount(db.Model): apikey = db.StringProperty(required=True,default='Default-APIKey') @@ -347,6 +202,9 @@ class ModelCount(db.Model): created = db.DateTimeProperty(auto_now_add=True) #The time that the model was created modified = db.DateTimeProperty(auto_now=True) +class VersionCount(db.Model): + sketchId = db.IntegerProperty(required=True, default=0) + lastVersion = db.IntegerProperty(required=True, default=0) class ActionHandler(webapp.RequestHandler): """Class which handles bootstrap procedure and seeds the necessary @@ -367,14 +225,14 @@ def respond(self,result): #Add a handler to automatically convert datetimes to ISO 8601 strings. dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None if callback: - content = str(callback) + '(' + json.dumps(result,default=dthandler) + ')' - return self.response.out.write(content) - + content = str(callback) + '(' + json.dumps(result,default=dthandler) + ')' + return self.response.out.write(content) + return self.response.out.write(json.dumps(result,default=dthandler)) def metadata(self,apikey): - #Fetch all ModelCount records for apikey to produce metadata on currently supported models. - models = [] + #Fetch all ModelCount records for apikey to produce metadata on currently supported models. + models = [] for mc in ModelCount.all().filter('apikey',apikey): models.append({'model':mc.model, 'count': mc.count}) @@ -384,7 +242,7 @@ def metadata(self,apikey): 'count': len(models), 'entities': models } - + return self.respond(result) #Dump apikey table @@ -430,21 +288,21 @@ def backup_test(self,apikey): def clear_apikey(self,apikey): """Clears the datastore for a an apikey. - """ + """ result = Backend.clearapikey(apikey) return self.respond({'method':'clear_apikey'}) def clear_model(self,apikey, model): """Clears the datastore for a model and apikey. """ - result = Backend.clear(apikey, model) + result = Backend.clear(apikey, model) return self.respond(result) def add_or_list_model(self,apikey,model): - #Check for GET paramenter == model to see if this is an add or list. - #Call Backend.add(apikey, model, data) or + #Check for GET paramenter == model to see if this is an add or list. + #Call Backend.add(apikey, model, data) or #Fetch all models for apikey and return a list. - + #Todo - Check for method. logging.info(self.request.method) if self.request.method=="POST": @@ -467,18 +325,18 @@ def add_or_list_model(self,apikey,model): result = Backend.get_entities(apikey, model,offset=offset) - return self.respond(result) + return self.respond(result) def delete_model(self,apikey,model, model_id): - result = Backend.remove(apikey,model, model_id) - - return self.respond(result) + result = Backend.remove(apikey,model, model_id) + + return self.respond(result) def get_or_edit_model(self,apikey,model, model_id): - #Check for GET parameter == model to see if this is a get or an edit - #technically the apikey and model are not required. - #To create an error message if the id is not from this apikey? - logging.info("**********************") + #Check for GET parameter == model to see if this is a get or an edit + #technically the apikey and model are not required. + #To create an error message if the id is not from this apikey? + logging.info("**********************") logging.info(self.request.method) logging.info("**********************") @@ -496,13 +354,13 @@ def get_or_edit_model(self,apikey,model, model_id): #result = json.loads(self.request.body) #logging.info(result) return self.respond(result)#(result) - else: + else: data = self.request.get("obj") - if data: - result = Backend.edit_entity(apikey,model,model_id,data) - else: - result = Backend.get_entity(apikey,model,model_id) - return self.respond(result) + if data: + result = Backend.edit_entity(apikey,model,model_id,data) + else: + result = Backend.get_entity(apikey,model,model_id) + return self.respond(result) application = webapp.WSGIApplication([ webapp.Route('/