Commit de0e9728 authored by Anze Vavpetic's avatar Anze Vavpetic
Browse files

Added mysql connectivity widget

parent c3f0d930
from django import forms
import mysql.connector as sql
class DBConnection:
def __init__(self, user, password, host, database):
self.user = user
self.password = password
self.host = host
self.database = database
def cursor(self):
self.con = sql.connect(user=self.user, password=self.password, host=self.host, database=self.database)
return self.con.cursor()
def close(self):
self.con.close()
class DBContext:
def __init__(self, connection):
cursor = connection.cursor()
cursor.execute('SHOW tables')
self.tables = [table for (table,) in cursor]
self.cols = {}
for table in self.tables:
cursor.execute("SELECT column_name FROM information_schema.columns WHERE table_name = '%s'" % table)
self.cols[table] = [col for (col,) in cursor]
self.connected = {}
cursor.execute(
"SELECT table_name, column_name, referenced_table_name, referenced_column_name \
FROM information_schema.KEY_COLUMN_USAGE \
WHERE referenced_table_name IS NOT NULL")
for (table, col, ref_table, ref_col) in cursor:
self.connected[(table, ref_table)] = (col, ref_col)
self.target_table = self.tables[0]
def update(self, postdata):
widget_id = postdata.get('widget_id')[0]
self.target_table = postdata.get('target_table%s' % widget_id)[0]
self.tables = postdata.get('tables%s' % widget_id)
# Propagate the selected tables
for table in self.cols.keys():
if table not in self.tables:
del self.cols[table]
for pair in self.connected.keys():
if pair[0] in self.tables and pair[1] in self.tables:
continue
del self.connected[pair]
for table in self.tables:
self.cols[table] = postdata.get('%s_columns%s' % (table, widget_id))
def __repr__(self):
return str((self.target_table, self.tables, self.cols, self.connected))
import mysql.connector as sql
# Widget 1
con = sql.connect(user='root', password='', host='localhost', database='test')
# Widget 2
# Tole bo DB Context Object
# - izberi relacije
# - izberi stolpce
# - povezi kljuce
cursor = con.cursor()
cursor.execute('SHOW tables')
tables = [table for (table,) in cursor]
cols = {}
for table in tables:
cursor.execute("SELECT column_name FROM information_schema.columns WHERE table_name = '%s'" % table)
cols[table] = [col for (col,) in cursor]
print cols
main_table = 'trains'
connected = {}
cursor.execute(
"SELECT table_name, column_name, referenced_table_name, referenced_column_name \
FROM information_schema.KEY_COLUMN_USAGE \
WHERE referenced_table_name IS NOT NULL")
for (table, col, ref_table, ref_col) in cursor:
connected[(table, ref_table)] = (col, ref_col)
print connected
# Widget 3
# - iz DB context obj zgeneriraj proper .b in .f/.n fajle
'''
MySQL interaction views.
@author: Anze Vavpetic <anze.vavpetic@ijs.si>
'''
from django.shortcuts import render
from context import DBContext
def mysql_db_context(request, input_dict, output_dict, widget):
con = input_dict['connection']
initial_context = DBContext(con)
return render(request, 'interactions/db_context.html', {'widget':widget, 'context': initial_context})
'''
MySQL connectivity library.
@author: Anze Vavpetic <anze.vavpetic@ijs.si>
'''
import mysql.connector as sql
from context import DBConnection, DBContext
def mysql_connect(input_dict):
user = str(input_dict['user'])
password = str(input_dict['password'])
host = str(input_dict['host'])
db = str(input_dict['database'])
con = DBConnection(user, password, host, db)
return {'connection' : con}
def mysql_db_context(input_dict):
return {'context' : None}
def mysql_db_context_finished(postdata, input_dict, output_dict):
con = input_dict['connection']
context = DBContext(con)
context.update(postdata)
return {'context' : context}
\ No newline at end of file
import os
# === STANDARD PACKAGE SETTINGS ===
PACKAGE_ROOT = os.path.dirname(__file__)
# === AUTO IMPORT OPTIONS ===
#If auto_import_package_data is true then given data file is automatically imported when ClowdFlows project is newly deployed or refreshed from git
AUTO_IMPORT_DB = True
#For auto_import_package_data_replace_option description see the 'replace' option in workflows/import_package command
AUTO_IMPORT_DB_REPLACE_OPTION = True
#If file(s) other than ./db/package_data.json should be imported, auto_import_package_data_files should be corrected
AUTO_IMPORT_DB_FILES = [os.path.join(PACKAGE_ROOT,'db/package_data.json')]
<div id="widgetinteract-{{widget.pk}}" rel="{{widget.pk}}" class="widgetinteractdialog" title="{{widget.name}} interaction" width="700" height="600">
<div>
<form>
<label>Target table:</label>
<select name="target_table{{widget.pk}}">
{% for table in context.tables %}
<option value="{{table}}">{{table}}</option>
{% endfor %}
</select>
<label>Select tables to be used:</label>
<select multiple name="tables{{widget.pk}}">
{% for table in context.tables %}
<option value="{{table}}">{{table}}</option>
{% endfor %}
</select multiple>
{% for table, cols in context.cols.items %}
<label>Select columns for {{table}}:</label>
<select multiple name="{{table}}_columns{{widget.pk}}">
{% for col in cols %}
<option value="{{col}}">{{col}}</option>
{% endfor %}
</select multiple>
{% endfor %}
<input type="hidden" name="widget_id" value="{{widget.pk}}"/>
</form>
</div>
</div>
\ No newline at end of file
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
#url(r'^get-adc-index/widget(?P<widget_id>[0-9]+)/nx/Index.html$', 'workflows.latino.views.get_adc_index', name='get adc index'),
#url(r'^get-adc-index/widget(?P<widget_id>[0-9]+)/(?P<narrow_doc>n?)x/Index.html$', 'workflows.latino.views.get_adc_index', name='get adc index'),
#url(r'^get-adc-index/widget(?P<widget_id>[0-9]+)/(?P<narrow_doc>n?)x/Index(?P<document_id_from>[0-9]+)-(?P<document_id_to>[0-9]+).html$', 'workflows.latino.views.get_adc_index', name='get adc index'),
#url(r'^get-adc-index/widget(?P<widget_id>[0-9]+)/(?P<narrow_doc>n?)x/Document(?P<document_id>[0-9]+).html', 'workflows.latino.views.get_adc_page', name='get adc page'),
)
\ No newline at end of file
'''
MySQL visualization library.
@author: Anze Vavpetic <anze.vavpetic@ijs.si>
'''
from django.shortcuts import render
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment