def listAuthorServices(self): authorName = self.request.matchdict["authorname"] dbSession = DBSession() try: authorId, = dbSession.query(Author.id).filter_by(author_name=authorName).one() except NoResultFound: self.request.response.status_int = 404 return {"error": "unknown author %s" % authorName} services = [] for service in ( dbSession.query(Service) .join(AuthorServiceMap) .filter(Service.id == AuthorServiceMap.service_id) .join(Author) .filter(AuthorServiceMap.author_id == authorId) .order_by(Service.service_name) ): services.append( { "service_id": service.id, "name": service.service_name, "color_icon_high_res": self.request.static_url("miapi:%s" % service.color_icon_high_res), "color_icon_medium_res": self.request.static_url("miapi:%s" % service.color_icon_medium_res), "color_icon_low_res": self.request.static_url("miapi:%s" % service.color_icon_low_res), "mono_icon_high_res": self.request.static_url("miapi:%s" % service.mono_icon_high_res), "mono_icon_medium_res": self.request.static_url("miapi:%s" % service.mono_icon_medium_res), "mono_icon_low_res": self.request.static_url("miapi:%s" % service.mono_icon_low_res), } ) return {"author_name": authorName, "services": services}
def getAuthorServiceInfo(self): authorName = self.request.matchdict['authorname'] serviceName = self.request.matchdict['servicename'] dbSession = DBSession() try: authorId, = 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: serviceId, = dbSession.query( Service.id).filter_by(service_name=serviceName).one() except NoResultFound: self.request.response.status_int = 404 return {'error': 'unknown service %s' % serviceName} try: asm = dbSession.query(AuthorServiceMap).filter_by( author_id=authorId, service_id=serviceId).one() except NoResultFound: self.request.response.status_int = 404 return {'error': 'unknown service for author'} response = { 'author': authorName, 'service': serviceName, 'last_update_time': datetime.isoformat(asm.last_update_time) if asm.last_update_time else None } if asm.access_token: response['access_token'] = asm.access_token if asm.access_token_secret: response['access_token_secret'] = asm.access_token_secret if asm.auxillary_data: response['auxillary_data'] = json.loads(asm.auxillary_data) return response
class AuthorSearchController(object): ''' Constructor ''' def __init__(self, request): self.request = request self.dbSession = DBSession() # return all authors that match the specified search criteria # @view_config(route_name='search.author', request_method='GET', renderer='jsonp', http_cache=0) def searchAuthors(self): # get author-id for authorName try: authorList = [] searchTerm = self.request.GET.get('name') if not searchTerm: self.request.response.status_int = 400; return {'error':'missing required query arg: name'} likeExpr = '%' + searchTerm + '%' for authorName, in self.dbSession.query(Author.author_name).filter(Author.author_name.like(likeExpr)).order_by(Author.author_name): authorList.append(authorName) except: self.equest.response.status_int = 404; return {'error':'unknown error'} return authorList;
class ServiceProfileController(object): ''' Constructor ''' def __init__(self, request): self.request = request self.dbSession = DBSession() # get all the service events for the author and service @view_config(route_name='author.services.profile', request_method='GET', renderer='jsonp', http_cache=0) def get_service_profile(self): authorName = self.request.matchdict['authorname'] serviceName = self.request.matchdict['servicename'] retriever = ProfileRetrievalFactory.get_retriever_for(serviceName) if not retriever: self.request.response.status_int = 404 return {'error':'profile information is not available for service %s' % serviceName} # get author-id for authorName try: author_id, = self.dbSession.query(Author.id).filter(Author.author_name == authorName).one() except: self.request.response.status_int = 404 return {'error':'unknown author %s' % authorName} # get service-id for serviceName try: serviceId, = self.dbSession.query(Service.id).filter(Service.service_name == serviceName).one() except: self.request.response.status_int = 404 return {'error':'unknown service %s' % authorName} try: mapping = self.dbSession.query(AuthorServiceMap).filter_by(service_id=serviceId,author_id=author_id).one() except: self.request.response.status_int = 404 return {'error':'unknown service for author'} profileJSON = retriever.get_author_profile(mapping,self.dbSession,oAuthConfig.get(serviceName)) self.dbSession.close() return profileJSON
def listAuthorServices(self): authorName = self.request.matchdict['authorname'] dbSession = DBSession() try: authorId, = dbSession.query( Author.id).filter_by(author_name=authorName).one() except NoResultFound: self.request.response.status_int = 404 return {'error': 'unknown author %s' % authorName} services = [] for service in dbSession.query(Service).join(AuthorServiceMap).filter( Service.id == AuthorServiceMap.service_id).join(Author).filter( AuthorServiceMap.author_id == authorId).order_by( Service.service_name): services.append({ 'service_id': service.id, 'name': service.service_name, 'color_icon_high_res': self.request.static_url( 'miapi:%s' % service.color_icon_high_res), 'color_icon_medium_res': self.request.static_url( 'miapi:%s' % service.color_icon_medium_res), 'color_icon_low_res': self.request.static_url( 'miapi:%s' % service.color_icon_low_res), 'mono_icon_high_res': self.request.static_url( 'miapi:%s' % service.mono_icon_high_res), 'mono_icon_medium_res': self.request.static_url( 'miapi:%s' % service.mono_icon_medium_res), 'mono_icon_low_res': self.request.static_url('miapi:%s' % service.mono_icon_low_res) }) return {'author_name': authorName, 'services': services}
def putAuthorService(self): authorName = self.request.matchdict['authorname'] serviceName = self.request.matchdict['servicename'] credentials = self.request.json_body accessToken = credentials.get('access_token') accessTokenSecret = credentials.get('access_token_secret') # check for auxillary data and convert to a string if it exists auxillaryData = credentials.get('auxillary_data') if auxillaryData: auxillaryData = json.dumps(auxillaryData) dbSession = DBSession() try: authorId, = 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: serviceId, = dbSession.query( Service.id).filter_by(service_name=serviceName).one() except NoResultFound: self.request.response.status_int = 404 return {'error': 'unknown service %s' % serviceName} authorServiceMap = AuthorServiceMap(authorId, serviceId, accessToken, accessTokenSecret, auxillaryData) try: dbSession.add(authorServiceMap) dbSession.commit() log.info("created author/service link: %s -> %s" % (authorName, serviceName)) except IntegrityError, e: dbSession.rollback() self.request.response.status_int = 409 return {'error': e.message}
def getAuthorServiceInfo(self): authorName = self.request.matchdict["authorname"] serviceName = self.request.matchdict["servicename"] dbSession = DBSession() try: authorId, = 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: serviceId, = dbSession.query(Service.id).filter_by(service_name=serviceName).one() except NoResultFound: self.request.response.status_int = 404 return {"error": "unknown service %s" % serviceName} try: asm = dbSession.query(AuthorServiceMap).filter_by(author_id=authorId, service_id=serviceId).one() except NoResultFound: self.request.response.status_int = 404 return {"error": "unknown service for author"} response = { "author": authorName, "service": serviceName, "last_update_time": datetime.isoformat(asm.last_update_time) if asm.last_update_time else None, } if asm.access_token: response["access_token"] = asm.access_token if asm.access_token_secret: response["access_token_secret"] = asm.access_token_secret if asm.auxillary_data: response["auxillary_data"] = json.loads(asm.auxillary_data) return response
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 ServicesController(object): ''' Constructor ''' def __init__(self, request): self.request = request self.dbSession = DBSession() # GET /v1/accounts # # return info on all services @view_config(route_name='services', request_method='GET', renderer='jsonp', permission='admin', http_cache=0) def list_accounts(self): serviceList = [] for service in self.dbSession.query(Service).order_by(Service.service_name): serviceList.append({'service_id':service.id, 'name':service.service_name, 'color_icon_high_res':self.request.static_url('miapi:%s' % service.color_icon_high_res), 'color_icon_medium_res':self.request.static_url('miapi:%s' % service.color_icon_medium_res), 'color_icon_low_res':self.request.static_url('miapi:%s' % service.color_icon_low_res), 'mono_icon_high_res':self.request.static_url('miapi:%s' % service.mono_icon_high_res), 'mono_icon_medium_res':self.request.static_url('miapi:%s' % service.mono_icon_medium_res), 'mono_icon_low_res':self.request.static_url('miapi:%s' % service.mono_icon_low_res)}) return {'services':serviceList} # retrieve information about a service @view_config(route_name='services.CRUD', request_method='GET', renderer='jsonp', permission='admin', http_cache=0) def get(self): serviceName = self.request.matchdict['servicename'] service = self.dbSession.query(Service).filter_by(service_name=serviceName).one() return {'service_id':service.id, 'name':service.service_name, 'color_icon_high_res':self.request.static_url('miapi:%s' % service.color_icon_high_res), 'color_icon_medium_res':self.request.static_url('miapi:%s' % service.color_icon_medium_res), 'color_icon_low_res':self.request.static_url('miapi:%s' % service.color_icon_low_res), 'mono_icon_high_res':self.request.static_url('miapi:%s' % service.mono_icon_high_res), 'mono_icon_medium_res':self.request.static_url('miapi:%s' % service.mono_icon_medium_res), 'mono_icon_low_res':self.request.static_url('miapi:%s' % service.mono_icon_low_res)} # add a new service @view_config(route_name='services.CRUD', request_method='PUT', renderer='jsonp', permission='admin', http_cache=0) def put(self): serviceName = self.request.matchdict['servicename'] images = self.request.json_body colorHighRes = images.get('color_icon_high_res',None) colorMedRes = images.get('color_icon_medium_res',None) colorLowRes = images.get('color_icon_low_res',None) monoHighRes = images.get('mono_icon_high_res',None) monoMedRes = images.get('mono_icon_medium_res',None) monoLowRes = images.get('mono_icon_low_res',None) service = Service(serviceName,colorHighRes,colorMedRes,colorLowRes,monoHighRes,monoMedRes,monoLowRes) try: self.dbSession.add(service) self.dbSession.commit() log.info("create service: %(servicename)s" % {'servicename':serviceName}) except IntegrityError, e: self.dbSession.rollback() self.request.response.status_int = 409 return {'error':e.message} service = self.dbSession.query(Service).filter_by(service_name=serviceName).first() return {'service': service.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