class FeatureController(object): ''' classdocs ''' def __init__(self,request): ''' Constructor ''' self.request = request self.dbSession = DBSession() @view_config(route_name='features', request_method='GET', renderer='jsonp', http_cache=0) def listFeatures(self): featureList = [] for feature in self.dbSession.query(Feature).order_by(Feature.name): featureList.append(feature.toJSONObject()) return {'features':featureList} @view_config(route_name='feature.CRUD', request_method='PUT', renderer='jsonp', http_cache=0) def addFeature(self): featureName = self.request.matchdict['featurename'] feature = Feature(featureName) try: self.dbSession.add(feature) self.dbSession.flush() self.dbSession.commit() log.info("create feature: %(featurename)s" % {'featurename':featureName}) except IntegrityError, e: self.dbSession.rollback() self.request.response.status_int = 409 return {'error':e.message} return {'service': feature.toJSONObject()}
class AuthorController(object): ''' Constructor ''' def __init__(self, request): self.request = request self.dbSession = DBSession() ## ## authors ## # GET /v1/authors # # get list of all authors @view_config(route_name='authors', request_method='GET', renderer='jsonp', http_cache=0) def authorsList(self): authorlist = [] for author in self.dbSession.query(Author).order_by(Author.author_name): authorJSON = author.toJSONObject() authorlist.append(authorJSON) return {'authors':authorlist} # GET /v1/authors/{authorname} # # get information about a single author @view_config(route_name='author.CRUD', request_method='GET', renderer='jsonp', http_cache=0) def authorGet(self): authorName = self.request.matchdict['authorname'] try: author = self.dbSession.query(Author).filter_by(author_name=authorName).one() except NoResultFound: self.request.response.status_int = 404 return {'error':'unknown author %s' % authorName} authorJSONObj = author.toJSONObject() authorJSONObj['features'] = getAuthorFeatures(self.dbSession,author.id) return {'author': authorJSONObj} ## ## Create/update/delete author ## # PUT /v1/authors/{authorname} # # create a new author or update an existing author @view_config(route_name='author.CRUD', request_method='PUT', renderer='jsonp', http_cache=0) def authorPut(self): authorname = self.request.matchdict['authorname'] authorInfo = self.request.json_body password = authorInfo.get('password') if password == None: self.request.response.status_int = 400 return {'error':'Missing required property: password'} fullname = authorInfo.get('fullname') if fullname == None: self.request.response.status_int = 400 return {'error':'Missing required property: fullname'} email = authorInfo.get('email') if email == None: self.request.response.status_int = 400 return {'error':'Missing required property: email'} template = authorInfo.get('template') try: author = Author(authorname,email,fullname,password,template) self.dbSession.add(author) self.dbSession.flush() # flush so we can get the id ''' ??? this might only be temporary ??? Create a default group (follow) and add the author to that group so that author is following themselves. ''' authorGroup = AuthorGroup(author.id,DEFAULT_AUTHOR_GROUP) self.dbSession.add(authorGroup) self.dbSession.flush() mapping = AuthorGroupMap(authorGroup.id,author.id) self.dbSession.add(mapping) self.dbSession.flush() ''' Add the new author to the authors access group ''' groupId, = self.dbSession.query(AccessGroup.id).filter_by(group_name=ACCESS_GROUP_AUTHORS).one() authorAccessGroupMap = AuthorAccessGroupMap(author.id,groupId) self.dbSession.add(authorAccessGroupMap) self.dbSession.flush() authorJSON = author.toJSONObject() self.dbSession.commit() log.info("create author %s and added to group %s" % (authorname, ACCESS_GROUP_AUTHORS)) except IntegrityError, e: self.dbSession.rollback() log.error(e.message) self.request.response.status_int = 409 return {'error':e.message} except NoResultFound, e: self.dbSession.rollback() log.error(e.message) self.request.response.status_int = 409 return {'error': e.message}
class AuthorGroupController(object): ''' Constructor ''' def __init__(self, request): self.request = request self.dbSession = DBSession() # GET /v1/authors/{authorname}/groups # # return all authors that match the specified search criteria # @view_config(route_name='author.groups', request_method='GET', renderer='jsonp', http_cache=0) def listGroupsHndlr(self): authorName = self.request.matchdict['authorname'] try: authorId, = self.dbSession.query(Author.id).filter(Author.author_name==authorName).one() except NoResultFound: self.request.response.status_int = 404 return {'error':'unknown author %s' % authorName} groupList = [] for group in self.dbSession.query(AuthorGroup).filter(AuthorGroup.author_id==authorId).order_by(AuthorGroup.group_name): groupList.append(group.toJSONObject()) return {'author_id': authorId, 'groups': groupList} # GET /v1/authors/{authorname}/groups/{groupname} # @view_config(route_name='author.groups.CRUD', request_method='GET', renderer='jsonp', http_cache=0) def getGroupDetailHndlr(self): authorName = self.request.matchdict['authorname'] groupName = self.request.matchdict['groupname'] try: authorId, = self.dbSession.query(Author.id).filter_by(author_name=authorName).one() except NoResultFound: self.request.response.status_int = 404 return {'error':'unknown author %s' % authorName} try: authorGroup = self.dbSession.query(AuthorGroup).filter(and_(AuthorGroup.author_id==authorId,AuthorGroup.group_name==groupName)).one() except NoResultFound: self.request.response.status_int = 404 return {'error':'unknown group %s for author %s' % (groupName,authorName)} return authorGroup.toJSONObject() # PUT /v1/authors/{authorname}/groups/{groupname} # @view_config(route_name='author.groups.CRUD', request_method='PUT', renderer='jsonp', http_cache=0) def addUpdateGroupHndlr(self): authorName = self.request.matchdict['authorname'] groupName = self.request.matchdict['groupname'] try: authorId, = self.dbSession.query(Author.id).filter_by(author_name=authorName).one() except NoResultFound: self.request.response.status_int = 404 return {'error':'unknown author %s' % authorName} authorGroup = AuthorGroup(authorId,groupName) jsonObject = None try: self.dbSession.add(authorGroup) self.dbSession.flush() jsonObject = authorGroup.toJSONObject() self.dbSession.commit() log.info("created author_group: %s" % authorGroup) except IntegrityError, e: self.dbSession.rollback() self.request.response.status_int = 409 return {'error':e.message} return jsonObject