コード例 #1
0
ファイル: htm.py プロジェクト: MontmereLimited/tiote
def generate_sidebar(request):
    ret_string = '' # store the string representation to be generated by this function
    static_addr = fns.render_template(request, '{{STATIC_URL}}') 
    conn_params = fns.get_conn_params(request, update_db=True)
    db_list = qry.common_query(conn_params, 'db_list', request.GET)['rows']

    # if sctn is 'home' give list of database
    # if sctn is overview give list of schema if postgresql or leave as above
    # if sctn is not 'home' or 'oveview' give a list of all the objects described by sctn
    if request.GET.get('sctn') == 'hm' or \
            (conn_params['dialect'] == 'mysql' and request.GET.get('sctn') == 'db'):
        li_list = []
        for db_row in db_list:
            sufx = "&schm=%s" % sa.get_default_schema(conn_params) if conn_params['dialect'] == 'postgresql' else ''
            a = "<a class='icon-database' href='#sctn=db&v=ov&db={0}{1}'>{0}</a>".format(db_row[0],sufx)
            active_li = ' class="active"' if request.GET.get('db') == db_row[0] else '' # this would mark an item for hightlighting 
                                                                                        # occurs in the 'db' section of MySQL dialect
                                                                                        # and it selects the currently displayed db
            li_list.append('<li{1}>{0}</li>'.format(a, active_li)) 
        ret_string += '<h6>Databases</h6><ul>' + ''.join(li_list) + '</ul>'

    elif request.GET.get('sctn') == 'db':
        # decide on what to do
        _dict = {'db': request.GET.get('db')}
        # get a dropdown of databases from db_list with its initial set to what is in request.GET
        db_selection_form = select_input(db_list, desc={'id':'db_select'}, initial= _dict['db'])
        schema_list = qry.common_query(conn_params, 'schema_list', request.GET)['rows']
        _list = []
        for schm_row in schema_list:
            a = '<a class="icon-schema schm-link" href="#">%s</a>' % schm_row[0]
            # decide selected schema link
            li_pfx = " class='active'" if request.GET.get('schm', sa.get_default_schema(conn_params) ) == schm_row[0] else ''
            # append whole li element
            _list.append("<li{0}>{1}</li>".format(li_pfx, a))
            
        placeholder = '<h6 class="placeholder">%ss:</h6>' % fns._abbr['schm']
        sfx = "<ul>{0}</ul>".format( ''.join(_list) )

        ret_string = '<h6><a class="icon-back" href="#sctn=hm&v=hm">back home</a></h6>\
<h6>quick nav:</h6><div class="sidebar-item"><img src="{3}/tt_img/databases.png" />{0}</div>{1}{2}'.format( 
            db_selection_form, placeholder, sfx, static_addr
        )

    elif request.GET.get('sctn') in ('tbl', 'seq', 'views') :
        _dict = {'db': request.GET.get('db')}
        # get a dropdown of databases from db_list with its initial set to what is in request.GET
        db_selection_form = select_input(db_list, desc={'id':'db_select'}, initial= _dict['db'])

        s = ''
        if conn_params['dialect'] == 'postgresql':
            _dict['schm'] = request.GET.get('schm')
            # append schema selection with default to public
            schema_list = qry.common_query(conn_params, 'schema_list', request.GET)['rows']
            schema_selection_form = select_input(schema_list,desc={'id':'schema_select'},initial=_dict['schm'])
            # s = '<div class="sidebar-item"><img class="icon-schemas" />' + schema_selection_form + "</div>"
            s = '<div class="sidebar-item"><img src="{1}/tt_img/schemas.png" />{0}</div>'.format(
                schema_selection_form, static_addr)
        
        # table selection ul
        # table_list = qry.rpr_query(conn_params, 'existing_tables', fns.qd(request.GET))
        table_list = sa.get_table_names(conn_params, request.GET)
        table_list.sort() # noted that the list is sometimes not sorted from postgresql
        sfx_list = []
        pg_sfx = '&schm=' + _dict['schm'] if conn_params['dialect']=='postgresql' else ''
        for tbl_row in table_list:
            # decide selected table
            li_pfx = " class='active'" if request.GET.get('tbl') == tbl_row else ''
            a = '<a class="icon-table tbl-link" href="#">%s</a>' % tbl_row
            sfx_list.append("<li{0}>{1}</li>".format(li_pfx, a))
        
        # generate the string to be returned. It has the following order
        # 1. h6 saying 'quick navigation'
        # 2. db selection select element and schm selection select element if dialect is postgresql
        # 3. a h6.placeholder element shouting 'object types:'
        # 4. a ul having li > a each saying the specific 'object type' and linking to its appropriate view
        # ret_string = '<h6>quick nav:</h6><div><a class="james pull-right" href="">overview</a></div><br />\
        bck_lnk = '<h6><a class="icon-back" href="#sctn=db&v=ov&db={0}{1}">back to overview</a>'.format(
            request.GET.get('db'),
            '&schm=%s' % request.GET.get('schm') if request.GET.get('schm') else ''
            )
        ret_string = bck_lnk + '</h6>\
            <h6>quick nav:</h6><div class="sidebar-item"><img src="{url}/tt_img/databases.png" /> {db_select}</div>\
            {schema_select}{placeholder_html}{tbl_list}'.format( 
            db_select = db_selection_form, 
            schema_select = s,
            placeholder_html = '<h6 class="placeholder">%ss:</h6>' % fns._abbr[request.GET.get('sctn')], # 2
            tbl_list = "<ul>{0}</ul>".format( ''.join(sfx_list) ),
            url = static_addr
        )

