def delete_model(self, model): """ Delete model. :param model: Model to delete """ try: if administrator() or login.current_user.id == model.user_id: picture_list = get_picture_list(model.id) self.on_model_delete(model) self.session.flush() self.session.delete(model) self.session.commit() # 级联删除数据库记录 delete_pictures(picture_list) # 删除所有的本地文件 return True else: flash('权限不够呢,所以无法删除', 'info') except Exception as ex: if self._debug: raise flash(gettext('Failed to delete model. %(error)s', error=str(ex)), 'error') log.exception('Failed to delete model') self.session.rollback() return False
def access_picture(tour_id): tour = Tour.query.filter(Tour.id == tour_id).first() if administrator(): return True if tour: if tour.user_id == login.current_user.id: return True return False
def update_model(self, form, model): """改写了update函数""" try: if administrator() or login.current_user.id == model.user_id: model.update(**form_to_dict(form)) self.session.commit() else: flash('权限不够呢,所以无法修改', 'info') except Exception, ex: flash(gettext('Failed to update model. %(error)s', error=str(ex)), 'error') logging.exception('Failed to update model') self.session.rollback() return False
def index_view(self): """ List view """ if administrator(): self.can_delete = True else: self.can_delete = False # Grab parameters from URL page, sort_idx, sort_desc, search, filters = self._get_extra_args() # Map column index to column name sort_column = self._get_column_by_idx(sort_idx) if sort_column is not None: sort_column = sort_column[0] # Get count and data if administrator(): user_id = None else: user_id = login.current_user.id count, data = self.get_list(page, sort_column, sort_desc, search, filters, user_id=user_id) # Calculate number of pages num_pages = count // self.page_size if count % self.page_size != 0: num_pages += 1 # Pregenerate filters if self._filters: filters_data = dict() for idx, f in enumerate(self._filters): flt_data = f.get_options(self) if flt_data: filters_data[idx] = flt_data else: filters_data = None # Various URL generation helpers def pager_url(p): # Do not add page number if it is first page if p == 0: p = None return self._get_url('.index_view', p, sort_idx, sort_desc, search, filters) def sort_url(column, invert=False): desc = None if invert and not sort_desc: desc = 1 return self._get_url('.index_view', page, column, desc, search, filters) # Actions actions, actions_confirmation = self.get_actions_list() return self.render(self.list_template, data=data, # List list_columns=self._list_columns, sortable_columns=self._sortable_columns, # Stuff enumerate=enumerate, get_pk_value=self.get_pk_value, get_value=self.get_list_value, return_url=self._get_url('.index_view', page, sort_idx, sort_desc, search, filters), # Pagination count=count, pager_url=pager_url, num_pages=num_pages, page=page, # Sorting sort_column=sort_idx, sort_desc=sort_desc, sort_url=sort_url, # Search search_supported=self._search_supported, clear_search_url=self._get_url('.index_view', None, sort_idx, sort_desc), search=search, # Filters filters=self._filters, filter_groups=self._filter_groups, filter_types=self._filter_types, filter_data=filters_data, active_filters=filters, # Actions actions=actions, actions_confirmation=actions_confirmation)