def index(self, parent_id=None, format='html'): """GET /``REST_Collection``: All items in the collection""" settings = self._get_settings() try: page_num = int(self._py_object.request.GET.get('p',1)) except ValueError: page_num = 1 try: limit = int(settings.get('maximum_items')) except (ValueError,TypeError): limit = 20 offset = (page_num - 1) * limit items = self._index(parent_id=parent_id, limit=limit, offset=offset) try: self._py_object.response.content_type = self.RESOURCE_FORMATS[format] extra_vars={'settings':settings,'page_num':page_num,'items':items,'ctrl_name':self.MULTIPLE} #if format == 'ajax': # return render_jinja2_block('/%s/index.html.jinja'%self.MULTIPLE, 'content', extra_vars=extra_vars) #else: return render_jinja2('/%s/index.%s.jinja'%(self.MULTIPLE,format), extra_vars=extra_vars) except TemplateNotFound, ex: self.LOGGER.error(ex) abort(415, detail='415 Unsupported Media Type')
def create(self, parent_id=None, format='html'): """POST /``REST_Collection``: Create a new item""" try: item = self._create(format, parent_id) except UnacceptedFormat: abort(415, detail='415 Unsupported Media Type') except InvalidForm, ex: return ex.value
def update(self, id, parent_id=None, format='html'): """PUT /``REST_Collection``/id: Update an existing item""" try: item = self._get_from_id(id, parent_id) if item is None: abort(404, detail='404 Not Found') self._update(item, format) except UnacceptedFormat: abort(415, detail='415 Unsupported Media Type') except InvalidForm, ex: return ex.value
def new(self, parent_id=None, format='html'): """GET /``REST_Collection``/new: Form to create a new item""" settings = self._get_settings() item = self._new(parent_id, with_defaults=True) try: extra_vars = {'item':item,'settings':settings,'ctrl_name':self.MULTIPLE} #if format == 'ajax': # return render_jinja2_block('/%s/new.html.jinja'%self.MULTIPLE, 'content', extra_vars=extra_vars) #else: return render_jinja2('/%s/new.%s.jinja'%(self.MULTIPLE,format), extra_vars=extra_vars) except TemplateNotFound, ex: self.LOGGER.error(ex) abort(415, detail='415 Unsupported Media Type')
def delete(self, id, parent_id=None, format='html'): """DELETE /``REST_Collection``/id: Delete an existing item""" item = self._get_from_id(id, parent_id) if item is None: abort(404, detail='404 Not Found') self._delete(item) if self.FORMAT_NEEDS_REDIRECT[format] is True: if parent_id is None: redirect(self._py_object.url(self.MULTIPLE)) else: redirect(self._py_object.url(self.MULTIPLE, parent_id=parent_id)) else: abort(200, detail='200 OK')
def edit(self, id, parent_id=None, format='html'): """GET /``REST_Collection``/id/edit: Form to edit an existing item""" settings = self._get_settings() item = self._get_from_id(id, parent_id) if item is None: abort(404, detail='404 Not Found') try: extra_vars = {'item':item,'settings':settings,'ctrl_name':self.MULTIPLE} #if format == 'ajax': # return render_jinja2_block('/%s/edit.html.jinja'%self.MULTIPLE, 'content', extra_vars=extra_vars) #else: return render_jinja2('/%s/edit.%s.jinja'%(self.MULTIPLE,format), extra_vars=extra_vars) except TemplateNotFound, ex: self.LOGGER.error(ex) abort(415, detail='415 Unsupported Media Type')
def show(self, id, parent_id=None, format='html'): """GET /``REST_Collection``/id: Show a specific item""" try: page_num = int(self._py_object.request.GET.get('p',1)) except ValueError: #someone put something stupid as the value for page page_num = 1 try: settings = self._get_settings() item = self._get_from_id(id, parent_id) if item is None: abort(404, detail='404 Not Found') settings = self._get_settings() self._py_object.response.content_type = self.RESOURCE_FORMATS[format] extra_vars = {'item':item,'settings':settings,'page_num':page_num,'ctrl_name':self.MULTIPLE} #if format == 'ajax': # return render_jinja2_block('/%s/show.html.jinja'%self.MULTIPLE, 'content', extra_vars=extra_vars) #else: return render_jinja2('/%s/show.%s.jinja'%(self.MULTIPLE,format), extra_vars=extra_vars) except TemplateNotFound, ex: self.LOGGER.error(ex) abort(415, detail='415 Unsupported Media Type')
def update(self): """PUT /accounts: Update an existing item""" # url('account') try: item = meta.Session.query(User).get(int(session['user_id'])) #add any other update stuff #item = self.update_from_format(ctrl_obj, item, format, request, session, app_globals, parent_id) if session.get('auth_type',None) == 'facebook': item.fb_id = session['oid'] elif session.get('auth_type',None) == 'twitter': item.twitter_id = session['oid'] elif session.get('auth_type',None) == 'openid': item.open_id = session['oid'] item.profile = request.POST.get('profile',u'').strip() if item.profile == u'': item.profile = None #session['user_name'] = item.name session['user_profile'] = item.profile session.save() item.save(session, app_globals) except: log.error(traceback.format_exc()) abort(500) redirect(url("edit_account"))
def index(self, format='html'): # Dashboard for admin if format == 'html': return render_jinja2('/admin/dashboard.jinja') else: abort(404)
"""POST /``REST_Collection``: Create a new item""" try: item = self._create(format, parent_id) except UnacceptedFormat: abort(415, detail='415 Unsupported Media Type') except InvalidForm, ex: return ex.value if self.FORMAT_NEEDS_REDIRECT[format] is True: if parent_id is None: redirect(self._py_object.url(self.MULTIPLE)) else: redirect(self._py_object.url(self.MULTIPLE, parent_id=parent_id)) else: if parent_id is None: abort(201, detail='201 Created', headers={'Location': self._py_object.url(self.SINGLE, id=item.id)}) else: abort(201, detail='201 Created', headers={'Location': self._py_object.url(self.SINGLE, parent_id=parent_id, id=item.id)}) def new(self, parent_id=None, format='html'): """GET /``REST_Collection``/new: Form to create a new item""" settings = self._get_settings() item = self._new(parent_id, with_defaults=True) try: extra_vars = {'item':item,'settings':settings,'ctrl_name':self.MULTIPLE} #if format == 'ajax': # return render_jinja2_block('/%s/new.html.jinja'%self.MULTIPLE, 'content', extra_vars=extra_vars) #else: return render_jinja2('/%s/new.%s.jinja'%(self.MULTIPLE,format), extra_vars=extra_vars) except TemplateNotFound, ex: self.LOGGER.error(ex)