def post(self): log.debug("REST POST %s <- %s" % (self.request.path, pprint.pformat(self.request_params, indent=2, width=160))) self.validate_params([ ('path', True), ('input', True), 'project_id', 'template_id']) proj_id = self.request_params['project_id'] s = session() project = s.query(mgen.model.Project).filter_by(project_id=proj_id).one() p = project.get_permission(self.current_profile.email) if not p & Permission.Edit: raise mgen.error.Forbidden().describe("You cannot modify project " + proj_id) p = mgen.model.Page(page_id=uuid.uuid4(), path=self.request_params['path'], input=json.loads(self.request_params['input']), project_id=self.request_params['project_id'], template_id=self.request_params['template_id']) s.add(p) self.commit_changes(s) self.set_status(201) log.debug('created new page: %s' % p.page_id) return self.get_objects(mgen.model.Template, self.query(), p.page_id)
def post(self): log.debug("REST POST %s <- %s" % (self.request.path, pprint.pformat(self.request_params, indent=2, width=160))) """POST to create a new project""" self.validate_params([ 'title', 'public_base_uri', 'options' ]) s = session() project = mgen.model.Project(project_id=uuid.uuid4(), title=self.request_params['title'], public_base_uri=self.request_params['public_base_uri']) s.add(project) perm = mgen.model.ProjectPermission.grant(Permission.all(), project.project_id, self.current_profile.email) s.add(perm) self.commit_changes(s) self.set_status(201) log.debug('created new project: %s' % project.project_id) return self.get_objects(mgen.model.Project, self.query(), project.project_id)
def query(self): return session().query(mgen.model.Item).join(mgen.model.Project, mgen.model.Project.project_id==mgen.model.Item.project_id)\ .join(mgen.model.project2profile, mgen.model.project2profile.c.project==mgen.model.Project.project_id)\ .join(mgen.model.Profile, mgen.model.Profile.email==mgen.model.project2profile.c.profile)\ .filter(mgen.model.Profile.email==self.current_profile.email)
def query(self): # select project.title, profile.name, project2profile.permission # from project join project2profile on project2profile.project = project.project_id # join profile on profile.email = project2profile.profile # where profile.email = @profile return session().query(mgen.model.Project).join(mgen.model.project2profile, mgen.model.project2profile.c.project==mgen.model.Project.project_id)\ .join(mgen.model.Profile, mgen.model.Profile.email==mgen.model.project2profile.c.profile)\ .filter(mgen.model.Profile.email==self.current_profile.email)
def put(self, template_id): self.validate_params([ ('pk', True), ('name', True), ('value', True) ]) s = session() tmpl = s.query(mgen.model.Template).filter_by(template_id=template_id).one() p = tmpl.project.get_permission(self.current_profile.email) if not p & Permission.Edit: raise mgen.error.Forbidden().describe("You cannot modify project " + proj_id) pk = self.request_params['pk'] name = self.request_params['name'] value = self.request_params['value'] if pk == 'template': # modify template property log.debug('modify template "%s": %s=%s' % (template_id, name, value)) setattr(tmpl, name, value) if 'template.params' in pk: # modify one of the params t, p, param_id = pk.split('.') log.debug('modify template parameter "%s.%s": %s=%s' % (template_id, param_id, name, value)) for p in tmpl.params: if p['id'] == param_id: p[name] = value tmpl.params.changed() break if 'erase.params' in pk: # remove one of the params e, p, param_id = pk.split('.') log.debug('remove template paramter "%s.%s"' % (template_id, param_id)) params = copy.deepcopy(tmpl.params) tmpl.params.clear() for p in params: if p['id'] != param_id: tmpl.params.append(p) tmpl.params.changed() if 'new.params' in pk: log.debug('new template parameter "%s.%s"' % (template_id, value['id'])) tmpl.params.append(value) tmpl.params.changed() s.add(tmpl) self.commit_changes(s) return self.get_objects(mgen.model.Template, self.query(), tmpl.template_id)
def post(self): """POST to create a new item""" log.debug("REST POST %s <- %s" % (self.request.path, pprint.pformat(self.request_params, indent=2, width=160))) self.validate_params([ 'name', 'type', 'body', 'published' ]) def_uri_path = self.request_params['name'].lower().replace(' ', '-').replace('/', '-').replace('\\', '-') uri_path = self.request_params.get('uri_path', def_uri_path) itm = mgen.model.Item(item_id=uuid.uuid4(), name=self.request_params['name'], type=self.request_params['type'], body=self.request_params['body'], uri_path=uri_path, published=self.request_params['published']) if itm.published: # publish it now itm.publish_date = datetime.datetime.now() elif 'publish_date' in self.request_params: itm.publish_date = datetime.datetime.strptime(self.request_params['publish_date'], '%d-%m-%Y') else: raise mgen.error.BadRequest().describe('no value given for \ publish_date and item is not published now') s = session() s.add(itm) if 'tags' in self.request_params: for tag in self.request_params['tags']: tag_name = tag.strip() log.debug('applying tag "%s" to item "%s"' % (tag_name, itm.item_id)) atag = s.query(mgen.model.Tag).filter_by(tag=tag_name).first() if not atag: log.debug(' -> it is a new tag, creating...') atag = mgen.model.Tag(tag=tag_name) s.add(atag) # append (new) tag to list of tags s.add(mgen.model.tag2item(tag=atag.tag, item=itm.item_id)) self.commit_changes(s) self.set_status(201) log.debug('created new item: %s' % itm.item_id) return self.get_objects(mgen.model.Item, self.query(), itm.item_id)
def get(self, template_id): s = session() tmpl = s.query(mgen.model.Template).filter_by(template_id=template_id).one() p = self.get_permissions(tmpl.project) if p['forbidden']: raise mgen.error.Forbidden().describe('access denied to project id "%s"' % tmpl.project_id) log.debug('displaying template %s' % tmpl.template_id) return self.render("template.html", user=self.current_user, profile=self.current_profile, item_types=mgen.generator.item_types, template_types=mgen.generator.template.template_types, template=tmpl, **p)
def get(self, project_id): s = session() proj = s.query(mgen.model.Project).filter_by(project_id=project_id).one() p = self.get_permissions(proj) if p['forbidden']: raise mgen.error.Forbidden().describe('access denied to project id "%s"' % project_id) log.debug('displaying project %s' % proj.project_id) return self.render("project.html", user=self.current_user, profile=self.current_profile, item_types=mgen.generator.item_types, template_types=mgen.generator.template.template_types, project=proj, **p )
def get(self): if self.get_argument('code', False): # read google auth info user = yield self.get_authenticated_user( redirect_uri=self.settings['google_oauth']['redirect_uri'], code=self.get_argument('code')) userinfo = yield self.oauth2_request( "https://www.googleapis.com/oauth2/v1/userinfo", access_token=user["access_token"]) log.debug('--- begin google profile ---') log.debug(pprint.pformat(userinfo, indent=2, width=160)) log.debug('--- end google profile ---') s = session() email = userinfo["email"] profile = s.query(mgen.model.Profile).filter_by(email=email).first() if not profile: profile = mgen.model.Profile( email=email, name = userinfo["name"], picture = userinfo["picture"]) s.add(profile) s.commit() log.debug('created new local profile: %s' % profile.email) self.set_secure_cookie('mgen-auth-principal', json.dumps(user, cls=mgen.util.JSONEncoder)) self.set_secure_cookie('mgen-auth-profile', json.dumps(profile, cls=mgen.util.JSONEncoder)) log.debug('session cookies saved') self.redirect('/') else: # redirect to google login form yield self.authorize_redirect( redirect_uri=self.settings['google_oauth']['redirect_uri'], client_id=self.settings['google_oauth']['key'], scope=['profile', 'email'], response_type='code', extra_params={'approval_prompt': 'auto'})
def commit_changes(self, s = None): cls_name = self.__class__.__name__ try: # use default session if param was omitted if s is None: s = session() s.commit() s.flush() log.debug("commited changes to session %s" % s) except sqlalchemy.exc.IntegrityError as ie: raise mgen.error.Conflict().duplicate_object( 'Failed to create new object in "{0}". Error: {1}'.format( cls_name, ie) ) except Exception as ex: raise mgen.error.BadRequest().describe( 'Failed to create new object in "{0}". Error: {1}'.format( cls_name, ex) )
def post(self): log.debug("REST POST %s <- %s" % (self.request.path, pprint.pformat(self.request_params, indent=2, width=160))) self.validate_params([ ('name', True), ('type', True), 'params', 'data']) proj_id = self.request_params['project_id'] s = session() project = s.query(mgen.model.Project).filter_by(project_id=proj_id).one() p = project.get_permission(self.current_profile.email) if not p & Permission.Edit: raise mgen.error.Forbidden().describe("You cannot modify project " + proj_id) tmpl = mgen.model.Template(template_id=uuid.uuid4(), name=self.request_params['name'], type=self.request_params['type'], project_id=self.request_params['project_id'], data=self.request_params['data'], params=self.request_params['params']) if tmpl.type not in mgen.generator.template.template_types: raise mgen.error.BadRequest().describe('unsupported template type: %s. supported: %s' % ( tmpl.type, ', '.join(mgen.generator.item_types.keys()))) s.add(tmpl) self.commit_changes(s) self.set_status(201) log.debug('created new template: %s' % tmpl.template_id) return self.get_objects(mgen.model.Template, self.query(), tmpl.template_id)
def get(self, profile_id=None): """GET list or single profile""" q = session().query(Profile) return self.get_objects(mgen.model.Profile, q, profile_id)
def get(self): s = session() return self.render("overview.html", user=self.current_user, profile=self.current_profile)