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}
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