#    return ret_string
    return HttpResponse(ret_string)
コード例 #2
0
ファイル: tbl.py プロジェクト: pombredanne/tiote
def cols_struct(request):
    # inits and first queries
    conn_params = fns.get_conn_params(request, update_db=True)
    # only needed in the MySQL dialect
    if conn_params['dialect'] == 'mysql':
        charset_list = qry.common_query(conn_params, 'charset_list')['rows']
        supported_engines = qry.common_query(conn_params, 'supported_engines')['rows']
    else:
        supported_engines = None
        charset_list = None
    tbl_names = sa.get_table_names(conn_params, request.GET)
    tbl_cols = qry.rpr_query(conn_params, 'table_structure', fns.qd(request.GET))

    # column editing/deleting
    if request.method == 'POST' and request.GET.get('upd8'):
        # format the where_stmt to a mapping of columns to values (a dict)
        l = request.POST.get('where_stmt').strip().split(';')
        conditions = fns.get_conditions(l)
        # determine query to run
        if request.GET.get('upd8') in ('delete', 'drop',):
            q = 'drop_column'
            query_data = {'conditions': conditions}
            for k in ['db', 'tbl',]: query_data[k] = request.GET.get(k)
            if request.GET.get('schm'):
                query_data['schm'] = request.GET.get('schm')
            
            return qry.rpr_query(conn_params, q, query_data)

    # handle submission of new column form
    elif request.method == 'POST':
        form = forms.ColumnForm(conn_params['dialect'], engines=supported_engines, charsets=charset_list,
            existing_tables=tbl_names, existing_columns=tbl_cols['rows'], label_suffix=':', data=request.POST)
        # handle invalid forms
        if not form.is_valid():
            return fns.response_shortcut(request, template='form_errors',
                extra_vars={'form':form,})
        # prep form fields: add all type_ together
        if conn_params['dialect'] == 'postgresql':
            keys = [key for key in form.cleaned_data.keys() if key.startswith('type_')]
            for key in keys:
                _temp = form.cleaned_data[key].split('|')
                form.cleaned_data[key] = _temp[0] # first index is the base type
                if _temp[-1] != '_default':
                    form.cleaned_data[key] += _temp[1] # the array specifier literal
        # do column creation and return error
        ret = qry.create_column(conn_params, request.GET, form.cleaned_data)
        return HttpResponse(json.dumps(ret))
        
    # table view
    http_resp = base_struct(request, tbl_data=tbl_cols, show_tbl_optns=True,
        tbl_props= {'keys': (('column', 'key'), )}, tbl_optn_type='tbl_like',
        subv='cols', empty_err_msg="Table contains no columns")

    form = forms.ColumnForm(conn_params['dialect'], engines=supported_engines, charsets=charset_list,
        existing_tables=tbl_names, existing_columns=tbl_cols['rows'], label_suffix=':')

    form_html= fns.render_template(request, 'tbl/tt_col.html', context={'form': form, 'edit':False,
        'table_fields': ['name', 'engine', 'charset', 'inherit', 'of_type'],
        'odd_fields': ['type','key','charset', 'not null'],
        'dialect': conn_params['dialect'],
        # 'table_with_columns': table_with_columns,
        }, is_file=True)

    http_resp.content += form_html
    return http_resp