def get_table_by_name(request): table_name = request.matchdict['table'] try: table = table_by_name(table_name) except KeyError: return {'success': False, 'msg': 'Ошибка: отсутствует таблица с указанным именем'} return table, table_name
def table_view(request): can_i_edit = has_permission('edit', request.context, request) can_i_edit = isinstance(can_i_edit, ACLAllowed) user_id = authenticated_userid(request) try: model = table_by_name(request.matchdict['table']) except KeyError: return {'success': False, 'msg': 'Ошибка: отсутствует таблица с указанным именем'} dbsession = DBSession() try: entity = dbsession.query(model).filter_by(id=request.matchdict['id']).one() user = dbsession.query(User).filter_by(id=user_id).one() if can_i_edit else None result = {'data': entity.as_json_dict(), 'success': True} except NoResultFound: result = {'success': False, 'msg': 'Результатов, соответствующих запросу, не найдено'} if hasattr(entity, 'inserter'): if isinstance(has_permission('admin', request.context, request), ACLAllowed): is_editable = True else: is_editable = entity.inserter == user.person_id if user else False else: is_editable = True result['editable'] = is_editable dbsession.close() return result
def _get_sorting_param(request): if 'jtSorting' not in request.GET: return asc(Person.name) table_name, field_sort = request.GET['jtSorting'].split('_') table = table_by_name(table_name) field, order = field_sort.split(' ') if order == 'ASC': return asc(getattr(table, field)) elif order == 'DESC': return desc(getattr(table, field))
def get_table_by_name(request): table_name = request.matchdict['table'] try: table = table_by_name(table_name) except KeyError: return { 'success': False, 'msg': 'Ошибка: отсутствует таблица с указанным именем' } return table, table_name
def _get_filter_conditions(request): if 'filter' not in request.POST or 'filter_fields' not in request.POST: return [] if not request.POST['filter'] or not request.POST['filter_fields']: return [] fields = request.POST['filter_fields'].split(',') filter_conditions = [] for field_name in fields: table_name, field = field_name.split('_') table = table_by_name(table_name) filter_conditions.append(getattr(table, field).like('%%%s%%' % request.POST['filter'])) return filter_conditions
def _get_filter_conditions(request): if 'filter' not in request.POST or 'filter_fields' not in request.POST: return [] if not request.POST['filter'] or not request.POST['filter_fields']: return [] fields = request.POST['filter_fields'].split(',') filter_conditions = [] for field_name in fields: table_name, field = field_name.split('_') table = table_by_name(table_name) filter_conditions.append( getattr(table, field).like('%%%s%%' % request.POST['filter'])) return filter_conditions
def table_download(request): dbsession = DBSession() modelname = request.matchdict['table'] try: model = table_by_name(modelname) except KeyError: return { 'success': False, 'msg': 'Ошибка: отсутствует таблица с указанным именем' } try: all = dbsession.query(model).all() dbsession.close() except DBAPIError: result = {'success': False, 'msg': 'Ошибка подключения к БД'} names = [] for c in model.__table__.columns: names.append(c.name) rows = [ names, ] for row in all: data = [] for name in names: data.append(try_encode(getattr(row, name))) rows.append(data) fname = tempfile.mktemp() try: file = open(fname, 'w') writer = csv.writer(file, delimiter='\t') writer.writerows(rows) file.close() file = open(fname, 'r') data = file.read() resname = modelname + '.csv' finally: # в любом случае удаляем файл os.remove(fname) return Response(content_type="application/octet-stream", content_disposition="attachment; filename=%s" % (resname, ), body=data)
def table_download(request): dbsession = DBSession() modelname = request.matchdict['table'] try: model = table_by_name(modelname) except KeyError: return {'success': False, 'msg': 'Ошибка: отсутствует таблица с указанным именем'} try: all = dbsession.query(model).all() dbsession.close() except DBAPIError: result = {'success': False, 'msg': 'Ошибка подключения к БД'} names = [] for c in model.__table__.columns: names.append(c.name) rows = [names, ] for row in all: data = [] for name in names: data.append(try_encode(getattr(row, name))) rows.append(data) fname = tempfile.mktemp() try: file = open(fname, 'w') writer = csv.writer(file, delimiter = '\t') writer.writerows(rows) file.close() file = open(fname, 'r') data = file.read() resname = modelname + '.csv' finally: # в любом случае удаляем файл os.remove(fname) return Response(content_type="application/octet-stream", content_disposition="attachment; filename=%s" % (resname, ), body=data)
def table_view(request): can_i_edit = has_permission('edit', request.context, request) can_i_edit = isinstance(can_i_edit, ACLAllowed) user_id = authenticated_userid(request) try: model = table_by_name(request.matchdict['table']) except KeyError: return { 'success': False, 'msg': 'Ошибка: отсутствует таблица с указанным именем' } dbsession = DBSession() try: entity = dbsession.query(model).filter_by( id=request.matchdict['id']).one() user = dbsession.query(User).filter_by( id=user_id).one() if can_i_edit else None result = {'data': entity.as_json_dict(), 'success': True} except NoResultFound: result = { 'success': False, 'msg': 'Результатов, соответствующих запросу, не найдено' } if hasattr(entity, 'inserter'): if isinstance(has_permission('admin', request.context, request), ACLAllowed): is_editable = True else: is_editable = entity.inserter == user.person_id if user else False else: is_editable = True result['editable'] = is_editable dbsession.close() return result
def _get_table(table_name, aliased_info): if table_name in aliased_info: return aliased_info[table_name] else: return table_by_name(table_name)
def table_browse(request): dbsession = DBSession() tablename = request.matchdict['table'] try: table = table_by_name(tablename) except KeyError: return {'success': False, 'msg': 'Ошибка: отсутствует таблица с указанным именем'} numRows = 0 items = [] success = True if ('id' in request.params) and request.params['id'].isdigit(): id = int(request.params['id']) try: items = dbsession.query(table) \ .filter(table.id == id) \ .all() numRows = 1 except DBAPIError: success = False else: start, count = helpers.get_paging_params(request.params) filter_conditions = [] parsed_name = helpers.get_parsed_search_attr(request.params, tablename) if parsed_name: filter_conditions.append(getattr(table, tablename).ilike(parsed_name)) org_type = request.params['org_type'] if 'org_type' in request.params else None if org_type and hasattr(table, 'org_type'): filter_conditions.append(getattr(table, 'org_type') == org_type) try: if (start is not None) and (count is not None): items = dbsession.query(table)\ .filter(*filter_conditions) \ .order_by(tablename + ' asc') \ .slice(start, start+count) \ .all() numRows = dbsession.query(table) \ .filter(*filter_conditions) \ .count() else: items = dbsession.query(table) \ .filter(*filter_conditions) \ .order_by(tablename + ' asc') \ .all() numRows = len(items) except DBAPIError: success = False dbsession.close() items_json = [] for row in items: items_json.append(row.as_json_dict()) return { 'items': items_json, 'success': success, 'numRows': numRows, 'identifier': 'id' }
def table_browse(request): dbsession = DBSession() tablename = request.matchdict['table'] try: table = table_by_name(tablename) except KeyError: return { 'success': False, 'msg': 'Ошибка: отсутствует таблица с указанным именем' } numRows = 0 items = [] success = True if ('id' in request.params) and request.params['id'].isdigit(): id = int(request.params['id']) try: items = dbsession.query(table) \ .filter(table.id == id) \ .all() numRows = 1 except DBAPIError: success = False else: start, count = helpers.get_paging_params(request.params) filter_conditions = [] parsed_name = helpers.get_parsed_search_attr(request.params, tablename) if parsed_name: filter_conditions.append( getattr(table, tablename).ilike(parsed_name)) org_type = request.params[ 'org_type'] if 'org_type' in request.params else None if org_type and hasattr(table, 'org_type'): filter_conditions.append(getattr(table, 'org_type') == org_type) try: if (start is not None) and (count is not None): items = dbsession.query(table)\ .filter(*filter_conditions) \ .order_by(tablename + ' asc') \ .slice(start, start+count) \ .all() numRows = dbsession.query(table) \ .filter(*filter_conditions) \ .count() else: items = dbsession.query(table) \ .filter(*filter_conditions) \ .order_by(tablename + ' asc') \ .all() numRows = len(items) except DBAPIError: success = False dbsession.close() items_json = [] for row in items: items_json.append(row.as_json_dict()) return { 'items': items_json, 'success': success, 'numRows': numRows, 'identifier': 'id' }